Dijkstra is a typical shortest path routing algorithm used to calculate the shortest path from one node to all other nodes. The main feature is to expand horizontally at the center of the starting point until the end point is reached. Dijkstra algorithm can obtain the optimal solution of the shortest path, but it is inefficient because it traverses Many computing nodes.
Dijkstra algorithm is a representative short-circuit algorithm. It has been described in detail in many professional courses, such as data structure, graph theory, and operational research.
The basic idea is to set the vertex set S and constantly make greedy choices to expand the set. A vertex belongs to the set S and is known only when the shortest path length from the source to the vertex is known.
Initially, S contains only the source. If u is a vertex of G, the path from source to u and passing through only the vertex in S is called a special path from source to u, use the dist array to record the shortest path length corresponding to each vertex. Dijkstra algorithm extracts vertex u with the shortest special path length from the V-S each time, add u to S, and make necessary modifications to the array dist. Once S contains all vertices in V, dist records the shortest path length between the source and all other vertices.
For example, for Directed Graphs in the following table, the Dijkstra algorithm is used to calculate the shortest path between Source Vertex 1 and other vertices.
Dijkstra algorithm iteration process:
Have a good understanding of the topic!
The specific implementation is as follows (C/C ++ ):
/*************************************** * About: dijkstra Algorithm Implementation of digraph * Author: tanky woo * blog: www.wutianqi.com *************************************** /# include <iostream> using namespace STD; const int maxnum = 100; const int maxint = 999999; // each array starts from subscript 1 int Dist [maxnum]; // indicates the shortest path length of the current point to the source point int Prev [maxnum]; // records the previous node int C [maxnum] [maxnum] of the current point; // record the path length between two points in the graph: int N, line; // number of nodes and path number of the graph // 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 two nodes 'distancevoid Dijkstra (int n, int V, int * Dist, int * Prev, int C [maxnum] [maxnum]) {bool s [maxnum]; // determine whether the point is saved to the for (INT I = 1; I <= N; ++ I) {Dist [I] = C [v] [I]; s [I] = 0; // If (Dist [I] = maxint) prev [I] = 0; elseprev [I] = V;} Dist [v] = 0; s [v] = 1; // sequentially place the node not included in the s set, take the node with the minimum Dist [] value, and put it in the combined S. // once S contains all vertices in the V, dist records the shortest path length from the source point to all other vertices. // note that the first node is the source point for (INT I = 2; I <= N; ++ I) {int TMP = maxint; int u = V; // find the Dist [J] minimum value for (Int J = 1; j <= N; ++ J) if ((! S [J]) & Dist [J] <TMP) {u = J; // U stores the number TMP = DIST [J];} s [u] = 1; // indicates that the u point has been saved to the S collection. // update distfor (Int J = 1; j <= N; ++ J) if ((! S [J]) & C [u] [J] <maxint) {int newdist = DIST [u] + C [u] [J]; if (newdist <Dist [J]) {Dist [J] = newdist; Prev [J] = u ;}}}// you can find the path from the Source Vertex V to the end vertex u, and output void searchpath (int * Prev, int V, int U) {int que [maxnum]; int tot = 1; que [tot] = u; Tot ++; int TMP = Prev [u]; while (TMP! = V) {que [tot] = TMP; Tot ++; TMP = Prev [TMP];} que [tot] = V; For (INT I = tot; i> = 1; -- I) if (I! = 1) cout <que [I] <"->"; elsecout <que [I] <Endl;} int main () {freopen ("input.txt ", "r", stdin); // each array starts from subscript 1 // input knots CIN> N; // number of input paths CIN> line; int P, q, Len; // enter P, Q, and its path length // initialize C [] [] as maxintfor (INT I = 1; I <= N; ++ I) for (Int J = 1; j <= N; ++ J) C [I] [J] = maxint; For (INT I = 1; I <= line; ++ I) {CIN> P> q> Len; If (LEN <C [p] [Q]) // has a duplicate edge {C [p] [Q] = Len; // P points to QC [Q] [p] = Len; // Q points to P, this indicates an undirected graph} For (INT I = 1; I <= N; ++ I) Dist [I] = maxint; For (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= N; ++ J) printf ("% 8d", C [I] [J]); printf ("\ n");} Dijkstra (n, 1, DIST, Prev, C ); // Shortest Path Length: cout <"shortest path length from the source point to the last vertex:" <Dist [N] <Endl; // path cout <"the path from the source point to the last vertex is:"; searchpath (prev, 1, n );}
Input data:
5
7
1 2 10
1 4 30
1 5 100
2 3 50
3 5 10
4 3 20
4 5 60
Output Data:
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
Minimum path length from the source to the last vertex: 60
Reproduced from: http://www.wutianqi.com /? P = 1890