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? $\Theta(n)$ $\Theta(n+m)$ $\Theta(n^2)$ $\Theta(m^2)$ Algorithms gatecse-2014-set1 algorithms graph-algorithms normal graph-search + – go_editor 19.4k views answer comment Share Follow Print See all 5 Comments 5 5 Comments reply Show 2 previous comments rishi71662data4 commented Nov 12, 2017 reply Follow flag What is dense graph ? Is a graph with maximum edges ? 0 0 replyShare vishalshrm539 commented Dec 8, 2017 reply Follow flag in dense graph , order of no. of edges = O(|V|)^2 in sparse graph, it is O(|V|) 7 7 replyShare Nalinj commented Mar 16, 2025 reply Follow flag this is asking for theta, not big Oh 0 0 replyShare Please log in or register to add a comment.
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$ Regina Phalange answered Apr 6, 2017 • edited Apr 29, 2019 by Naveen Kumar 3 Regina Phalange comment Share Follow See all 5 Comments 5 5 Comments reply Ayush Upadhyaya commented Jan 2, 2019 reply Follow flag $O(V)$ calls are made to DFS visit and in DFS visit, examining neighbour of each vertex takes $O(V)$ times so total time $O(V^2)$ 24 24 replyShare Abhrajyoti00 commented Dec 12, 2022 reply Follow flag Useful : CS161Lecture09.pdf (stanford.edu) 1 1 replyShare pavansan commented Jan 2, 2025 reply Follow flag got it 0 0 replyShare panipuri commented Aug 14, 2025 reply Follow flag also even in adjacency list representation, if our graph is a complete graph then the time complexity to find neighbours is O(v) only 0 0 replyShare pavansan commented Nov 16, 2025 reply Follow flag to whomever confusing whether it is n^2 or m^2 dfs and bfs both are based on vertices but not edges so its n^2 not m^2 2 2 replyShare Please log in or register to add a comment.
18 18 votes 1. Depth-first search requires O(V + E) time if implemented with adjacency lists 2. Depth-first search requires O(V2) time if implemented with an adjacency matrix Sougatamoy Biswas 1 answered Jan 19, 2016 • edited Jan 6, 2018 by Puja Mishra Sougatamoy Biswas 1 comment Share Follow 0 reply Please log in or register to add a comment.
14 14 votes Ans (C) http://web.eecs.utk.edu/~huangj/CS302S04/notes/graph-searching.html Keith Kr answered Jan 12, 2015 • edited Oct 27, 2017 by kenzou Keith Kr comment Share Follow See all 2 Comments 2 2 Comments reply Mohnish commented Dec 6, 2020 reply Follow flag link not working 0 0 replyShare abhishek29 commented Oct 12, 2022 reply Follow flag Working link: Graph Searching (archive.org) 0 0 replyShare Please log in or register to add a comment.
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). varunrajarathnam answered Aug 7, 2020 varunrajarathnam comment Share Follow 0 reply Please log in or register to add a comment.
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. Krish_Vg answered Nov 19, 2025 Krish_Vg comment Share Follow 0 reply Please log in or register to add a comment.