0% found this document useful (0 votes)
23 views76 pages

Lecture 4.5 - Applications of BFS and DFS

Uploaded by

smitstudying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views76 pages

Lecture 4.5 - Applications of BFS and DFS

Uploaded by

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

Applications of BFS and DFS

Madhavan Mukund
https://www.cmi.ac.in/~madhavan

Programming, Data Structures and Algorithms using Python


Week 4
BFS and DFS
BFS and DFS systematically compute
reachability in graphs 0
1
BFS works level by level
2
Discovers shortest paths in terms of number 3
of edges
4 6
DFS explores a vertex as soon as it is visited 5
neighbours
Suspend a vertex while exploring its
neighbours 8
7
DFS numbering describes the order in which
vertices are explored 9
Beyond reachability, what can we find out
about a graph using BFS/DFS?

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 2 / 11
Connectivity

An undirected graph is connected if every Connected Graph


vertex is reachable from every other vertex 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 3 / 11
Connectivity

An undirected graph is connected if every Connected Graph


vertex is reachable from every other vertex 0 1
In a disconnected graph, we can identify
2
the connected components

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

An undirected graph is connected if every Connected Graph


vertex is reachable from every other vertex 0 1
In a disconnected graph, we can identify
2
the connected components
Maximal subsets of vertices that are
3 4
connected

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

An undirected graph is connected if every Connected Graph


vertex is reachable from every other vertex 0 1
In a disconnected graph, we can identify
2
the connected components
Maximal subsets of vertices that are
3 4
connected

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

An undirected graph is connected if every Connected Graph


vertex is reachable from every other vertex 0 1
In a disconnected graph, we can identify
2
the connected components
Maximal subsets of vertices that are
3 4
connected
Isolated vertices are trivial components 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
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

Pick smallest unvisited node j 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

Pick smallest unvisited node j Disconnected Graph


Increment component number to 1 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

Pick smallest unvisited node j Disconnected Graph


Increment component number to 1 0 0 1 2 3
Run BFS/DFS from node j
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

Pick smallest unvisited node j Disconnected Graph


Increment component number to 1 0 0 1 2 3 1
Run BFS/DFS from node j
Assign each visited node component 4 5 6 7
number 1
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

Pick smallest unvisited node j Disconnected Graph


Increment component number to 1 0 0 1 2 3 1
Run BFS/DFS from node j
Assign each visited node component 4 5 2 6 7
number 1
8 9 10 11
Repeat until all nodes are visited
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Identifying connected components
Assign each vertex a component number def Components(AList):
component = {}
Start BFS/DFS from vertex 0
for i in AList.keys():
Initialize component number to 0 component[i] = -1
All visited nodes form a connected
(compid,seen) = (0,0)
component
Assign each visited node component while seen <= max(AList.keys()):
number 0 startv = min([i for i in AList.keys()
if component[i] == -1])
Pick smallest unvisited node j visited = BFSList(AList,startv)
Increment component number to 1 for i in visited.keys():
if visited[i]:
Run BFS/DFS from node j seen = seen + 1
Assign each visited node component component[i] = compid
number 1 compid = compid + 1

Repeat until all nodes are visited return(component)


Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 4 / 11
Detecting cycles

A cycle is a path (technically, a walk) that


starts and ends at the same vertex 0 1

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

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

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

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

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

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

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

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
Simple cycle — only repeated vertices
are start and end 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

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

A graph is acyclic if it has no cycles


4 5 6 7

8 9 10 11

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 5 / 11
BFS tree

Edges explored by BFS form a tree Acyclic Graph

Technically, one tree per component 0 1


Collection of trees is a forest
2

3 4

Graph with 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 6 / 11
BFS tree

Edges explored by BFS form a tree Acyclic Graph

Technically, one tree per component 0 1


Collection of trees is a forest
2
Any non-tree edge creates a cycle
Detect cycles by searching for non-tree 3 4
edges
Graph with 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 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) = ({},{},{})

Maintain a DFS def DFSInitPrePost(AList):


counter, initially 0 1 2 3 # Initialization
0 for i in AList.keys():
visited[i] = False
4 5 6 7 pre[i],post[i]) = (-1,-1)
Increment return
counter each time
8 9 10 11
we start and def DFSPrePost(AList,v,count):
finish exploring a visited[v] = True
As before, non-tree pre[v] = count
node count = count+1
edges generate cycles
for k in AList[v]:
Each vertex is To compute pre and if (not visited[k]):
assigned an entry post pass counter via count = DFSPrePost(AList,k,count)
number (pre) and recursive DFS calls post[v] = count
exit number count = count+1
return(count)
(post)
Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 7 / 11
Directed cycles

In a directed graph, a cycle


must follow same direction 1 0 2
0 → 2 → 3 → 0 is a
cycle
0 → 5 → 1 ← 0 is not 4 5 3

7 6

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles

In a directed graph, a cycle 0 15


0
must follow same direction 1 0 2
0 → 2 → 3 → 0 is a 1 2
1 10 11 14
cycle
0 → 5 → 1 ← 0 is not 4 5 3 4 3
2 9 12 13

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

In a directed graph, a cycle 0 15


0
must follow same direction 1 0 2
0 → 2 → 3 → 0 is a 1 2
1 10 11 14
cycle
0 → 5 → 1 ← 0 is not 4 5 3 4 3
2 9 12 13
Tree edges
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

In a directed graph, a cycle 0 15


0
must follow same direction 1 0 2
0 → 2 → 3 → 0 is a 1 2
1 10 11 14
cycle
0 → 5 → 1 ← 0 is not 4 5 3 4 3
2 9 12 13
Tree edges
5 6
3 6 7 8
Different types of non-tree 7 6
edges 7
4 5

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles

In a directed graph, a cycle 0 15


0
must follow same direction 1 0 2
0 → 2 → 3 → 0 is a 1 2
1 10 11 14
cycle
0 → 5 → 1 ← 0 is not 4 5 3 4 3
2 9 12 13
Tree edges
5 6
3 6 7 8
Different types of non-tree 7 6
edges 7
4 5
Forward edges

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles

In a directed graph, a cycle 0 15


0
must follow same direction 1 0 2
0 → 2 → 3 → 0 is a 1 2
1 10 11 14
cycle
0 → 5 → 1 ← 0 is not 4 5 3 4 3
2 9 12 13
Tree edges
5 6
3 6 7 8
Different types of non-tree 7 6
edges 7
4 5
Forward edges
Back edges

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles

In a directed graph, a cycle 0 15


0
must follow same direction 1 0 2
0 → 2 → 3 → 0 is a 1 2
1 10 11 14
cycle
0 → 5 → 1 ← 0 is not 4 5 3 4 3
2 9 12 13
Tree edges
5 6
3 6 7 8
Different types of non-tree 7 6
edges 7
4 5
Forward edges
Back edges
Cross edges

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 8 / 11
Directed cycles

In a directed graph, a cycle 0 15


0
must follow same direction 1 0 2
0 → 2 → 3 → 0 is a 1 2
1 10 11 14
cycle
0 → 5 → 1 ← 0 is not 4 5 3 4 3
2 9 12 13
Tree edges
5 6
3 6 7 8
Different types of non-tree 7 6
edges 7
4 5
Forward edges
Back edges Only back edges
correspond to cycles
Cross edges

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

Take directions into account


1 0 2

4 5 3

7 6

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 10 / 11
Connectivity in directed graphs

Take directions into account


Vertices i and j are strongly connected if there 1 0 2
is a path from i to j and a path from j to i

4 5 3

7 6

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 10 / 11
Connectivity in directed graphs

Take directions into account


Vertices i and j are strongly connected if there 1 0 2
is a path from i to j and a path from j to i
Directed graphs can be decomposed into
strongly connected components (SCCs) 4 5 3
Within an SCC, each pair of vertices is
strongly connected

7 6

Madhavan Mukund Applications of BFS and DFS PDSA using Python Week 4 10 / 11
Connectivity in directed graphs

Take directions into account


Vertices i and j are strongly connected if there 1 0 2
is a path from i to j and a path from j to i
Directed graphs can be decomposed into
strongly connected components (SCCs) 4 5 3
Within an SCC, each pair of vertices is
strongly connected

DFS numbering can be used to compute SCCs


7 6

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

In a directed graph, non-tree edges can be forward / back / cross


Only back edges generate cycles
Classify non-tree edges using DFS numbering

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

In a directed graph, non-tree edges can be forward / back / cross


Only back edges generate cycles
Classify non-tree edges using DFS numbering

Directed graphs decompose into strongly connected components


DFS numbering can be used to compute SCC decomposition

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

In a directed graph, non-tree edges can be forward / back / cross


Only back edges generate cycles
Classify non-tree edges using DFS numbering

Directed graphs decompose into strongly connected components


DFS numbering can be used to compute SCC decomposition

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

In a directed graph, non-tree edges can be forward / back / cross


Only back edges generate cycles
Classify non-tree edges using DFS numbering

Directed graphs decompose into strongly connected components


DFS numbering can be used to compute SCC decomposition

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

You might also like