Graph Traversal - DFS & BFS
Graph Traversal - DFS & BFS
Agenda:
• Graph traversal techniques
GRAPH
GRAPH TRAVERSAL
Similar way all the vertices in the stack F, E, C, B, A are popped from the stack.
EXAMPLE
PROBLEM
1. Traverse the given graph using DFS traversal technique and construct the
corresponding DFS Tree.
Step Stack(S) V=adj(s[top]) Nodes visited S POP(Stack) Output
Intial a - a - -
1
2
3
4
5
6
7
8
9
10
11
12
13
PROBLEM
1. Traverse the given graph using DFS traversal technique and construct the
corresponding DFS Tree.
Step Stack(S) V=adj(s[top]) Nodes visited S POP(Stack) Output
Intial a - a - -
1
2
3
4
5
6
7
8
9
10
11
12
13
RULES FOR DFS
1. Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it
in a stack.
2. Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will
pop up all the vertices from the stack, which do not have adjacent vertices.)
3. Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
STEPS TO IMPLEMENT DFS
Step 2 - Select vertex according to order as starting point for traversal. Visit that vertex
and push it on to the Stack.
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top
of stack and push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at
the top of the stack.
STEPS TO IMPLEMENT DFS
Step 5 - When there is no new vertex to visit then use back tracking and pop one
vertex from the stack.
Step 7 - When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph
DFS ALGORITHM
Weighted Graph:
In a weighted graph, DFS graph traversal generates the shortest path tree and
minimum spanning tree.
Detecting a Cycle in a Graph:
A graph has a cycle if we found a back edge during DFS. Therefore, we should run
DFS for the graph and verify for back edges.
Path Finding:
We can specialize in the DFS algorithm to search a path between two vertices.
APPLICATIONS OF DFS
Topological Sorting:
It is primarily used for scheduling jobs from the given dependencies among the
group of jobs. In computer science, it is used in instruction scheduling, data
serialization, logic synthesis, determining the order of compilation tasks.
Searching Strongly Connected Components of a Graph:
It used in DFS graph when there is a path from each and every vertex in the graph
to other remaining vertexes.
Solving Puzzles with Only One Solution:
DFS algorithm can be easily adapted to search all solutions to a maze by including
nodes on the existing path in the visited set.
BFS TRAVERSAL
1. Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it
in a queue.
2. Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
3. Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
STEPS TO IMPLEMENT BFS
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
Step 4 - When there is no new vertex to be visited from the vertex which is at front of
the Queue then delete that vertex.
Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph
PROBLEM
1. Traverse the given graph using BFS traversal technique and construct the
corresponding BFS Tree.
STEP u=del(Q) v=adj(u) Nodes Visited S Q Output
T(u->v)
Initial - - a a -
1
2
3
4
f b c g
5
6
7 d a e
______________:TREE EDGES
-----------------------:CROSS EDGES
STEP u=del(Q) v=adj(u) Nodes Visited S Q Output
T(u->v)
Intial - - a a -
1 a b,c,d,e a,b,c,d,e b,c,d,e a-b
a-c
a-d
a-e
______________:TREE EDGES
-----------------------:CROSS EDGES
PROBLEM
1. Traverse the given graph using BFS traversal technique and construct the
corresponding BFS Tree.
STEP u=del( v=adj(u) Nodes Visited S Q Output
Q) T(u->v)
Initial - - a a -
1
2
3
4
5
6
7
8
9
10
11
BFS ALGORITHM
BFS(vertices, start) for all vertices i adjacent with u, do
Input: The list of vertices, and the start vertex. if vertices[i] is unvisited, then
Output: Traverse all of the nodes, if the graph is mark vertices[i] as temporarily
connected. visited
Begin
add v into the queue
Define an empty queue que
mark
at first mark all nodes status as unvisited
done
add the start vertex into the que
while que is not empty, do mark u as completely visited
delete item from que and set to u done
display the vertex u End
DIFFERENCE AND SIMILARITIES BETWEEN DFS & BFS
DFS BFS
1. Depth First Search 1. Breadth First Search
2. Stack DS 2. Queue DS
3. Suitable for games and puzzle 3. Decision making trees
4. DFS Tree 4. BFS Tree
5. Tree edge & Back edge 5. Tree edge & Cross edge
6. Fast 6. Slow as compare to DFS
7. Time complexity O|V+E| 7. Time complexity O|V+E|
8. Traverse according to tree depth 8. Traverse according to tree level
9. Implemented using LIFO 9. Implemented using FIFO
10. DFS goes to the bottom of a subtree, then 10. BFS finds the shortest path to the
backtracks. destination.
EXERCISE
1. Traverse the given graph using DFS traversal technique and construct the
corresponding DFS Tree.
EXERCISE
1. Traverse the given graph using DFS & BFS traversal technique and construct the
corresponding DFS & BFS Tree. Consider vertex ‘A’ as a starting vertex.
THANK YOU