递归实现深度优先搜索,找到在Java的目标从源路径递归、路径、深度、目标

2023-09-12 21:25:26 作者:笑谈心酸

我有这方面的工作code寻找到目标源之间的路径以非递归。 我想实现递归,但我有困难怎么办呢。

I have this working code for finding path between source to destination non-recursively. I would like to implement recursively, but I am having difficulty in how to do it.

这是我的code的非递归执行

This is my code for non-recursive implementation

public boolean depthFirstSearch(String name1, String name2){
        Vertex source = vertexMap.get(name1);
        Vertex dest = vertexMap.get(name2);
        clearAll();

        Stack<Vertex> stack = new Stack<Vertex>();
        source.setVisited(true);
        stack.push(source);

        while(!stack.isEmpty()){
            source = stack.peek();
            System.out.println(source.name);
            if (dest.name.equals(source.name))
                return true;
            Vertex v = adjacentUnvisitedVertex(source);
            if (v!=null){
                v.setVisited(true);
                v.prev=source;
                stack.push(v);
            }
            else stack.pop();   
        }
        if (!source.name.equals(dest.name)){
            System.out.println("destination cannot be reached");
            return true;
        }
        else return false;
    }

    private Vertex adjacentUnvisitedVertex(Vertex v){

        for (Edge e : v.adj){
            if (!e.target.isVisited){
                return e.target;
            }
        }

       return null;
    }

而一个简单的DFS递归实现是在这里,是访问的所有节点:

while a simple DFS recursive implementation is here, that visit all nodes:

static void dfs (Graphnode n) {
   n.setVisited( true );
   Iterator it = n.getSuccessors().iterator();
   while (it.hasNext()) {
      Graphnode m = (Graphnode)it.next();
      if (! m.getVisited()) dfs(m);
   }
}

我想实现查找两个节点A和B之间的路径 因此,DFS将采取两个节点之间找到递归路径。

I would like to implement for finding path between two nodes A and B. so the dfs would take two nodes and find path between recursively.

感谢

推荐答案

在这里你stack.push()的地方似乎是自然的地方递归调用。 POP()或许应该对应于具有虚假返回结果。

The places where you do stack.push() seem to be natural places for the recursive calls. pop() should probably corresponds to returning with false as the result.

但也许是更直接转换的递归 DFS()功能到你所需要的功能:

But maybe it's more straightforward to convert the recursive dfs() function into the function you need:

转换 DFS()到目标数据结构( Graphnode - > 顶点迭代 - > 而(adjacentUnvisitedVertex(...)等),检查它是否适用于的System.out.println()。这可能是最难的部分。如果你卡住了,但在这里发表的输出。

Convert dfs() to your target data structure (Graphnode -> Vertex, Iterator... -> while(adjacentUnvisitedVertex(...) etc.). Check if it works with System.out.println(). This might be the hardest part. If you get stuck there, post the output here.

只需添加一个 DEST 参数和检查,如果节点匹配(符在这种情况下,真正的循环否则为false 之后)。请务必检查的递归 DFS在循环的结果()调用和返回,如果它已经找到了元素。

Just add a dest parameter and the check if the nodes match (return true in this case, false after the loop otherwise). Make sure you check the result of the recursive dfs() call in the loop and return true if it has found the element.