The Dijkstra (Dijkstra) algorithm is a typical shortest path algorithm used to calculate the shortest path of a node to another node.
Its main feature is to extend from the center of the starting point to the outer layer (breadth-first search idea) until it expands to the end point.
Basic ideas
When you calculate the shortest path in Figure G by Dijkstra, you need to specify the starting point s (which is calculated from the vertex s).
In addition, two sets of S and U are introduced. The function of S is to record the vertices (and the corresponding shortest path lengths) that have been found for the shortest path, while U is the vertex (and the distance from the vertex to the start) of the shortest path that the record has not yet obtained.
Initially, only the starting point in S s;u is a vertex other than S, and the path of the vertex in U is the "Origin s to the path of the vertex". Then, find the shortest path vertex from u and add it to S, then update the path of the vertex and vertex in U. Then, find the shortest path vertex from u and add it to S, then update the path of the vertex and vertex in U. ... Repeat the operation until all vertices are traversed.
Operation Steps
(1) Initially, S contains only the starting point S;u contains the other vertices except s, and the distance of the vertices in U is "the distance from the beginning s to that vertex" [For example, the distance of Vertex v in U is (s,v), and then S and V are not adjacent, then V is the distance of ∞].
(2) Select the shortest vertex k from U and add the vertex k to S, and remove the vertex k from U.
(3) update the distance from each vertex in U to the beginning S. The distance from the vertex in U is updated because the last step determines that K is the vertex of the shortest path, so that the distance from the other vertices can be updated by using K, for example, (S,V) may be greater than (S,K) + (k,v) distance.
(4) Repeat steps (2) and (3) until all vertices are traversed.
Simple to see the above theory may be more difficult to understand, the following examples to illustrate the algorithm.
Dijkstra algorithm Diagram
Take G4 as an example to perform an algorithm demonstration of Dijkstra (starting with the 4th Vertex D).
Initial state : S is the collection of vertices that have been calculated for the shortest path, and U is a collection of vertices that are not counted except for the shortest path!
1th Step : Add vertex D to S.
At this time, 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.
2nd Step : Add vertex c to S.
After the previous operation, the distance from vertex c to start D in U is the shortest, so C is added to S, and the distance from the vertex in U is updated. In the case of Vertex F, the distance from the previous F to D is ∞, but after C is added 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 (+), E (4), F (9), G (∞)}.
3rd Step : Add vertex e to S.
After the previous operation, the distance from vertex e to start D in U is the shortest, so add e to S and update the distance of the vertices in U. In the case of Vertex F, the distance before F to D is 9, but after E is added 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 (+), F (6), G (12)}.
4th Step : Add vertex f to S.
At this time, s={d (0), C (3), E (4), F (6)}, U={a (a), B (d), G (12)}.
5th Step : Add vertex g to S.
At this time, s={d (0), C (3), E (4), F (6), G (d)}, U={a (a), B (13)}.
6th Step : Add vertex b to S.
At this time, s={d (0), C (3), E (4), F (6), G (d), B (+)}, U={a (22)}.
7th Step : Add Vertex A to S.
At this time, s={d (0), C (3), E (4), F (6), G (a), B (d), A (22)}.
At this point, the shortest distance from the beginning D to each vertex is calculated:a (A) B (+) C (3) D (0) E (4) F (6) G (a).
code description for the Dijkstra algorithm
Taking "adjacency Matrix" as an example to illustrate the Dijkstra algorithm, the "adjacency table" Implementation of the graph in the following will give the corresponding source code.
1. Basic definition
//adjacency Matrixtypedefstruct_graph{CharVexs[max];//Vertex Collection intVexnum;//number of vertices intEdgnum;//Number of sides intMatrix[max][max];//adjacency Matrix}graph, *pgraph;//structure of the Edgetypedefstruct_edgedata{CharStart//the beginning of the side CharEnd//the end of the edge intWeight//the weight of the edge}edata;
Graph is the corresponding structure of the adjacency matrix.
Vexs is used to save vertices, Vexnum is the number of vertices, edgnum is the number of edges, and the matrix is a two-dimensional array to hold the matrix information. For example, matrix[i][j]=1 means "vertex I (i.e. vexs[i])" and "Vertex J (i.e. Vexs[j])" are adjacency points; matrix[i][j]=0, they are not adjacency points.
Edata are the structures that correspond to the edges of adjacent matrices.
2. Dijkstra algorithm
#include <stdio.h>#include<stdlib.h>#include<malloc.h>#include<string.h>#defineMAX 100#defineINF (~ (0x1<<31))typedefstructgraph{CharVexs[max]; intVexnum; intEdgnum; intMatrix[max][max];} Graph,*Pgraph;typedefstructedgedata{Charstart; Charend; intweight;} EData;Static intGet_position (Graph G,Charch) { inti; for(i=0; i<g.vexnum; i++) if(g.vexs[i]==ch)returni; return-1;} Graph*create_graph () {Charvexs[]= {'A','B','C','D','E','F','G'}; intmatrix[][7]= { {0, A, Inf,inf,inf, -, -}, { A,0,Ten, Inf,inf,7, INF}, {INF,Ten,0,3,5,6, INF}, {inf,inf,3,0,4, Inf,inf}, {inf,inf,5,4,0Inf8}, { -,7,6Inf2,0,9}, { -, Inf,inf,inf,8,9,0} }; intvlen=sizeof(VEXS)/sizeof(vexs[0]); inti,j; Graph*PG; if((pg= (graph*)malloc(sizeof(Graph))) ==NULL)returnNULL; memset (PG,0,sizeof(PG)); PG->vexnum=Vlen; for(i=0; i<pg->vexnum; i++) PG->vexs[i]=Vexs[i]; for(i=0; i<pg->vexnum; i++) for(j=0; j<pg->vexnum; J + +) PG->matrix[i][j]=Matrix[i][j]; for(i=0; i<pg->vexnum; i++) { for(j=0; j<pg->vexnum; J + +) { if(i!=j&&pg->matrix[i][j]!=INF) PG->edgnum++; }} PG->edgnum/=2; returnPG;}voidprint_graph (graph G) {inti,j; printf ("Matrix Graph: \ n"); for(i=0; i<g.vexnum; i++) { for(j=0; j<g.vexnum; J + +) printf ("%10d", G.matrix[i][j]); printf ("\ n"); }}edata*get_edges (Graph G) {EData*edges; Edges= (edata*)malloc(g.edgnum*sizeof(EData)); inti,j; intindex=0; for(i=0; i<g.vexnum; i++) { for(j=i+1; j<g.vexnum; J + +) { if(g.matrix[i][j]!=INF) {Edges[index].start=G.vexs[i]; Edges[index].end=G.vexs[j]; Edges[index].weight=G.matrix[i][j]; Index++; } } } returnedges;}voidDijkstra (Graph G,intVsintPrev[],intdist[]) { inti,j,k; intmin; inttmp; intFlag[max]; for(i=0; i<g.vexnum;i++) {Flag[i]=0; Prev[i]=vs; Dist[i]=G.matrix[vs][i]; } Flag[vs]=1; DIST[VS]=0; for(i=1; i<g.vexnum;i++) {min=INF; for(j=0; j<g.vexnum;j++) { if(flag[j]==0&&dist[j]<min) {min=Dist[j]; K=J; }} Flag[k]=1; for(j=0; j<g.vexnum;j++) {tmp= ((G.matrix[k][j]==inf)? INF: (min+g.matrix[k][j])); if(flag[j]==0&&tmp<Dist[j]) {Dist[j]=tmp; PREV[J]=K; }}} printf ("Dijktra (%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]);}intMain () {Graph*PG; PG=create_graph (); Print_graph (*PG); intPrev[max]; intDist[max]; Dijkstra (*PG,3, prev,dist); inti; for(i=0; i<pg->vexnum;i++) printf ("%c%c \ n",pg->vexs[prev[i]],pg->vexs[i]);}
Operation Result:
Dijkstra Algorithm Introduction