Skip to content
Official PYQ · 2014

GATE CS 2014 Set 1 Q11 — Graph Traversals (BFS, DFS)

Explain with AI

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
View worked solution

Answer: C

To find the running time of Depth First Search (DFS) on a graph GG represented as an adjacency matrix, we analyze the work done during the traversal:

  1. Initialization: Initializing the visited array of size nn takes Θ(n)\Theta(n) time.
  2. Vertex Exploration: DFS visits each of the nn vertices exactly once.
  3. Finding Neighbors: For each vertex uu being visited, we must identify all its adjacent vertices to explore further. In an adjacency matrix of size n×nn \times n, finding the neighbors of uu requires scanning the entire row corresponding to uu. This scan takes Θ(n)\Theta(n) time per vertex, independent of the actual number of edges mm incident on uu.

Summing this up over all nn vertices:

Total Time=Θ(n)+∑i=1nΘ(n)=Θ(n)+Θ(n2)=Θ(n2)\text{Total Time} = \Theta(n) + \sum_{i=1}^{n} \Theta(n) = \Theta(n) + \Theta(n^2) = \Theta(n^2)

Thus, the tightest upper bound on the running time of DFS when using an adjacency matrix is Θ(n2)\Theta(n^2).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Graph Traversals (BFS, DFS) questions