Question

Let GG be a graph with nn vertices and mm edges. What is the tightest upper bound on the running time of Depth First Search on GG, when GG is represented as an adjacency matrix?

Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2014 answer key

Solution

Analyze the number of operations in Depth First Search when using an adjacency matrix representation.

In Depth First Search (DFS), each of the nn vertices is visited at most once.

When a graph is represented as an adjacency matrix of size n×nn \times n:

  • To determine all outgoing edges from a vertex uu, the algorithm must inspect the entire row corresponding to uu in the adjacency matrix.
  • Each row has length nn, requiring Θ(n)\Theta(n) time per vertex.
  • Since there are nn vertices, inspecting all rows takes n×Θ(n)=Θ(n2)n \times \Theta(n) = \Theta(n^2) time.

Even if the graph is sparse (m≪n2m \ll n^2), the algorithm must still scan all n2n^2 entries of the matrix.

Therefore, the running time is Θ(n2)\Theta(n^2).

Final answer

C

Common trap

Confusing the adjacency matrix DFS running time Θ(n2)\Theta(n^2) with the adjacency list running time Θ(n+m)\Theta(n + m).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 1
Question
21 of 65, printed as CS Q.11
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.