Advertisement
FahimFaisal

Dijkstra

Nov 13th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.03 KB | None | 0 0
  1. import static java.lang.System.out;
  2. /*
  3.  *1-
  4. 0-1-
  5. 2-1-
  6. 8-2-1-
  7. 7-1-
  8. 5-2-1-
  9. 6-7-1-
  10. 3-2-1-
  11. 4-5-2-1-
  12.  */
  13. import java.io.File;
  14. import java.util.PriorityQueue;
  15. import java.util.Scanner;
  16. /*
  17.  * 0-
  18. 1-0-
  19. 7-0-
  20. 6-7-0-
  21. 5-6-7-0-
  22. 2-1-0-
  23. 8-2-1-0-
  24. 3-2-1-0-
  25. 4-5-6-7-0-
  26. 0 4 12 19 21 11 9 8 14 The updated list after finding shortest Path
  27. [0, 1, 7, 6, 5, 2, 8, 3, 4]
  28.  
  29.  */
  30. public class Dijkstra {
  31.     public static int node;
  32.     public static int [][] matrix;
  33.     public static int [] color;
  34.     public static int [] parent;
  35.     public static int [] vertices= {0,1,2,3,4,5,6,7,8};
  36.     public static int [] d;
  37.    
  38.     public static PriorityQueue<Integer> q = new PriorityQueue<Integer>();
  39.    
  40.    
  41.  
  42.     public static void main(String [] args) {
  43.      try{
  44.             Scanner scn=new Scanner(new File("C://Users//user//Desktop//Dijkstra.txt"));
  45.             String[] splitA;
  46.            
  47.             String temp=scn.nextLine();
  48.             node=Integer.parseInt(temp);
  49.             node=node;
  50.            
  51.          String temp1=scn.nextLine();
  52.             int edge=Integer.parseInt(temp1);
  53.            
  54.            
  55.          //   out.println(v);
  56.             matrix=new int[node][node];
  57.             color=new int[node];
  58.             parent=new int[node];
  59.             d=new int[node];
  60.             while(scn.hasNext())
  61.             {
  62.             String line=scn.nextLine();
  63.             splitA=line.split(",");
  64.             int x=Integer.parseInt(splitA[0]);
  65.             int y=Integer.parseInt(splitA[1]);
  66.             int w=Integer.parseInt(splitA[2]);
  67.             matrix[x][y]=w;
  68.             matrix[y][x]=w;
  69.             }
  70.          for(int i=0;i<node;i++) {
  71.                 for(int j=0;j<node;j++) {
  72.                     System.out.print(matrix[i][j]+",");
  73.                 }
  74.                 System.out.println();
  75.             }
  76.      
  77.      getShortestPath(1);
  78.      }
  79.             catch(Exception e) {
  80.             e.printStackTrace();   
  81.             }
  82.     }
  83.     public static void sortQueue() {
  84.         q.clear();
  85.         for (int index = 0; index < node; index++) {
  86.             if (color[index]==0) {
  87.                 q.add(d[index]);
  88.             }
  89.         }
  90.     }
  91.     public static void getShortestPath(int s) {
  92.         for(int i=0;i<node;i++) {
  93.             if (i != s) {
  94.             d[i]=999;
  95.              //q.add(d[i]);
  96.             color[i]=0;
  97.             parent[i]=-1;
  98.         }
  99.         }
  100.    
  101.     d[s]=0;
  102.     parent[s]=-1;
  103.     q.add(d[s]);
  104.     int count=0;
  105.     while (!(q.isEmpty())) {
  106.         int u=findIndex(d,q.poll());
  107.         //System.out.println(q.peek());
  108.         //print();
  109.         color[u]=1;
  110.         for(int v=0;v<node;v++) {
  111.             if(matrix[u][v]!=0) {
  112.                 if(color[v]==0) {
  113.                  if(d[v]>(d[u]+matrix[u][v])) {
  114.                      d[v]=d[u]+matrix[u][v];
  115.                      parent[v]=u;
  116.                    
  117.                  }
  118.                 }
  119.             }
  120.         }
  121.         //System.out.print(vertices[u]+"=>");
  122.         printPath(u);
  123.      sortQueue();
  124.      count++;
  125.     }
  126.     print();
  127.     printCost();
  128.     }
  129.     public static void printPath(int node){
  130.         int x=node;
  131.        
  132.         while(x!=-1 )
  133.         {
  134.             out.print(x+"-");
  135.             x=parent[x];
  136.            
  137.         }
  138.         out.println();
  139.     }
  140.    
  141.     public static void printCost() {
  142.         for(int m=0;m<node;m++) {
  143.             System.out.print(m+"=>" +d[m]+"$ ,");
  144.     }
  145.     }
  146.    
  147.     public static void print() {
  148.          System.out.println();
  149.          System.out.println();
  150.          for(int c=0;c<node;c++) {
  151.              
  152.               if(color[c]==1) {
  153.                  System.out.print("Visited"+",");
  154.                  
  155.              }
  156.              else {
  157.                  System.out.print("Undiscovered"+",");
  158.              }
  159.         }
  160.          System.out.println();
  161.         }
  162.    
  163.      public static int findIndex(int arr[], int t)
  164.         {
  165.      
  166.             // if array is Null
  167.             if (arr == null) {
  168.                 return -1;
  169.             }
  170.      
  171.             // find length of array
  172.             int len = arr.length;
  173.             int i = 0;
  174.      
  175.             // traverse in the array
  176.             while (i < len) {
  177.      
  178.                 // if the i-th element is t
  179.                 // then return the index
  180.                 if (arr[i] == t && color[i]==0) {
  181.                     return i;
  182.                 }
  183.                 else {
  184.                     i = i + 1;
  185.                 }
  186.             }
  187.             return -1;
  188.         }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement