我希望DFS方法将路径打印为1-> 2-> 4-> 5,但它显示1-> 2-> 3-> 4-> 5可以请您提示如何使用最少量的代码附录修复该方法?
/**
* Created by mona on 5/28/16.
*/
import java.util.Stack;
public class DepthFirstSearch {
public static void DFS(GraphNode root, int num) {
if (root.val == num) {
System.out.println("root has the value "+num);
}
System.out.println(" current value is "+root.val);
Stack<GraphNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
for (GraphNode g : stack.pop().neighbors) {
if (!g.visited) {
System.out.println(" current value is "+g.val);
if (g.val == num) {
System.out.println("Found");
}
g.visited = true;
stack.push(g);
}
}
}
}
public static void main(String[] args) {
GraphNode n1 = new GraphNode(1);
GraphNode n2 = new GraphNode(2);
GraphNode n3 = new GraphNode(3);
GraphNode n4 = new GraphNode(4);
GraphNode n5 = new GraphNode(5);
n1.neighbors = new GraphNode[] {n2};
n2.neighbors = new GraphNode[] {n4,n3};
n3.neighbors = new GraphNode[] {n4};
n4.neighbors = new GraphNode[] {n5};
n5.neighbors = new GraphNode[] {};
DFS(n1, 5);
}
}
这里是GraphNode类的代码:
/**
* Created by mona on 5/27/16.
*/
public class GraphNode {
int val;
GraphNode next;
GraphNode[] neighbors;
boolean visited;
GraphNode(int val) {
this.val = val;
this.visited = false;
}
GraphNode(int val, GraphNode[] neighbors) {
this.val = val;
this.neighbors = neighbors;
this.visited = false;
}
public String toString() {
return "value is: "+this.val;
}
}
答案 0 :(得分:1)
要获取节点的路径,仅仅添加您遇到的所有节点是不够的,因为您可能在图中遇到“死胡同”或添加实际上不在路径上的节点。为了防止这种情况,您需要在将节点插入堆栈时跟踪包含节点的节点:
Map<GraphNode, GraphNode> parents = new HashMap<>();
outer: while (!stack.isEmpty()) {
GraphNode currentElement = stack.pop();
for (GraphNode g : currentElement.neighbors) {
if (!g.visited) {
parents.put(g, currentElement);
System.out.println(" current value is "+g.val);
if (g.val == num) {
System.out.println("Found");
List<GraphNode> path = reconstructPath(parents, g);
// use path, e.g.
System.out.println(path.stream().map(n -> Integer.toString(n.val)).collect(Collectors.joining("->")));
break outer;
}
g.visited = true;
stack.push(g);
}
}
}
static List<GraphNode> reconstructPath(Map<GraphNode, GraphNode> parents, GraphNode end) {
List<GraphNode> list = new ArrayList<>();
while (end != null) {
list.add(end);
end = parents.get(end);
}
Collections.reverse(list);
return list;
}