Graph Traversal
Graph Traversal
In this section, we will discuss various types of techniques to traverse a graph. As we all know,
a graph is a collection of nodes and edges. Thus, traversing in a graph is the process of visiting
each node and edge in some systematic approach. Therefore, there are two types of standard
graph traversal techniques, which are:
Breadth first search is a traversal technique that uses the queue as an auxiliary data structure
for traversing all member nodes of a graph. In this technique, first we will select any node in
the graph as a starting node, and then we will take all the nodes adjacent to the starting node.
We will maintain the same approach for all the other nodes. Also, we will maintain the status
of all the traversed/visited nodes in a queue so that no nodes are traversed again. Now, let us
take a graph and apply BFS to traverse the graph.
Now, we will start the traversal of the graph by taking node A as a starting node of the above
sample graph. Then, we will traverse all the nodes adjacent to the starting node A. As we can
see, B, C, and E are the adjacent nodes of A. So we will traverse these nodes in any order, say
E, C, B. So the traversal is:
Now, we will traverse all the nodes adjacent to E. Node C is adjacent to node E. But node C
has already been traversed, so we will ignore it and we will move to the next step. Now, we
will traverse all the nodes adjacent to node C. As we can see, D is the adjacent node of C. So
we will traverse node D and the traversal is:
Now, we can see that all the nodes have been traversed and hence, this was the breadth first
search traversal by taking node A as a starting node.
Now, we will implement the breadth first search traversal technique with the help of a queue.
In this, we will maintain an array which will store all the adjacent unvisited neighbor nodes of
a given under consideration. Initially, the front and rear are set to -1. We will also maintain the
status of the visited nodes in a Boolean array, which will have value 1 if the node is visited and
0 if it is not visited.
For Example – Consider the following sample graph and traverse the graph using the breadth
first search technique.
Step 3: Similarly, node B is deleted from the queue and FRONT is incremented by 1. Now,
insert all the nodes adjacent to B, which is node C, by incrementing REAR. But C has already
been inserted in the queue. So now in this case, node C is also deleted by incrementing FRONT
by 1, and the node adjacent to C, that is, D, is inserted into the queue. Therefore, nodes A, B,
and C are traversed.
Step 4: Now, we will again delete the front element from the queue which is D. We will insert
the adjacent node of D, that is, B. But it is already traversed. Finally, as we delete the front
element D, we notice that FRONT > REAR, which is not possible. Hence, we have traversed
all the nodes in the graph.
Therefore, the breadth first search traversal of the graph is given as:
Now, let us look at the function for a breadth first search traversal.
Breadth_First_Search(int node)
{
int i, front, rear;
int queue[SIZE];
front=rear= -1;
printf("%d", node);
visited[node]= 1;
rear++;
front++;
queue[rear]= node;
while(front<=rear)
{
node = queue[front];
front++;
for(i=1; i<=n; i++)
{
if(adjacent[node][i]==1) && visited[node]==0)
{
printf("%d", node);
visited[i]=1;
rear++;
queue[rear]= i;
}
}
}
}
Depth first search is another traversal technique that uses the stack as an auxiliary data structure
for traversing all the member nodes of a graph. Also in this technique, we first select any node
in the graph as a starting node, and then we travel along a path which begins from the starting
node. We will visit the adjacent node of the starting node, and again the adjacent node of the
previous node, and so on. We will maintain the same approach for all the other nodes. Now,
let us take a graph and apply DFS to traverse the graph.
Now, we will start the traversal of the graph by taking node A as a starting node. Then, we will
traverse any of the nodes adjacent to the starting node A. As we can see, B, C, and E are the
adjacent nodes of A. If we traverse node E, then we will traverse the node adjacent to E, that
is, C. After traversing C, we will traverse the node adjacent to C which is D. Now, there is no
adjacent node to D, hence, we have reached the dead end. Thus, the traversal until now is:
Because of the dead end, we will move backward. Now, we reach node C. We will check if
there is any other node adjacent to C. There is no such node and thus, we again move backward.
Now, we reach E. We will again check if there is any other node adjacent to E. There is no
such node and thus, we again move backward. Now, we reach A. We will check if there is any
other node adjacent to A. There are two nodes, B and C, adjacent to node A. As C is already
traversed, it will be ignored. Now, we will traverse node B. After traversing B, we will traverse
the node adjacent to B which is D, but D is already traversed. Thus, we can't move backward
or forward. Thus, we have completed the traversal. The final traversal is given as:
Now, we will implement the depth first search traversal technique with the help of a stack. In
this, we will maintain an array which will store all the adjacent unvisited neighbor nodes of a
given node. Initially, the top is set to -1. We will also maintain the status of the visited nodes
in a Boolean array, which will have value 1 if the node is visited and 0 if it is not visited.
For Example – Consider the following sample graph and traverse the graph using the breadth
first search technique.
Step 2: Now, pop the topmost element from the stack, that is, A. Thus, A is traversed. Now,
push all the nodes adjacent to A, that is, push B and C.
Step 3: Again, pop the topmost element from the stack, that is, C. Thus, C is also traversed.
Now, push all the nodes adjacent to C, that is, push D.
Step 4: Now, again pop the topmost element from the stack, that is, D. Thus, D is also
traversed. Now, push all the nodes adjacent to D, that is, push B. But B is already in the stack.
Therefore, no push is performed. Thus, the stack becomes:
Step 5: Again, pop the topmost element from the stack, that is, B. Thus, B is also traversed.
Now, push all the nodes adjacent to B, that is, push C. But C is already traversed; hence, the
stack becomes empty.
Therefore, the depth first search traversal of the graph is given as follows:
Now, let us look at the function for the depth first search traversal.
Depth_First_Search(int node)
{
inti, stack[SIZE], top = -1, pop;
top++;
stack[top] = node;
while(top >= 0)
{
pop = stack[top];
top--;
if(visited[pop] == 0)
{
printf("%d", pop);
visited[pop] = 1;
}
else
continue
for(i=n; i>=1;i--)
{
if(adjacent[pop][node] == 1 && visited[node] == 0)
{
top++;
stack[top] = node;
}
}
}
}