0% found this document useful (0 votes)
20 views

Graph Traversal

This document discusses and provides examples of two standard graph traversal techniques: breadth-first search (BFS) and depth-first search (DFS). BFS uses a queue to traverse all nodes level-by-level from the starting node, while DFS uses a stack to traverse along paths in a graph from the starting node in a depth-wise manner. Pseudocode functions for implementing BFS and DFS on sample graphs are also provided.

Uploaded by

ratnakar babu M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Graph Traversal

This document discusses and provides examples of two standard graph traversal techniques: breadth-first search (BFS) and depth-first search (DFS). BFS uses a queue to traverse all nodes level-by-level from the starting node, while DFS uses a stack to traverse along paths in a graph from the starting node in a depth-wise manner. Pseudocode functions for implementing BFS and DFS on sample graphs are also provided.

Uploaded by

ratnakar babu M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

12.

4 Graph Traversal Techniques

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:

1. Breadth First Search (BFS)


2. Depth First Search (DFS)

So now, we will discuss both of these techniques in detail.

12.4.1 Breadth First Search

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.

Figure 12.23: A sample 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.

• First, we will en-queue/insert the starting node into the queue.


• Second, the first node/element in the queue is deleted from the queue and all the
adjacent unvisited nodes are inserted into the queue. This is repeated until the queue
becomes empty.

For Example – Consider the following sample graph and traverse the graph using the breadth
first search technique.

Figure 12.24: A sample graph


The appropriate adjacency list representation of a graph is given as follows:

Node Adjacency List


A B, C
B C
C D
D B

In this example, we are taking A as a starting node.

Step 1: First, node A is inserted into the queue.


Step 2: Node A is deleted from the queue and FRONT is incremented by 1. Now, insert all the
nodes adjacent to A, which are nodes B and C, by incrementing REAR. Also, node A has been
traversed.

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.

Function for 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;
}
}
}
}

12.4.2 Depth First Search

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.

Figure 12.25: A Sample 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.

• First, we will push the starting node onto the stack.


• Second, the topmost node/element is popped out from the stack and is traversed. If it is
already traversed, then we will ignore it.
• Third, all the adjacent unvisited nodes of the popped node/element are pushed onto the
stack. This process is repeated until the queue becomes empty. The steps are repeated
until the stack becomes empty.

For Example – Consider the following sample graph and traverse the graph using the breadth
first search technique.

Figure 12.26: A sample graph


In this example, we are taking A as a starting node.

Step 1: Push A onto the stack.

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.

Function for 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;
}
}
}
}

You might also like