Mr_HO1A

Dijkstra Shortest Path

Oct 9th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.72 KB | None | 0 0
  1. package foxo;
  2.  
  3. /**
  4.  * @author N-Finity
  5.  */
  6. public class Dijkstra {
  7.     // Primary Graph
  8.     int graph[][];
  9.     // Array to store shortest distances from source node
  10.     int sDistance[];
  11.     // Boolean array that will hold the shortest
  12.     // Path tree set True if the distance is finilized
  13.     boolean sptSet[];
  14.     // Number of nodes
  15.     int numberOfNodes;
  16.     // This will hold the source node value for output purpose
  17.     int SrcNode;
  18.     // This array will hold all the parent nodes
  19.     int parents[];
  20.     // Defining a var if there is no parent
  21.     private static int NO_PARENT = -1;
  22.     /**
  23.      * Primary constructor will take parameter
  24.      * that is number of nodes for the graph
  25.      * and will init the graph and also vars necessary for
  26.      * Dijkstra
  27.      * @param Nodes The number of nodes that graph has
  28.      */
  29.     public Dijkstra(int Nodes) {
  30.         this.graph = new int[Nodes][Nodes];
  31.         this.sDistance = new int[Nodes];
  32.         this.sptSet = new boolean[Nodes];
  33.         this.numberOfNodes = Nodes;
  34.         this.parents = new int[Nodes];
  35.     }
  36.     /**
  37.      * This function will add the new node
  38.      * in graph and update all the locations in graph
  39.      * @param SourceNode Connection from node
  40.      * @param DestinationNode Connection to node
  41.      * @param weight Weight to go form one node to another
  42.      */
  43.     public void addNode(int SourceNode, int DestinationNode, int weight){
  44.         this.graph[SourceNode][DestinationNode] = weight;
  45.         this.graph[DestinationNode][SourceNode] = weight;
  46.     }
  47.     /**
  48.      * This function will return the minimum distance
  49.      * out of the Shortest Distance Array
  50.      * And also check if it is the finalized Shortest Distance
  51.      * if the value of index id true in sptSet
  52.      * @return index of shortest distance
  53.      */
  54.     public int minDistance(){
  55.         int minDistance = Integer.MAX_VALUE;
  56.         int minDistanceIndex = -1;
  57.         for(int i=0;i<this.numberOfNodes;i++){
  58.             if(this.sDistance[i] < minDistance && this.sptSet[i] == false){
  59.                 minDistance = this.sDistance[i];
  60.                 minDistanceIndex = i;
  61.             }
  62.         }
  63.         return minDistanceIndex;
  64.     }
  65.     /**
  66.      * This Function will print the sDistance array
  67.      */
  68.     public void printDistances(){
  69.         System.out.println("From\tTo\tDistance");
  70.         for(int i = 0; i < this.numberOfNodes; i++){
  71.             System.out.println(this.SrcNode+"\t"+i+"\t"+this.sDistance[i]);
  72.         }
  73.         System.out.println("All Shortest Distances!");
  74.     }
  75.     /**
  76.      * This is a utility  function to
  77.      * print parent of current node's
  78.      * @param CurrentNode int
  79.      */
  80.     public void printPaths(int CurrentNode){
  81.         if(this.parents[CurrentNode] == NO_PARENT){
  82.             System.out.print(CurrentNode);
  83.             return;
  84.         }
  85.         this.printPaths(this.parents[CurrentNode]);
  86.         System.out.print(" -> "+CurrentNode);
  87.     }
  88.     /**
  89.      * This function will show shortest path
  90.      * with the "Path" followed to reach the destination
  91.      */
  92.     public void printDistancesWithPath(){
  93.         System.out.println("Source\tDestination\tDistance\tPath Followed");
  94.         for(int i = 0; i < this.numberOfNodes; i++){
  95.             System.out.print(this.SrcNode+"\t"+i+"\t\t"+this.sDistance[i]+"\t\t");
  96.             this.printPaths(i);
  97.             System.out.println();
  98.         }
  99.     }
  100.     /**
  101.      * This function will process the graph and
  102.      * produce the shortest distance form supplied
  103.      * "Source Node" To All nodes in graph
  104.      * @param SourceNode  Node to start
  105.      */
  106.     public void calculateDijkstra(int SourceNode){
  107.         // Holding SourceNode value for printing purpose
  108.         this.SrcNode = SourceNode;
  109.        
  110.         // First init all the arrays
  111.         // sDistance array will initially hold infinite Shortest Distances
  112.         // sptSet will hold all values false as none of the distance is finilized as
  113.         // shortest Distances
  114.         for(int i = 0; i<this.numberOfNodes; i++){
  115.             this.sDistance[i] = Integer.MAX_VALUE;
  116.             this.sptSet[i] = false;
  117.             // For Path Printing
  118.             this.parents[i] = -1;
  119.         }
  120.         // Now as we know distance form the source node to itself
  121.         // is always 0 we are going to define it
  122.         this.sDistance[SourceNode] = 0;
  123.         // Because of this during first ittration the index of source node
  124.         // is returned
  125.        
  126.         // Now we will loop through all nodes and calculate the Distance from
  127.         // Source node
  128.         for(int count = 0; count < this.numberOfNodes; count++){
  129.             // Now we will get the shortest distance index form the above
  130.             // minDistance function it is the index of the node with
  131.             // Shortest distance
  132.             int SDI = this.minDistance();
  133.            
  134.             // As this is the current minimum shortest distance we are going
  135.             // to update the index value in sptSet
  136.             this.sptSet[SDI] = true;
  137.             // Now we are going to update the distances in sDistance Array
  138.             // Only if
  139.             // 1. It is connected to some other node i.e distance to other node is not 0
  140.             //    if it is 0 then node is not connected with each other
  141.             // 2. The shortest distance is not yet finalized ie index value in sptSet is not True
  142.             //    if it finalized no need to update value
  143.             // 3. And if its value in dist array is more then previous Min Distance plus the distance
  144.             //    to the next connected node
  145.             // Repeat this for all the Nodes connected to the current node ie node with minimum distance
  146.             // Node
  147.             for(int n = 0; n < this.numberOfNodes; n++){
  148.                 if(this.graph[SDI][n] > 0 && this.sptSet[n] == false &&
  149.                         this.sDistance[n] > this.sDistance[SDI] + this.graph[SDI][n]){
  150.                     this.sDistance[n] = sDistance[SDI] + this.graph[SDI][n];
  151.                     this.parents[n] = SDI;
  152.                 }
  153.             }
  154.         }
  155.     }
  156.     // Main method
  157.     public static void main(String[] args) {
  158.         Dijkstra dijkstra = new Dijkstra(9);
  159.         dijkstra.graph = new int[][]
  160.         {{0, 4, 0, 0, 0, 0, 0, 8, 0},
  161.         {4, 0, 8, 0, 0, 0, 0, 11, 0},
  162.         {0, 8, 0, 7, 0, 4, 0, 0, 2},
  163.         {0, 0, 7, 0, 9, 14, 0, 0, 0},
  164.         {0, 0, 0, 9, 0, 10, 0, 0, 0},
  165.         {0, 0, 4, 14, 10, 0, 2, 0, 0},
  166.         {0, 0, 0, 0, 0, 2, 0, 1, 6},
  167.         {8, 11, 0, 0, 0, 0, 1, 0, 7},
  168.         {0, 0, 2, 0, 0, 0, 6, 7, 0}
  169.         };
  170.         dijkstra.calculateDijkstra(0);
  171.         dijkstra.printDistances();
  172.         dijkstra.printDistancesWithPath();
  173.     }
  174.    
  175. }
Add Comment
Please, Sign In to add comment