Java 迭代到递归

Java 迭代到递归,java,recursion,topological-sort,Java,Recursion,Topological Sort,我正在研究拓扑排序,我发现这个函数递归地进行拓扑排序 void topologicalSortUtil(int v, Boolean visited[],Stack stack) { // Mark the current node as visited. visited[v] = true; Integer i; // Recur for all the vertices adjacent to this vertex Iterator<Inte

我正在研究拓扑排序,我发现这个函数递归地进行拓扑排序

void topologicalSortUtil(int v, Boolean visited[],Stack stack)
{
    // Mark the current node as visited.
    visited[v] = true;
    Integer i;

    // Recur for all the vertices adjacent to this vertex
    Iterator<Integer> it = adj[v].iterator();
    while (it.hasNext())
    {
        i = it.next();
        if (!visited[i])
            topologicalSortUtil(i, visited, stack);
    }

    // Push current vertex to stack which stores result
    stack.push(new Integer(v));
}
void topologicalSortUtil(int v,Boolean[],堆栈)
{
//将当前节点标记为已访问。
访问[v]=正确;
整数i;
//对与该顶点相邻的所有顶点重复
迭代器it=adj[v]。迭代器();
while(it.hasNext())
{
i=it.next();
如果(!访问[i])
topologicalSortUtil(i,已访问,堆栈);
}
//将当前顶点推送到存储结果的堆栈
stack.push(新整数(v));
}

我将如何迭代地进行此操作?当分解递归函数时,我的方法过程应该是什么?任何帮助都将不胜感激

什么是adj定义?您似乎已经从中获得了部分代码,并且在那里得到了很好的解释。请注意,这只是通过后序访问首先遍历图形深度。图上有许多关于迭代DFS的参考。