Advertisement
rootUser

dijkstra

Jun 13th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.62 KB | None | 0 0
  1. package dijkstra;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DIJKSTRA
  6. {
  7.     static int MAXNODES = 50;
  8.     static int MAX1 =150;
  9.     static int INFINITY = 5000;
  10.  
  11.     static int[][] weight=new int[MAXNODES][MAXNODES];
  12.     static int i,j;
  13.     static int[] distance=new int[MAXNODES];
  14.     static int[] visit=new int[MAXNODES];
  15.     static int[] precede=new int[MAXNODES];
  16.     static int itsFinal=0;
  17.     static int[] path=new int[MAX1];
  18.     static int smalldist,newdist,k,s,d,current,n,distcurr;
  19.  
  20.     static void Display_Result()
  21.     {
  22.         i=d;
  23.         path[itsFinal]=d;
  24.         itsFinal++;
  25.         while(precede[i]!=s)
  26.         {
  27.             j=precede[i];
  28.             i=j;
  29.             path[itsFinal]=i;
  30.             itsFinal++;
  31.         }
  32.         path[itsFinal]=s;
  33.         System.out.format("\nThe shortest path followed is :\n\n");
  34.         for(i=itsFinal; i>0; i--)
  35.         {
  36.             System.out.format("\t\t(%d -> %d) with cost = %d\n\n",path[i],path[i-1],weight[path[i]][path[i-1]]);
  37.         }
  38.         System.out.format("\nFor total cost = %d",distance[d]);
  39.     }
  40.  
  41.     public static void main(String[] args)
  42.     {
  43.         Scanner scanner = new Scanner(System.in);
  44.         System.out.format("Enter the number of nodes(Less than 50)in the matrix : ");
  45.         n=scanner.nextInt();
  46.  
  47.         System.out.format("\nEnter the cost matrix :\n\n");
  48.         for(i=0; i<n; i++)
  49.         {
  50.             for(j=0; j<n; j++)
  51.             {
  52.                 weight[i][j]=scanner.nextInt();
  53.             }
  54.         }
  55.         System.out.format("\nEnter the source node (0 to %d) : ",n-1);
  56.  
  57.         s=scanner.nextInt();
  58.         System.out.format("\nEnter the destination node (0 to %d) : ",n-1);
  59.  
  60.         d=scanner.nextInt();
  61.         for(i=0; i<n; i++)
  62.         {
  63.             distance[i]=INFINITY;
  64.             precede[i]=INFINITY;
  65.         }
  66.         distance[s]=0;
  67.         current=s;
  68.         visit[current]=1;
  69.         while(current!=d)
  70.         {
  71.             distcurr=distance[current];
  72.             smalldist=INFINITY;
  73.             for(i=0; i<n; i++)
  74.                 if(visit[i]==0)
  75.                 {
  76.                     newdist=distcurr+weight[current][i];
  77.                     if(newdist<distance[i])
  78.                     {
  79.                         distance[i]=newdist;
  80.                         precede[i]=current;
  81.                     }
  82.                     if(distance[i]<smalldist)
  83.                     {
  84.                         smalldist=distance[i];
  85.                         k=i;
  86.                     }
  87.                 }
  88.             current=k;
  89.             visit[current]=1;
  90.         }
  91.         Display_Result();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement