Reference: https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/
Assume the labels are integers from 0 to n.
public class GraphInAdjacencyMatrix {
int V;
int[][] graph;
GraphInAdjacencyMatrix(int v) {
this.V = v;
this.graph = new int[v][v];
}
}
public class GraphInAdjacencyList {
int V;
List<Integer>[] graph;
GraphInAdjacencyList(int v) {
this.V = v;
this.graph = new LinkedList[v];
for (int i=0; i<v; i++) {
this.graph.add(new LinkedList<>());
}
}
}
public class GraphInAdjacencySet {
int V;
Set<Integer>[] graph;
GraphInAdjacencySet(int v) {
this.V = v;
this.graph = new HashSet[v];
for (int i=0; i<v; i++) {
this.graph.add(new HashSet<>());
}
}
}