GATE / CS ARCHIVE

GATE CSE 2024 SET 1 / QUESTION 60

Recover components from DFS-forest edges

Algorithms · Graph Algorithms · Full GATE CSE 2024 Set 1 paper

GATE 2024 Set 1, Q60 2 marks · NAT
Report issue

Graph Algorithms

Question 60, GATE 2024: An undirected graph has 100 vertices. A complete DFS traversal produces a spanning forest containing 40 edges. How many connected components does the graph have?
Tap to enlarge
Answer & solution

Official answer: 60

Approach: DFS starts a new tree exactly when it moves to a previously unvisited connected component. Each resulting tree spans that component.
1. Suppose there are c components and their vertex counts are v1,…,vc. The counts sum to 100.
2. A tree with vi vertices has exactly vi−1 edges. This includes an isolated vertex: vi=1 gives zero edges.
3. Summing over the DFS forest gives 40=Σ(vi−1)=Σvi−c=100−c.
4. Solving yields c=100−40=60.
Answer: 60 connected components. The original graph may have additional nontree edges and cycles; the given 40 counts only forest edges. One can also view each of those 40 tree edges as discovering one nonroot vertex, leaving 60 roots, one for each component.

Original paper, page 35 · Answer key