Advertisement
FahimFaisal

dijkstra2_updated

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