#### Question

Show that we can use a depth-first search of an undirected graph G to identify the connected components of G, and that the depth-first forest contains as many trees as G has connected components. More precisely, show how to modify depth-first search so that it assigns to each vertex an integer label v.cc between 1 and k, where k is the number of connected components of G, such that u.cc = v.cc if and only if u and are in the same connected component.

Verified

#### Step 1

1 of 3

Take a look at the $\textsf{DFS}$ algorithm. The key observation here is that we are in an undirected graph so each time the function $\textsf{DFS-Visit}$ is called in $\textsf{DFS}$, it will discover all connected vertices, that is, it will discover $\textbf{one connected component }$of the graph. That is where we can sneak in our $.cc$ label that identifies all the components of the graph. The algorithm is as follows:

## Create an account to view solutions

#### Computer Organization and Design MIPS Edition: The Hardware/Software Interface

5th EditionDavid A. Patterson, John L. Hennessy
220 solutions

#### Fundamentals of Database Systems

7th EditionRamez Elmasri, Shamkant B. Navathe
687 solutions

#### Introduction to Algorithms

3rd EditionCharles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
726 solutions

#### Introduction to the Theory of Computation

3rd EditionMichael Sipser
389 solutions