Notice that the space complexity required for the recursive implementation of DFS is O(h) where h is the maximum depth of the graph. This is because of the space needed to store the recursive calls. DFS can also be implemented iteratively following the same idea as BFS but using a stack instead of a queue. This implementation uses O(v) extra memory where v is the number of vertices in the graph.
Notice that the space complexity required for the recursive implementation of DFS is O(h) where h is the maximum depth of the graph. This is because of the space needed to store the recursive calls. DFS can also be implemented iteratively following the same idea as BFS but using a stack instead of a queue. This implementation uses O(v) extra memory where v is the number of vertices in the graph.