I have this graph
in graph S and P take values{1,2,3,4,5}
I want to represent this graph in java
can you help me with idea?
thank you in advance
I have this graph
in graph S and P take values{1,2,3,4,5}
I want to represent this graph in java
can you help me with idea?
thank you in advance
You can also use adjancy list using the following code snippet -
int n;
List<Integer>[] adj;
AdjacencyLists(int n0) {
n = n0;
adj = (List<Integer>[])new List[n];
for (int i = 0; i < n; i++)
adj[i] = new ArrayStack<Integer>(Integer.class);
}
Then add edge using the function -
void addEdge(int i, int j) {
adj[i].add(j);
}
Or can remove edge -
void removeEdge(int i, int j) {
Iterator<Integer> it = adj[i].iterator();
while (it.hasNext()) {
if (it.next() == j) {
it.remove();
return;
}
}
}
Or even check if a edge is exists -
boolean hasEdge(int i, int j) {
return adj[i].contains(j);
}
You can found more detail about adjacency list here.
Or you may use adjacency matrix
Hope it will help.
Thanks a lot