如何从递归算法转换为迭代算法

时间:2011-10-04 06:24:33

标签: c++ algorithm data-structures recursion

如何将以下recursive算法转换为iterative

DFS-Visit(U)
    color[u] = GREY 
    d[u]  =time
    time = time + 1
    for each v = Adj[u] do 
        if color[v] = WHITE
            then p[v] = u
                DFS-Visit(v)
    color[u] = BLACK
    f[u] = time
    time = time + 1

1 个答案:

答案 0 :(得分:0)

USE堆栈保存状态而不是递归调用它

这可能会对您有所帮助:http://www.cs.mcgill.ca/~pnguyen/251F09/DFS.pdf