Dijkstra algorithm,
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 array dist to record the minimum special path length of 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 the S package 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 for Directed Graphs
* Author: Tanky Woo
* Blog: www.WuTianQi.com
***************************************/
# Include <iostream>
Using namespace std;
Const int maxnum= 100;
Const int maxint= 999999;
Void Dijkstra (int n, int v, int * dist, int * prev, int c [maxnum] [maxnum])
{
Bool s [maxnum]; // determines whether it has been saved to the point to the S Collection
For (int I = 1; I <= n; ++ I)
{
Dist [I] = c [v] [I];
S [I] = 0; // never used this point
If (dist [I] = maxint)
Prev [I] = 0;
Else
Prev [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 V, dist records the shortest path length from the source point to all other vertices
For (int I = 2; I <= n; ++ I)
{
Int tmp = maxint;
Int u = v;
// Find the dist [j] minimum value of the currently unused vertex j.
For (int j = 1; j <= n; ++ j)
If ((! S [j]) & dist [j] <tmp)
{
U = j; // u stores the number of the nearest vertex in the current adjacent contact
Tmp = dist [j];
}
S [u] = 1; // indicates that the u point has been saved to the S collection.
// Update dist
For (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;
}
}
}
}
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] <"->"; else cout <que [I] <endl;} int main () {freopen ("input.txt", "r", stdin); // 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 // input number of nodes: cin> n; // number of input paths cin> line; int p, q, len; // input p, q two points and their path length // initialize c [] [] as maxint for (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 q c [q] [p] = len; // q points to p, which 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); // length of the shortest path cout <"length of the shortest path 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
The path from the source point to the last vertex is: 1-> 4-> 3-> 5
Source http://www.cnblogs.com/tanky_woo/archive/2011/01/19/1939041.html