Lecture 4.5 - Applications of BFS and DFS
Lecture 4.5 - Applications of BFS and DFS
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 2 / 11
Connectivity
3 4
Disconnected Graph
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 3 / 11
Connectivity
3 4
Disconnected Graph
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 3 / 11
Connectivity
Disconnected Graph
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 3 / 11
Connectivity
Disconnected Graph
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 3 / 11
Connectivity
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 3 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
0 1
3 4
Disconnected Graph
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
3 4
Disconnected Graph
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
Initialize component number to 0
2
3 4
Disconnected Graph
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
Initialize component number to 0
All visited nodes form a connected 2
component
3 4
Disconnected Graph
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
Initialize component number to 0 0
All visited nodes form a connected 2
component
Assign each visited node component 3 4
number 0
Disconnected Graph
0 0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
Initialize component number to 0 0
All visited nodes form a connected 2
component
Assign each visited node component 3 4
number 0
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
Initialize component number to 0 0
All visited nodes form a connected 2
component
Assign each visited node component 3 4
number 0
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
Initialize component number to 0 0
All visited nodes form a connected 2
component
Assign each visited node component 3 4
number 0
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
Initialize component number to 0 0
All visited nodes form a connected 2
component
Assign each visited node component 3 4
number 0
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number
Connected Graph
Start BFS/DFS from vertex 0 0 1
Initialize component number to 0 0
All visited nodes form a connected 2
component
Assign each visited node component 3 4
number 0
3 4
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 5 / 11
Detecting cycles
3 4
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 5 / 11
Detecting cycles
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 5 / 11
Detecting cycles
0 1 2 3
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 5 / 11
Detecting cycles
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 5 / 11
Detecting cycles
Acyclic Graph
A cycle is a path (technically, a walk) that
starts and ends at the same vertex 0 1
4 − 8 − 9 − 4 is a cycle
2
Cycle may repeat a vertex:
2 − 3 − 7 − 10 − 6 − 7 − 2
3 4
Cycle should not repeat edges: i − j − i
is not a cycle, e.g., 2 − 4 − 2
Graph with cycles
Simple cycle — only repeated vertices
are start and end 0 1 2 3
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 5 / 11
BFS tree
3 4
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 6 / 11
BFS tree
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 6 / 11
DFS tree
Maintain a DFS
counter, initially 0 1 2 3
0
4 5 6 7
8 9 10 11
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
Maintain a DFS
counter, initially 0 1 2 3
0
4 5 6 7
Increment
counter each time
8 9 10 11
we start and
finish exploring a
node
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
Maintain a DFS
counter, initially 0 1 2 3
0
4 5 6 7
Increment
counter each time
8 9 10 11
we start and
finish exploring a
node
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7
Increment
counter each time
8 9 10 11
we start and
finish exploring a
node
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7 1
1
Increment
counter each time
8 9 10 11
we start and
finish exploring a
node
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7 1
1 2
Increment post
counter each time
8 9 10 11
we start and
finish exploring a
node
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7 1 4
1 2 3
Increment post
counter each time
8 9 10 11
we start and
finish exploring a
node
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7 1 4
1 2 3
Increment post
counter each time 8
8 9 10 11 4
we start and
finish exploring a
node
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7 1 4
1 2 3
Increment post
counter each time 8
8 9 10 11 4
we start and
finish exploring a 9
node 5
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7 1 4
1 2 3
Increment post
counter each time 8
8 9 10 11 4
we start and
finish exploring a 9
node 5 6
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7 1 4
1 2 3
Increment post
counter each time 8
8 9 10 11 4 7
we start and
finish exploring a 9
node 5 6
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0
counter, initially 0 1 2 3 0
0
4 5 6 7 1 4
1 2 3 8
Increment post
counter each time 8
8 9 10 11 4 7
we start and
finish exploring a 9
node 5 6
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9
counter, initially 0 1 2 3 0
0
4 5 6 7 1 4
1 2 3 8
Increment post
counter each time 8
8 9 10 11 4 7
we start and
finish exploring a 9
node 5 6
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4
1 2 3 8
Increment post
counter each time 8
8 9 10 11 4 7
we start and
finish exploring a 9
node 5 6
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8
8 9 10 11 4 7
we start and
finish exploring a 9
node 5 6
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8 7
8 9 10 11 4 7 12
we start and
finish exploring a 9
node 5 6
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8 7
8 9 10 11 4 7 12
we start and
finish exploring a 9 6
node 5 6 13
Each vertex is
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8 7
8 9 10 11 4 7 12
we start and
finish exploring a 9 6
node 5 6 13
Each vertex is 10
14
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8 7
8 9 10 11 4 7 12
we start and
finish exploring a 9 6
node 5 6 13
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8 7
8 9 10 11 4 7 12
we start and
finish exploring a 9 6
node 5 6 13 16
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8 7
8 9 10 11 4 7 12
we start and
finish exploring a 9 6 11
node 5 6 13 16 17
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8 7
8 9 10 11 4 7 12
we start and
finish exploring a 9 6 11
node 5 6 13 16 17 18
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11
Increment post
counter each time 8 7
8 9 10 11 4 7 12 19
we start and
finish exploring a 9 6 11
node 5 6 13 16 17 18
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11 20
Increment post
counter each time 8 7
8 9 10 11 4 7 12 19
we start and
finish exploring a 9 6 11
node 5 6 13 16 17 18
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10 21
counter, initially 0 1 2 3 0 2
0
4 5 6 7 1 4 3
1 2 3 8 11 20
Increment post
counter each time 8 7
8 9 10 11 4 7 12 19
we start and
finish exploring a 9 6 11
node 5 6 13 16 17 18
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10 21 22
counter, initially 0 1 2 3 0 2 5
0
4 5 6 7 1 4 3
1 2 3 8 11 20
Increment post
counter each time 8 7
8 9 10 11 4 7 12 19
we start and
finish exploring a 9 6 11
node 5 6 13 16 17 18
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10 21 22 23
counter, initially 0 1 2 3 0 2 5
0
4 5 6 7 1 4 3
1 2 3 8 11 20
Increment post
counter each time 8 7
8 9 10 11 4 7 12 19
we start and
finish exploring a 9 6 11
node 5 6 13 16 17 18
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
pre
Maintain a DFS
0 9 10 21 22 23
counter, initially 0 1 2 3 0 2 5
0
4 5 6 7 1 4 3
1 2 3 8 11 20
Increment post
counter each time 8 7
8 9 10 11 4 7 12 19
we start and
finish exploring a 9 6 11
As before, non-tree 5 6 13 16 17 18
node
edges generate cycles
Each vertex is 10
14 15
assigned an entry
number (pre) and
exit number
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
DFS tree
(visited,pre,post) = ({},{},{})
7 6
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles
5 6
3 6 7 8
7 6
7
4 5
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Classifying non-tree edges in directed graphs
0 15
Use pre/post numbers 0
1 0 2
1 2
1 10 11 14
4 5 3
4 3
2 9 12 13
7 6
5 6
3 6 7 8
7
4 5
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 9 / 11
Classifying non-tree edges in directed graphs
0 15
Use pre/post numbers 0
1 0 2
Tree edge/forward edge (u, v )
1 2
Interval [pre(u), post(u)] contains 1 10 11 14
4 5 3
[pre(v ), post(v )] 4 3
2 9 12 13
7 6
5 6
3 6 7 8
7
4 5
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 9 / 11
Classifying non-tree edges in directed graphs
0 15
Use pre/post numbers 0
1 0 2
Tree edge/forward edge (u, v )
1 2
Interval [pre(u), post(u)] contains 1 10 11 14
4 5 3
[pre(v ), post(v )] 4 3
2 9 12 13
Back edge (u, v ) 7 6
Interval [pre(v ), post(v )] contains 5 6
3 6 7 8
[pre(u), post(u)]
7
4 5
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 9 / 11
Classifying non-tree edges in directed graphs
0 15
Use pre/post numbers 0
1 0 2
Tree edge/forward edge (u, v )
1 2
Interval [pre(u), post(u)] contains 1 10 11 14
4 5 3
[pre(v ), post(v )] 4 3
2 9 12 13
Back edge (u, v ) 7 6
Interval [pre(v ), post(v )] contains 5 6
3 6 7 8
[pre(u), post(u)]
7
Cross edge (u, v ) 4 5
Intervals [pre(u), post(u)] and
[pre(v ), post(v )] are disjoint
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 9 / 11
Connectivity in directed graphs
4 5 3
7 6
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 10 / 11
Connectivity in directed graphs
4 5 3
7 6
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 10 / 11
Connectivity in directed graphs
7 6
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 10 / 11
Connectivity in directed graphs
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 10 / 11
Summary
BFS and DFS can be used to identify connected components in an undirected graph
BFS and DFS identify an underlying tree, non-tree edges generate cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 11 / 11
Summary
BFS and DFS can be used to identify connected components in an undirected graph
BFS and DFS identify an underlying tree, non-tree edges generate cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 11 / 11
Summary
BFS and DFS can be used to identify connected components in an undirected graph
BFS and DFS identify an underlying tree, non-tree edges generate cycles
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 11 / 11
Summary
BFS and DFS can be used to identify connected components in an undirected graph
BFS and DFS identify an underlying tree, non-tree edges generate cycles
DFS numbering can also be used to identify other features such as articulation
points (cut vertices) and bridges (cut edges)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 11 / 11
Summary
BFS and DFS can be used to identify connected components in an undirected graph
BFS and DFS identify an underlying tree, non-tree edges generate cycles
DFS numbering can also be used to identify other features such as articulation
points (cut vertices) and bridges (cut edges)
Directed acyclic graphs are useful for representing dependencies
Given course prerequisites, find a valid sequence to complete a programme
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 11 / 11