简体   繁体   English

如何在Java中使用递归深度优先搜索的图形连接两个节点?

[英]How can I figure out if two nodes are connected in a graph with recursive depth first search in Java?

Note: the code below now reflects a working solution to the problem, I figured out the error. 注意:下面的代码现在反映了该问题的有效解决方案,我发现了错误。

I am trying to solve the simple problem of seeing if two nodes are connected. 我正在尝试解决看看是否连接了两个节点的简单问题。 There are many solutions available that use a stack, and I can find much DFS code that is recursive, but non that use recursion and actually search for something and return true/ false. 有许多使用堆栈的解决方案,我可以找到许多递归的DFS代码,但没有使用递归并实际搜索某些内容并返回true / false的DFS代码。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

  public static boolean routeBetween(int[][] graph, int startNode, int targetNode){

  //where we can keep track of the visited vertices
  int numberOfVertices = graph[0].length;
  boolean[] visited = new boolean[numberOfVerticies];

  //set all verticies to not visited
  for(int i=0; i<visited.length; i++){
    visited[i] = false;
  }

  return dfs(graph, visited, startNode, targetNode);
}

//where the actual dfs / recursion will happen, need this to keep track of
//visited
public static boolean dfs(int[][] graph, boolean[] visited, int startNode, int targetNode){

  if(startNode == targetNode){
    return true;
  }
  boolean foundNode = false;

  if(!visited[startNode]){
    visited[startNode] = true;
    for(int i=0; i<graph[startNode].length;i++){
      if(graph[startNode][i] ==1){
        boolean currentChild = dfs(graph, visited, i, targetNode);
        foundNode = currentChild || foundNode;
      }
    }
  }
  return foundNode;
}

Here is some code that I was using to test the above code: 这是我用来测试以上代码的一些代码:

  int [][] matrix = {
      {0, 1, 0, 0, 1, 1, 0, 0},
      {1, 0, 0, 0, 0, 1, 1, 0},
      {0, 0, 0, 1, 0, 0, 1, 0},
      {0, 0, 1, 0, 0, 0, 0, 1},
      {1, 0, 0, 0, 0, 1, 0, 0},
      {1, 1, 0, 0, 1, 0, 0, 0},
      {0, 1, 1, 0, 0, 0, 0, 1},
      {0, 0, 0, 1, 0, 0, 1, 0}
    };

    System.out.println(GraphTools.routeBetween(matrix,0,1));
    System.out.println(GraphTools.routeBetween(matrix,0,2));

I know that you have already figured out your issue, but sometimes it's worthwhile to see things worked out differently. 我知道您已经解决了问题,但是有时候值得一提的是,情况有所不同。

Since you are already keeping track of all the nodes that you visit in a boolean array, much of the work you do in your dfs method turns out to be redundant. 由于您已经跟踪了布尔数组中访问的所有节点,因此在dfs方法中所做的许多工作都是多余的。

Another way to do it is as follows: 另一种方法如下:

public static boolean dfs2(int[][] graph, boolean[] visited, int startNode, int targetNode) {

    // if you have not visited the current node or the target node
    // then visit this node and recursively explore its unvisited 
    //neighbors
    if (!visited[startNode] && !visited[targetNode]) {
        // visit the start node
        visited[startNode] = true;
        for (int i = 0; i < graph[startNode].length; i++) {
            if (graph[startNode][i] == 1) {
                return dfs(graph, visited, i, targetNode);
            }
        }
    }
    // otherwise we just return whether or not we have visited the
    // target node and continue... If we have visited the target node 
    //we never go into the if-statement and we always return true

    return visited[targetNode];

}

Your way is perfectly fine, I just wanted to offer an alternative solution. 您的方法非常好,我只想提供替代解决方案。 Hope this is helpful. 希望这会有所帮助。

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

相关问题 图中两个节点之间的深度优先搜索和宽度优先搜索 - Depth first search and Breadth first search between two nodes in graph 深度优先搜索的递归实现,以查找Java中两个节点之间的路径 - recursive implementation of depth first search to find path between two nodes in Java 如何将Python中的这种递归深度优先搜索转换为Java? - How can I convert this recursive Depth-First Search in Python to Java? 如何将此递归深度优先搜索转换为深度受限搜索? - How can i convert this recursive depth first search into a depth limited search? 图 java 上的深度优先搜索 - Depth First Search on graph java Graph中基于非递归堆栈的深度优先搜索未按顺序返回结果 - Non recursive stack based depth fIrst search in Graph returning results out of order 如何使用 Java 实现图的广度优先搜索? - How can I implement a Breadth first search of a graph using Java? 具有深度优先搜索的递归算法 - Recursive algorithm with Depth First Search 如何在邻接矩阵的广度优先搜索中跟踪每个顶点的深度? (爪哇) - How can I track the depth of each vertex in a breadth first search of an adjacency matrix? (Java) 带有子节点的Java 8拼图深度优先搜索问题 - Java 8-puzzle Depth first search issue with children nodes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM