Sie sind auf Seite 1von 22

Depth First Search

Depth First Search


In DFS
• Each vertex is initially white
– It is grayed when it is discovered in the search, and
– It is blackened when it is finished, that is, when its
adjacency list has been examined completely.
Discovery and Finish Times
• It guarantees that each vertex ends up in exactly one
depth-first tree, so that these trees are disjoint.

• It timestamps each vertex


– the first timestamp d[v] records when v is first
discovered (and grayed), and
– the second timestamp f [v] records when the search
finishes examining v's adjacency list (and blackens v).

• For every vertex u


d[u] < f[u]
Algorithm: Depth First Search

DFS-Visit(u)
DFS(G)
1 for each vertex u  V [G] 1 color [u] ← GRAY
2 time ← time + 1
2 do color [u] ← WHITE
3 d [u] ← time
3 π[u] ← NIL
4 for each v  Adj [u]
4 time ← 0
5 for each vertex u  V [G] 5 do if color [v] = WHITE
6 then π[v] ← u
6 do if color [u] = WHITE
7 DFS-Visit (v)
7 then DFS-Visit (u)
8 color [u] ← BLACK
9 f[u] ← time ← time + 1
Depth First Search
For each vertex u  V(G)
color [u]  WHITE
π [u]  NIL
u v w
time  0

x y z
Depth First Search
Considering white vertex u
color [u]  GRAY
d[u]  time + 1 = 0 + 1 = 1
Adj[u] = v, x
u v w
1/ color [v] = WHITE
π[v] ← u
DFS-VISIT (v)

x y z
Depth First Search

color [v]  GRAY


d[v]  time + 1 = 1 + 1 = 2
Adj[v] = y
u v w
1/ 2/ color [y] = WHITE
π[y] ← v
DFS-VISIT (y)

x y z
Depth First Search

color [y]  GRAY


d[y]  time + 1 = 2 + 1 = 3
Adj[y] = x
u v w
1/ 2/ color [x] = WHITE
π[x] ← y
DFS-VISIT (x)

3/
x y z
Depth First Search

color [x]  GRAY


d[x]  time + 1 = 3 + 1 = 4
u v w
Adj[x] = v
1/ 2/ color [v] ≠ WHITE

4/ 3/
x y z
Depth First Search

The edge (x, v) is a back


u v w edge that is a non tree edge
1/ 2/ and is labeled as B

4/ 3/
x y z
Depth First Search
The vertex x is finished.

color [x]  BLACK


u v w
f[x]  time + 1 = 4 + 1 = 5
1/ 2/
B

4/5 3/
x y z
Depth First Search

The vertex y is finished.

color [y]  BLACK


u v w f[y]  time + 1 = 5 + 1 = 6
1/ 2/
B

4/5 3/6
x y z
Depth First Search

The vertex v is finished.


u v w
1/ 2/7 color [v]  BLACK
f[v]  time + 1 = 6 + 1 = 7
B

4/5 3/6
x y z
Depth First Search
The edge (u, x) is a forward
edge that is a non tree edge
and is labeled as F
u v w
1/ 2/7
F B

4/5 3/6
x y z
Depth First Search
The vertex u is finished.

color [u]  BLACK


u v w
f[u]  time + 1 = 7 + 1 = 8
1/8 2/7
F B

4/5 3/6
x y z
Depth First Search
Considering white vertex w
color [w]  GRAY
d[w]  time + 1 = 8 + 1 = 9
u v w
Adj[w] = y, z
1/8 2/7 9/
F B color [y] ≠ WHITE

4/5 3/6 color [z] = WHITE


x y z π[z] ← w
DFS-VISIT (z)
Depth First Search

The edge (w, y) is a cross


edge that is a non tree edge
u v w and is labeled as C
1/8 2/7 9/
F B C

4/5 3/6
x y z
Depth First Search

color [z]  GRAY


d[z]  time + 1 = 9 + 1 = 10
u v w Adj[z] = z
1/8 2/7 9/
B C color [z] ≠ WHITE
F

4/5 3/6 10/


x y z
Depth First Search
The edge (z, z) is a back
edge that is a non tree edge
and is labeled as B
u v w
1/8 2/7 9/
F B C B

4/5 3/6 10/


x y z
Depth First Search
The vertex z is finished.

color [z]  BLACK


u v w f[z]  time + 1 = 10 + 1 = 11
1/8 2/7 9/
F B C B

4/5 3/6 10/11


x y z
Depth First Search
The vertex w is finished.

color [w]  BLACK


u v w
f[w]  time + 1 = 11 + 1 = 12
1/8 2/7 9/12
F B C B

4/5 3/6 10/11


x y z
Properties of Depth First Search
• It yields valuable information about structure of a graph.
– Predecessor subgraph Gπ does indeed form a forest of
trees, since the structure of the depth-first trees exactly
mirrors the structure of recursive calls of DFS-VISIT.

• Discovery and finishing times have parenthesis


structure.
– If we represent the discovery of vertex u with a left
parenthesis “(u” and represent its finishing by a right
parenthesis “u)”, then
– history of discoveries and finishing makes well-formed
expression in a sense that parentheses properly nested.

Das könnte Ihnen auch gefallen