0% found this document useful (0 votes)
11 views18 pages

Lec 17 Dfs

Uploaded by

Saksham Rathi
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)
11 views18 pages

Lec 17 Dfs

Uploaded by

Saksham Rathi
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/ 18

CS213/293 Data Structure and Algorithms 2023

Lecture 17: Graphs - Depth-first search

Instructor: Ashutosh Gupta

IITB India

Compile date: 2023-10-23

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 1
Topic 17.1

Depth-first search (DFS)

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

Algorithm 17.1: DFS( Graph G = (V , E ), vertex r , Value x )


1 Stack S;
2 set visited;
3 S.push(r );
4 while not S.empty () do
5 v := S.pop();
6 if v .label == x then
7 return v
8 if v ∈
/ visited then
9 visited := visited ∪ {v };
10 for w ∈ G .adjacent(v ) do
11 S.push(w )

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 4
Example: DFS
Initially: S = [entry ]

After visiting entry: S = [a]

After visiting a: S = [f , b] entry d g

After visiting f: S = [b]

After visiting b: S = [d, e]


a b e exit
After visiting d: S = [g , e]

After visiting g: S = [e]

After visiting e: S = [exit, b] f

After visiting exit: Node is found

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 )

Algorithm 17.5: DFSRec( Graph G , vertex v )


1 v .visited := True;
2 v .arrival := time + +;
3 for w ∈ G .adjacent(v ) do
4 if w .visited == False then
5 w .parent := v ;
6 DFSRec(w )

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

DFSRec(G ,d) In the extra edge,


DFSRec(G ,g ) b is ancestor of e
2
Exit DFSRec(G ,g ) 3 f
DFSRec(G ,e)
DFSRec(G ,exit)
Exercise 17.2
What are the exit timings of the remaining nodes?
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 8
Non-tree edges
A run of DFS induces a tree. There are two kinds of possible extra edges.

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.

v1 is ancestor of v2 in the DFS tree. (Why?)

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 |).

In call DFSRec(G , v ), the loop iteration is bounded by degree(v ).

Therefore, the total number of iterations are O(|E |).

Therefore, the running time is O(|E | + |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

Depth-first search on directed graph

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

Now we have three kind of extra-edges.


▶ Forward edge:(b, e), where b.departure > e.departure > e.arrival > b.arrival
▶ Back edge:(f , entry ), where entry .departure > f .departure > f .arrival > entry .arrival
▶ Cross edge:(e, g ), where e.departure > e.arrival > g .departure > g .arrival
There is no other possibility.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 13
Topic 17.3

Topological sort

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 14
Algorithm: Topological sort using DFS

Algorithm 17.6: TopologicalSort( Directed graph G = (V , E ) )


1 Stack Sorted;
2 while ∃v such that v .visited == False do visit(v ) ;

Algorithm 17.7: Visit( Graph G = (V , E ), vertex v )


if v .visited then return;
if v .onPath then throw Cycle found ;
v .onPath = True;
for w ∈ G .adjacent(v ) do
Visit(w )
v .onPath = False;
v .visited = True;
Sorted.push(v );

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

You might also like