I want to implement a graph class. I decided to represent the graph by using an adjacency map, like this:
// Option 1:
HashMap<Node<T>, List<Edge<T>>> adjacencyMap; // not so good.
Now my question regards the best practice of this kind of structure. Isn't it a bad idea to use a Node class as the key in this map? I'm not even able to adress adjacencyMap, because the identifying value is vertex, a field of the node class.
public class Node<T> {
private T vertex;
}
Otherwise if i map the explicit vertex (the specific value) of the nodes, i connect a vertex (value) to a list of nodes (class containing that vertices). Is this a good idea or am I running into problems?
// Option 2:
HashMap<<T>, List<Edge<T>>> adjacencyMap; // better
For the sake of completeness, here are the other classes:
public class Graph<T> {
private HashMap<Node<T>, List<Edge<T>>> adjacencyMap;
}
public class Edge<T> {
private Node<T> nodeX;
private Node<T> nodeY;
private double weight;
}
An obvious design choice would be to put the list of edges into the node class and just map my vertices to the node classes, but i find it a weird structure to maintain edges in nodes.
Can someone provide any kind of best practice regarding this problem?