Algorithmic Thinking
Extends from the center of the starting point to the outer layer until it extends to the end point.
Algorithm main steps
1. Construct a two-dimensional array weight storage graph, weight[i][j] represents the weight of node I to node J, that is, the distance from node I to Node J (hereinafter denoted by Dij).
2. Build the array shortpath, store the starting node (0) to the minimum distance of each node , i.e. d0j (j for all nodes), initialize shortpath[0] = 0, and the other values are infinity.
3. Build the array visited to mark whether the nodes have been extended (assuming 0 is extended, 1 is extended), initialize visited[0] = 1, and the other values are zero.
4. Iterate the algorithm, traverse the two-dimensional array weight, select the unmarked node K, which is the shortest distance from the starting node, record the d0k to ShortPath, and Mark K as expanded.
The distance from the starting node to the other nodes is updated by K , if d0j > d0k + dkj, then d0j = d0k + dkj.
Code implementation
Public Static int[] Dijsktra (int[] weight,intstart) { intlength = Weight.length;//get the number of vertices int[] ShortPath =New int[Length];//Shortest Path Arrayshortpath[0] = 0;//string[] Path=NewString[length];//record the shortest path from the starting point to each point for(inti = 0; i < length; i++) {Path[i]= start + "+" +i; } int[] visited =New int[Length];//records whether the shortest path to the current vertex has been calculated, 1 indicates that for(inti = 0; i < length; i++) {Visited[i]= 0; } visited[0] = 1;//the shortest path to the starting point has been calculated for(intm = 1; m < length; M + +){ intK =-1; intDMin =Integer.max_value; //Select an unlabeled vertex closest to the starting point, and the shortest path to the starting point is DMin for(intn = 0; n < length; n++){ if(Visited[n] = = 0 && Weight[start][n] <DMin) {DMin=Weight[start][n]; K=N; }} Shortpath[k]=DMin; VISITED[K]= 1; //To update the distance from the starting point to the other unlabeled points with K as the middle point for(intj = 0; J < length; J + +){ if(Visited[j] = = 0 && weight[k][j]! = Integer.max_value && weight[start][k] + Weight[k][j] <Weight[start][j]) {Weight[start][j]= Weight[start][k] +Weight[k][j]; PATH[J]= Path[k] + "+" +J; } } } for(inti = 1; i < length; i + +) {System.out.println ("Starting point to" + i + "Shortest path is:" + path[i] + "Distance:" +Shortpath[i]); } returnShortPath; }
Declaring constants
Public Static Final int MAX = Integer.max_value;
Code testing
Public Static void Main (string[] args) { int[] [] Weigth = {{0,50,70, Max,max}, {50,0,15,30, MAX}, {70,15,0,max,40}, {MAX,30,max,0,20}, {max,max,40,20,0}}; Dijsktra (Weigth,0); }
Run results
The shortest path from the starting point to 1 is: 0->1 Distance: 50
The shortest path from the starting point to 2 is: 0->1->2 Distance: 65
The shortest path from the starting point to 3 is: 0->1->3 Distance: 80
The shortest path from the starting point to 4 is: 0->1->3->4 Distance: 100
Java implementation of Dijkstra shortest path algorithm