【问题标题】:Iterative DFS with side effect具有副作用的迭代 DFS
【发布时间】:2018-08-06 12:07:48
【问题描述】:

我想知道如何编写对遍历有副作用的迭代 DFS?

function DFS(x) {
  x.in = time++          // this is obvious
  foreach (child in x.children) {
    DFS(child)
  }
  x.out = time++         // it looks problematic
}

time 通过引用访问(因此与所有foo 调用共享)。

【问题讨论】:

  • 而 Foo() 和 DFS() 之间的联系是 .... ?
  • 为什么会有问题?

标签: algorithm recursion graph iteration


【解决方案1】:

将堆栈用于迭代 dfs,如果是递归 dfs,您正在使用函数调用堆栈

【讨论】:

    猜你喜欢
    • 2015-02-27
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多