Graph Traversal Depth First Search: B.Tech (CSE), Semester 5, Analysis and Design of Algorithms
Graph Traversal Depth First Search: B.Tech (CSE), Semester 5, Analysis and Design of Algorithms
v
Find a sequence of vertices
0
v0, v1, …, vk such that
• v0 is source
v v
2 4
v
Graph Traversal: Depth First Search
• Start from i, visit a neighbour j
1
Depth First Search
1
Start at 4 2
1 3
4
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9
1
Depth First Search
1
Start at 4 2
1 3
4 1
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9
1
Depth First Search
1 1
Start at 4 2
1 3
4 1
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9
4
1
Depth First Search
1 1
Start at 4 2 1
1 3
4 1
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9
4 1
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5
Visite
6
2 5 d
7
8
4
6 9
7 10
StackofSuspended Vertices
8 9
4 1 2
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5
Visite
6
5 d
2 7
8
4 9
6 7 10
StackofSuspended Vertices
8 9
4 1
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5
Visite
6
5 d
2 7
8
4 9
6 7 10
StackofSuspended Vertices
8 9
4
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5
Visite
6
5 d
2 7
8
4
9
6 7 10
StackofSuspended Vertices
8 9
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6
5 d
2 7
8
4 9
6 7 10
StackofSuspended Vertices
8 9
4
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7
8
4 9
6 7 10
StackofSuspended Vertices
8 9
4 5
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8
4 9
6 7 10
StackofSuspended Vertices
8 9
4 5 6
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8
4 9
6 7 10
StackofSuspended Vertices
8 9
4 5
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8 1
4 9
6 7 10
StackofSuspended Vertices
8 9
4 5 6
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8 1
4 9 1
6 7 10
StackofSuspended Vertices
8 9
4 5 6 8
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4
8 1
6 9 1
7 10 1
StackofSuspended Vertices
8 9
4 5 6 8 9
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4
8 1
6 9 1
7 10 1
StackofSuspended Vertices
8 9
4 5 6 8
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4
8 1
6 9 1
7 10 1
StackofSuspended Vertices
8 9
4 5 6
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8 1
4 9 1
6 7 10 1
StackofSuspended Vertices
8 9
4 5
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
5 d
2 7 1
8 1
4 9 1
6 7 10 1
StackofSuspended Vertices
8 9
4
1
Depth First Search
1 1
Start at 4 2 1
1 3 1
4 1
3 5 1
Visite
6 1
2 5 d
7 1
4
8 1
6 9 1
7 10 1
StackofSuspended Vertices
8 9
1
Depth First Search
//Initialization
for j = 1..n {visited[j] = 0; parent [j] =-1}
//Mark I as visited
visited[i] = 1;
re d !
explo
Depth First Search
DFS finds all the nodes reachable from the starting point
start
D E A
F
A B E
C
B
Lemma. Let s be any node. G is strongly connected iff every node is reachable
from s, and s is reachable from every node.
ok if paths overlap
s u
v
Strong Connectivity
Theorem. Can determine if G is strongly connected in O(m + n) time. Pf.
・ Pick any node s.
・
・ Run
Run BFS
BFS from
from ss in
in G.
G reverse.
・ Return true if all nodes reached in both BFS executions.
s tro ng ly c o n n e c t e d n o t st ro ng ly c o n ne c t e d
Strong Components
Example
Strongly Connected Components
cd
abe
h
fg