简体   繁体   English

使用邻接矩阵的图的DFS(Java)

[英]DFS of a graph using an adjacency matrix (Java)

Hello I'm having an issue with my code displaying the wrong order when I perform a Depth-First Traversal 您好,我在执行深度优先遍历时代码显示错误的顺序时遇到问题

class graphs{

public static void main(String [] args){
    int[][] adjMatrix = { {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}};
    boolean[] visited = {false, false, false, false, false, false, false, false};
    int n = 8;

    DFS(adjMatrix, visited, n, 0);
}

public static void DFS(int[][] adjMatrix, boolean [] visited,int n, int i){
    System.out.print(" " + (i+1));
    visited[i]= true;
    for (int j = 0; j<n;j++){
        if(!(visited[j]) && adjMatrix[i][j]==1){
            DFS(adjMatrix, visited, n, j);
        }

    }
  }
}

I was told that I should be getting : 1 2 6 7 4 3 5 8 有人告诉我我应该得到:1 2 6 7 4 3 5 8

I, however, keep getting : 1 2 6 5 7 3 4 8 我,但是,不断得到:1 2 6 5 7 3 4 8

What am I doing wrong? 我究竟做错了什么?

Edit: It's supposed to display the order visited. 编辑:应该显示访问的订单。 Maybe that's where I'm messing up? 也许那是我搞砸的地方?

Edit: Additionally, how could I have it display the order of the dead ends? 编辑:此外,我如何让它显示死胡同的顺序?

For the dead ends would something like this work: 对于死胡同,这样的工作:

public static void DFS(int[][] adjMatrix, boolean [] visited,int n, int i){
System.out.print(" " + (i+1));
visited[i]= true;
for (int j = 0; j<n;j++){
    if(!(visited[j]) && adjMatrix[i][j]==1){
        DFS(adjMatrix, visited, n, j);
    }

}
a.add(i+1); //assume a is an integer ArrayList and will be printed out later
}

You were told wrong, your output is correct. 有人告诉您错误,您的输出正确。

You can check it by hand: 您可以手动检查它:

       1  2  3  4  5  6  7  8
    1 {0, 1, 0, 0, 1, 1, 0, 0}
    2 {1, 0, 0, 0, 0, 1, 1, 0}
    3 {0, 0, 0, 1, 0, 0, 1, 0}
    4 {0, 0, 1, 0, 0, 0, 0, 1}
    5 {1, 0, 0, 0, 0, 1, 0, 0}
    6 {1, 1, 0, 0, 1, 0, 0, 0}
    7 {0, 1, 1, 0, 0, 0, 0, 1}
    8 {0, 0, 0, 1, 0, 0, 1, 0}

You just "jump" until you find a 0, we start at 1, the first 1 in the first row is at column marked 2, we move to row 2, the first 1 in row 2 that was not yet visited/is not being visited currently is at 6, we move to row 6, again the first interesting 1 is at 5, we move to row 5, there are no interesting 1s in that row because we are currently on a path that already visited 1 and 6 so we backtrack, at this point we have 1,2,6,5. 您只是“跳转”,直到找到0,我们从1开始,第一行的第一个1在标记为2的列,我们移至第2行,第2行中的第一个尚未访问/未访问的1当前访问的是6,我们移至第6行,第一个有趣的1是在5,我们移至第5行,该行中没有有趣的1,因为我们当前所处的路径已经访问过1和6,所以我们回溯,此时我们有1,2,6,5。 We backtrack back to row 2, first interesting 1 is at 7, in row 7 we get 3, at 3 we go to 4, then backtrack to 7 and go to 8 which finishes all nodes with your result. 我们回溯到第2行,第一个有趣的1是7,在第7行,我们得到3,在3处,我们转到4,然后回溯到7,然后到8,这将完成所有结点的运算。

@Edit: just to make it clear other answers might be possible with DFS (depending in which order you will be processing neighbours) but the answer you were supposed to provide is definitely not possible for this input. @Edit:只是要清楚其他的答案也许是可能的DFS(根据其命令你要处理的邻居),但你应该提供答案肯定是不可能的此输入。

@Edit: so to be precise it would be 1 -> 2 -> 6 -> 5 -> backtrack(2) -> 7 -> 3 -> 4 -> 8 -> end @Edit:准确地说是1-> 2-> 6-> 5-> backtrack(2)-> 7-> 3-> 4-> 8->结束

根据您的矩阵,adjMatrix [6] [5] = 1但adjMatrix [6] [7] = 0,因此您的预期答案1 2 6 7 4 3 5 8不正确,因为6和7之间没有链接。应该再次检查输入数据,很有可能您输入错误。

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

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