Docsity

Depth-first Search Algorithm and Edge Classification - Prof. Zhaojun Bai, Assignments of Computer Science

Toggle sidebar
bg1
ECS122A Handout May 17, 2009
Depth-first search
DFS(G)
for each vertex u∈V
do color[u] = white
endfor
time = 0
for each vertex u∈Vdo
if color[u] = white then
DFS-visit(u)
endif
end for
DFS-visit(u)
color[u] = gray
time = time + 1
d[u] = time
for each v∈Adj[u] do
if color[v] = white then
DFS-visit(v)
color[u] = black
time = time + 1
f[u] = time
Classification of edges
•Tree edge (T): encounter new (white) vertex
gray to white
•Back edge (B): from descendant to ancester
gray to gray
•Forward edge (F): from ancestor descendant
gray to black
•Cross edge (C): any other edges
gray to black
Note: In an undirected graph, there may be some ambiguity since edge (u, v) and (v, u) are the
same edge. Classify by the first type above that matches. Therefore, if Gis undirected, a DFS
produces only tree (T) and back (B) edges.
1

Partial preview of the text

Download Depth-first Search Algorithm and Edge Classification - Prof. Zhaojun Bai and more Assignments Computer Science in PDF only on Docsity!

ECS122A Handout May 17, 2009

Depth-first search

DFS(G) for each vertex u ∈ V do color[u] = white endfor time = 0 for each vertex u ∈ V do if color[u] = white then DFS-visit(u) endif end for

DFS-visit(u) color[u] = gray time = time + 1 d[u] = time for each v ∈ Adj[u] do if color[v] = white then DFS-visit(v) color[u] = black time = time + 1 f [u] = time

Classification of edges

  • Tree edge (T): encounter new (white) vertex gray to white
  • Back edge (B): from descendant to ancester gray to gray
  • Forward edge (F): from ancestor descendant gray to black
  • Cross edge (C): any other edges gray to black

Note: In an undirected graph, there may be some ambiguity since edge (u, v) and (v, u) are the same edge. Classify by the first type above that matches. Therefore, if G is undirected, a DFS produces only tree (T) and back (B) edges.