You are on page 1of 32

Depth First Search

Instructor: Ashok Singh Sairam


Lecture Plan
• Depth First Search
 Algorithm
 Example
 Complexity Analysis
• Properties of DFS
 Parenthesis Theorem
 DFS Trees

MA512: Data Structures and Algorithms


2
DFS: Idea
• Depth First Search
 From the most recently discovered vertex (v), explore
edges not yet explored
 When all of v’s edges are explored, the search backtracks
leaving the vertex from which v was discovered
 Continue process until all vertices from the original
source are discovered
 If any vertex left undiscovered, select one of them as the
new source and continue DFS process

MA512: Data Structures and Algorithms


3
Depth First Search
• Input: G = (V, E), directed or undirected.
 No source vertex given!
• Output:
 2 timestamps on each vertex. Integers between 1 and 2|V|.
• d[v] = discovery time (v turns from white to gray)
• f [v] = finishing time (v turns from gray to black)
 [v] : predecessor of v = u, such that v was discovered
during the scan of u’s adjacency list.
• Uses the same coloring scheme for vertices as BFS.

MA512: Data Structures and Algorithms


4
DFS: Algorithm
DFS(G) DFS-Visit(u)
1. for each vertex u  V[G] 1. color[u]  GRAY  White vertex u
2. do color[u]  white has been discovered
3. [u]  NIL 2. time  time + 1
4. time  0 3. d[u]  time
5. for each vertex u  V[G] 4. for each v  Adj[u]
6. do if color[u] = white 5. do if color[v] = WHITE
7. then DFS-Visit(u) 6. then [v]  u
7. DFS-Visit(v)
Uses a global timestamp time. 8. color[u]  BLACK  Blacken u;
it is finished.
9. f[u]  time  time + 1

MA512: Data Structures and Algorithms


5
DFS: Example
u v w
1/

x y z

(Courtesy of Prof. Jim Anderson)

MA512: Data Structures and Algorithms


6
u v w
1/ 2/

x y z

MA512: Data Structures and Algorithms


7
u v w
1/ 2/

3/
x y z

MA512: Data Structures and Algorithms


8
u v w
1/ 2/

4/ 3/
x y z

MA512: Data Structures and Algorithms


9
u v w
1/ 2/
B

4/ 3/
x y z
B: Back edge

MA512: Data Structures and Algorithms


10
u v w
1/ 2/
B

4/5 3/
x y z
B: Back edge

MA512: Data Structures and Algorithms


11
u v w
1/ 2/
B

4/5 3/6
x y z
B: Back edge

MA512: Data Structures and Algorithms


12
u v w
1/ 2/7
B

4/5 3/6
x y z
B: Back edge

MA512: Data Structures and Algorithms


13
u v w
1/8 2/7

F B

4/5 3/6
x y z
B: Back edge F: Forward edge

MA512: Data Structures and Algorithms


14
u v w
1/8 2/7 9/

F B

4/5 3/6
x y z
B: Back edge F: Forward edge

MA512: Data Structures and Algorithms


15
u v w
1/8 2/7 9/

F B C

4/5 3/6
x y z
B: Back edge F: Forward edge
C: Cross edge

MA512: Data Structures and Algorithms


16
u v w
1/8 2/7 9/

F B C

4/5 3/6 10/


x y z
B: Back edge F: Forward edge
C: Cross edge

MA512: Data Structures and Algorithms


17
u v w
1/8 2/7 9/

F B C

4/5 3/6 10/ B


x y z

MA512: Data Structures and Algorithms


18
u v w
1/8 2/7 9/

F B C

4/5 3/6 10/11 B


x y z
B: Back edge F: Forward edge
C: Cross edge

MA512: Data Structures and Algorithms


19
u v w
1/8 2/7 9/12

F B C

4/5 3/6 10/11 B


x y
z
B: Back edge F: Forward edge
C: Cross edge

MA512: Data Structures and Algorithms


20
DFS: Analysis
DFS(G) DFS-Visit(u)
Θ(𝑉) 1. for each vertex u  V[G] 1. color[u]  GRAY  White vertex u
has been discovered
2. do color[u]  white
2. time  time + 1
3. [u]  NIL
3. d[u]  time
4. time  0
4. for each v  Adj[u]
Θ(𝑉) 5. for each vertex u  V[G]
5. do if color[v] = WHITE
6. do if color[u] = white
6. then [v]  u Θ(𝐸)
7. then DFS-Visit(u)
7. DFS-Visit(v)
Uses a global timestamp time. 8. color[u]  BLACK  Blacken u;
it is finished.
9. f[u]  time  time + 1

MA512: Data Structures and Algorithms


21
DFS: Analysis
• Loops on lines 1-2 & 5-7 take (V) time, excluding
time to execute DFS-Visit.

• DFS-Visit is called once for each white vertex vV


when it’s painted gray the first time. Lines 3-6 of
DFS-Visit is executed |Adj[v]| times. The total cost
of executing DFS-Visit is vV|Adj[v]| = (E)

• Total running time of DFS is (V+E).

MA512: Data Structures and Algorithms


22
Properties of DFS
• It has two main properties
1. Predecessor subgraph 𝐺𝜋 form a forest of trees
2. The discovery and finishing time have
parenthesis structure

MA512: Data Structures and Algorithms


23
Parenthesis Theorem
For all u, v, exactly one of the following holds:
1. d[u] < f [u] < d[v] < f [v] or d[v] < f [v] < d[u] < f [u] and
neither u nor v is a descendant of the other.
2. d[u] < d[v] < f [v] < f [u] and v is a descendant of u.
3. d[v] < d[u] < f [u] < f [v] and u is a descendant of v.

So d[u] < d[v] < f [u] < f [v] cannot happen.


Like parentheses:
OK: ( ) [ ] ( [ ] ) [ ( ) ]
Not OK: ( [ ) ] [ ( ] )
Corollary
v is a proper descendant of u iff d[u] < d[v] < f [v] < f [u].

MA512: Data Structures and Algorithms


24
y z s t
3/6 2/9 1/10 11/16

B F C B

4/5 7/8 C 12/13 C 14/15


C
x w v u

MA512: Data Structures and Algorithms


25
Depth First Trees
• Predecessor subgraph defined slightly different
from that of BFS.
• The predecessor subgraph of DFS is G = (V, E)
where E ={([v],v) : v  V and [v]  NIL}.
 How does it differ from that of BFS?
 The predecessor subgraph G forms a depth-first forest

composed of several depth-first trees. The edges in E


are called tree edges.
Definition:
Forest: An acyclic graph G that may be disconnected.

MA512: Data Structures and Algorithms


26
White-path Theorem
• v is a descendant of u if and only if at time d[u],
there is a path u v consisting of only white
vertices. (Except for u, which was just colored
gray.)

MA512: Data Structures and Algorithms


27
Review
• Perform a recursive depth-first traversal on this
graph
 start from node A

MA512: Data Structures and Algorithms


28
Theorem:
In DFS of an undirected graph, we get only tree and back edges.
No forward or cross edges.

MA512: Data Structures and Algorithms


29
Classification of Edges
• Tree edge
 Edges in the depth-first forest.
 Found by exploring (u, v).
• Back edge
 (u, v), where u is a descendant of v (in the depth-first tree).
• Forward edge
• (u, v), where v is a descendant of u, but not a tree
edge.
• Cross edge
 Any other edge. Can go between vertices in same depth-first
tree or in different depth-first trees.

MA512: Data Structures and Algorithms


30
Exercise

Click to add text

MA512: Data Structures and Algorithms


31
Acknowledgement
David A. Plaisted
Department of Computer Science
University of North Carolina at Chapel Hill

Analysis of Algorithms
32

You might also like