[英]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.