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

The depth-first search (dfs) algorithm for traversing a graph, explaining the coloring scheme and the classification of edges as tree, back, forward, or cross. The dfs algorithm is crucial for graph theory and computer science, and this handout offers a clear and concise explanation.

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-urs
koofers-user-urs 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ECS122A Handout May 17, 2009
Depth-first search
DFS(G)
for each vertex uV
do color[u] = white
endfor
time = 0
for each vertex uVdo
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 vAdj[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.