【发布时间】:2016-02-29 00:47:17
【问题描述】:
我只是想知道把这段代码放在哪里
if(node == goal){
System.out.print("Found path: ");
for(int n : stack){
System.out.print(n + " ");
}
}
在这个方法中:
public void performRecursiveDFS(Graph G, int node, int goal) {
Stack<Integer> stack = new Stack<Integer>();
stack.push(node);
while (!stack.isEmpty()) {
node = stack.pop();
if (!visited[node]) {
visited[node] = true;
for (int w : G.adjList(node)) {
stack.push(w);
}
}
}
}
原因是,我想打印从起始节点到目标节点的路径。例如像这样,Found path: 0 1 2 3 4 7。我尝试将它放在node = stack.pop() 之后,但它向我展示了类似Found path: 3 的内容。有什么理由/建议吗?我的代码有问题吗?如果有,请详细指导我。欢迎提问。提前致谢。
【问题讨论】:
标签: java stack graph-algorithm path-finding depth-first-search