簡體   English   中英

我需要幫助將所有對最短路徑算法實現到給定的代碼中

[英]I need help implement all pair shortest path algorithm into a code given

如果有人可以幫助將以下算法實現到以下 java 代碼中,我真的會,我還會添加圖表和示例 output。 [Java 代碼對不起它不讓我發帖][ https://i.stack.imgur.com/meipB.png] [算法}[ https://i.stack.imgur.com/pE6Pv.png] [圖][ https://i.stack.imgur.com/WnSFk.png] [輸出][ https://i.stack.imgur.com/Mpdoz.png] [輸出][ Z5E056A7B710iBADE5018B47B7B710iB.ADE .imgur.com/geBJP.png]

在這里,我發布了實現的算法,它似乎沒有給出打印頂點所需的正確 L[ ] 值。 請在我無法繼續之前查看算法是否正確。

    public class Graph {

    public static void main(String[] args) {

        int[][] W = new int[][] { { 0, 2, Integer.MAX_VALUE, 2 }, { 0, 0, Integer.MAX_VALUE, 2 },
                { 1, -1, 0, Integer.MAX_VALUE }, { Integer.MAX_VALUE, -1, -1, 0 } };

        int[][] result = APSP1(W);

        display(4, result);
    }

    /**
     * @param w
     * @return
     */
    private static int[][] APSP1(int[][] w) {
        int n = w.length;
        LinkedList<int[][]> L = new LinkedList<int[][]>();
        L.add(w.clone());

//      display(1, L.get(0));

        for (int i = 1; i < n - 1; i++) {
            display(i, L.get(i-1));
            L.add(Extend(L.get(i - 1), w));
        }
        return L.get(n - 2);
    }

    /**
     * @param i
     * @param integers
     */
    private static void display(int i, int[][] arr) {
        System.out.println("L" + i);
        for (int[] ar : arr) {
            for (int a : ar) {
                if (a == Integer.MAX_VALUE) {
                    System.out.print(" ~ ");
                } else {
                    System.out.print(" " + a + " ");
                }
            }
            System.out.println();
        }

        System.out.println();
    }

    /**
     * @param integers
     * @param w
     * @return
     */
    private static int[][] Extend(int[][] l, int[][] w) {
        int n = l.length;
        int[][] l$ = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                l$[i][j] = Integer.MAX_VALUE;
                for (int k = 0; k < n; k++) {
                    int n2;
                    if (l[i][k] == Integer.MAX_VALUE || w[k][j] == Integer.MAX_VALUE) {
                        n2 = Integer.MAX_VALUE;
                    } else {
                        n2 = l[i][k] + w[k][j];
                    }
                    l$[i][j] = min(l[i][j], n2);
                }

            }
        }

        return l$;
    }

    /**
     * @param integer
     * @param i
     * @return
     */
    private static int min(int n1, int n2) {
        if (n1== Integer.MAX_VALUE && n2 == Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        }
        if (n1 >= Integer.MAX_VALUE) {
            return n2;
        }
        if (n2 >= Integer.MAX_VALUE) {
            return n1;
        }
        return n1 < n2 ? n1 : n2;
    }

}

在您的 geBJP 圖像中,將ABDC顯示為最短路徑 [從 A -> C] A->(2)->B->(2)->D->(-1)->C總成本 = 3 但在圖A ->(2)->D->(-1)->C是成本 = 1 的最短路徑

暫無
暫無

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

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