The Dijkstra (Dijkstra) algorithm is a typical shortest path routing algorithm used to calculate the shortest path of a node to all other nodes. The main feature is to extend from the center of the starting point to the outer layer until it expands to the end point. The Dijkstra algorithm can get the optimal solution of the shortest path, but it is inefficient because it traverses many nodes.
Dijkstra algorithm is a very representative of the shortest path algorithm, in many professional courses as the basic content of the detailed introduction, such as data structure, graph theory, operations research and so on.
The basic idea is to set up the vertex set S and continue to make greedy choices to augment this set. A vertex belongs to the set S when and only if the shortest path length from the source to the vertex is known.
Initially, S contains only the source. Set U is a vertex of G, the path from source to u and only through the vertices of s in the middle is called a special path from source to u, and the shortest special path length corresponding to each vertex is recorded with array Dist. The Dijkstra algorithm removes the vertex u with the shortest special path length from the V-s each time, adds u to S, and makes the necessary modifications to the array dist. Once s contains all the vertices in V, the Dist records the shortest path length from source to all other vertices.
For example, for a forward graph in a pair, the process of applying the Dijkstra algorithm to calculate the shortest path from source vertex 1 to other vertices is listed in the following table.
The iterative process of the Dijkstra algorithm:
The following is a concrete implementation (C + +):
#include <iostream>#include<stdio.h>#include<stdlib.h>using namespacestd;Const intMaxnum = -;Const intMaxint =999999;//Each array starts with subscript 1intDist[maxnum];//represents the shortest path length of the current point to the source pointintPrev[maxnum];//record the previous node of the current pointintC[maxnum][maxnum];//path length between two points of the record graphintn, line;//number of nodes and paths of graphs//N--N nodes//V--The source node//dist[]--the distance from the ith node to the source node//prev[]--The previous node of the ith node//c[][]--Every, nodes ' distancevoidDijkstra (intNintVint*dist,int*prev,intC[maxnum][maxnum]) { BOOLS[maxnum];//determine if the point has been deposited into the S collection for(intI=1; i<=n; ++i) {Dist[i]=C[v][i]; S[i]=0;//the point has not been used initially if(Dist[i] = =maxint) Prev[i]=0; ElsePrev[i]=v; } Dist[v]=0; S[V]=1; //A node that is not placed in the S collection, taking the dist[] minimum value, into the Union s//Once s contains all the vertices in V, Dist records the shortest path length from the source point to all other vertices//Note that starting with the second node, the first one is the source point for(intI=2; i<=n; ++i) {intTMP =Maxint; intU =v; //find the Dist[j] minimum of the point J currently unused for(intj=1; j<=n; ++j)if((!s[j]) && dist[j]<tmp) {u= J;//U Save the number of the point with the smallest distance in the current adjacency pointTMP =Dist[j]; } S[u]=1;//indicates that the U point has been deposited in the S collection//Update Dist for(intj=1; j<=n; ++j)if((!s[j]) && c[u][j]<maxint) { intNewdist = Dist[u] +C[u][j]; if(Newdist <Dist[j]) {Dist[j]=newdist; PREV[J]=u; } } }}//find the path from the source point V to the end of U, and outputvoidSearchPath (int*prev,intVintu) { intQue[maxnum]; inttot =1; Que[tot]=u; Tot++; intTMP =Prev[u]; while(TMP! =v) {Que[tot]=tmp; Tot++; TMP=Prev[tmp]; } Que[tot]=v; for(intI=tot; i>=1; --i)if(I! =1) cout<< Que[i] <<" -"; Elsecout<< Que[i] <<Endl;}intMain () {Freopen ("Input.txt","R", stdin); //Each array starts with subscript 1//input node numbersCIN >>N; //number of input pathsCIN >>Line ; intP, Q, Len;//input p, q two point and its path length//Initialize c[][] to Maxint for(intI=1; i<=n; ++i) for(intj=1; j<=n; ++j) C[i][j]=Maxint; for(intI=1; i<=line; ++i) {cin>> p >> Q >>Len; if(Len < c[p][q])//with heavy edges{C[p][q]= Len;//P point to QC[q][p] = len;//Q points to P, which means no graph } } for(intI=1; i<=n; ++i) dist[i]=Maxint; for(intI=1; i<=n; ++i) { for(intj=1; j<=n; ++j) printf ("%8d", C[i][j]); printf ("\ n"); } Dijkstra (N,1, Dist, Prev, c); //Shortest Path lengthcout <<"the shortest path length of the source point to the last vertex:"<< Dist[n] <<Endl; //Pathcout <<"the path of the source point to the last vertex is:"; SearchPath (prev,1, n);}
Input.txt File Contents:
5
7
1 2 10
1 4 30
1 5 100
2 3 50
3 5 10
4 3 20
4 5 60
Output Result:
999999 10 999999) 30 100
10 999999 50) 999999 999999
999999 50 999999) 20 10
30 999999 20) 999999 60
100 999999 10) 60 999999
Shortest path length of source point to last vertex: 60
The path of the source point to the last vertex is: 1, 4, 3, 5
Process returned 0 (0x0) execution time:0.024 s
Press any key to continue.
Original link: http://www.wutianqi.com/?p=1890
Thanks to the original author!
The analysis and implementation of the shortest Path algorithm-dijkstra (Dijkstra) algorithm (c + +)