Question
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?
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 vertices is visited at most once.
When a graph is represented as an adjacency matrix of size :
- To determine all outgoing edges from a vertex , the algorithm must inspect the entire row corresponding to in the adjacency matrix.
- Each row has length , requiring time per vertex.
- Since there are vertices, inspecting all rows takes time.
Even if the graph is sparse (), the algorithm must still scan all entries of the matrix.
Therefore, the running time is .
C
Confusing the adjacency matrix DFS running time with the adjacency list running time .
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.