Let be a graph of vertices and edges. If a depth first search is performed on what is the tightest upper bound of the running time?
Details and Assumptions
The graph is represented in adjacency matrix.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
A 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 ) .