Skip to main content

GATE CSE 2014Algorithms › Graph AlgorithmsVerified

GATE CSE 2014 PYQ: Let G G G be a graph with n n n vertices and m m m edges. What is the tightest upper bound on the running…

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?

  1. A
    Θ(n)\Theta(n)
  2. B
    Θ(n+m)\Theta(n+m)
  3. C
    Θ(n2)\Theta(n^2)
    Correct answer
  4. D
    Θ(m2)\Theta(m^2)

Answer

(C) Θ ( n 2 ) \Theta(n^2) Θ ( n 2 )

Approach

In an adjacency-matrix representation, DFS must inspect the entire row of the matrix for each visited vertex in order to discover that vertex's neighbors. Since there are $n$ rows and each row has $n$ entries, the total work is $\Theta(n^2)$. The adjacency-list bound $\Theta(n+m)$ does not apply here because the matrix stores non-edges as well as edges.

The tightest upper bound is $\Theta(n^2)$ because adjacency-matrix DFS scans an $n$-length row for every visited vertex, totaling $n \times n = n^2$ operations. Therefore option C is correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.