I have a Social Graph assignment, and I have a pretty good idea of what I want to do, I just want to know if I’m on the right track, and any hints you guys can provide.
Anyway, fairly simple implementation (I found a very complex one here – How to model social graph in Java, but I think it’s far more than what I actually need). Essentially my idea is to make a “User” object and a hashmap to keep everything in. A User object will have 4 data structures within it – name (string), student (boolean), school (string), and friends (integer array).
Each user will be added to the hashmap, and thus given a unique key. When a friendship is to be made, say between A and B, I go to the user A in the hashmap and ad the key for user B into A’s friends array, and vice versa. That way I can keep track of everyone and who they’re friends with.
Does this make sense? It works out in my head, but I feel like I’m missing something in the implementation that will make this not work as well as I think it should.
The answer to this will depend on the requirements and what you want to do with your social graph (especially on whether you want to persist the data or not).
If you are using a hashmap as your user store, then I assume you have a separate class that is generating your ids (or you have a UserStore class that wraps the hashmap and generates them)? If you are not deleting users, then you could suffice to have an ArrayList as you store, with the index being the user key.
When it comes to the users themselves, you could hold their friends in a List, but that may complicate your delete user code slightly (assuming you have that functionality).
UPDATE:
If you want to do analysis, then you may get some benefit from storing a User’s friends as a Set<“UserKey”> instead of as an array (but depends on how you plan to do your analysis). You would still need a counter class (or master UserStore class that assigns the ids).