Shortest path algorithm--Dijkstra (Dijkstra)

Source: Internet
Author: User

Algorithmic thinking
    • Set g= (v,e) is a weighted graph
    • Divides the vertex set V in the graph into two groups
    • The first set is the set of vertices for which the shortest path has been found (s), where there is only one source point in the initial S, and each time a shortest path is obtained, it is added to the set S until all the vertices are added to s, and the algorithm ends.
    • The second set is the set of vertices for the remaining indeterminate shortest paths (denoted by u)
    • Add the vertices of the second group to s in the ascending order of the shortest path length
    • In the process of joining, the shortest path length that always remains from the source Point V to the vertices in S is not longer than the shortest path length from the source point V to any vertex in U
    • In addition, each vertex corresponds to a distance, and the distance from the vertex in S is the shortest path length from the source point V to the vertex
    • The distance from the vertex in U, which is the current shortest path length from the source point V to the vertex that only includes the vertices in s as the middle vertex
Dijkstra algorithm for C-language pseudo-code description
void Shortestpath_dij (mgraph G, int v0, Pathmatrix &p, shortpathtable &d) {//Use the Dijkstra algorithm to find the shortest path to the v0 vertex of the net G to the remaining vertex v    P[V] and its weighted length d[v].    If P[V][W] is true, then W is the vertex on the shortest path currently obtained from V0 to V.    FINAL[V] is true when and only if v∈s, that is, the shortest path from V0 to V has been obtained. for (v = 0; v < g.vexnum; + + V) {Final[v] = FALSE;        D[V] = G.arcs[v0][v]; for (w = 0; w < g.vexnum; + ww) p[v][w] = false;//Set Empty path if (D[v] < INFINITY) {p[v][v0] = TRUE;    P[V][V] = TRUE;} }//for d[v0] = 0;  FINAL[V0] = true;//initialization, v0 vertices belong to the S set//start main loop, each time the shortest path of v0 to a v vertex is obtained, plus V to s set for (i = 1; i < g.vexnum; + + i) {//Rest G.vexnum -1 vertices min = infinity;//The nearest distance from V0 vertex for (w = 0; w < G.vexnum; + + W) if (!final[w])//w vertex at V -S in if (D[w] < min) {v = w; min = d[w];} W Vertex closer to v0 vertex final[v] = true;//Nearest V to V0 Vertex joins S set for (w = 0; w < G.vexnum; + + W)//update current shortest path and distance if (!final[w] && (min + g.arcs[v][w] < d[w])) {//Modify D[W] and p[w],x∈v-s d[w] = min + G.arcs[V]                [W]; P[W] = P[v]; P[W][W] = true;//p[w] = P[v] + p[w]}//if}//for}//shortestpath_dij
The algorithm of C language Program description
void Dijkstra (float cost[][n], int v) {//Seek the shortest path and length of the source Point V to the remaining vertices, cost is a weighted adjacency matrix with a forward net//set max value of 32767, representing a large number v1 = V-1;  for (i = 0; i < n; i + +) {Dist[i] = cost[v1][i];//Initialization dist if (Dist[i] < max) Pre[i] = v; else Pre[i]    = 0;    } Pre[v1] = 0;  for (i = 0; i < n; i + +) s[i] = 0;//The first set begins with the empty s[v1] = 1;//Source Point v is incorporated into the first set for (i = 0; i < n; i + +) {//Expand First Group min        = max; for (j = 0; J < N; j + +) if (!        S[J] && (Dist[j] < min)) {min = dist[j]; k = j;} S[k] = 1;//adds k+1 to the first set for (j = 0; J < N; j + +) if (!  S[J] && (Dist[j] > Dist[k] + cost[k][j]) {//fixed the distance value of each vertex of the second set disk[j] = Disk[k] + cost[k][j]; Pre[j] = k + 1;//K + 1 is the pre-}//of J + 1 All vertices have been expanded to s for (j = 0; J < N; j + +) {//Print result printf ("%f\n%d"            , Dist[i], i + 1);            p = pre[i];                while (P! = 0) {//continue to find the front vertex printf ("<--%d", p);            p = pre[p-1];      }  }}//Dijkstra} 

Shortest Path algorithm--Dijkstra (Dijkstra)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.