Advertisement
Eternoseeker

djakstralinkstate

Jun 7th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | Source Code | 0 0
  1. import java.util.*;
  2.  
  3. public class djakstralinkstate {
  4.     public int distance[] = new int[10];
  5.     public int cost[][] = new int[10][10];
  6.  
  7.     public void calc(int n, int s) {
  8.         int flag[] = new int[n + 1];
  9.         int i, minpos = 1, k, c, minimum;
  10.         for (i = 1; i <= n; i++) {
  11.             flag[i] = 0;
  12.             this.distance[i] = this.cost[s][i];
  13.         }
  14.         c = 2;
  15.         while (c <= n) {
  16.             minimum = 99;
  17.             for (k = 1; k <= n; k++) {
  18.                 if (this.distance[k] < minimum && flag[k] != 1) {
  19.                     minimum = this.distance[i];
  20.                     minpos = k;
  21.                 }
  22.             }
  23.             flag[minpos] = 1;
  24.             c++;
  25.             for (k = 1; k <= n; k++) {
  26.                 if (this.distance[minpos] + this.cost[minpos][k] < this.distance[k] && flag[k] != 1)
  27.                     this.distance[k] = this.distance[minpos] + this.cost[minpos][k];
  28.             }
  29.         }
  30.     }
  31.  
  32.     public static void main(String args[]) {
  33.         int nodes, source, i, j;
  34.         Scanner in = new Scanner(System.in);
  35.         System.out.println("Enter the Number of Nodes \n");
  36.         nodes = in.nextInt();
  37.         djakstralinkstate d = new djakstralinkstate();
  38.         System.out.println("Enter the Cost Matrix Weights: \n");
  39.         for (i = 1; i <= nodes; i++)
  40.             for (j = 1; j <= nodes; j++) {
  41.                 d.cost[i][j] = in.nextInt();
  42.                 if (d.cost[i][j] == 0)
  43.                     d.cost[i][j] = 999;
  44.             }
  45.         System.out.println("Enter the Source Vertex :\n");
  46.         source = in.nextInt();
  47.         d.calc(nodes, source);
  48.         System.out.println("The Shortest Path from Source\t" + source + "\t to all other vertices are : \n");
  49.         for (i = 1; i <= nodes; i++)
  50.             if (i != source)
  51.                 System.out.println(
  52.                         "source :" + source + "\tdestination :" + i + "\t MinCost is :" + d.distance[i] + "\t");
  53.     }
  54. }
  55. /*
  56.  * OUTPUT
  57.  * iotlab@iotlab-Veriton-M200-B360:~$ javac Dijkstra.java
  58.  * iotlab@iotlab-Veriton-M200-B360:~$ java Dijkstra
  59.  * Enter the Number of Nodes
  60.  * 5
  61.  * Enter the Cost Matrix Weights:
  62.  * 0 1 0 2 0
  63.  * 1 0 1 0 4
  64.  * 0 1 0 0 3
  65.  * 2 0 0 0 3
  66.  * 0 4 3 3 0
  67.  * Enter the Source Vertex :
  68.  * 1
  69.  * The Shortest Path from Source 1 to all other vertices are :
  70.  * source :1 destination :2 MinCost is :1
  71.  * source :1 destination :3 MinCost is :2
  72.  * source :1 destination :4 MinCost is :2
  73.  * source :1 destination :5 MinCost is :5
  74.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement