How would I traverse through this graph? Provide example code, please!
class Edge {
int src, dest;
Edge(int src, int dest)
{
this.src = src;
this.dest = dest;
}
};
// class to represent a graph object
class Graph
{
// A list of lists to represent adjacency list
List<List<Integer>> adj = new
ArrayList<>();
// Constructor to construct graph
public Graph(List<Edge> edges)
{
// allocate memory for adjacency
list
for (int i = 0; i <
edges.size(); i++) {
adj.add(i, new
ArrayList<>());
}
// add edges to the undirected
graph
for (Edge current : edges)
{
// allocate new
node in adjacency List from src to dest
adj.get(current.src).add(current.dest);
// Uncomment line 39 for undirected graph
// allocate
new node in adjacency List from dest to src
//
adj.get(current.dest).add(current.src);
}
}
}
class Main
{
// print adjacency list representation of graph
private static void printGraph(Graph graph)
{
int src = 0;
int n = graph.adj.size();
while (src < n)
{
// print current
vertex and all its neighboring vertices
for (int dest :
graph.adj.get(src)) {
System.out.print("(" + src + " --> " + dest +
")\t");
}
System.out.println();
src++;
}
}
// Directed Graph Implementation in Java
public static void main (String[] args)
{
// Input: List of edges in a
directed graph (as per above diagram)
List<Edge> edges =
Arrays.asList(new Edge(0, 1), new Edge(1, 2),
new
Edge(2, 0), new Edge(2, 1),new Edge(3, 2),
new
Edge(4, 5), new Edge(5, 4));
// construct graph from given
list of edges
Graph graph = new Graph(edges);
// print adjacency list
representation of the graph
printGraph(graph);
}
}
In the given code, the graph is saved as an adjecency list. There are in total two ways of representing a graph in an algorithm. An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph.
Think of the data structure as a list of list of integers. See this line: List<List<Integer>> adj = new ArrayList<>();
This means we have a list (linked list) of many sources and each of those sources have various destinations in the graph. Have a look at this adjecency list (a vertical stack of source nodes and a list of other nodes which are connected to this particular node):

Now, you have (b) which is an adjecency list. (c) denotes an adjecency matrix.
For traversing through the graph you can use the following process:
Let's say you are trying to reach node 3 from node 2 from node 1. In a gist 1 -> 2 -> 3
step 1: select a starting node (may be 1)
step 2: in the List, search for the first node (node 1) and then search the list associated with (1) if node (2) exists there. The nodes associated with (1) are 2 and 5 so you are sure that 2 exists.
step 3: since 2 was there in list of node 1, you can be sure that you can reach 2, now try searching node 3 in the second node's list. in the List, search for the second node (node 2) and then search the list associated with (2) if node (3) exists there.
since 3 exists, we have traversed from 1 to 3
The code will look as follows:
private static void goFrom1To3Graph(Graph graph)
{
int src = 1;
int n = graph.adj.size();
for (int dest : graph.adj.get(src))
{
System.out.print("(" + src + " --> " + dest + ")\t");
if (dest == 2) {
for(int dest2 : graph.adj.get((2)) {
System.out.print("(" + 2 + " --> " + dest + ")\t");
if (dest2 == 3)
System.out.println("reached 3 through 2 through 1");
}
}
}
}
How would I traverse through this graph? Provide example code, please! class Edge { int...