-
Divide all vertices into two parts: vertex set P with known shortest path and vertex set Q with unknown shortest path. Initially, there is only one vertex in the vertex set P with the shortest path known. We use a book[i] array here to record which points are in the set P. For example, for a vertex i, if book[i] is 1, it means that this vertex is in set P, and if book[i] is 0, it means that this vertex is in set Q.
-
Set the shortest path from the source point s to itself to 0, that is, dis=0. If there is a vertex i that can be reached directly from the source point, set dis[ i ] to e[s][ i ]. At the same time, set the shortest paths of all other vertices (that the source point cannot reach directly) to ∞.
-
Among all the vertices of the set Q, select a vertex u that is closest to the source point s (that is, the dis[u] is the smallest) and add it to the set P. And examine all the edges starting from point u, and relax each edge. For example, if there is an edge from u to v, then a path from s to v can be extended by adding the edge u->v to the end. The length of this path is dis[u]+e[u][v] . If this value is smaller than the currently known value of dis[v], we can replace the current value in dis[v] with the new value.
-
Repeat step 3, if the set Q is empty, the algorithm ends. The final value in the dis array is the shortest path from the source point to all vertices.
code show as below:
#include <iostream> #include <cstring> #define MAX 6 #define INF 0xFFFFFFFF /* * Dijkstra shortest path. * That is, the shortest path from the "vertex" to every other vertex in the statistical graph. * * Parameter Description: * vMatrix -- adjacency matrix * apex -- start vertex. That is, calculate the shortest path from "vertex apex" to other vertices. * prePoint -- array of predecessor vertices. The value of prePoint[i] is the vertex located before "vertex i" among all the vertices experienced by the shortest path from "vertex apex" to "vertex i". * finalPointVal -- an array of lengths. dist[i] is the length of the shortest path from "vertex apex" to "vertex i". */ void dijkstra(unsigned int vMatrix[][MAX], int apex, unsigned int prePoint[], unsigned int finalPointVal[]) { int i, k; unsigned int temp, min; int flag[MAX] = { 0 }; // flag[i]=1 indicates that the shortest path from "apex apex" to "vertex i" has been successfully obtained. for ( int i= 0 ; i<MAX; i++ ) { flag[i] = 0 ; // The shortest path of vertex i has not been obtained yet. prePoint[i] = 0 ; // The predecessor vertex of vertex i is 0. finalPointVal[i] = vMatrix[apex][i]; // The shortest path of vertex i is the weight from "vertex apex" to "vertex i". } // Initialize "apex apex" itself flag[apex] = 1 ; prePoint[apex] = 0; // Traverse, each time to find the shortest path to a vertex. for (i= 1 ; i<MAX; i++ ) { min = INF; for ( int j= 0 ; j<MAX; j++) // find the current smallest path, that is, the array finalPointVal, the fixed point of the smallest weight { if (flag[j]== 0 && finalPointVal[j] < min) { min = finalPointVal [j]; k = j; } } flag[k] = 1 ; // mark "vertex k" as having obtained the shortest path for ( int j= 0 ; j<MAX; j++) // correct the current shortest path and predecessor vertices, that is: after "the shortest path of vertex k", update "the shortest path and predecessor vertex of the vertex that does not obtain the shortest path" " { temp = (vMatrix[k][j]==INF ? INF:(min+vMatrix[k][j])); if (flag[j]==0 && temp<finalPointVal[j]) { finalPointVal [j] = temp; prePoint[j] = k; } } } for (i = 0; i < MAX; i++) std::cout << "shortest(1," << i+1 << ") = " << finalPointVal[i] << std::endl; } int main(int argc, char *argv[]) { unsigned int prePoint[MAX]; unsigned int desPoint[MAX]; unsigned int vMatrix[MAX][MAX] = {{0, 1, 12, INF, INF, INF}, {INF, 0 , 9 , 3 , INF, INF}, {INF, INF, 0 , INF, 5 , INF}, {INF, INF, 4 , 0 , 13 , 15 }, {INF, INF, INF, INF, 0 , 4 }, {INF, INF, INF, INF, INF, 0 }}; memset(prePoint, 0, sizeof(prePoint)); memset(desPoint, 0, sizeof(desPoint)); dijkstra(vMatrix, 0, prePoint, desPoint); return 0; }
Reference blog: https://blog.csdn.net/heroacool/article/details/51014824