edited by
19,412 views
51 51 votes

Let $G$ be a graph with $n$ vertices and $m$ edges. What is the tightest upper bound on the running time of Depth First Search on $G$, when $G$ is represented as an adjacency matrix?

  1. $\Theta(n)$
  2. $\Theta(n+m)$
  3. $\Theta(n^2)$
  4. $\Theta(m^2)$

5 Answers

Best answer
52 52 votes

Depth First Search of a graph takes $O(m+n)$ time when the graph is represented using adjacency list. In adjacency matrix representation, graph is represented as an $n * n$ matrix. To do DFS, for every vertex, we traverse the row corresponding to that vertex to find all adjacent vertices (In adjacency list representation we traverse only the adjacent vertices of the vertex). Therefore time complexity becomes $O(n^2)$.

Correct Answer: $C$

edited by
4 4 votes
DFS visits each vertex once and as it visits each vertex, we need to find all of its neighbours to figure out where to search next. Finding all its neighbours in an adjacency matrix requires O(V ) time, so overall the running time will be O(V2).
1 1 vote
For  V  vertices and  E  edges,

Both BFS & DFS take O( V + E ) if implemented with adjacency list

& O(V^2) time if implemented with adjacency matrix.
Answer:
Position:
Show:

Related questions

78 78 votes
11 answers 11 answers
33.4k
33.4k views
go_editor asked Sep 28, 2014
33,402 views
Consider a $6$-stage instruction pipeline, where all stages are perfectly balanced. Assume that there is no cycle-time overhead of pipelining. When an application is exec...
63 63 votes
5 answers 5 answers
32.7k
32.7k views
go_editor asked Sep 28, 2014
32,704 views
Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume that a recursive call to visit a vertex is made only after first checkin...
65 65 votes
9 answers 9 answers
18.6k
18.6k views
go_editor asked Sep 28, 2014
18,555 views
Consider the tree arcs of a BFS traversal from a source node $W$ in an unweighted, connected, undirected graph. The tree $T$ formed by the tree arcs is a data structure f...
71 71 votes
10 answers 10 answers
24.0k
24.0k views
Akash Kanase asked Feb 12, 2016
24,019 views
Breadth First Search (BFS) is started on a binary tree beginning from the root vertex. There is a vertex $t$ at a distance four from the root. If $t$ is the $n^{\text{th}...