franklinchou / dna

Data structures & algorithms
0 stars 1 forks source link

[chore] Make DFS iterative #47

Open franklinchou opened 6 years ago

franklinchou commented 6 years ago

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.