【发布时间】:2015-02-25 17:13:12
【问题描述】:
这是来自维基百科的拓扑排序伪代码:
L ← Empty list that will contain the sorted nodes
while there are unmarked nodes do
select an unmarked node n
visit(n)
function visit(node n)
if n has a temporary mark then stop (not a DAG)
if n is not marked (i.e. has not been visited yet) then
mark n temporarily
for each node m with an edge from n to m do
visit(m)
mark n permanently
unmark n temporarily
add n to head of L
我想在不丢失 cicle 检测的情况下以非递归方式编写它。
问题是我不知道该怎么做,而且我已经想到了很多方法。基本上问题是做 DFS,但要记住“当前路径”(它对应于上面伪代码中的“临时标记”某些节点)。因此,使用堆栈的传统方法没有给我任何东西,因为当使用堆栈(并将每个节点的邻居放入其中)时,我将节点放在那里,即使我会在“未确定的未来”看到它们,我只想跟踪节点“在我当前的路径上”(我认为它是在迷宫中行走,我要留下一条线 - 当我看到死胡同时,我会转身并在这样做时以及在任何时候“包裹胎面”有时间我想记住“上面有线程”的节点以及线程至少存在一次的节点)。有什么提示可以为我指明正确的方向吗?我的意思是 - 我应该考虑使用 2 个堆栈而不是 1 个,也许是其他一些数据结构?
或者也许这个算法没问题,我应该把它保留为递归形式。对于足够大的图,我只担心超过“递归深度”。
【问题讨论】:
标签: c++ algorithm recursion graph topological-sort