简体   繁体   中英

Recursive and Iterative DFS Algorithm Time Complexity and Space Complexity

So I am having some problems understanding why the time complexity of a recursive DFS and an iterative DFS is the same, perhaps someone can guide me through an easy explanation?

Thanks in advance.

A recursive implementation requires, in the worst case, a number of stack frames (invocations of subroutines that have not finished running yet) proportional to the number of vertices in the graph. This worst-case bound is reached on, eg, a path graph if we start at one end.

An iterative implementation requires, in the worst case, a number of stack entries proportional to the number of vertices in the graph. The same inputs reach this worst-case time as for the recursive implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM