Choosing a data structure for calculating class schedule - java

I'm having trouble figuring out what kind of data structure to use. I'm trying to represent classes that need to be taken to complete a major, complete with prereqs. Originally, I was thinking of using and adjacency matrix to represent the data. However, I'm currently using an arraylist to store each course. Then, in each course object, I have an arraylist containing the parents of each course and the children of each course. I'm planning on having a dummy course with the first courses in its children array. Then I plan on doing a BFS and adding classes so long as the parents have been taken and the current course hasn't. I can't help but feeling that this isn't the ideal solution. I tried looking at the difference between lists, sets, and maps, and I can't come to a clear reason why one is superior to the other. All of the courses are unique and sets don't contain duplicates, so that seems to make sense. On the other hand, the order of classes taken matters, so a list seems like a good idea. Then again, I could have the course number be the key and the course object be the value in a map.

Related

Optimize removing process complexity

when I have an Array and I want to remove one value from it I need to shift the next element to lift but the idea is to do shifting one time when a n of null value in array.
Of course it is micro-optimisation, and ArrayList (maybe LinkedList) would be a production quality data structure for dynamic arrays.
Here you might keep an extra list of nulled entries. At a certain threshold one could do **System.arraycopy**s to remove the gaps. If there are many index based inserts too, you might opt for keeping gaps, maybe collecting small gaps together.
This is a traditional technique in editors for text.
For several data structures one might search through guava classes.
For instance write-on-copy data structures.
Or concurrency, compactifying in the background.
For a specific data structure & algorithm maybe someone else can give pointers.

An alternative to Treemap that allows duplicates

I am writing a program to analyze some spreadsheet data. There are two columns: start time and duration (both variables are Doubles). The spreadsheet is not sorted. I need to sort the columns together by start time (that is, the durations have to stay with their matching start times). There are a few thousand rows, and analysis will happen periodically so I don't want to keep sorting the entire collection over and over again as more data gets added.
A Treemap using start time as the key and duration as the value seemed perfect because it would insert the information into the correct position as it gets read in, and keep the two pieces of data together as it goes.
And it did work perfectly for 90% of my data. Unfortunately I realized tonight that sometimes 2 events will have the same start time. Since the Treemap doesn't keep duplicate keys, I lose a row when the new data overwrites the old one.
There are many posts about this (see this and this and sort of this) and I see two suggestions keep coming up:
a custom comparator to 'trick' the Treemap into allowing duplicates.
using something like Treemap(Double,List(Double)) to store multiple values for a key.
The first suggestion is easiest for me to implement but I read comments that this breaks the contract of the Treemap and isn't a good idea. The second suggestion can be done but will make the analysis more complicated as I'll have to iterate through the list as I iterate through the keys, instead of simply iterating through the keys alone.
What I need is a way to keep two lists sorted together and allow duplicate entries. I'm hoping someone can suggest the best way to do this. Thanks so much for your help.

I can't seem to find the right Collection for my case. Thinking about list of hashmap of list, which appears very abstract

I'm currently trying to write this programm challenge: https://www.reddit.com/r/dailyprogrammer/comments/2hcwzn/09242014_challenge_181_intermediate_average_speed/
Basically, I've gotte to the point where the cars pass the 4 cameras, and I want to save the time every car has passed a specific camera individually.
So far my thought process was to do a list of a hashmap of lists, but I feel like I'm thinking too abstract, mostly because I am really not feeling too safe with which collection to use at which time (thats why I'm doing this exercise mostly).
Basically, my list would consist of various hashmaps of lists. The "lowest" lists would contain all 4 time dates for a specific car ( data for camera one, data for camera two, and so on). Then I would attach this list to a hashmap, which would have the specific plate of the car as key. The "highest" collection list would then include all the hashmaps, so basically the list would store all car plates which store all data for each camera.
I feel like I made it a little bit hard to follow, so I painted this mindmap for you: https://i.imgur.com/SXGLTvX.png
I hope someone can point me in the right direction.
For the speed limit, you just need a number... Maybe use a double?
For the cameras, you need an array or a List of distance (ints). Either adt is fine
To track if a car is speeding, you can use a Map<String,Date> to record their last position. Then, as you process each set of camera data, you can use this Map to get the last data, do your speeding calculations, and then put the new data into the Map so that when you're processing the next camera data, it will be up to date.
That's how I might design it... Make sense?

What is a good Java data structure to store RPG Game items?

I'm building a RPG dungeon game in Java and I'm stuck on creating a data structure.
I have a lot of Thing objects that I can copy to populate a dungeon with. For instance, there is a bread Thing object, and a sword Thing object, a chain mail Thing object, and monster Thing(s), etc. I want to store them in a central Library and then be able to retrieve an object using certain queries. I want to store them using the following fields:
int minLevel
int maxLevel
double probability
int[] types
So a rusty sword would have a minLevel of 1, a maxLevel of 3, a probability of rarity(3%),and [type.SWORD,type.WEAPON,type.ITEM,TYPE.EQUIP]. A better sword would have minLevel 2, maxLevel 10, rarity (1%).
Then I want to retrieve a random type.SWORD from the library and say I'm at level 3. I should get a rusty sword more often than the better sword based on their probabilities. If I retrieved a type.SWORD from the library requesting level 10, I would only get back the better sword.
I hope this makes sense.
EDIT
At the initialization stage, all the basic objects will be created. Things like the available weapons, armor, foods, potions, wands, all the basic possible Things that have a unique graphic tile in the game. Then when I want to place an object somewhere, I just make a copy of one of the available Things, adjust it's stats a little, and plunk it down in the world. The actual items are all subclass of the root Thing class, such as class Creature,Item,Equip(extends Item),Weapon(extends Equip),Armor(extends Equip),Food(extends Item), etc. But I want to tag them different in the Library database, I want to use extra tags, such as type.RARE, type.ARTIFACT, type.CURSED, so I want extra tags besides the class.
The game use LIBGDX to be available on Android and as an Applet. I use the free Rltile set, which has thousands of good tiles. I will use Pubnub or Google App Engine to provide multiplayer support.
i can think of three answers:
write your own Library that stores these things in Maps with custom methods.
so you might have a Map<Type,List<Object>> that stores lists of things by type
and then a method that takes a type, retrieves the list from the map, and selects
something by probability (that's easy to do - you just some up the probabilities,
generate a random number between 0 and the sum, and then walk through the list,
subtracting the item's probability from your random value until it's negative - you
return the item that made it negative[*]). you can also filter the list first by
level, for example.
if you have a real mix of different things, and don't want to base this on types,
then another option (slower, but more flexible) is to place everything in a list
and then filter by your needs. a nice way to do that is with guava - see
Iterables.filter and Predicate at https://code.google.com/p/guava-libraries/.
you could provide an interface that takes a predicate and returns a random
selection from whatever is left after filtering. predicates are easy to construct
"inline" with anonymous classes - see examples at
https://code.google.com/p/guava-libraries/wiki/FunctionalExplained#Functions_and_Predicates
stick all this in a database. maybe i am too enterprisey, and games people would
never do this, but it seems to me that a small, embedded database like sqlite or
H2 would be perfect for this. you can then select things with SQL queries (this
is already a long answer so i won't give more details here).
change your design. what you describe is not very OO. instead of having types,
your Things could implement interfaces. so the Weapon interface would have a
getMinLevel() method, for example. and then, with a design like this, use a
database with ORM (hibernate).
what you're doing is kind of ambitious and i suspect is more about learning than anything else (no criticism intended - this is how i learn stuff, by making things, so just assuming you are like me). so choose whichever you feel most comfortable with.
[*] this assumes that you always want to return something. if the probabilities are normalized and you want to be able to return nothing, select the initial value from 0-1 (or 0-100 if using percentages). and, if nothing turns the value negative when you run through the list, return nothing.
The easiest approach is to put all your objects in a single large arraylist, and use repeated sampling to select an object.
The procedure to select a random item is very simple:
Select a random number from 0 up to the size of the ArrayList
Get the object at that index from the library
If the object fails to meet any criteria you specify (e.g. "is a of type.SWORD or type.MACE?") go back to start
If the object is outside the minimum or maximum level, go back to start
If the object has a rarity of less than 100%, create a random number from 0-100%. If the random number exceeds the object's rarity, loop back to start. Most objects should have a rarity of say 10-100%, if you want extremely common objects then you can add them multiple times to the library.
This procedure will produce an object that meets the criteria sooner or later (if it exists) and will do so according to the rarity percentage.
The one slight trickness is that it will loop infinitely if no such object exists. Suppose there is no weapon in the library at level 17 for example? To get around this, I would propose widening the minLevel and maxLevel after every 100 tries to ensure that eventually one is found. Ensure you always have a level 1 object of each type available.
For safety, you might also want a bailout after say 100,000 tries (but remember to throw an exception - this is a problem if you are asking for things that don't exist in the library!).
P.S. I implemented a similar library system in a roguelike game called Tyrant that I created many years ago. Source is here if you are interersted:
https://github.com/mikera/tyrant/blob/master/src/main/java/mikera/engine/Lib.java

Game Interface Design

So I'm making a game similar to Age of Empires 2. So far, what I've been doing is using an Applet and making an array of tiles (grid) to contain units and buildings. I feel that in the long run this is going to be inefficient. I'm sure there is a better way to do this, perhaps using GUI, but I'm not a very experienced programmer yet so I've resorted to this more primitive method. Is there a better, more efficient way to have tiles that contain characters and buildings on the map?
Thanks.
This is pretty much how I want it to look like:
An array of tiles should be fine - this is what is usually used to represent the map in a 2D game.
Note that you will probably want to distinguish between:
Map terrain tiles, where each square contains just one tile
Units / buildings / other game objects, where each square might contain multiple objects
For the terrain a single array is fine, with one tile in each position.
For the other objects, you need a way to store multiple objects in each square. An array with an ArrayList in each square can work - though you will probably want to leave nulls in all the squares that don't have any contents to avoid creating a lot of empty ArrayLists.
It's also useful to create a global collection of game objects so that you can iterate over all game objects and/or find a speciifc object quickly without searching the whole map. I typically use something like a HashMap for this, where the integer key is a unique ID for each game object.
It depends on what you consider to be the 'inefficient' part. If most of the array elements are going to be containing data, and you need to be able to look up that data quickly and easily, than arrays are certainly the best way to go. They're easy to understand, and the logic mikera described for storing data is a very good way to get started.
There are data structures that very big tile based games use for storing data. Most of those are optimized for 'sparse' graphs at the like. A sparse graph is one where most of the locations are empty, but a few of them have content. Very big world maps often have this qualit. If you're curious about these scenarios, then you should google "quad tree tiling".
But in general, if you're still learning development, stick to the array. Premature optimization is the root of all evil. :)

Categories