Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package foxo;
- /**
- * @author N-Finity
- */
- public class Dijkstra {
- // Primary Graph
- int graph[][];
- // Array to store shortest distances from source node
- int sDistance[];
- // Boolean array that will hold the shortest
- // Path tree set True if the distance is finilized
- boolean sptSet[];
- // Number of nodes
- int numberOfNodes;
- // This will hold the source node value for output purpose
- int SrcNode;
- // This array will hold all the parent nodes
- int parents[];
- // Defining a var if there is no parent
- private static int NO_PARENT = -1;
- /**
- * Primary constructor will take parameter
- * that is number of nodes for the graph
- * and will init the graph and also vars necessary for
- * Dijkstra
- * @param Nodes The number of nodes that graph has
- */
- public Dijkstra(int Nodes) {
- this.graph = new int[Nodes][Nodes];
- this.sDistance = new int[Nodes];
- this.sptSet = new boolean[Nodes];
- this.numberOfNodes = Nodes;
- this.parents = new int[Nodes];
- }
- /**
- * This function will add the new node
- * in graph and update all the locations in graph
- * @param SourceNode Connection from node
- * @param DestinationNode Connection to node
- * @param weight Weight to go form one node to another
- */
- public void addNode(int SourceNode, int DestinationNode, int weight){
- this.graph[SourceNode][DestinationNode] = weight;
- this.graph[DestinationNode][SourceNode] = weight;
- }
- /**
- * This function will return the minimum distance
- * out of the Shortest Distance Array
- * And also check if it is the finalized Shortest Distance
- * if the value of index id true in sptSet
- * @return index of shortest distance
- */
- public int minDistance(){
- int minDistance = Integer.MAX_VALUE;
- int minDistanceIndex = -1;
- for(int i=0;i<this.numberOfNodes;i++){
- if(this.sDistance[i] < minDistance && this.sptSet[i] == false){
- minDistance = this.sDistance[i];
- minDistanceIndex = i;
- }
- }
- return minDistanceIndex;
- }
- /**
- * This Function will print the sDistance array
- */
- public void printDistances(){
- System.out.println("From\tTo\tDistance");
- for(int i = 0; i < this.numberOfNodes; i++){
- System.out.println(this.SrcNode+"\t"+i+"\t"+this.sDistance[i]);
- }
- System.out.println("All Shortest Distances!");
- }
- /**
- * This is a utility function to
- * print parent of current node's
- * @param CurrentNode int
- */
- public void printPaths(int CurrentNode){
- if(this.parents[CurrentNode] == NO_PARENT){
- System.out.print(CurrentNode);
- return;
- }
- this.printPaths(this.parents[CurrentNode]);
- System.out.print(" -> "+CurrentNode);
- }
- /**
- * This function will show shortest path
- * with the "Path" followed to reach the destination
- */
- public void printDistancesWithPath(){
- System.out.println("Source\tDestination\tDistance\tPath Followed");
- for(int i = 0; i < this.numberOfNodes; i++){
- System.out.print(this.SrcNode+"\t"+i+"\t\t"+this.sDistance[i]+"\t\t");
- this.printPaths(i);
- System.out.println();
- }
- }
- /**
- * This function will process the graph and
- * produce the shortest distance form supplied
- * "Source Node" To All nodes in graph
- * @param SourceNode Node to start
- */
- public void calculateDijkstra(int SourceNode){
- // Holding SourceNode value for printing purpose
- this.SrcNode = SourceNode;
- // First init all the arrays
- // sDistance array will initially hold infinite Shortest Distances
- // sptSet will hold all values false as none of the distance is finilized as
- // shortest Distances
- for(int i = 0; i<this.numberOfNodes; i++){
- this.sDistance[i] = Integer.MAX_VALUE;
- this.sptSet[i] = false;
- // For Path Printing
- this.parents[i] = -1;
- }
- // Now as we know distance form the source node to itself
- // is always 0 we are going to define it
- this.sDistance[SourceNode] = 0;
- // Because of this during first ittration the index of source node
- // is returned
- // Now we will loop through all nodes and calculate the Distance from
- // Source node
- for(int count = 0; count < this.numberOfNodes; count++){
- // Now we will get the shortest distance index form the above
- // minDistance function it is the index of the node with
- // Shortest distance
- int SDI = this.minDistance();
- // As this is the current minimum shortest distance we are going
- // to update the index value in sptSet
- this.sptSet[SDI] = true;
- // Now we are going to update the distances in sDistance Array
- // Only if
- // 1. It is connected to some other node i.e distance to other node is not 0
- // if it is 0 then node is not connected with each other
- // 2. The shortest distance is not yet finalized ie index value in sptSet is not True
- // if it finalized no need to update value
- // 3. And if its value in dist array is more then previous Min Distance plus the distance
- // to the next connected node
- // Repeat this for all the Nodes connected to the current node ie node with minimum distance
- // Node
- for(int n = 0; n < this.numberOfNodes; n++){
- if(this.graph[SDI][n] > 0 && this.sptSet[n] == false &&
- this.sDistance[n] > this.sDistance[SDI] + this.graph[SDI][n]){
- this.sDistance[n] = sDistance[SDI] + this.graph[SDI][n];
- this.parents[n] = SDI;
- }
- }
- }
- }
- // Main method
- public static void main(String[] args) {
- Dijkstra dijkstra = new Dijkstra(9);
- dijkstra.graph = new int[][]
- {{0, 4, 0, 0, 0, 0, 0, 8, 0},
- {4, 0, 8, 0, 0, 0, 0, 11, 0},
- {0, 8, 0, 7, 0, 4, 0, 0, 2},
- {0, 0, 7, 0, 9, 14, 0, 0, 0},
- {0, 0, 0, 9, 0, 10, 0, 0, 0},
- {0, 0, 4, 14, 10, 0, 2, 0, 0},
- {0, 0, 0, 0, 0, 2, 0, 1, 6},
- {8, 11, 0, 0, 0, 0, 1, 0, 7},
- {0, 0, 2, 0, 0, 0, 6, 7, 0}
- };
- dijkstra.calculateDijkstra(0);
- dijkstra.printDistances();
- dijkstra.printDistancesWithPath();
- }
- }
Add Comment
Please, Sign In to add comment