Dijkstra (Dijkstra) algorithm is a typical shortest path algorithm, which is used to compute the shortest path of a node to another node.
Its main feature is to extend the starting point to the outer layer (breadth-first search idea) until it is extended to the end. Basic Ideas
When you calculate the shortest path in Figure G by Dijkstra, you need to specify the starting point S (that is, starting from the vertex s).
In addition, the introduction of two sets S and U. The function of S is to record the vertex of the shortest path (and the corresponding shortest path length), and U is to record the vertex of the shortest path (and the distance from the vertex to the start s).
At the beginning, only the starting point in S s;u is the vertex except S, and the path of the vertex in U is "the path of the beginning S to the vertex". Then, find the shortest vertex of the path from U and add it to S, then update the path of the vertices and vertices in U. Then, find the shortest vertex of the path from U and add it to S, then update the path of the vertices and vertices in U. ... Repeat until all vertices are traversed. Operation Steps
At the beginning, S contains only the starting point s;u other vertices except S. and the distance of the vertices in U is "distance from the beginning s to the vertex" [For example, the distance of Vertex v in U is (s,v) length, then S and V are not adjacent, the distance of V is ∞].
Select "Shortest vertex k" from U and add vertex k to S, and remove vertex k from U.
Updates the distance from each vertex in U to the start S. The reason for updating the distance of the vertices in U is that K is the vertex of the shortest path, so that K can be used to update the distance of other vertices, for example, (s,v) distance may be greater than (S,K) + (K,V).
Repeat steps (2) and (3) until all vertices are traversed.
Simply looking at the above theory may be more difficult to understand, the following examples to illustrate the algorithm. graphic
The above figure G4 as an example to perform an algorithmic demo of Dijkstra (starting with the 4th Vertex D). 23 of the following B nodes should be 13.
Initial state: S is the set of vertices for which the shortest path has been computed, and U is the set of vertices that are not computed except for the shortest path.
Step 1th: Add vertex D to S.
At this point, s={d (0)}, u={a (∞), B (∞), C (3), E (4), F (∞), G (∞)}. Note: C (3) indicates that the distance from C to start D is 3.
Step 2nd: Add vertex c to S.
After the previous operation, the distance between vertex C to start D is the shortest in U, so adding C to S, and updating the distance of vertices in U. Take vertex f as an example, the distance from F to D is ∞, but after adding C to S, the distance F to D is 9= (f,c) + (c,d).
At this point, s={d (0), C (3)}, U={a (∞), B (d), E (4), F (9), G (∞)}.
Step 3rd: Add vertex e to S.
After the previous operation, you have the shortest distance from vertex e to start D, so add E to S and update the distance of the vertices in U. In the case of Vertex F, the distance from F to D is 9, but after adding E to S, the distance from F to D is 6= (f,e) + (e,d).
At this point, s={d (0), C (3), E (4)}, U={a (∞), B (d), F (6), G (12)}.
Step 4th: Add vertex f to S.
At this point, s={d (0), C (3), E (4), F (6)}, U={a (d), B (d), G (12)}.
Step 5th: Add vertex g to S.
At this point, s={d (0), C (3), E (4), F (6), G (A)}, U={a (d), B (13)}.
Step 6th: Add Vertex b to S.
At this point, s={d (0), C (3), E (4), F (6), G (a), B (a), U={a (22)}.
Step 7th: Add Vertex A to S.
At this point, s={d (0), C (3), E (4), F (6), G (a), B (A), A (22)}.
At this point, the shortest distance from the beginning D to each vertex is calculated: A (B) C (3) D (0) E (4) F (6) G (12). Code
The adjacency matrix, for example,
The adjacency matrix
typedef struct _GRAPH
{
char vexs[max]; Vertex set
int vexnum; Vertex number
int edgnum; Number of edges
int Matrix[max][max];//adjacency Matrix
}graph, *pgraph;
Edge of the structure of the
typedef struct _EDGEDATA
{
char start;//edge of the beginning
char end; Edge of the end of the
int weight//edge of the weight
}edata;
Graph is the corresponding structure of adjacency matrix.
Vexs is used to save vertices, Vexnum is the number of vertices, edgnum is the number of edges, and matrix is a two-dimensional array to hold the information of matrices.
For example, matrix[i][j]=1, which means "vertex I (i.e. vexs[i])" and "Vertex J (i.e. Vexs[j])" are adjacency points, and matrix[i][j]=0 means that they are not adjacency points.
The edata is the corresponding structure of the adjacent matrix edges. Dijkstra Algorithm
* * Dijkstra Shortest path.
* That is, the shortest path of "Vertex vs" to the other vertices in the statistical graph (G). * * Parameter Description: * G--figure * vs--start vertex (start vertex).
The shortest path to the vertex vs to the other vertices is computed. * Prev--array of precursor vertices.
That is, the value of Prev[i] is the culmination of all the vertices experienced by the shortest path of "Vertex vs" to "vertex i", at the vertex before "vertex i". * Dist--length array.
That is, Dist[i] is the length of the shortest path of "Vertex vs" to "vertex i".
*/void Dijkstra (Graph G, int vs, int prev[], int dist[]) {int i,j,k;
int min;
int tmp; int Flag[max];
Flag[i]=1 indicates that the shortest path to "Vertex vs" to "vertex i" has been successfully acquired. Initialize for (i = 0; i < G.vexnum i++) {flag[i] = 0;
The shortest path to vertex i is not yet available. Prev[i] = 0;
Vertex i has a precursor vertex of 0.
Dist[i] = The shortest path of g.matrix[vs][i];//vertex i is the right of "Vertex vs" to "vertex i".
//Initialize Flag[vs to vertex vs itself] = 1;
Dist[vs] = 0;
Traverse g.vexnum-1 time, each time to find the shortest path of a vertex.
for (i = 1; i < G.vexnum; i++) {//Find the current minimum path;//That is, the nearest vertex (k) is found in the vertex of the shortest path not obtained.
min = INF;
for (j = 0; J < G.vexnum; J +) {if (flag[j]==0 && dist[j]<min) { min = Dist[j];
K = J;
}//Mark "Vertex K" for the shortest path has been obtained flag[k] = 1;
Fix the current shortest path and the predecessor vertex//That is, after the shortest path of vertex K, update the shortest path and the predecessor vertex of the vertex with no shortest path. for (j = 0; J < G.vexnum; J + +) {tmp = (g.matrix[k][j]==inf? INF: (min + g.matrix[k][j]);
Prevent overflow if (flag[j] = = 0 && (tmp < DIST[J])) {dist[j] = tmp;
PREV[J] = k;
The results of the Dijkstra shortest path are printed ("Dijkstra (%c): \ n", G.vexs[vs]);
for (i = 0; i < g.vexnum i++) printf ("Shortest (%c,%c) =%d\n", G.vexs[vs], g.vexs[i], dist[i]); }
reference materials
1, http://www.cnblogs.com/skywang12345/p/3711512.html