#### 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.

#### Solution

Verified#### Step 1

1 of 3Take 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: