Data Structures & AlgorithmsModerateGATE CS · TCS NQT

Problem: An undirected forest (a graph with no cycles) has 9 vertices and 6 edges. How many connected components does it have?

  1. A. 4
  2. B. 2
  3. C. 3
  4. D. 5

Correct answer: C. 3

A forest with n vertices and c components has n − c edges, so 9 − 6 = 3 components. Using the edge count directly as components is the classic slip.

More Coding Interview Problems questions

Exams that ask this topic