具有优先级队列的Dijkstra算法Java实现

时间:2018-05-13 11:28:31

标签: java graph shortest-path dijkstra

您好我正在尝试使用Dijkstra算法在加权图中找到最小可能路线。我正在尝试从geeksforgeeks.com实现以下算法:

1) Initialize distances of all vertices as infinite.

2) Create an empty priority_queue pq.  Every item
   of pq is a pair (weight, vertex). Weight (or 
   distance) is used used as first item  of pair
   as first item is by default used to compare
   two pairs

3) Insert source vertex into pq and make its
   distance as 0.

4) While either pq doesn't become empty
    a) Extract minimum distance vertex from pq. 
       Let the extracted vertex be u.
    b) Loop through all adjacent of u and do 
       following for every vertex v.

           // If there is a shorter path to v
           // through u. 
           If dist[v] > dist[u] + weight(u, v)

               (i) Update distance of v, i.e., do
                     dist[v] = dist[u] + weight(u, v)
               (ii) Insert v into the pq (Even if v is
                    already there)

5) Print distance array dist[] to print all shortest
   paths. 

我的主要数据结构是:

 // HashMap of Intersections 
    private static HashMap<String, Intersection> intersections;
    // Edge/Rail List
    private static ArrayList<Rail> railList;

我的下面的函数称为路由,它首先获取源和目标  从命令的顶点作为String,然后我尝试实现上面的算法。 但是我不知道如何将它们存储在最终的Arraylist中以便我可以返回路径。另外,我认为这是有问题的

public void route(String wholeLine){
        String[] temp = wholeLine.split(" |\\>");
        int vel = Integer.parseInt(temp[3]);
        double totalpath = 0;
        int numOfSwitches = 0;
        // My Source and Destination Nodes
        Intersection src = intersections.get(temp[1]);
        Intersection dest = intersections.get(temp[2]);
        // Start the operation
        PriorityQueue<Intersection> pq = new PriorityQueue<RailNetwork.Intersection>();
        // Setting Distance to 0 for Source Node
        src.setDistanceFromSource(0);
        pq.add(src);
        // While Priority Queue not empty
        while(pq.size() != 0){
            // Extract the vertex with minimum distance value node from Min Heap. Let the extracted vertex be u.
            Intersection u = pq.remove();
            // For every adjacent vertex v of u, check if v is in Min Heap. 

            for(Intersection v : u.getNeigbours()){
                double weight = 0;
                // Find edgeweight of u and v from rail/edge list
                for(int i = 0;i<railList.size();i++){
                    if(railList.get(i).getSrc().getLabel().equals(u.label) && railList.get(i).getDest().getLabel().equals(v.label)){
                        weight = railList.get(i).getDistance();
                    }
                    else if(railList.get(i).getSrc().getLabel().equals(v.label) && railList.get(i).getDest().getLabel().equals(u.label)){
                        weight = railList.get(i).getDistance();
                    }
                }
                //If v is in Min Heap and distance value is more than weight of u-v plus distance value of u, then update the distance value of v.
                if(u.getDistanceFromSource() > v.getDistanceFromSource() + weight){
                    //Update distance of v, i.e., do dist[v] = dist[u] + weight(u, v)
                    v.setDistanceFromSource(u.getDistanceFromSource() + weight);
                    System.out.println(v.label + "\t" + v.distanceFromSource);
                    pq.add(v);
                }
            }

        }
    }

**有人可以帮助我找到我的错误,以便正确实施吗?**任何帮助或提示都表示赞赏。在此先感谢stackoverflow系列。

0 个答案:

没有答案