Lec 17 Dfs
Lec 17 Dfs
IITB India
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 1
Topic 17.1
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 2
Let us solve the maze again
Breadth-first search is about considering all available options before exploring further.
We can have another strategy of search: explore a choice fully before considering another choice.
entry d g
f a b e exit
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 3
Algorithm: DFS for search
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 4
Example: DFS
Initially: S = [entry ]
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 5
Algorithm: Recursive DFS
Recursive description of DFS is easier to follow.
Algorithm 17.2: DFS( graph G = (V , E ), vertex v )
1 for v ∈ V do
2 v .visited := False
3 DFSRec(G , v )
v can be in three possible states
Algorithm 17.3: DFSRec( Graph G , vertex v ) ▶ v is not visited
1 v .visited := True; ▶ v is on call stack
2 for w ∈ G .adjacent(v ) do
▶ v is visited and not on call stack
3 if w .visited == False then
4 DFSRec(w )
Exercise 17.1
Why there is no stack in the recursive DFS?
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 6
DFS Tree
Algorithm 17.4: DFS( graph G = (V , E ), vertex v )
1 global time := 0;
2 for v ∈ V do
3 v .visited := False
4 DFSRec(G , v )
7 v .departure := time + +;
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 7
Example: recursive execution
Green numbers are arrival times and blue numbers are the departure times.
DFSRec(G ,entry ) 0
entry 5
g 6
d 7
DFSRec(G ,a)
DFSRec(G ,f )
Exit DFSRec(G ,f )
1 4 9
a b e exit
DFSRec(G ,b) 8
Exercise 17.3
a. Is blue edge possible? yes.
b. Is red edge possible? no.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 9
Reachable coverage
Theorem 17.1
Let G = (V , E ) be a connected graph. For each {v1 , v2 } ∈ E , v1 is ancestor of v2 in DFS tree or
vice versa.
Proof.
Without loss of generality, we assume v1 .arrival < v2 .arrival at the end of DFS.
During the run of DFSRec(G , v1 ), v2 will be visited in one of the following two ways.
1. DFSRec(G , v2 ) is called by DFSRec(G , v1 ). v1 is parent of v2 .
2. DFSRec(G , v2 ) has been called already, when the loop in DFSRec(G , v1 ) reaches to v2 .
In either case, v2 .arrival < v1 .departure.
In case 1, we call {v1 , v2 } a tree edge. In case 2, we call {v1 , v2 } a back edge.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 10
Running time of DFS
Theorem 17.2
The running time of DFS is O(|E | + |V |).
Proof.
The total number of recursive calls and iterations of initializations is O(|V |).
Exercise 17.4
Prove that the running time besides initialization is O(|E |).
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 11
Topic 17.2
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 12
Example : DFS on the directed graph
Consider the following directed graph
0 5 6
15
entry 12 d g 7
2 9
3 f a b e exit 10
1 14 4 13 8 11
Topological sort
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 14
Algorithm: Topological sort using DFS
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 15
Topic 17.4
Problems
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 16
Algorithm: Topological sort using DFS
Exercise 17.5
Is this variation of DFS correct? If yes, will it be more efficient or less?
Algorithm 17.8: DFSRec( Graph G , vertex v )
v .visited = True;
for w ∈ G .adjacent(v ) do
if v .visited then
4 DFSRec(w )
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 17
End of Lecture 17
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 18