Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prims algorithm to Dijkstra algorithm

Tags:

java

algorithm

I am trying to make my existing implementation of Prim's algorithm to keep track distances from source . Since prim's and Dijkstra's algorithm are almost same. I can't figure out where am I missing something.

I know what the problem is but cannot figure it out.

Here is my code, how do I modify it to print the shortest distance from source to all other vertex. Shortest distance is stored in array named : dist[]

Code:

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);
        }
    }
}

There is a mistake in calculation of distance array as it forgets to add the distance of any intermediate nodes from source to destination.

like image 371
user000111181 Avatar asked Aug 06 '26 13:08

user000111181


1 Answers

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

Of course intermediate edges don't get added, because you only add the current edge. You probably want something like:

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

And get rid of the min variable.

Although your algorithm does not look correct to me. You're supposed to pick the node with minimum d[] at each step, and update that node's neighbors as I wrote above, then mark it as picked and never pick it again.

like image 186
IVlad Avatar answered Aug 08 '26 01:08

IVlad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!