java - Prims 算法到 Dijkstra 算法

标签 java algorithm

我正在尝试使我现有的 Prim 算法实现与源保持跟踪距离。由于 prim 和 Dijkstra 的算法几乎相同。我不知道我在哪里遗漏了什么。

我知道问题出在哪里,但无法弄清楚。

这是我的代码,如何修改它以打印从源到所有其他顶点的最短距离。最短距离存储在名为:dist[]

的数组中

代码:

package Graphs;

import java.util.ArrayList;

public class Prims {

    static int no_of_vertices = 0;

    public static void main(String[] args) {
        int[][] graph = {{0, 2, 0, 6, 0},
                {2, 0, 3, 8, 5},
                {0, 3, 0, 0, 7},
                {6, 8, 0, 0, 9},
                {0, 5, 7, 9, 0},
               };
        no_of_vertices = graph.length;
        int [][] result =  new int [no_of_vertices][no_of_vertices];
        boolean[] visited = new boolean[no_of_vertices];
        int dist[] = new int[no_of_vertices];
        for (int i = 0; i < no_of_vertices; i++)
            for (int j = 0; j < no_of_vertices; j++) {
                result[i][j]= 0;
                if (graph[i][j] == 0) {
                    graph[i][j] = Integer.MAX_VALUE;
                }
            }

        for (int i = 0; i < no_of_vertices; i++) {
            visited[i] = false;
            dist[i] = 0;

        }
        ArrayList<String> arr = new ArrayList<String>();
        int min;
        visited[0] = true;
        int counter = 0;
        while (counter < no_of_vertices - 1) {
            min = 999;
            for (int i = 0; i < no_of_vertices; i++) {
                if (visited[i] == true) {
                    for (int j = 0; j < no_of_vertices; j++) {
                        if (!visited[j] && min > graph[i][j]) {
                            min = graph[i][j];
                            dist[i] += min; //  <------ Problem here
                            visited[j] = true;
                            arr.add("Src :" + i + " Destination : " + j
                                    + " Weight : " + min);
                        }
                    }
                }
            }
            counter++;
        }


        for (int i = 0; i < no_of_vertices; i++) {
            System.out.println("Source :  0" + " Destination : " + i
                    + " distance : " + dist[i]);
        }

        for (String str : arr) {
            System.out.println(str);
        }
    }
}

距离数组的计算有一个错误,因为它忘记添加任何中间节点从源到目的地的距离。

最佳答案

for (int j = 0; j < no_of_vertices; j++) {
    if (!visited[j] && min > graph[i][j]) {
        min = graph[i][j];
        dist[i] += min; //  <------ Problem here

当然不会添加中间边,因为您只添加了当前边。你可能想要这样的东西:

if (dist[i] + graph[i][j] < dist[j])
    dist[j] = dist[i] + graph[i][j];

并去掉 min 变量。

尽管您的算法在我看来并不正确。你应该在每一步选择具有最小 d[] 的节点,并按照我上面写的更新该节点的邻居,然后将其标记为已选择并且不再选择它。

关于java - Prims 算法到 Dijkstra 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905551/

相关文章:

java - 在 Fedora 25 上使用 React Native for Android 时遇到的问题

java - JSpinner 值变化事件

java - 如何在更短的时间内遍历大型 Java 整数列表?

java - 将 Queue 和 Stack 相互复制

algorithm - 如何计算目标中加权值的相似性以创建良好的聚类

algorithm - 动态规划 : Why Knuth's improvement to Optimal Binary Search Tree O(n^2)?

c - 简单加密 - C 中的哈希总和

java - Android 用光标填充 ListView

java - JSF2.0 + Eclipse

java - Java代码模型是否支持GenericEntity