Data Structure-weighted directed graph (Dijkstra algorithm)

Source: Internet
Author: User
Dijkstra algorithm-find the shortest path in a Directed Graph

Dijkstra AlgorithmIt was discovered by Dutch computer scientist izger Dickus. The algorithm solves the shortest path problem in a directed graph.

For example, if the vertices in the figure represent cities, and the weights on the sides represent the distance between cities. Dijkstra algorithm can be used to find the shortest path between two cities.

The input of the Dijkstra algorithm contains a weighted directed graph.GAnd a Source Vertex in G.S. We useVIndicatesGSet of all vertices in. Each edge in the graph is an ordered element pair formed by two vertices. (U,V) Indicates the slave vertex.UToVA path is connected. HypothesisE isThe set of all edges, and the edge weight is based on the weight function.W:E→ [0, ∞. Therefore,W(U,VFrom VertexUTo VertexV(Cost ). The edge cost can be imagined as the distance between two vertices. The cost value of the path between any two points is the total cost value of all edges in the path. Known to haveVVertex existsSAndT, Dijkstra algorithm can be foundSToT(I. e. Shortest Path ). This algorithm can also be found from a vertex in a graph.SThe shortest path to any other vertex.

Algorithm Description

This algorithm usesVKeep the shortest path from S to V found so far. Initially, the path length value of source point S is assigned to 0 (D [s]= 0), and set the path length of all other vertices to infinity, that is, we do not know any path to these vertices (VAll vertices inVDivisionSExternalD [v]= ∞ ). When the algorithm ends,D [v]Is stored fromSToVOr infinity (if the path does not exist ).

The basic operation of the dijstra algorithm is edge expansion: if there is a slaveUToVFromSThe shortest path to V can be (U,V) Added to the end of S to u to expand. The length of this path is d [u] + W (u, v ). If this value is greater than the currently knownD [v]The value must be small. We can use a new value to replace the currentD [v]. The operation of the extended edge is always executed until all d [v] represents the cost of the shortest path from S to v. This algorithm is properly organizedD [u]When it reaches its final value, each edge (U,V) Are expanded only once.

The algorithm maintains two vertex sets S and Q. Set S retains all the known d [v] values that are already the vertex of the shortest path value, while set Q retains all other vertices. The initial state of set S is null, and then each step has a vertex moving from Q to S. The selected vertex is the vertex with the smallest d [u] value in Q. When a vertex u is transferred from Q to S, the algorithm expands each external edge (u, v.

Algorithm IDEA

Set S to the vertex set that has been determined for the shortest distance (as the red vertex set), and The V-S is the vertex set that has not been determined for the shortest distance (as the blue vertex set ).
① Initialization
During initialization, only the shortest distance of the Source Vertex S is known (SD (S) = 0), so the red vertex set S = {s}, and the blue vertex set is empty.
② Repeat the following steps to generate the shortest path of each vertex in ascending order of path length
In the current Blue Point Set, select a blue point with the smallest shortest distance to expand the red point set to ensure that the shortest path of each vertex is generated in the order of increasing path weights.
When there are only blue points with the shortest distance ∞ left in the blue point set, or all the blue points have been expanded to the red vertex set, the shortest path from S to all vertices is obtained.
Note:
① If the path from the source point to the blue point does not exist, assume that the shortest path of the blue point is a virtual path with an infinite length.
② The shortest path from the source point S to the end point V is short for V; the shortest path from S to V is short for V and is recorded as SD (V ).
(3) Select a blue vertex K with the smallest shortest distance in the blue vertex set to expand the red vertex set.
According to the idea of generating the Shortest Path in ascending order of length, the shortest path of the blue vertex K with the smallest shortest distance is:
Source, red dot 1, red dot 2 ,..., Red dot n, blue dot K
Distance: the shortest distance from the source point to the red point N + <red point N, blue point K> edge Length
For ease of solving, set a vector d [0 .. n-1], for each blue point V in V-S, use d [v] to record the arrival of V from the source point S and do not pass through any blue point except V (if there is a middle point, it must be red) the "shortest" path length (Estimated distance for short) ).
If K is the vertex with the smallest distance in the blue point set, the estimated distance of K is the shortest distance, that is, if d [k] = min {d [I] iε V-S }, then d [k] = SD (k ).
Initially, the value of d [c] for each blue point V should be w <s, V>, and there is no intermediate point in the path from S to v, because the path contains only one edge <s, V>.
Note:
The key to the Dijkstra algorithm is to select a blue vertex K with the smallest shortest distance in the blue vertex set to expand the red vertex set.

(4) modify the estimated distance of the blue vertex set after K expands the red vertex set S
After K is expanded to the red point, the estimated distance of the remaining blue point set may be reduced due to the addition of the new red point K. At this time, the estimated distance of the corresponding blue point must be adjusted.
For any blue vertex J, if K is changed from blue to red and D [J] becomes smaller, it must be because there is a shorter path from S to J and contains the new red dot K: P = <s ,..., K, j>. And the new path P reduced by d [J] may only be due to path <s ,..., K> and edge <K, j>.
Therefore, when length (p) = d [k] + W <K, j> is smaller than D [J], the length of P should be used to modify the value of d [J.

(5) Dijkstra Algorithm

Dijkstra (G, D, S ){
// Use the Dijkstra algorithm to obtain the shortest path length from the source point S of the Directed Network G to each vertex
// The following is the initialization operation
S = {s}; d [s] = 0; // you can specify the initial red vertex set and the shortest distance.
For (all I ε V-S) Do // For each vertex in a blue vertex set I
D [I] = G [s] [I]; // set the initial estimated distance of I to W <s, I>
// The following is an extended red dot set
For (I = 0; I <n-1; I ++) do {// expand a maximum of N-1 blue points to the red point set.
D [k] = min {d [I]: All I V-S}; // select the minimum distance vertex K in the current Blue Point Set
If (d [k] equals ∞)
Return; // when the estimated distance of all the blue points in the blue point set is ∞,
// Indicates that the shortest path of These vertices does not exist.
S = s ∪ {k}; // red dot K to expand to the red dot set
For (all J, V-S) Do // adjust the estimated distance of the remaining Blue Points
If (d [J]> d [k] + G [k] [J])
// Change the value of the original d [J] to an hour with the new red dot K, and change the value of d [J] with the length of the new path.
// Bring J closer to S.
D [J] = d [k] + G [k] [J];
}
}

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.