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?
View worked solution
Answer: C
To find the running time of Depth First Search (DFS) on a graph represented as an adjacency matrix, we analyze the work done during the traversal:
- Initialization: Initializing the
visitedarray of size takes time. - Vertex Exploration: DFS visits each of the vertices exactly once.
- Finding Neighbors: For each vertex being visited, we must identify all its adjacent vertices to explore further. In an adjacency matrix of size , finding the neighbors of requires scanning the entire row corresponding to . This scan takes time per vertex, independent of the actual number of edges incident on .
Summing this up over all vertices:
Thus, the tightest upper bound on the running time of DFS when using an adjacency matrix is .
Free practice · Your answers on this page are not saved.
