Advertisement
manthanthakker40

Dijkstra's Algorithm

Jan 3rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 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 dijstras;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author manth
  13.  */
  14. public class Dijstras {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     static double graph[][];
  20.     public static void main(String[] args) {
  21.         build_graph();
  22.         shortest_path();
  23.     }
  24.     public static void shortest_path()
  25.     {
  26.         int selected[]=new int[graph.length];
  27.         double distance[]=new double[graph.length];
  28.         for(int i=0;i<graph.length;i++)
  29.         {
  30.             distance[i]=Double.POSITIVE_INFINITY;
  31.         }
  32.         int current=0;
  33.         distance[current]=0;
  34.         selected[current]=1;
  35.         int preced[]=new int[graph.length];
  36.         int k=0;
  37.         while(!all_selected(selected))
  38.         {
  39.           double smalldist=Double.POSITIVE_INFINITY;
  40.          
  41.           for(int i=0;i<graph.length;i++)
  42.           {
  43.             double new_dist=distance[current]+graph[current][i];
  44.             if(selected[i]==0)
  45.             {
  46.                     if(new_dist<distance[i])
  47.                     {
  48.                         distance[i]=new_dist;
  49.                         preced[i]=current+1;
  50.                     }
  51.                     if(smalldist>distance[i])
  52.                     {
  53.                         k=i;
  54.                         smalldist=distance[i];
  55.                     }
  56.             }
  57.            
  58.           }
  59.           current=k;
  60.           selected[current]=1;
  61.          
  62.         }
  63.         for(double d:distance)
  64.         {
  65.             System.out.println(d);
  66.         }
  67.         System.out.println("preced array ");
  68.         for(double d:preced)
  69.         {
  70.             System.out.println(d);
  71.         }
  72.        
  73.        
  74.     }
  75.     public static Boolean all_selected(int []selected)
  76.     {
  77.         for(int i=0;i<selected.length;i++)
  78.         {
  79.             if(selected[i]==0)
  80.                 return false;
  81.         }
  82.         return true;
  83.     }
  84.     public static void build_graph()
  85.     {
  86.         Scanner sc=new Scanner(System.in);
  87.         System.out.println(" Enter Number nodes ");
  88.         int no_nodes=Integer.parseInt(sc.nextLine());
  89.         System.out.println(" Enter Number of edges ");
  90.         int no_edges=Integer.parseInt(sc.nextLine());
  91.         graph=new double[no_nodes][no_nodes];
  92.         for(int i=0;i<no_nodes;i++)
  93.         {
  94.          for(int j=0;j<no_nodes;j++)
  95.          {
  96.              graph[i][j]=Double.POSITIVE_INFINITY;
  97.          }
  98.         }
  99.        
  100.         for(int i=0;i<no_edges;i++)
  101.         {
  102.             String input[]=sc.nextLine().split(" ");
  103.             int from_node=Integer.parseInt(input[0])-1;
  104.             int to_node=Integer.parseInt(input[1])-1;
  105.             double cost=Double.parseDouble(input[2]);
  106.             graph[from_node][to_node]=cost;
  107.             graph[to_node][from_node]=cost;
  108.            
  109.            
  110.            
  111.            
  112.         }
  113.         for(int i=0;i<no_nodes;i++)
  114.         {
  115.          for(int j=0;j<no_nodes;j++)
  116.          {
  117.              System.out.print(graph[i][j]+" ");
  118.          }
  119.          System.out.println();
  120.         }
  121.        
  122.        
  123.     }
  124.    
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement