Let be a graph with vertices and edges. What is the tightest upper bound on the running time of Depth First Search on , when is represented as an adjacency matrix?
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…
- A
- B
- CCorrect answer
- D
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.