Advertisement
FahimFaisal

Dijkstra_2020

Feb 25th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Lab6_Dijkstra;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.util.LinkedList;
  11. import java.util.PriorityQueue;
  12. import java.util.Queue;
  13. import java.util.Scanner;
  14.  
  15.  
  16. /**
  17.  *
  18.  * @author acer
  19.  */
  20. public class Dijkstra {
  21.     public static int node;
  22.     public static int [] d;
  23.     public static int [] parent;
  24.     public static int [] [] matrix;
  25.      public static int [] color;
  26.      
  27.      public static PriorityQueue<Integer> q =new PriorityQueue<Integer>();
  28.    
  29.    
  30.     public static void main(String[] args) {
  31.   //float a=0;
  32.         try{
  33.             Scanner scn=new Scanner(new File("F:\\CSE_BRACU\\Courses_TAKEN\\LABS\\Algorithm_221\\Ins.txt"));
  34.             String[] splitA;
  35.            
  36.            
  37.             node=Integer.parseInt(scn.nextLine());
  38.            
  39.  
  40.             int edge=Integer.parseInt(scn.nextLine());
  41.            
  42.            
  43.          //   out.println(v);
  44.             matrix=new int[node][node];
  45.             color=new int[node];
  46.             parent=new int[node];
  47.             d=new int[node];
  48.             while(scn.hasNext())
  49.             {
  50.             String line=scn.nextLine();
  51.             splitA=line.split(",");
  52.             int x=Integer.parseInt(splitA[0]);
  53.             int y=Integer.parseInt(splitA[1]);
  54.             int w=Integer.parseInt(splitA[2]);
  55.             matrix[x][y]=w;
  56.             matrix[y][x]=w;
  57.             }
  58.          for(int i=0;i<node;i++) {
  59.                 for(int j=0;j<node;j++) {
  60.                     System.out.print(matrix[i][j]+",");
  61.                 }
  62.                 System.out.println();
  63.             }
  64.      
  65.            
  66.         } catch (Exception ex) {
  67.             ex.printStackTrace();
  68.         }
  69.         dijkstra(0);
  70.        
  71.          
  72.     }
  73.     public static void dijkstra(int s) {
  74.        
  75.        
  76.        for(int i=0;i<node;i++) {
  77.          
  78.             d[i]=999;
  79.              //q.add(d[i]);
  80.             color[i]=0;
  81.             parent[i]=-1;
  82.        
  83.         }
  84.    
  85.     d[s]=0;
  86.     parent[s]=-1;
  87.     q.add(d[s]);
  88.  
  89.     while (!(q.isEmpty())) {
  90.         int u=findIndex(q.poll(),d);
  91.         //System.out.println(q.peek());
  92.         //print();
  93.        
  94.         for(int v=0;v<node;v++) {
  95.             if(matrix[u][v]!=0) {
  96.                 if(color[v]==0) {
  97.                  if(d[v]>(d[u]+matrix[u][v])) {
  98.                      d[v]=d[u]+matrix[u][v];
  99.                      parent[v]=u;
  100.                    
  101.                  }
  102.                 }
  103.             }
  104.         }
  105.         color[u]=1;
  106.         queueSort();
  107.         System.out.println("for node "+u+": ");
  108.        
  109.      printPath(u);
  110.      //findPath(u);
  111.    
  112.    
  113.     }
  114.    
  115.     }
  116.          public static int findIndex(int value,int [] arr) {
  117.          int index=-1;
  118.          for(int i=0;i<arr.length;i++){
  119.              if(value==arr[i] ){
  120.                  index=i;
  121.              }
  122.          }
  123.         return index;
  124.     }
  125.      
  126.       public static void queueSort(){
  127.        q.clear();
  128.         for(int k=0;k<node;k++){
  129.             if(color[k]==0){
  130.             q.add(d[k]);
  131.             //System.out.println(d[k]);
  132.                
  133.         }
  134.        
  135.        }
  136.           //System.out.println("queue"+q.toString());
  137.      
  138.      
  139.     }
  140.      
  141.      /*public static void findPath(int x) {
  142.      
  143.          while(x!=-1){
  144.          System.out.print(x+"=:::>");
  145.         x=parent[x];
  146.          }
  147.        
  148.     }*/
  149.      
  150.      public static void printPath(int node){
  151.         int x=node;
  152.        
  153.         while(x!=-1 )
  154.         {
  155.             System.out.print(x+"<<-");
  156.             x=parent[x];
  157.            
  158.         }
  159.         System.out.println();
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement