Introduction to Dijkstra's algorithm:
Dijkstra's algorithm is a typical shortest path algorithm, which is used to calculate the shortest path from one node to other nodes.
Its main feature is that it expands from the starting point to the outer layer (the idea of breadth-first search) until it reaches the end point.
Basic idea:
When calculating the shortest path in the graph G by Dijkstra, you need to specify the starting point s (that is, starting from the vertex s).
Furthermore, two sets S and U are introduced. The role of S is to record the vertices for which the shortest path has been found (and the corresponding shortest path length), while U is to record the vertices for which the shortest path has not been found (and the distance from the vertex to the starting point s).
Initially, there is only the starting point s in S; there are vertices other than s in U, and the path of the vertices in U is "the path from the starting point s to this vertex". Then, find the vertex with the shortest path from U and add it to S;
Next, update the vertices in U and the paths corresponding to the vertices. Then, find the vertex with the shortest path from U and add it to S; then, update the vertex in U and the path corresponding to the vertex. ... repeat the operation,
until all vertices are traversed.
Steps:
(1) Initially, S only contains the starting point s; U contains other vertices except s, and the distance of the vertices in U is "the distance from the starting point s to the vertex" [for example, the distance of the vertex v in U is (s, v), then s and v are not adjacent, then the distance of v is ∞].
(2) Select "vertex k with the shortest distance" from U, and add vertex k to S; at the same time, remove vertex k from U.
(3) Update the distance from each vertex in U to the starting point s. The reason why the distance of the vertices in U is updated is because k is determined in the previous step to be the vertex for finding the shortest path, so that k can be used to update the distance of other vertices; for example, the distance of (s, v) may be greater than (s, k)+(k,v) distance.
(4) Repeat steps (2) and (3) until all vertices are traversed.
It may be difficult to understand simply by looking at the above theory, and the algorithm will be described below through an example.
Diagram of Dijkstra's algorithm
The above figure G4 is taken as an example to demonstrate the algorithm of Dijkstra (taking the fourth vertex D as the starting point).
Initial state: S is the set of vertices for which the shortest path has been calculated, and U is the set of vertices for which the shortest path has not been calculated!
Step 1: Add vertex D to S.
At this time, S={D(0)}, U={A(∞), B(∞), C(3), E(4), F(∞), G(∞)}. Note: C(3) means that the distance from C to the starting point D is 3.
Step 2: Add vertex C to S.
After the previous operation, the distance from vertex C in U to the starting point D is the shortest; therefore, C is added to S, and the distance of vertex in U is updated at the same time. Taking vertex F as an example, the distance from F to D before is ∞; but after adding C to S, the distance from F to D is 9=(F,C)+(C,D).
At this time, S={D(0), C(3)}, U={A(∞), B(23), E(4), F(9), G(∞)}.
Step 3: Add vertex E to S.
After the previous operation, the distance between the vertex E in U and the starting point D is the shortest; therefore, E is added to S, and the distance of the vertex in U is updated at the same time. Taking vertex F as an example, the distance from F to D before is 9; but after adding E to S, the distance from F to D is 6=(F,E)+(E,D).
At this time, S={D(0), C(3), E(4)}, U={A(∞), B(23), F(6), G(12)}.
Step 4: Add vertex F to S.
At this time, S={D(0), C(3), E(4), F(6)}, U={A(22), B(13), G(12)}.
Step 5: Add vertex G to S.
At this time, S={D(0), C(3), E(4), F(6), G(12)}, U={A(22), B(13)}.
Step 6: Add vertex B to S.
At this time, S={D(0), C(3), E(4), F(6), G(12), B(13)}, U={A(22)}.
Step 7: Add vertex A to S.
At this time, S={D(0), C(3), E(4), F(6), G(12), B(13), A(22)}.
At this point, the shortest distance from the starting point D to each vertex is calculated: A(22) B(13) C(3) D(0) E(4) F(6) G(12).
code show as below:
1 #include " stdafx.h " 2 #include<iostream> 3 #include< string > 4 #define MAX_VERTEX_NUM 100 5 #define INFINITY 65535 6 typedef int Pathmatirx[MAX_VERTEX_NUM]; // The array that stores the shortest path index 7 typedef int ShortPathTable [MAX_VERTEX_NUM]; // The sum of the weights stored to the shortest path of each vertex 8 using namespace std; 9 typedef struct Graph // The adjacency matrix of the directed graph 10 { 11 char vexs[MAX_VERTEX_NUM]; // Array for storing vertices 12 int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // Define a critical matrix 13 int vexnum, arcnum; // Total number of vertices, total number of edges 14 }Graph; 15 16 int LocateVex(Graph G, char ch) // search for 17 { 18 for ( int i = 0 ; i < G.vexnum; i++ ) 19 if (G.vexs[i] == ch) 20 return i; 21 return - 1; 22 } 23 24 void CreateGraph(Graph &G) // Create an undirected graph 25 { 26 char c1, c2; // Arc tail, arc head 27 int i, j, weight; // weight is weight 28 cout << " Please enter the total number of vertices and total edges (space separated): " ; 29 cin >> G.vexnum >> G.arcnum; 30 cout << " Please input vertex information (space separated): " << endl; 31 for (i = 0 ; i < G.vexnum; i++ ) 32 { 33 cin >> G.vexs[i]; 34 } 35 for (i = 0 ; i < G.vexnum; i++ ) 36 for (j = 0 ; j < G.vexnum; j++ ) 37 G.arcs[i ][j] = INFINITY; 38 cout << " Please enter the arc tail, arc head and weight: " << endl; 39 for ( int k = 0 ; k < G.arcnum; k++ ) 40 { 41 cin >> c1 >> c2 >> weight; 42 i = LocateVex(G, c1); 43 j = LocateVex(G, c2); 44 G.arcs[i][j] = weight; 45 } 46 } 47 48 void ShortestPath_Dijkstra(Graph G, int v0, int prev[ ], int dist[]) // Dijkstra's algorithm 49 { // Find the shortest path prev[v] and weighted length dist[v], prev[v] from the v0 vertex of the directed graph G to the remaining vertex v The value of is the index of the predecessor vertex, and dist[v] represents the sum of the shortest path lengths from v0 to v. 50 int v , w, k, min; 51 int final[MAX_VERTEX_NUM]; // final[w]=1 means to find the shortest path from vertex v0 to v(w) 52 for (v = 0 ; v < G.vexnum; v++) // initialize data 53 { 54 final[v] = 0 ; // initialize all vertices to unknown shortest path state 55 dist[v] = G.arcs[v0 ][v]; // Add weight 56 to the vertex connected to v0 prev[v] = 0 ; // Initialize path array prev to 0 57 } 58 dist[v0] = 0 ; // v0 to The path of v0 is 0 59 final[v0] = 1 ; // v0 to v0 do not need to find the path 60 for (v = 1 ; v < G.vexnum; v++) // Start the main loop and find the shortest path from v0 to a v vertex each time 61 { 62 min = INFINITY; // The closest distance to the v0 vertex currently known 63 for (w = 0 ; w < G.vexnum; w++) // find v0 nearest vertex 64 { 65 if (!final[w] && dist[w] < min) 66 { 67 k = w; 68 min = dist[w]; // vertex w is closest to vertex v0 69 } 70 } 71 final[k] = 1; // Set the value of the closest vertex found so far to 1 72 for (w = 0 ; w < G.vexnum; w++) // correct the current shortest path and distance 73 { // If the path through the v vertex is smaller than the current one If the length of the path is short 74 if (!final[w] && (min + G.arcs[k][w] < dist[w])) 75 { // Indicate that a shorter path was found, modify dist[w ] and prev[w] 76 dist[w] = min + G.arcs[k][w]; // modify the path length 77 prev[w] = k; 78 } 79 } 80 } 81 cout << "Starting point: " ; // This is the output function 82 cout << G.vexs[v0]<< endl; 83 cout << " From the starting point " << G.vexs[v0] << " The shortest point to each point The distance is: " << endl; 84 for ( int i = 0 ; i < G.vexnum; i++ ) 85 cout << " The distance to " << G.vexs[i] << " is: " << dist [i] << endl; 86 } 87 88 int main() 89 { 90 Graph G; 91 int prev[MAX_VERTEX_NUM]; 92 int dist[MAX_VERTEX_NUM]; 93 int v0; 94 CreateGraph(G); 95 cout << "Please input v0:"; 96 cin >> v0; 97 ShortestPath_Dijkstra(G, v0, prev, dist); 98 }
Example show:
Reference: http://www.cnblogs.com/skywang12345/p/3711512.html