Dijkstra Algorithm for Shortest Path

Source: Internet
Author: User

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.

1. algorithm idea g = (V, E) is a weighted directed network, which divides vertex set V in the graph into two groups: the vertex set S of the shortest path has been obtained. (In the initial time, only the source nodes in S are obtained. Each time a shortest path is obtained, the corresponding vertex is added to the set S, until all vertices are added to S.) The vertex set V-S of the shortest path is not determined. In addition, the length of the shortest path of each vertex from the source node v to S is not greater than the shortest path length of any vertex from the source node v to the V-S.

2. Algorithm Description

(1) S is the end point set of the shortest path that has been found from v, and its initial status is empty set. Then, starting from v to the remaining vertices (endpoints) in the graph) the initial value of the shortest path length possible for vi (vi, V-S) is:

D [I] = arcs [LocateVex (G, v)] [I], vi ε V

(2) Select vj, so that d [j] = Min {d [I] | vi belongs to the V-S}, vj is the end of a obtained Shortest Path starting from v. Set S = S limit {j };

(3) modify the shortest path length from v to any vertex of the Set V-S vk. If d [j] + arcs [j] [k] <d [k], change d [k]: d [k] = d [j] + arcs [j] [k];

(4) Repeat (2), knowing that all vertices are contained in S, the shortest path from v to the other vertices on the graph is an ascending sequence of path length.

Specific legends and Algorithm Execution steps: (the shortest path from A to each node)


The specific steps are as follows:


PS: The Source author's blog address is in the lower-right corner of the image.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPGgxPjOhosvjt6i + 38zlyrxp11_vade + CjxwcmUgY2xhc3M9 "brush: java;"> typedef int PathMatrix; // The vertex subscript array typedef EdgeType limit pathtable for storing the Shortest Path Sequences; // stores the weights and

/* Dijkstra algorithm requires the shortest path from vertex having a subscript of v 0 to other vertex having a subscript of v [v] and the weighted length D [v] * // * P [v] is the subscript of the forward vertex, D [v] indicates the shortest path length from v0 to v and */void ShortestPath_DIJ (Graph * g, int v0, PathMatrix p [], ShortPathTable d []) {int I, v, w, k; EdgeType min; bool final [MAX_VEX]; /* final [w] = 1: Obtain the shortest path from vertex V0 to Vw * // initialize data for (v = 0; v <g-> vexNum; v ++) {final [v] = false; // all vertices are initialized to unknown Shortest Path state d [v] = g-> arc [v0] [v]; // Add the vertex that is connected to v0 with the weight p [v] = v0; // The initialization path array P is v0} d [v0] = 0 ; // The path from v0 to v0 is 0 final [v0] = true; // the path from v0 to v0 does not need to be specified. // The main cycle starts, obtain the shortest path from v0 to a v vertex each time, and add v to the set Sfor (I = 0; I <g-> vexNum; I ++) {if (I = v0) continue; min = INFINITY; for (w = 0; w <g-> vexNum; w ++) // find the vertex closest to v0 in the V-S {if (! Final [w] & d [w] <min) {min = d [w]; // The vertex whose subscript is w is closer to v0 v = w ;}} final [v] = true; // The vertex whose subscript is k is merged into the set S, mark the vertex with the shortest path as true // update the current Shortest Path and distance for (w = 0; w <g-> vexNum; w ++) {// if the path passing through the v vertex is shorter than the current path, if (! Final [w] & (min + g-> arc [v] [w] <d [w]) {// The shorter path is found, modify D [w] and P [w] d [w] = min + g-> arc [v] [w]; p [w] = v ;}}}}

The following shows how to obtain the specific path Sequence Based on the path array PathMatrix:

// Find the path from the Source Vertex v to the end vertex u, and output void SearchPath (VertexType vex [], PathMatrix * prev, int v, int u) {int que [MAX_VEX]; int tot = 0; que [tot ++] = u; // end uint tmp = prev [u]; // The Last vertex subscript on the path to vertex subscript u while (tmp! = V) {que [tot ++] = tmp; tmp = prev [tmp]; // The Last vertex subscript on the path to the vertex subscript tmp} que [tot] = v; for (int I = tot; I> = 0; -- I) if (I! = 0) printf ("% c->", vex [que [I]); elseprintf ("% c", vex [que [I]);}

The preceding example shows the running result:


Dijkstra algorithm:

HDOJ 1874 smooth engineering continued. Current solution: www.wutianqi.com /? P = 1894
The shortest path of HDOJ 2544 is short. The current solution is www.wutianqi.com /? P = 1892

Reference: http://hi.baidu.com/zealot886/item/c8a499ee5795bcddeb34c950

Data structure (c)


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.