I have this Problem and solving it is not the problem, more like what is the fastest way doing it.
So i am asking the more experienced of you to help me find a fast solution.
I have People, each defined as a Integer between 1000 and 3000.
Every of these people can be assigned to someone else, this would look like:
<Integer for p1, Integer for p2>
There are some rules for these connections, there will be not more than 10000, but at least one of them
and each pair of people can only occur once, so <1000,2000> and <2000,1000> are not allowed!
At the Moment i store all of these connections in a LinkedList where Connection is a Class containing the two Integers of the two people.
I then need to find the person occuring the most times in all connections, if there are more than one, i would need to have all of them unsorted.
After that, i will iterate through the LinkedList and delete all connections where these people participated and redo the process until the list is empty.
Some problems i encountered are Concurred Access or use of the wrong Maps/Lists and a slow method of sorting.
I have no code at the moment, since i saw the performance of my old one and started from scratch with now nothing other than processing the input (witch is already optimized) 😉
What would help me most is someone looking at my case and telling me his experiences of how fast different solutions with different Datatypes are. I want to write the code mostly myself, i just need some hints how to do it right.
Thanks for the attention and hopefully for an answer.
If something is unclear, i appologise for that and will clarify it upon asking 🙂
If we look at this the object oriented way, we can have each Person store a List of their friends:
The advantage of this approach is that all operations complete in constant expected time.
This is much better than keeping a linked list of a friendships where finding the number of friends of a single person requires us to go through the list of all 10000 friendships.
It is also significantly faster than the two-dimensional array described by rogelware, where finding the number of friends requires checking all 2000 other persons for friendship, and removing a person requires clearing the friendship to all 2000 other persons.