100% found this document useful (1 vote)
220 views42 pages

Graph Traversal - DFS & BFS

This lecture discusses graph traversal techniques such as depth-first search (DFS) and breadth-first search (BFS). DFS uses a stack data structure and traverses vertices according to the depth of the tree, outputting a DFS tree with tree and back edges. BFS uses a queue data structure and traverses vertices layer by layer, outputting a BFS tree with tree and cross edges. Examples are provided to illustrate the traversal process for DFS and BFS. The differences and similarities between DFS and BFS are also outlined.

Uploaded by

Kishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
220 views42 pages

Graph Traversal - DFS & BFS

This lecture discusses graph traversal techniques such as depth-first search (DFS) and breadth-first search (BFS). DFS uses a stack data structure and traverses vertices according to the depth of the tree, outputting a DFS tree with tree and back edges. BFS uses a queue data structure and traverses vertices layer by layer, outputting a BFS tree with tree and cross edges. Examples are provided to illustrate the traversal process for DFS and BFS. The differences and similarities between DFS and BFS are also outlined.

Uploaded by

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

Established as per the Section 2(f) of the UGC Act, 1956

Approved by AICTE, COA and BCI, New Delhi

Data Structures using C

School of Computer Science and Applications


LECTURE

Agenda:
• Graph traversal techniques
GRAPH
GRAPH TRAVERSAL

Graph traversal refers to the Such traversals are classified by


process of visiting each vertex the order in which the vertices
in a graph. are visited.
GRAPH TRAVERSAL TECHNIQUES

DFS - Depth First Search

BFS- Breadth First Search


DATA STRUCTURES OF GRAPH TRAVERSAL

DFS uses Data Structure Stack

BFS uses Data Structures Queue


REVISION OF MINIMUM SPANNING TREES
DFS TRAVERSAL

To visit all the vertices of a graph in the Systematic manner.

DFS will add all edges leading out of v to the stack.

The next vertex to be visited is determined by popping the stack and


following that edge.

Output is DFS Tree.

DFS tree consisting of Tree edge and Back edge.


EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE
EXAMPLE

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 1 - Define a Stack of size total number of vertices in the graph.

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 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
DFS ALGORITHM

Begin  for all nodes i connected to u, do


   initially make the state to unvisited for all if ith vertex is unvisited, then
nodes
push ith vertex into the stack
   push start into the stack
            mark ith vertex as visited
   while stack is not empty, do
      done
    pop element from stack and set to u
   done
    display the node u
End
    if u is not visited, then
      mark u as visited
APPLICATIONS OF DFS

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

To visit all the vertices of a graph in the Systematic manner.

BFS will add all edges leading out of v to the Queue.

Visits the nodes in the graph Layerwise

Output is BFS Tree.

BFS tree consisting of Tree edge and Cross edge.


RULES FOR BFS

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 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 non-visited adjacent vertices of the vertex which is at front of


the Queue and insert them into the Queue.
STEPS TO IMPLEMENT BFS

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 5 - Repeat steps 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
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

2 b a,d,f a,b,c,d,e,f c,d,e,f b-f f b c g


3 c a,g a,b,c,d,e,f,g d,e,f,g c-g
4 d a,b,f a,b,c,d,e,f,g E,f,g -
5 e a,g a,b,c,d,e,f,g F,g - d a e
6 f d,b a,b,c,d,e,f,g g -
7 g c,e a,b,c,d,e,f,g Empty -

______________: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

You might also like