Document

Depth-First Search
1.
2.
3.
Algorithm
Running time analysis
DFS property analysis
Depth-First Search - Ideas
In DFS, edges are explored out of the most recently
discovered vertex v. Only edges to undiscovered
vertices are explored.
When all of v’s incident edges have been explored,
search will backtrack to explore edges leaving the
vertex from which v is discovered.
The process continues until all the vertices reachable
from the original source vertex are discovered.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
2
Depth-First Search - Ideas
If any undiscovered vertex remain, then one of them is
selected as a new source vertex, and the search repeats
from this new source vertex.
The process is repeated until all the vertices are
discovered.
The strategy of DFS is to search “deeper” vertex
whenever it is possible.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
3
Four Arrays for DFS Algorithm
color[u]: the color of each vertex visited



WHITE means undiscovered
GRAY means discovered but not finished processing
BLACK means finished processing.
[u]: the predecessor of u, indicating the vertex from which u is
discovered.
d[u]: discovery time, a counter indicating when vertex u is
discovered.
f[u]: finishing time, a counter indicating when the processing of
vertex u (and the processing of all its descendants ) is finished.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
4
DFS Algorithm
Graph is G = (V, E). As the algorithm runs, each vertex
is given a discovery time d[u], indicating when it is
discovered and a finish time f[u], indicating when all its
descendants are finished processing.
The output is a collection of trees, i.e., a forest.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
5
DFS Algorithm
DFS(G)
1 for each vertex u  V(G) do
2 color[u]  WHITE
3 [u]  NIL
4 endfor
5 time  0
6 for each vertex u  V(G) do
7 if color[u] = WHITE then
DFS-Visit(u)
8 endfor
9 return
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
6
DFS Algorithm
DFS-Visit(u)
1 color[u]  GRAY
2 time  time + 1
3 d[u]  time
4 for each v  adj[u] do
5 if color[v] = WHITE then
6
[v]  u
7
DFS-Visit(v)
8 endif
9 endfor
© 2005~11 SDU
10 color[u]  BLACK
11 time  time + 1
12 f[u]  time
13 return
http://www.sciencenet.cn/u/algzhang/
7
An Example
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
8
An Example
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
9
What does DFS do?
Given a graph G, it traverses all vertices of G and
Constructed a collection of rooted trees together with a
set of source vertices (roots)
Outputs two arrays d[ ]/f[ ]
Note: forest is stored in array [ ], the [ ] value of a root
is null.
DFS forest: DFS creates a forest F = (V, Ef), where
Ef = {([u], u)| where [ ] is computed in the DFSVISIT calls}
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
10
Running time analysis of DFS
DFS(G)
for each u in V
do color[u]=white;
[u]=null;
//2n
time=0;
//1
for each u in V
//2n
do if color[u]= white
then DFS-VISIT(u); //sum: 4n+1
© 2005~11 SDU
DFS-VISIT(u)
color[u]=gray;
//1
time=time+1;
//1
d[u]=time;
//1
for each v adj[u]
//outdegree of v //1
do if color[v]=white //1
then [v]=u;
//1
DFS-VISIT(v); //1
color[u]=black; //1
time=time+1; //1
f[u]=time; //1 sum: T(u)<=7+4odeg(u)
http://www.sciencenet.cn/u/algzhang/
11
Running time analysis of DFS
Total running time is
(4V  1)   T (u )
uV
T (u )   (7  4o deg( u ))  7V  4 E
where u
V
uV
and since each vertex is grayed exactly once,
DFS-VISIT(v) is called exactly once, hence
 T (u)   (7  2o deg( u))  7V  2E
uV
uV
Hence, we have the running time T(V, E) satisfying:
11V  2 E  1  T (V , E )  11V  4 E  1
i.e., T(V, E)= (V+E)
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
12
Properties of DFS
Definition: In a rooted tree, any vertex u except v on
the root-v path is called an ancestor of v, and v is called
a descendant of u.
Note: u = [v] iff DFS-VISIT(v) was called during a
search of u’s adjacency list. u is called the parent of v.
So:
Vertex v is a descendant of vertex u in the DFS forest
iff v is discovered during the time in which u is gray.
(when v is discovered, DFS-VISIT([v]) is called,
DFS-VISIT([[v]]), …, DFS-VISIT(u) is called.)
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
13
Parenthesis Theorem
In any DFS of a graph, for each pair of vertices u, v,
exactly one of the following conditions holds:
1. u is a descendant of v, and [d[u], f[u]] is a subinterval
of [d[v], f[v]].
2. u is an ancestor of v, and [d[v], f[v]] is a subinterval
of [d[u], f[u]].
3. Neither u is a descendant of v nor v is a descendant of
u, and [d[u], f[u]] and [d[v], f[v]] are disjoint.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
14
An Example
Is
ds
Iv
Iz
Iy
It
fs dt
ft
Iu
Iw
Ix
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
15
Proof
It is obvious that each pair u,v satisfies exactly one of
the following:
1. u is a descendant of v
2. v is a descendant of u
3. neither is descendant of the other
It is enough to prove that
1 holds if and only if 1 [d[u], f[u]] is a subinterval of [d[v],
f[v]];
2 holds if and only if [d[v], f[v]] is a subinterval of [d[u],
f[u]];
3 holds if and only if [d[u], f[u]] and [d[v], f[v]] are disjoint.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
16
Proof (cont.)
If 1 holds, then u must be discovered when v is gray,
that means d[v] < d[u] < f[u] < f[v].
In the opposite, if d[v] < d[u] < f[u] < f[v], then u is
discovered when v is gray, that is, u is a descendant of v.
Then second case is similar to the above.
If neither is descendant of the other. Without loss of
generality, suppose d[u] < d[v].
Then, since v is not a descendant of u, v must be
discovered after u finishes, that is, f[u] < d[v], hence,
[d[u], f[u]] is disjoint with [d[v], f[v]].
In the opposite, if [d[u], f[u]] and [d[v], f[v]] are disjoint,
then neither is discovered when the other is gray. So,
neither is descendant of the other.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
17
Parenthesis Theorem - Proof
Wlog, assume d[u] < d[v].
case 1, d[v] < f[u].

v is discovered when u is still not finished (marked gray),
therefore, v is a descendant of u, and since v is discovered
more recently than u, all its outgoing edges are explored,
and v is finished, before the search returns to and finishes u.
Hence, [d[v], f[v]] is subinterval of [d[u], f[u]].
case 2, d[v] > f[u].

[d[u], f[u]] and [d[v], f[v]] are disjoint. Hence, neither vertex
was discovered while the other was gray, and so neither
vertex is a descendant of the other.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
18
Nesting of descendants’ intervals
Corollary: vertex v is a proper descendant of vertex u in
the depth-first forest for a graph G if and only if
d[u] < d[v] < f[v] < f[u].
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
19
White Path Theorem
In a depth-first forest of a graph G = (V, E), vertex v is a
descendant of u if and only if at time d[u], vertex v can
be reached from u along a path consisting entirely of
white vertices.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
20
Proof of White-path theorem
(=>) Assume v is a descendant of u, there is a unique
path from u to v in the DFS forest. Suppose w is an
arbitrary vertex on the path from u to v in the depthfirst forest, then w is a descendant of u. By the nesting
of descendants’ interval corollary, d[u]<d[w], and so at
time d[u], w is white.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
21
Proof of White-path theorem
(<=) On the contrary, suppose at time d[u], there is a
path from u to v consisting only of white vertices, but v
does not become a descendant of u in the depth-first
tree. Without loss of generality, assume that every other
vertex along the path becomes a descendant of u.
(otherwise, let v be the closest vertex to u along the
path that does not become a descendant of u.) Let w be
the predecessor of v in the path, so that w is a
descendant of u, and by nesting of descendants’
intervals corollary, f[w] <= f[u].
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
22
Proof of White-path theorem
Note that v must be discovered after u is discovered,
but before w is finished. Therefore, d[u] < d[v] < f[w]
<= f[u]. Parenthesis theorem implies that either
d[u]<d[v]<f[v]<d[w]<f[w]<=f[u] or
d[u]<d[w]<d[v]<f[v]<f[w]<=f[u], in either case the
interval [d[v], f[v]] is contained entirely within the
interval [d[u], f[u]]. By the nesting of descendants’
intervals corollary, v must be a descendant of u, a
controdiction.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
23
Nesting of descendants’ intervals
Corollary: vertex v is a proper descendant of vertex u in
the depth-first forest for a graph G if and only if d[u]<
d[v] < f[v] < f[u].
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
24
Classification of Edges
Let F = (V, E) be the DFS forest of directed graph G = (V,
E), then the edges of G can be classified into four
categories (the non-tree edges are classified according
to the relationship between the end points):
1. Tree edges: edges in F.
2. Back edges: edges (u, v) connecting a vertex u to its
ancestor v in the forest.

Self-loops in directed graphs are considered to be back
edges.
3. Forward edges: those non-tree edges (u, v) connecting
a vertex u to its descendant v in the forest.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
25
Classification of Edges
4. Cross edges: all other edges. They can go between
vertices in the same depth-first tree as long as neither
vertex is ancestor of the other, or they go between
vertices in different trees.
Note: remembering that for each pair of vertices u, v,
there are only three probabilities between them (u is
descendant of v, opposite, neither).
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
26
Classification of Edges (Example)
1/8
a
T
B
C
9/14
d
b 2/7
T
10/11
3/6
e
T
T
F
c
12/13
C
g
T
f
4/5
© 2005~11 SDU
T: tree edge
F: forward edge
B: back edge
C: cross edge
http://www.sciencenet.cn/u/algzhang/
27
Observation
 In a depth-first search, an edge (u, v) can be
classified according to the color of v when (u, v) is
first explored:



WHITE indicates a tree edge
GRAY indicates a back edge
BLACK indicates a forward or cross edge
− d[u] < d[v] means a a forward edge
− d[u] > d[v] means a cross edge
Problem: Modify the DFS algorithm to classify edges.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
28
Classifying Edges in An Undirected Graph
In an undirected graph, (u, v) and (v, u) are in fact the
same edge. To avoid ambiguity, we classify the edge
according to whichever of (u, v) or (v, u) is
encountered first during the execution of the DFS
algorithm.
In a depth-first search of an undirected graph G,
every edge of G is either a tree edge or a back edge.
(see next page for the proof)
Forward edges and Cross edges will never occur in a
depth-first search of an undirected graph.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
29
Classifying Edges in An Undirected Graph
TH 22.10. In a depth-first search of an undirected
graph G, every edge of G is either a tree edge or a
back edge.
Proof: let (u, v) be an arbitrary edge of G, and w.l.o.g.,
suppose d[u] < d[v].
According to the white-path theorem ((u, v) is a white
path), v will become a descendant of u. When we scan
u’s adjacency list in DFS-VISIT(u), v will be found
since (u, v) is an edge of G.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
30
Proof of TH 22.10 (cont.)
If v is WHITE, which indicates (u, v) is explored first
in the direction from u to v, (u, v) becomes a tree edge.
O.w. v is not WHITE. Then DFS-Visit(v) has been
called before the scan comes to v when scan u’s
adjacency list in DFS-Visit(u). Note that u is also in v’s
adjacency list, so (u, v) must be first explored in the
direction from v to u. Since u is an ancestor of v, (v, u)
becomes a back edge.
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
31
Thanks for attention!
© 2005~11 SDU
http://www.sciencenet.cn/u/algzhang/
32