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

Learn Graph-Traversal Through Animations

Graph traversal animation using BFS and DFS method

Uploaded by

Subash Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Learn Graph-Traversal Through Animations

Graph traversal animation using BFS and DFS method

Uploaded by

Subash Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

A presentation

on
Graph Traversal
-By Subash Joshi
Introduction
 Graph traversal is the process of visiting all the
nodes or vertices of a graph in a systematic way.
 It is used for searching a vertex in graph.
 Using graph traversal, we visit all vertices of a
graph without getting into looping path.
 Types of graph traversal techniques:
 BFS (Breadth First Search)
 DFS (Depth First Search)
Breadth First Search (BFS)

 BFS starts at the root of the graph or tree and


explores all nodes at the current depth level before
moving on to the nodes at the next depth level
 BFS traversal of a graph produces a spanning tree
as final result.
 We use a queue with maximum size of total number
of vertices in the graph to implement BFS traversal
of a graph.
Algorithm
 1. Initialize an empty queue Q for temporary storage of
vertices.
 2. Enqueue v0 into Q.
 3. Repeat 4-6 while Q is empty.
 4. Dequeue Q and set such item into v.
 5. Add x to vertex list, L.
 6. Do step 7 for each vertex w that is adjacent to v.
 7. If y has not been visited, do steps 8-9.
 8. Add the edge vw to edge list T.
 9. Enqueue y into queue Q.
Consider the following graph

A B C

D E F

Queue:
Step 1

A B C

D E F

Queue:
A
-Select the vertex A as starting point (visit A)
-Insert A into the Queue.
Step 2

A B C

D E F

Queue:
D E B
-Visit all adjacent vertices of A which are not
visited (D,E,B)
-Insert these vertices to queue and delete A from queue.
Step 3

A B C

D E F

Queue:
E B
-Visit all adjacent vertices of D which are not
visited (no such vertex)
-delete D from queue.
Step 4

A B C

D E F

Queue:
B C F
-Visit all adjacent vertices of E which are not
visited (C, F)
-Insert these vertices to queue and delete E from queue.
Step 5

A B C

D E F

Queue:
C F
-Visit all adjacent vertices of B which are not
visited (no such vertices)
-Delete B from queue.
Step 6

A B C

D E F

Queue:
F G
-Visit all adjacent vertices of C which are not
visited (G)
-Insert these vertices to queue and delete C from queue.
Step 7

A B C

D E F

Queue:
G
-Visit all adjacent vertices of F which are not
visited (no such vertices)
-Delete F from queue.
Step 8

A B C

D E F

Queue:

-Visit all adjacent vertices of G which are not


visited (no such vertices)
-Delete G from queue.
 The queue is empty. So, stop the BFS process.
 Final result of BFS is a spanning Tree as shown below

A B C

D E F
Depth First Search (DFS)

 DFS starts at the root node and explores as far as


possible along each branch before backtracking
 DFS traversal of a graph produces a spanning tree
as result.
 DFS uses a stack data structure to keep track of the
nodes that are to be explored next.
Algorithm
1. Start
2. Define a stack of size total number of vertices in the graph
3. Select any vertex as starting point for traversal. Visit that the vertex and push it on
to the stack.
4. 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.
5. Repeat step 4 until there are no new vertex to be visited from the vertex on top of
the stack.
6. When there is no new vertex to be visited then use back tracking and pop one
vertex from the stack.
7. Repeat steps 4, 5 and 6 until stack becomes empty.
8. When stack becomes empty, then produce final spanning tree by removing unused
edges from the graph.
9. Stop.
Consider the following graph

A B C

D E F

Stack:
Step 1

A B C

D E F

Stack:
A
-Select the vertex A as starting point (visit A)
-Push A into the Stack.
Step 2

A B C

D E F

Stack:
A B
-Visit any adjacent vertices of A which is not
visited (B)
-Push B onto the Stack.
Step 3

A B C

D E F

Stack:
A B C
-Visit any adjacent vertices of B which is not
visited (C)
-Push C onto the Stack.
Step 4

A B C

D E F

Stack :
A B C E
-Visit any adjacent vertices of C which is not
visited (E)
-Push E onto the Stack.
Step 5

A B C

D E F

Stack :
A B C E D
-Visit any adjacent vertices of E which is not
visited (D)
-Push D onto the Stack.
Step 6

A B C

D E F

Stack :
A B C E
-There is no new node from D. So use backtrack.
-Pop D onto the Stack.
Step 7

A B C

D E F

Stack :
A B C E F
-Visit any adjacent vertices of E which is not
visited (F)
-Push F onto the Stack.
Step 8

A B C

D E F

Stack :
A B C E F G
-Visit any adjacent vertices of F which is not
visited (G)
-Push G onto the Stack.
Step 9

A B C

D E F

Stack :
A B C E F
-There is no new node from G. So use backtrack.
-Pop G from the Stack.
Step 10

A B C

D E F

Stack :
A B C E
-There is no new node from F. So use backtrack.
-Pop F from the Stack.
Step 11

A B C

D E F

Stack :
A B C
-There is no new node from E. So use backtrack.
-Pop E from the Stack.
Step 12

A B C

D E F

Stack :
A B
-There is no new node from C. So use backtrack.
-Pop C from the Stack.
Step 13

A B C

D E F

Stack :
A
-There is no new node from B. So use backtrack.
-Pop B from the Stack.
Step 14

A B C

D E F

Stack :

-There is no new node from A. So use backtrack.


-Pop A from the Stack.
 The stack is empty. So, stop the DFS process.
 Final result of DFS is a spanning Tree as shown below

A B C

D E F

You might also like