Graph Algorithms - DFS: Outlines: Graphs Part-3
Graph Algorithms - DFS: Outlines: Graphs Part-3
PART-3
Depth-First Search:
DFS Algorithm
DFS Example
DFS Running Time
DFS Predecessor Subgraph
DFS Time Stamping
DFS Edge Classification
DFS and Graph Cycles
1
Depth-First Search
Depth-First Search
Initialize
color all vertices white
Visit each and every white vertex using DFS-Visit
2
Depth-First Search
Init all
vertices
Visit all
children
recursively
3
Depth-First Search: Algorithm
DFS(G) DFS_Visit(u)
{ {
for each vertex u V[G] color[u] = GRAY;
{ time = time+1;
color[u] = WHITE; d[u] = time;
p[u] = NIL; for each v Adj[u]
}
time = 0; if (color[v] == WHITE)
for each vertex u V[G] p[v] = u;
{ DFS_Visit(v);
if (color[u] == WHITE)
DFS_Visit(u); color[u] = BLACK;
} time = time+1;
} f[u] = time;
}
4
Depth-First Search: Algorithm
DFS(G) DFS_Visit(u)
{ {
1 for each vertex u V[G] 1 color[u] = GRAY;
{ 2 time = time+1;
2 color[u] = WHITE; 3 d[u] = time;
3 p[u] = NIL 4 for each v Adj[u]
}
4 time = 0; 5 if (color[v] == WHITE)
5 for each vertex u V[G] 6 p[v] = u;
{ 7 DFS_Visit(v);
6 if (color[u] == WHITE)
7 DFS_Visit(u); 8 color[u] = BLACK;
} 9 time = time+1;
} 10 f[u] = time;
}
10
5
Depth-First Search: DFS-Visit Procedure
DFS Example
source
vertex
12
6
DFS Example
source
vertex
d f
1 | | |
| |
| | |
13
DFS Example
source
vertex
d f
1 | | |
2 | |
| | |
14
7
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | | |
15
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 | |
16
8
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | |
17
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | 6 |
18
9
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
19
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 9 |
3 | 4 5 | 6 |
10
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 9 |10
3 | 4 5 | 6 |
21
DFS Example
source
vertex
d f
1 | 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
22
11
DFS Example
source
vertex
d f
1 |12 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
23
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 |
24
12
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|
25
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|15
26
13
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
27
28
14
Depth-First Search: Algorithm
DFS(G) DFS_Visit(u)
{ {
for each vertex u V[G] color[u] = GRAY;
{ time = time+1;
color[u] = WHITE; d[u] = time;
p[u] = NIL for each v Adj[u]
}
time = 0; if (color[v] == WHITE)
for each vertex u V[G] p[v] = u;
{
(V) if (color[u] == WHITE)
DFS_Visit(v);
DFS_Visit(u);
} color[u] = BLACK;
} time = time+1;
f[u] = time;
}
29
30
15
DFS: Running Time
Running time
the loops in DFS take time (V) each, excluding the
time to execute DFS-Visit
DFS-Visit is called once for every vertex
its only invoked on white vertices, and
paints the vertex gray immediately
Adj[v] ( E )
vV
Predecessor Subgraph
32
16
DFS Timestamping
33
DFS Timestamping
Vertex u is
white before time d[u]
gray between time d[u] and time f[u], and
black thereafter
34
17
DFS Edge Classification
Tree edge (gray to white)
encounter new vertices (white)
included in depth-first forest Gp
Back edge (gray to gray)
from descendant to ancestor
non-tree edges in depth-first tree
35
36
18
DFS Edge Classification
37
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
38
19
DFS: Kinds Of Edges
Theorem 22.10:
If G is undirected, a DFS produces only tree and
back edges
source
Proof by contradiction: u
Assume there’s a forward edge
But F? edge must actually be a F?
back edge (why?)
v
Since edge (w, u) is from
descendant to ancestor
(gray to gray)
w
39
Proof by contradiction:
Assume there’s a cross edge
source
But C? edge cannot be cross:
must be explored from one of the
vertices it connects, becoming a tree
vertex, before other vertex is explored
So in fact the picture is wrong…both
blue and red edges cannot in fact be
tree edges.
One should be tree edge and
the other should be back edge (cycle graph) C?
40
20
DFS and Graph Cycles
Theorem:
An undirected graph is acyclic (no cycles) iff a
DFS yields no back edges
If acyclic, no back edges (because a back edge
implies a cycle
If no back edges, acyclic
No back edges implies only tree edges (Why?)
Only tree edges implies we have a tree or a forest
Which by definition is acyclic
Thus, can run DFS to find whether a graph has a
cycle. (How)
41
DFS Example
u v w u v w u v w
1/ 1/ 2/ 1/ 2/
3/
x y z x y z x y z
u v w u v w u v w
1/ 2/ 1/ 2/ 1/ 2/
B B
4/ 3/ 4/ 3/ 4/5 3/
x y z x y z x y z
42
21
DFS Example
u v w u v w u v w
1/ 2/ 1/ 2/7 1/ 2/7
B B F B
4/5 3/6 4/5 3/6 4/5 3/6
x y z x y z x y z
u v w u v w u v w
1/8 2/7 1/8 2/7 9/ 1/8 2/7 9/
B B B C
F F F
4/5 3/6 4/5 3/6 4/5 3/6
x y z x y z x y z
43
DFS Example
u v w u v w u v w
1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/
B C B C B C
F F F
4/5 3/6 10/ 4/5 3/6 10/ B 4/5 3/6 10/11 B
x y z x y z x y z
u v w
If you have a back edge in a
1/8 2/7 9/12
F B C directed graph, then you may have
4/5 3/6 10/11 a cycle or must have a cycle?
B
x y z
44
22