GATE / CS ARCHIVE

GATE CSE 2008 / QUESTION 7

Counting graph connected components

Algorithms · Graph Algorithms · Full GATE CSE 2008 paper

GATE 2008, Q7 1 mark · MCQ
Report issue

Graph Algorithms

Question 7, GATE 2008: What is the most efficient time complexity to count connected components in an undirected graph with n vertices and m edges?

(A) Θ(n)
(B) Θ(m)
(C) Θ(m+n)
(D) Θ(mn)
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Maintain a visited flag for each vertex. Start a BFS/DFS at every vertex not yet visited and increment a component counter.
2. Each search visits precisely one previously unvisited component. Across all searches each vertex is processed once and each undirected edge twice.
3. With adjacency lists the total is Θ(n+m), C. The n term includes isolated vertices; an adjacency-matrix representation instead requires Θ(n²).

Original paper, page 2