DS - Lecture - 19 (DFS)
DS - Lecture - 19 (DFS)
● Input: 1 2
■ G = (V, E) (No source vertex given!) 3
● Goal: 5 4
■ Explore the edges of G to “discover” every vertex in V starting at the most
recent visited node
■ Search may be repeated from multiple sources
● Output:
■ 2 timestamps on each vertex:
○ d[v] = discovery time
○ f[v] = finishing time (done with examining v’s adjacency list)
■ Depth-first forest
Depth-First Search
B C G
8
DFS Example
source
vertex
dS f D F
1 | | |
A
| |
E
| | |
B C G
9
DFS Example
source
vertex
dS f D F
1 | | |
A
2 | |
E
| | |
B C G
10
DFS Example
source
vertex
dS f D F
1 | | |
A
2 | |
E
3 | | |
B C G
11
DFS Example
source
vertex
dS f D F
1 | | |
A
2 | |
E
3 | 4 | |
B C G
12
DFS Example
source
vertex
dS f D F
1 | | |
A
2 | |
E
3 | 4 5 | |
B C G
13
DFS Example
source
vertex
dS f D F
1 | | |
A
2 | |
E
3 | 4 5 | 6 |
B C G
14
DFS Example
source
vertex
dS f D F
1 | | |
A
2 | 7 |
E
3 | 4 5 | 6 |
B C G
15
DFS Example
source
vertex
dS f D F
1 | 8 | |
A
2 | 7 |
E
3 | 4 5 | 6 |
B C G
16
DFS Example
source
vertex
dS f D F
1 | 8 | |
A
2 | 7 9 |
E
3 | 4 5 | 6 |
B C G
What is the structure of the grey vertices?
What do they represent?
17
DFS Example
source
vertex
dS f D F
1 | 8 | |
A
2 | 7 9 |10
E
3 | 4 5 | 6 |
B C G
18
DFS Example
source
vertex
dS f D F
1 | 8 |11 |
A
2 | 7 9 |10
E
3 | 4 5 | 6 |
B C G
19
DFS Example
source
vertex
dS f D F
1 |12 8 |11 |
A
2 | 7 9 |10
E
3 | 4 5 | 6 |
B C G
20
DFS Example
source
vertex
dS f D F
A
2 | 7 9 |10
E
3 | 4 5 | 6 |
B C G
21
DFS Example
source
vertex
dS f D F
A
2 | 7 9 |10
E
3 | 4 5 | 6 14|
B C G
22
DFS Example
source
vertex
dS f D F
A
2 | 7 9 |10
E
3 | 4 5 | 6 14|15
B C G
23
DFS Example
source
vertex
dS f D F
A
2 | 7 9 |10
E
3 | 4 5 | 6 14|15
B C G
24
Depth-First Search Analysis
● This running time argument is an informal
example of amortized analysis
■ “Charge” the exploration of edge to the edge:
○ Each loop in DFS_Visit can be attributed to an edge in
the graph
○ Runs once per edge if directed graph, twice if undirected
○ Thus loop will run in O(E) time, algorithm O(V+E)
Considered linear for graph, adj list requires O(V+E) storage
25
DFS: Kinds of edges
● DFS introduces an important distinction
among edges in the original graph:
■ Tree edge: encounter new (white) vertex
○ The tree edges form a spanning forest
○ Can tree edges form cycles? Why or why not?
No
26
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
Tree edges
27
DFS: Kinds of edges
● DFS introduces an important distinction
among edges in the original graph:
■ Tree edge: encounter new (white) vertex
■ Back edge: from descendent to ancestor
○ Encounter a grey vertex (grey to grey)
○ Self loops are considered as to be back edge.
28
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
30
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
32
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
34
More about the edges
● Let (u,v) is an edge.
■ If (color[v] = WHITE) then (u,v) is a tree edge
■ If (color[v] = GRAY) then (u,v) is a back edge
■ If (color[v] = BLACK) then (u,v) is a
forward/cross edge
○ Forward Edge: d[u]<d[v]
○ Cross Edge: d[u]>d[v]
35
Depth-First Search - Timestamps
a b s c
3/6 2/9 1/10 11/16
B F C
B
4/5 7/8 12/13 14/15
d C e C f C g
36
Depth-First Search - Timestamps
s c
C B
F
b f g
C
a e C
B
C
d
37
Depth-First Search: Detect Edge
Data: color[V], time, DFS_Visit(u)
prev[V],d[V], f[V] {
color[u] = GREY;
DFS(G) // where prog starts time = time+1;
{ d[u] = time;
for each vertex u V for each v Adj[u]
{
{
detect edge type using “color[v]”
color[u] = WHITE; if(color[v] == WHITE){
prev[u]=NIL; prev[v]=u;
DFS_Visit(v);
f[u]=inf; d[u]=inf;
}}
} color[u] = BLACK;
time = 0; time = time+1;
for each vertex u V f[u] = time;
}
if (color[u] == WHITE)
DFS_Visit(u);
}
38
Cycle Detection
● How to detect if there is a cycle using DFS?
● Look for back edges.
39
Reference
● Cormen –
■ Chapter 22 (Elementary Graph Algorithms)
● Exercise –
■ 22.3-4 –Detect edge using d[u], d[v], f[u], f[v]
40