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?
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? Correct Answer Θ (n<sup>2</sup>)
Concept
In adjacency matrix representation, graph is represented as an “n x n” matrix.
Graph:
[ alt="F1 Raju Shraddha 10.07.2020 D1" src="//storage.googleapis.com/tb-img/production/20/07/F1_Raju_Shraddha_10.07.2020_D1.png" style="width: 157px; height: 153px;">
Then, its adjacency matrix will be
[ alt="F1 Raju Shraddha 10.07.2020 D2" src="//storage.googleapis.com/tb-img/production/20/07/F1_Raju_Shraddha_10.07.2020_D2.png" style="width: 132px; height: 138px;">
Explanation:
For performing DFS on the above graph or any adjacency matrix of a graph G, we traverse the row corresponding to that vertex. This way, we find all the adjacent vertices.
Hence, the tightest upper bound on the running time of Depth First Search on G, when G is represented as an adjacency matrix is Θ (n2).