Graph Traversal - II
Depth- First Search
• edges are explored out of the most recently discovered
vertex
Depth- First Search
• π[v] - predecessor of v
• d[v] - time when v is discovered ( i.e., v turns gray)
• f[v] - when all neighbours of v are visited (v turns black)
Depth- First Search
DFS(G)
for each vertex u ∈ V[G]
color[u] = white
π[u] = NIL
time = 0
for each u ∈ V[G]
if color[u]=white
DFS-VISIT(u)
Depth- First Search
DFS-VISIT(u)
color[u]=gray
time = time+1
d[u] = time
for each v ∈ Adj[u]
if color[v] = white
π[v] = u
DFS-VISIT(v)
color[u] = black
f[u]= time
time = time+1
Depth- First Search
Properties of DFS :
The predecessor graph form a forest
u = π[v] if and only if DFS-VISIT(v) was called during a
search of u’-s adjacency list.
Depth- First Search
Properties of DFS :
Parenthesis Structure :
Depth- First Search
Properties of DFS :
Parenthesis theorem
In any DFS of a directed or undirected graph G = (V,E), for any two
vertices u and v, exactly one of the following 3 conditions holds.
1. The intervals [d[u],f[u]] and [[d[v],f[v]] are entirely disjoint.
Neither u nor v is a descendant of the other in the DFS forest.
2. [d[u],f[u]] is entirely contained in [[d[v],f[v]] and u is a
descendant of v in a DFS tree
3. [[d[v],f[v]] is entirely contained in [d[u],f[u]] and v is a
descendant of u in a DFS tree
Depth- First Search
Properties of DFS :
If v is a proper descendant of u if and only if d[u] < d[v] <
f[v] < f[u] .
Depth- First Search
Properties of DFS :
White-Path theorem
v is a descendant of u if and only if at the time d[u], v can
be reached from u along a path consisting entirely of
white vertices.
Depth- First Search
Properties of DFS :
Classi cation of edges :
1. Tree Edges :
2. Back Edges :
3. Forward Edges :
4. Cross Edges :
fi
Depth- First Search
Properties of DFS :
Classi cation of edges :
1. Tree Edges : (u,v) is a tree edge if v was rst discovered by
exploring edge (u,v)
2. Back Edges :(u,v) where v is an ancestor of u
3. Forward Edges : non-tree edges (u,v) connecting u to a
descendant v
4. Cross Edges : Edges between different DFS trees or edges
between vertices in the same tree, when they are not
ascendant/descendant of each other.
fi
fi