Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的BFS最短路径在Java中的实现有什么问题_Java_Algorithm_Graph Algorithm_Shortest Path_Breadth First Search - Fatal编程技术网

我的BFS最短路径在Java中的实现有什么问题

我的BFS最短路径在Java中的实现有什么问题,java,algorithm,graph-algorithm,shortest-path,breadth-first-search,Java,Algorithm,Graph Algorithm,Shortest Path,Breadth First Search,我需要获取节点2和节点3之间的最短路径。您试图将一个元素添加到一个空的链表中。您需要在索引s处初始化链表,然后才能添加到其中 import java.io.*; import java.util.*; class Graph { private int V; // No. of vertices private LinkedList<Integer> adj[]; //Adjacency Lists private LinkedList<Integ

我需要获取节点2和节点3之间的最短路径。

您试图将一个元素添加到一个空的链表中。您需要在索引s处初始化链表,然后才能添加到其中

import java.io.*;
import java.util.*;

class Graph
{
    private int V;   // No. of vertices
    private LinkedList<Integer> adj[]; //Adjacency Lists
    private LinkedList<Integer> path[];


    Graph(int v)
    {
        V = v;
        adj = new LinkedList[v];
        for (int i=0; i<v; ++i)
            adj[i] = new LinkedList();
        path = new LinkedList[v];
        for(int i=0;i<v;++i)
            adj[i]=new LinkedList();
    }


    void addEdge(int v,int w)
    {
        adj[v].add(w);
    }

    // prints BFS traversal from a given source s
    void BFS(int s,int d)
    {
        boolean visited[] = new boolean[V];
        LinkedList<Integer> queue = new LinkedList<Integer>();


        visited[s]=true;
        queue.add(s);
        path[s].addLast(s);
        while (queue.size() != 0)
        {

            s = queue.poll();
            //System.out.print(s+" ");


            Iterator<Integer> i = adj[s].listIterator();
            while (i.hasNext())
            {
                int n = i.next();
                if (!visited[n])
                {
                    visited[n] = true;
                    queue.add(n);
                    path[n]=path[s];
                    path[n].addLast(n);
                }
            }
        }

        System.out.print("Following is the path from source to destination\n");
        while(path[d].size()!=0)
        {
            int xyz=path[d].getFirst();
            path[d].poll();
            System.out.print(xyz+" ");
        }
    }

    // Driver method to
    public static void main(String args[])
    {
        Graph g = new Graph(4);

        g.addEdge(0, 1);
        g.addEdge(0, 2);
        g.addEdge(1, 2);
        g.addEdge(2, 0);
        g.addEdge(2, 3);
        g.addEdge(3, 3);

        System.out.println("Following is the desired path\n");

        g.BFS(2,3);
    }
}
此外,您的代码失败,因为在设置path[n]的值时未克隆path数组:

path[s] = new LinkedList();
path[s].addLast(s);
您需要将此更改为:

path[n]=path[s];

这样,n的列表就不会保留对s列表的引用。目前,该引用被保留,因此每当您向n的列表中添加某个内容时,该内容也将添加到s中

由于没有任何信息表明哪些行为不符合预期,因此很难说出错误是什么。但我发现代码有两个问题:

首先,不初始化路径的元素。相反,在构造函数中初始化adj的元素两次。所以,你应该替换其中一行

path[n]= (LinkedList) path[s].clone();
这导致path的每个元素使用具有相同元素的相同LinkedList。相反,您应该创建一个包含相同元素的新LinkedList:

path[n] = path[s];
path[n].addLast(n);
编辑:正如Soggiorno在回答中所说,在向BFS方法添加元素之前,您必须至少在BFS方法的开头初始化路径:

path[n] = new LinkedList(path[s]);
path[n].addLast(n);
当然,如果您对路径的所有元素进行初始化,则不需要这样做

path[n] = path[s];
path[n].addLast(n);
path[n] = new LinkedList(path[s]);
path[n].addLast(n);
path[s] = new LinkedList();
path[s].addLast(s);