Introduction To Algorithms, Cormen Et Al, Chap22 Solutions
Introduction To Algorithms, Cormen Et Al, Chap22 Solutions
X
e2E
T
D
bie bej
bie bje
e2E
Thus,
BB T .i; j / D
22-2
DFS.G/
for each vertex u 2 G:V
u:color D WHITE
u: D NIL
time D 0
counter D 0
for each vertex u 2 G:V
if u:color == WHITE
counter D counter C 1
DFS-V ISIT.G; u; counter/
DFS-V ISIT.G; u; counter/
u:cc D counter
// label the vertex
time D time C 1
u:d D time
u:color D GRAY
for each 2 G:Adju
if :color == WHITE
: D u
DFS-V ISIT.G; ; counter/
u:color D BLACK
time D time C 1
u:f D time
This DFS increments a counter each time DFS-V ISIT is called to grow a new tree
in the DFS forest. Every vertex visited (and added to the tree) by DFS-V ISIT is
labeled with that same counter value. Thus u:cc D :cc if and only if u and are
visited in the same call to DFS-V ISIT from DFS, and the final value of the counter
is the number of calls that were made to DFS-V ISIT by DFS. Also, since every
vertex is visited eventually, every vertex is labeled.
Thus all we need to show is that the vertices visited by each call to DFS-V ISIT
from DFS are exactly the vertices in one connected component of G.
All vertices in a connected component are visited by one call to DFS-V ISIT
from DFS:
Let u be the first vertex in component C visited by DFS-V ISIT. Since a vertex
becomes non-white only when it is visited, all vertices in C are white when
DFS-V ISIT is called for u. Thus, by the white-path theorem, all vertices in C
become descendants of u in the forest, which means that all vertices in C are
visited (by recursive calls to DFS-V ISIT) before DFS-V ISIT returns to DFS.
All vertices visited by one call to DFS-V ISIT from DFS are in the same connected component:
If two vertices are visited in the same call to DFS-V ISIT from DFS, they are in
the same connected component, because vertices are visited only by following
paths in G (by following edges found in adjacency lists, starting from some
vertex).
22-3