2016-05-28 97 views
0

我期望DFS方法打印路径为1-> 2-> 4-> 5,但它显示1-> 2-> 3-> 4-> 5请问您可以提示方法可以使用最少的代码量来修正?DFS方法不能正常工作

/** 
* 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; 
    } 

} 

回答

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; 
} 
+0

所以这个问题是使用'为(GraphNode G:堆栈。 pop()。neighbors){'? –

+0

运行你的代码,我仍然得到相同的结果http://pastebin.com/hj5P8CpL –

+1

@MonaJalal:对不起,这是你从堆栈中弹出它们的顺序。我没有注意到你在将它们添加到堆栈时将它们打印出来......但是如果你想要一个路径,只需添加所有找到的节点就不够了......为此添加了一个修复程序。 – fabian