簡體   English   中英

Dijkstra Alogrithm給出了錯誤的最短路徑

[英]Dijkstra Alogrithm gives wrong shortest path

我將Dijkstra's Algorithm C++實現轉換為Java 當我運行Java代碼時,沒有得到預期的輸出

預期從我的C++代碼:

Minimum distance for source vertex 0 to reach vertex 0 is 0
Minimum distance for source vertex 0 to reach vertex 1 is 4
Minimum distance for source vertex 0 to reach vertex 2 is 12
Minimum distance for source vertex 0 to reach vertex 3 is 19
Minimum distance for source vertex 0 to reach vertex 4 is 21
Minimum distance for source vertex 0 to reach vertex 5 is 11
Minimum distance for source vertex 0 to reach vertex 6 is 9
Minimum distance for source vertex 0 to reach vertex 7 is 8
Minimum distance for source vertex 0 to reach vertex 8 is 14

Java代碼實際:

Minimum distance for source vertex 0 to reach vertex 0 is 0
Minimum distance for source vertex 0 to reach vertex 1 is 4
Minimum distance for source vertex 0 to reach vertex 2 is 2
Minimum distance for source vertex 0 to reach vertex 3 is 7
Minimum distance for source vertex 0 to reach vertex 4 is 9
Minimum distance for source vertex 0 to reach vertex 5 is 2
Minimum distance for source vertex 0 to reach vertex 6 is 1
Minimum distance for source vertex 0 to reach vertex 7 is 1
Minimum distance for source vertex 0 to reach vertex 8 is 2

我試圖在Java代碼中尋找錯誤,我再次檢查是否正確復制了C ++代碼,但沒有發現任何不同。

我已經花了很多時間調試我的代碼。

我不明白怎么了! 我迫切需要幫助,謝謝!

碼:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;

public class Dijkstra
{
    public static final int INF = 100000;

    private static class IPair implements Comparable<IPair>
    {
        int first;
        int second;

        IPair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }

        public int compareTo(IPair that)
        {
            return this.first - that.first;
        }
    }

    public static int[] dijkstra(List<IPair>[] adj, int source)
    {
        Queue<IPair> pq = new PriorityQueue<>();
        int[] dist = new int[adj.length];
        boolean[] visited = new boolean[adj.length];

        Arrays.fill(dist, INF);

        pq.add(new IPair(0, source));
        dist[source] = 0;

        while (!pq.isEmpty())
        {
            int u = pq.poll().second;

            if (visited[u])
                continue;

            System.err.println(u);

            visited[u] = true;

            for (IPair pair : adj[u])
            {
                int v = pair.first;
                int weight = pair.second;

                if (dist[v] > dist[u] + weight)
                {
                    dist[v] = dist[u] + weight;
                    pq.add(new IPair(dist[v], v));

                    System.err.println(Arrays.toString(dist));
                }
            }
        }

        return dist;
    }

    private static void addEdge(List<IPair>[] adj, int u, int v, int weight)
    {
        adj[u].add(new IPair(v, weight));
        adj[v].add(new IPair(u, weight));
    }

    public static void main(String[] args)
    {
        int V = 9;

        List<IPair>[] adj = new ArrayList[V];

        Arrays.fill(adj, new ArrayList<IPair>());

        addEdge(adj, 0, 1, 4);
        addEdge(adj, 0, 7, 8);
        addEdge(adj, 1, 2, 8);
        addEdge(adj, 1, 7, 11);
        addEdge(adj, 2, 3, 7);
        addEdge(adj, 2, 8, 2);
        addEdge(adj, 2, 5, 4);
        addEdge(adj, 3, 4, 9);
        addEdge(adj, 3, 5, 14);
        addEdge(adj, 4, 5, 10);
        addEdge(adj, 5, 6, 2);
        addEdge(adj, 6, 7, 1);
        addEdge(adj, 6, 8, 6);
        addEdge(adj, 7, 8, 7);

        int[] dist = dijkstra(adj, 0);

        for (int i = 0; i < V; ++i)
            System.out.println("Minimum distance for source vertex " + 0 + " to reach vertex " + i + " is " + dist[i]);
    }
}

Arrays.fill(adj, new ArrayList<IPair>())
等效於:

List<IPair> list = new ArrayList<>();
Arrays.fill(adj, list)

這意味着您將在所有數組元素中存儲相同的 List對象。 當您更改List的對象adj[x]它改變了List的所有對象adj元素,因為它是同一個對象。
解決方案是在每個adj元素中存儲一個新的List對象:

//Arrays.fill(adj, new ArrayList<IPair>());
for (int i = 0; i < V; ++i) {
   adj[i] = new ArrayList<>();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM