Graph Traversing
Graph Traversing
2
Graph
A graph is a pictorial representation of a set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by points termed
as vertices, and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set
of edges, connecting the pairs of vertices. Take a look at the following graph − Graph
Basics.
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
03/24/2025
3
Types of Traversing
2. DFS(Depth-First-Search)
a. DFS use a Stack for future processing
b. This follows LIFO (Last In First Out)
03/24/2025
4
Define Status
During the execution of any algorithm, each node N of G will be in one of the three
states. called the status of N as follows:
03/24/2025
5
BFS
Algorithm
Step 1: Initialize all nodes to the ready state (STATUS = 1)
Step 2: Put the starting node A in Queue and change its status to the waiting state
(STATUS = 2)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty:
Step 4: Remove the front node N of Queue. Process N and change the status of N
to the processed state (STATUS = 3)
Step 5: Add to the rear of QUEUE all the neighbors of N that are in the steady state
(STATUS= 1), and change their status to the waiting state (STATUS = 2).
[End of Step 3 loop]
Step 6: Exit.
03/24/2025
6
BFS Process
Step 1: Define a Queue of size total number of vertices in the graph.
Step 2: Select any vertex as starting point for traversal. Visit that vertex and insert
it into the Queue.
Step 3: Visit all the adjacent vertices of the vertex which is at front of the Queue
which is not visited and insert them into the Queue.
Step 4: When there is no new vertex to be visit from the vertex at front of the Queue
then delete that vertex from the Queue.
Step 5: Repeat step 3 and 4 until queue becomes empty.
Step 6: When queue becomes Empty, then produce final spanning tree by removing
unused edges from the graph
03/24/2025
BFS Example
BFS Example
BFS Example
BFS Example
11
DFS
Algorithm
Step 1: Initialize all nodes to the ready state (STATUS = 1)
Step 2: Put the starting node A onto STACK and change its status to the waiting state
(STATUS = 2)
Step 3: Repeat Steps 4 and 5 until STACK is empty:
Step 4: POP the top node N of STACK. Process N and change its status to the processed
state(STATUS=3)
Step 5: Push onto STACK all neighbors of N that are still in the ready state(STATUS=1),
and change their Status to the waiting state(STATUS=2).
[End of Step 3 loop]
Step 6: Exit.
03/24/2025
12
DFS Process
Step 1: Define a Stack of size total number of vertices in the graph.
Step 2: Select any vertex as starting point for traversal. Visit that vertex and push
it on to the Stack.
Step 3: Visit any one of the adjacent vertex of the vertex which is at top of the stack
which is not visited and push it on to the stack.
Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on top
of the stack.
Step 5: When there is no new vertex to be visit then use back tracking and pop one
vertex from the stack.
Step 6: Repeat steps 3, 4 and 5 until stack becomes Empty.
Step 7: When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph
03/24/2025
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example
DFS Example