Basic ideas
When calculating the shortest path in Figure G by Dijkstra, you need to specify the starting point vs (that is, compute from vertex vs).
In addition, two sets of S and U are introduced. The function of S is to record the vertices that have been found for the shortest path, while U is the vertex (and the distance from the vertex to the start vs) where the shortest path is not yet calculated.
Initially, only the starting point in S vs;u is a vertex other than vs, and the path to the vertex in U is "Origin vs to the path of the vertex". Then, find the shortest path vertex from u and add it to S, then update the path of the vertex and vertex in U. Then, find the shortest path vertex from u and add it to S, then update the path of the vertex and vertex in U. ... Repeat the operation until all vertices are traversed.
Operation Steps
(1) Initially, S contains only the starting point vs;u contains other vertices other than vs, and the distance of the vertices in U is "start vs to that vertex distance" [For example, the distance of Vertex v in U is (vs,v), then vs and V are not adjacent, then V is the distance of ∞].
(2) Select the shortest vertex k from U and add the vertex k to S, and remove the vertex k from U.
(3) Update the distance from each vertex in U to start vs. The distance from the vertex in U is updated because the last step determines that K is the vertex of the shortest path, so that the distance from the other vertices can be updated by using K, for example, (VS,V) may be greater than (VS,K) + (k,v) distance.
(4) Repeat steps (2) and (3) until all vertices are traversed.
Let's take a simple example to solve the problem:
Packagecom.darrenchan.graph;Importjava.util.ArrayList;Importjava.util.List; Public classShortestpathdijkstra {/**adjacency Matrix*/ Private int[] Matrix; /**indicates positive Infinity*/ Private intMax_weight =Integer.max_value; /**Vertex Collection*/ Privatestring[] vertexes; /*** Create Diagram*/ Private voidCreategraph (intindex) {Matrix=New int[Index][index]; Vertexes=NewString[index]; int[] V0 = {0, 1, 5, Max_weight, Max_weight, Max_weight, Max_weight, Max_weight, max_weight}; int[] V1 = {1, 0, 3, 7, 5, Max_weight, Max_weight, Max_weight, max_weight}; int[] v2 = {5, 3, 0, Max_weight, 1, 7, Max_weight, Max_weight, max_weight}; int[] V3 = {max_weight, 7, max_weight, 0, 2, max_weight, 3, Max_weight, max_weight}; int[] v4 = {max_weight, 5, 1, 2, 0, 3, 6, 9, max_weight}; int[] v5 = {max_weight, max_weight, 7, Max_weight, 3, 0, Max_weight, 5, max_weight}; int[] V6 = {max_weight, max_weight, Max_weight, 3, 6, Max_weight, 0, 2, 7 }; int[] V7 = {max_weight, max_weight, Max_weight, Max_weight, 9, 5, 2, 0, 4 }; int[] V8 = {max_weight, max_weight, Max_weight, Max_weight, Max_weight, Max_weight, 7, 4, 0 }; matrix[0] =V0; matrix[1] =v1; matrix[2] =v2; matrix[3] =v3; matrix[4] =v4; matrix[5] =v5; matrix[6] =V6; matrix[7] =V7; matrix[8] =V8; vertexes[0] = "V0"; vertexes[1] = "V1"; vertexes[2] = "V2"; vertexes[3] = "V3"; vertexes[4] = "V4"; vertexes[5] = "V5"; vertexes[6] = "V6"; vertexes[7] = "V7"; vertexes[8] = "V8"; } /*** Dijkstra Shortest path. * * VS--The starting vertex (start vertex) is the shortest path of "Vertex vs" to each other vertex in the chart. */ Public voidDijkstraintvs) { //Flag[i]=true indicates that the shortest path to vertex vs to Vertex I has been successfully obtained Boolean[] flag =New Boolean[Vertexes.length]; //U is the record has not yet found the shortest path of the vertex (and the vertex to the beginning of the distance), with the flag used, flag[i] = True indicates that the I vertex in U has been removed int[] U =New int[Vertexes.length]; //The value of the predecessor vertex array, that is, Prev[i], is the vertex from vertex vs to the shortest path of vertex I, which precedes the vertex i. int[] prev =New int[Vertexes.length]; //The function of S is to record the vertices that have found the shortest pathstring[] S =NewString[vertexes.length]; //Step One: Initially, only the starting point in S vs;u is a vertex other than vs, and the path of the vertex in U is "Origin vs to the path of the vertex". for(inti = 0; i < vertexes.length; i++) {Flag[i]=false;//the shortest path to vertex i is not yet available. U[i] = Matrix[vs][i];//The initial distance between vertex I and Vertex vs is the right of "Vertex vs" to "vertex i". That is, the adjacency matrix vs row data. Prev[i]= 0;//Vertex i has a predecessor vertex of 0 } //"Remove" vs from u (used with flag)FLAG[VS] =true; U[VS]= 0; //adding vs vertices to sS[0] =Vertexes[vs]; //Step one end//Step four: Repeat step two or three until all vertices are traversed. //Traverse vertexes.length-1 times, and find the shortest path of a vertex at a time. intK = 0; for(inti = 1; i < vertexes.length; i++) { //Step Two: Find the shortest path vertex from u and add it to s (if the VS vertex to the X vertex has a shorter path, then//there is bound to be a Y vertex to the VS vertex path shorter than the former and not joined in S//so, the path to the shortest path vertex in U is the shortest path to that vertex.//that is, in the vertex that does not get the shortest path, find the closest vertex (k) to vs. intMin =Max_weight; for(intj = 0; J < Vertexes.length; J + +) { if(Flag[j] = =false&& U[j] <min) {min=U[j]; K=J; } } //put K in SS[i] =Vertexes[k]; //Step two End//Step Three: Update the paths of vertices and vertices in u//Mark "Vertex K" as already acquired to the shortest path (the vertex in update U, the flag corresponding to the K vertex is marked as true)FLAG[K] =true; //fixed the current shortest and precursor vertices (paths corresponding to the remaining vertices in the update u)//that is, after the shortest path to vertex k is already, update the shortest path and precursor vertex for vertices that do not get the shortest path. for(intj = 0; J < Vertexes.length; J + +) { //connect other vertices at the location of the K vertex to determine if the other vertices are less than the current shortest path through the shortest path vertex k arrival vs Vertex, yes, update into U, no, do not handle intTMP = (Matrix[k][j] = = Max_weight? Max_weight: (min +matrix[k][j])); if(Flag[j] = =false&& (TMP <U[j])) {U[j]=tmp; //Update J Vertex Shortest path precursor vertex is kPREV[J] =K; } } //end of Step three } //end of Step four//Print the results of the Dijkstra shortest pathSystem.out.println ("Starting vertex:" +Vertexes[vs]); for(inti = 0; i < vertexes.length; i++) {System.out.print ("Shortest Path (" + Vertexes[vs] + "," + vertexes[i] + "):" + u[i] + ""); List<String> Path =NewArraylist<>(); intj =i; while(true) {Path.add (vertexes[j]); if(j = = 0) Break; J=Prev[j]; } for(intx = Path.size ()-1; x >= 0; x--) { if(x = = 0) {System.out.println (Path.get (x)); } Else{System.out.print (path.get (x)+ "-"); }}} System.out.println ("The vertices are placed in the order of S:"); for(inti = 0; i< vertexes.length; i++) {System.out.print (s[i]); if(I! = vertexes.length-1) System.out.print ("--"); } } Public Static voidMain (string[] args) {Shortestpathdijkstra Dij=NewShortestpathdijkstra (); Dij.creategraph (9); Dij.dijkstra (0); }}
Ref: 56839285
[Data structure] Dijkstra (Dijkstra) algorithm