Java 为什么此DFS代码在某些情况下不起作用?

Java 为什么此DFS代码在某些情况下不起作用?,java,data-structures,microsoft-distributed-file-system,Java,Data Structures,Microsoft Distributed File System,根据上面的图片,DFS应该是:013542,但它正在返回01352(这只发生在一个案例中。我在这里做错了什么?) 代码: import java.util.Stack; 公共类DFSDetectCycleSelf{ 静态int arr[]]={ { 0, 1, 1, 0, 0, 0 }, { 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1,

根据上面的图片,DFS应该是:
013542
,但它正在返回
01352
(这只发生在一个案例中。我在这里做错了什么?)

代码:

import java.util.Stack;
公共类DFSDetectCycleSelf{
静态int arr[]]={
{ 0, 1, 1, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0 }
//静态int arr[][]={{0,1,1,0,0,0}的工作良好,
// {0,0,0,1,1,0},
//{0,0,0,0,0,1},
//{0,0,0,0,0,0},
//{0,0,0,0, 0,0},
//{0,0,0,0,0,0}};
静态堆栈;
DFSDetectCycleSelf(){
堆栈=新堆栈();
}
公共静态void main(字符串[]args){
DFSDetectCycleSelf df=新的DFSDetectCycleSelf();
PrintDFS();
}
公共静态void PrintDFS(){
int源=0;
int numberOfNodes=arr[source].length;
int[]已访问=新int[numberOfNodes];
INTV;
堆栈推送(源);
而(!stack.isEmpty()){
v=stack.pop();
如果(访问[v]==0){
访问量[v]=1;
系统输出打印Ln(v);
}
对于(int i=0;i这应该可以:

public static void PrintDFS(){
    int source = 0;
    int numberOfNodes = arr[source].length;
    int [] visited = new int[numberOfNodes];
    int v;
    stack.push(source);


    while (!stack.isEmpty()){
        v = stack.pop();
        if(visited[v]==0) {
          visited[v] = 1;
          System.out.println(v);
          for(int i=0;i<numberOfNodes;i++){
            if(arr[v][i]==1)
              stack.push(i);
          }
        }
    }
}
(b) 如果边是有序的(从某种意义上说,边
(x)->(y)
应该在边
(x)->(y+1)
之前遍历),那么实际上,正如Alexis C前面所建议的,for循环需要向后

    for (int i = numberOfNodes - 1; i >= 0; i--) {
应用这些修复程序后,输出将变为:

0
1
3
5
4
2

代码的问题是

...
v = i; // shouldn't be there
...
这是访问图中所有节点的一般迭代算法

WHILE there exists a graph node not marked loop
    Find an unmarked node F
    Add node F to collection (stack or queue)
    WHILE the collection is not empty loop
        Remove a node N from the collection
        IF the node N is unmarked
            Mark node N
            Add all adjacent nodes of node N to the collection
            Process node N
收集取决于您需要解决的问题。如果问题将通过查看最短路径来解决,那么队列(表示BFS)就是要走的路。如果问题将通过了解迷宫中的路线来解决,那么堆栈(表示DFS)就是要走的路。此外,对于树(如本问题中的树)如果知道根节点,则不需要算法的前两行

内部循环的一个重要部分是准备未来处理相邻节点,但重要的是不要跟随这些链接到相邻节点,
v=i;
通过跟随链接更改节点。不需要跟随链接,因为这些节点被放置在集合中,并将在e未来


内环的作用仅强调节点N。对问题的这种划分有助于简化算法,并使只访问和处理所有节点一次的更大任务更容易执行。

该数组表示什么?非常不清楚。。。(当值只能是0或1时,为什么要使用整数?只需使用
boolean
…)@JonSkeet我猜一行代表一个顶点及其边(因此,如果看第一行,顶点0连接到顶点1和2)。我建议以面向对象的方式来处理这个问题。@JonSkeet 1表示两个对象之间存在一条边nodes@AlexisC.:如果是这种情况,那么第2行和第3行似乎都是第5项的“父项”…看起来数据已损坏。如果您从无效配置开始,则代码不起作用并不奇怪。(2和4都应该是叶节点,没有子节点。)“不那样打印”是什么意思?您期望的正确打印输出是什么?
...
v = i; // shouldn't be there
...
WHILE there exists a graph node not marked loop
    Find an unmarked node F
    Add node F to collection (stack or queue)
    WHILE the collection is not empty loop
        Remove a node N from the collection
        IF the node N is unmarked
            Mark node N
            Add all adjacent nodes of node N to the collection
            Process node N