Data Structures and Algorithms
Data Structures and Algorithms
Submitted to:
The data structure used to represent the social graph in our code is an Undirected Graph
implemented on a HashMap in Java.
public Network(String filePath) throws IOException {
graph = new HashMap<>();
loadGraph(filePath);
}
This is a data structure that maps keys to values wherein the keys are the
account IDs and the values are the sets of friends for each of the accounts. The use of
HashMap has allowed us to represent the social graph data and access the relationships
between the accounts.
In the sense of being an Undirected Graph, it is also accompanied by the data
structure of adjacency lists which represents the set of friends of a selected person. The
undirected graph represents the connections of people whose mutual friends are also
connected to each other. The vertices of the graph represent the people respectively.
The program utilized the use of HashMap and ArrayLists to store and represent the data
given by the files which would be converted into an undirected graph by adding edges
which represents the friendship of two people. The edges are then stored in a HashSet
to create an Adjacency list or Friend list for the person.
graph.putIfAbsent(person, new LinkedHashSet<>());
graph.get(person).add(friend);
if (current == person2) {
int tracePerson = current;
while (tracePerson != person1) {
path.add(tracePerson);
tracePerson = parentMap.get(tracePerson);
}
path.add(person1);
Collections.reverse(path);
return path;
}
If the traversal is performed and person B is not found in the same graph, then there is
no connection between person A and person B.
A. getFriendList() - The graph map has already stored the person-friend pairs data
in a HashSet while loading and reading the file. In this method, it would only need
to take an account ID as an input and it will return the set of friends from the
graph map in a HashSet. Empty set if the account ID is not in the map.
In conclusion, the program basically shows the friend list of the input that
the user typed and then it also shows the connections to other people. This
depends on what the user wants to do but those are just the options of this
program.
The program represents an adjacency list to store the pairings on the
social network graph. The undirected graph is then represented using a Map with
a LinkedHashSet whereas each account has a set of friends.
VI. Contributions
Name Contributions
VII. References
Adjacency list (With code in C, C++, Java and Python). (n.d.). Programiz: Learn to Code
for Free. https://www.programiz.com/dsa/graph-adjacency-list
Java HashMap (With examples). (n.d.). Programiz: Learn to Code for Free.
https://www.programiz.com/java-programming/hashmap