I am trying to understand what is the space complexity of DFS and BFS in a graph.
I understand that the space complexity of BFS while using adjacency matrix would be O(v^2)
where v
is the number of vertices.
By using the adjacency list space complexity would be decreased in average case i.e < v^2
. But in the worst case, it would be O(v^2)
.
Even including Queue, it would be O(n^2)
(neglecting the lower value)
But, what is the scenario with DFS? Even if we use the adjacency matrix/list. Space Complexity would be O(v^2). But that seems to be a very loose bound, that too without considering stack frames.
Am I correct, regarding the complexities? If, not what are the space complexities of BFS/DFS? and while calculating Space Complexity for DFS, do we consider stack frame or not?
what is the tight bound of space complexity, for BFS and DFS for graph