简体   繁体   English

深度优先搜索的递归实现,以查找Java中两个节点之间的路径

[英]recursive implementation of depth first search to find path between two nodes in Java

I am trying to implement the recursive version of DFS for depth first search between two nodes. 我正在尝试实现DFS的递归版本,以便在两个节点之间进行深度优先搜索。 Here is my code. 这是我的代码。 The method will return true if there is a path that exists and it also updates the prev field of the node to keep track of the path. 如果存在路径,则该方法将返回true,并且该方法还将更新节点的prev字段以跟踪该路径。 I have implemented this non-recursive way using stacks and it works fine which is here. 我已经使用堆栈实现了这种非递归方式,并且在这里工作正常 . This one is not working. 这是行不通的。 That is it is not giving me the path from Node A to Node B. It always seem to return false. 那不是给我从节点A到节点B的路径。它似乎总是返回false。

public boolean recursiveDFS(String start, String end){
  clearAll();
  Vertex source = vertexMap.get(start);
  Vertex dest = vertexMap.get(end);
  clearAll();

  return recursiveDFShelper(source,dest);
}

private boolean recursiveDFShelper(Vertex sor, Vertex des){
  if (!sor.name.equals(des.name)){
    sor.setVisited(true);
    Iterator<Edge> it = sor.adj.iterator();

    while (it.hasNext()){
      Vertex v = it.next().target;

      if(!v.isVisited){
        sor.prev=v;
        recursiveDFShelper(v, des); 
      }
    }
    return false;
  }
  else 
    return true;
}

EDIT: After below suggestions, I have this code 编辑:以下建议后,我有此代码

public boolean recursiveDFS(String start, String end){
        clearAll();
        Vertex source = vertexMap.get(start);
        Vertex dest = vertexMap.get(end);
        clearAll();

        return recursiveDFShelper(source,dest);

    }

    private boolean recursiveDFShelper(Vertex sor, Vertex des){
        //System.out.println(sor.name);

        if (!sor.name.equals(des.name)){
        sor.setVisited(true);
        System.out.println(sor.adj);
        Iterator<Edge> it = sor.adj.iterator();
        while (it.hasNext()){
            Vertex v = it.next().target;
            if(!v.isVisited){
                v.prev=sor;
                System.out.printf("prev of %s is %s\n",sor,sor.prev);
                return recursiveDFShelper(v, des);  
                }
            }
        //System.out.println("returning false");
        return false;
        }
        else {
           System.out.println("returning true");
           return true;
        }
        }

for a given directed graph like this, 对于这样的给定有向图,

A B
B C
C D
A E
E D 

it is able to find the correct path from A to D, but fails from A to E which is just one node away. 它能够找到从A到D的正确路径,但是从A到E却失败,而E则只有一个节点。

You need to change 你需要改变

return recursiveDFShelper(v, des);

to

if(recursiveDFShelper(v, des)) {
  return true;
}

Otherwise, you always return from the loop after the first recursive call. 否则,您总是在第一个递归调用之后从循环中返回。 But you need to continue searching if the recursion did not find the element. 但是,如果递归找不到元素,则需要继续搜索。 On the other hand, if you have found the element in the recursive call, you want to stop the search. 另一方面,如果您在递归调用中找到了该元素,则想停止搜索。

Well, the problem in your code is that the only time you are going to return true is when the sor and des parameters are equal on the first call to the recursive function, otherwise you are always returning false. 嗯,代码中的问题是, 唯一要返回true的时间是在第一次调用递归函数时sordes参数相等,否则,您总是返回false。

So to fix this, inside recursiveDFShelper , you need to return the result of your recursive call. 因此,要解决此问题,请在recursiveDFShelper内部,返回递归调用的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM