in a weighted graph (net), the sum of the weights of points A to point B in all paths is the shortest path, called the shortest route between A and b two points, and the first vertex on the path is the source point (source) and the last vertex is the end point (Destination). In an unauthorized graph, the shortest path is the path with the fewest number of edges experienced between two points. In fact, as long as each edge on the power map is considered to be a weight of 1 of the edge, then the power map and the shortest path with a weighted graph is consistent. given a weighted graph g= (v,e), specifying the V of a vertex in Figure G as the source point and finding the shortest path from V to each other vertex is called the shortest path problem of single source point. Dijkstra (Dijkstra) based on the increment of the length of the order to generate the shortest path from the source point v0 to other vertices, the shortest path currently being generated in addition to the end point, the remaining vertices of the shortest path has been generated this idea, put forward by the path length increment order of the Shortest Path generation algorithm (in this , the path length is the sum of the weights of the top and arc of the path). The idea of Dijkstra algorithm is: to the weighted graph g= (v,e), set up two vertex sets S and t=v-s, all the end points (vertices) with v0 as the source point and have determined the shortest path are incorporated into the set S, the initial state of the set S only contains the source point V0, while the vertices whose shortest path are not determined belong to set T, The initial state of the collection T contains vertices other than the source point v0. The path length of each vertex in the set T is put in order of the length increment of the shortest path between each vertex and v0. Also, each new vertex u in the set S is modified to modify the shortest path length of the remaining vertices in the source point V0 to the set T, i.e., the new shortest path length value of each vertex v in the set T or the original shortest path length value. or the shortest path length value of the vertex u plus the small value of the path length value of the vertex u to vertex v. The process of adding a vertex from a set T to the set S is repeated until the vertices of the set T are all added to the set S. in the implementation of the Dijkstra algorithm, a two-dimensional array gm is used as the storage structure of the N-Vertex weighted graph g= (v,e), and a one-dimensional array s (subscript is 0~n-1) is used to mark the vertices of the shortest path found in the set S, and it is stated that if S[i] is 0, Indicates that the shortest path of source point V0 to Vertex VI is not found, that is, VI is in the set T, and if S[i] is 1, the shortest path to the vertex VI of the source point V0 is found (at this time VI in the collection s). In addition to the array s, an array dist is set (the subscript is 0~n-1). Used to save the length of the current shortest path from the source point V0 to the Endpoint VI. The initial value of the dist is the weight on the <v0,vi> edge, and if V0 to VI has no edge, then the weight is & (infinity). The dist[i] value may be modified less each time a new vertex enters the collection S. The one-dimensional array path (subscript is 0~n-1) is used to hold the vertex sequence that passes through the top of the path in the shortest path length, where Path[i] Save from source point V0 to Endpoint VI the previous vertex number in the shortest path, its initial value is: if the V0 to VI has edge is placed Path[i] is the number of v0, if v0 to VI without edge path[i] is -1.Reference Code:
1#include <stdio.h>2 #defineMAXSIZE 63 #defineINF 327674 5 voidPpath (intPath[],intIintV0)//first-order recursion finds vertices on the shortest path (source point is V0)6 {7 intK;8k=Path[i];9 if(k!=v0)//when Vertex VK is not a source point V0Ten { OnePpath (PATH,K,V0);//recursively finds the previous vertex of a vertex VK Aprintf"%d,", k);//output vertex VK - } - } the - voidDispath (intDist[],intPath[],intS[],intV0,intN//Output Shortest Path - { - inti; + for(i=0; i<n;i++) - if(s[i]==1)//Vertex VI in the set S + { Aprintf"the shortest path length from%d to%d is:%d, the path is:", V0,i,dist[i]); atprintf"%d,", V0);//the source point on the output path is v0; -Ppath (PATH,I,V0);//Intermediate vertex vi on the output path -printf"%d\n", i);//end point on the output path - } - Else -printf"there is no path from%d to%d \ n", v0,i); in } - to voidDijkstra (intGm[][maxsize],intV0,intN//Dijkstra Algorithm + { - intDist[maxsize],path[maxsize],s[maxsize]; the intI,j,k,mindis; * for(i=0; i<n;i++) $ {Panax NotoginsengDist[i]=gm[v0][i];//V0 to VI the shortest path initial value assigned to Dist[i] -s[i]=0;//s[i]=0 indicates that vertex VI belongs to the T-set the if(Gm[v0][i]<inf)//path initialization, INF is the most desirable constant +path[i]=V0; A Else thepath[i]=-1;//V0 to VI No side + } -s[v0]=1;p ath[v0]=0;//V0 incorporates the set S and V0 a vertex in the current shortest path $ for(i=0; i<n;i++)//find the shortest path for n-1 vertices except V0, that is, loop n-1 times $ { -mindis=INF; - for(j=0; j<n;j++)//Select a vertex with the shortest path length from the current set T VK the if(s[j]==0&&dist[j]<Mindis) - {Wuyik=J; themindis=Dist[j]; - } Wus[k]=1;//Vertex VK joins the set S - for(j=0; j<n;j++)//adjusts the path length of the source point V0 to any vertex in the set T VJ About if(s[j]==0)//Vertex VJ in the collection T $ if(Gm[k][j]<inf&&dist[k]+gm[k][j]<dist[j])//when the path length of the V0 to VJ is less than the path length of V0 to VK and VK to VJ - { -dist[j]=dist[k]+Gm[k][j]; -Path[j]=k;//VK is the previous vertex of VJ in the current shortest path A } + } theDispath (Dist,path,s,v0,n);//Output Shortest Path - } $ the voidMain () the { the intG[maxsize][maxsize]={{inf, -, the, inf,inf,inf},{2, Inf,inf,inf,Ten, -},{inf,4, Inf,inf,inf,Ten}, the{Inf,inf,inf,inf,inf,inf},{inf,inf,inf, the, Inf,inf},{inf,inf,inf,4,Ten, INF}};//define adjacency Matrix G -Dijkstra (G,0,6);//finding the shortest path to vertex 0 in}
Output:
Weighted graphs and adjacency matrices:
Dijkstra algorithm for single source point shortest path