简体   繁体   English

递归和迭代DFS算法时间复杂度和空间复杂度

[英]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?所以我在理解为什么递归 DFS 和迭代 DFS 的时间复杂度相同时遇到了一些问题,也许有人可以通过简单的解释来指导我?

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.与递归实现相同的输入达到这个最坏情况的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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