Dijkstra Shortest Path algorithm

Source: Internet
Author: User

Last week we introduced the magic of only five elements of the Floyd shortest path algorithm, it can easily get any two points of minimum paths, which is called "Multi-Source shortest". This week to introduce the shortest path that specifies a point (source point) to the rest of the vertices, also known as the single source shortest path. For example, find the shortest path for vertices 1th to 2, 3, 4, 5, and 6th in the following figure.

As with the Floyd-warshall algorithm, the two-dimensional array e is still used to store the relationships between vertices, and the initial values are as follows.

We also need to use a one-dimensional array dis to store the initial distances of the 1th vertices to the rest of the vertices, as follows.

We refer to the value in the DIS array at this time as the shortest "estimated value".

Since it is the shortest distance from the 1th vertex to the rest of the vertices, find a vertex closest to the 1th vertex. It is known from the array dis that the top of the 1th vertex is the nearest 2nd vertex. When the number 2nd vertex is selected, the value of dis[2] has changed from "Estimated value" to "fixed value", i.e. the shortest distance from vertex 1th to 2nd is the current dis[2] value. Why, then? You want to ah, the current point of 1th is the nearest vertex 2nd Vertex, and all the edges of this figure is positive, then it is certainly impossible to pass through the third vertex, so that the 1th vertex to 2nd vertices of the distance further shortened. Because the distance from the 1th vertex to the other vertices is definitely not 1th to 2nd vertices short, right O (∩_∩) o~

Now that you've chosen the apex of number 2nd, let's look at the edges of the 2nd vertex. There are 2->3 and 2->4 these two sides. Let's talk about whether the edge of the 1th vertex to the 3rd vertex can be shortened by 2->3. This means now to compare the size of dis[3] and dis[2]+e[2][3]. Where Dis[3] represents the distance from vertex 1th to vertex 3rd. DIS[2]+E[2][3] Dis[2] represents the distance from vertex 1th to 2nd, e[2][3] means 2->3 this edge. So dis[2]+e[2][3] means that from point 1th to Vertex 2nd, and then through 2->3 this edge, to reach the 3rd vertex of the road.

We found dis[3]=12,dis[2]+e[2][3]=1+9=10,dis[3]>dis[2]+e[2][3], so dis[3] to be updated to 10. This process has a technical term called "slack". That is, the distance from vertex 1th to point 3rd is dis[3], through which the edge of the 2->3 is relaxed and successful. This is the main idea of the Dijkstra algorithm: "Edge" to relax the number 1th vertices to the rest of the distance.

Similarly, by 2->4 (E[2][4]), the value of dis[4] can be set from ∞ to 4 (dis[4) initially ∞,dis[2]+e[2][4]=1+3=4,dis[4]>dis[2]+e[2][4], and therefore dis[4 to be updated to 4).

Just now we have slack on all the edges of vertex 2nd. After relaxation, the dis array is:

Next, continue with the remaining 3, 4, 5, and 6th vertices to select the nearest vertex from the 1th vertex. By updating the DIS array above, the current vertex from point 1th is recently vertex 4th. At this point, the value of dis[4] has changed from "Estimated value" to "determined value". The following continues the relaxation of all edges (4->3,4->5 and 4->6) of vertex 4th using just the method. After relaxation, the dis array is:

Continue in the remaining 3, 5 and 6th vertices, select the nearest vertex from the 1th vertex, this time to choose the vertex 3rd. At this point, the value of dis[3] has changed from "Estimated value" to "determined value". All edges (3->5) of vertex 3rd are relaxed. After relaxation, the dis array is:

Continue in the remaining 5 and 6th vertices, select the nearest vertex from the 1th vertex, this time to choose the 5th vertex. At this point, the value of dis[5] has changed from "Estimated value" to "determined value". All edges (5->4) of vertex 5th are relaxed. After relaxation, the dis array is:

At last, the points of the vertex of 6th vertices are relaxed. Since the 6th vertex has no edges in this example, no processing is necessary. Up to this, all the values in the DIS array have been changed from "Estimated value" to "determined value".

The final dis array is as follows, which is the shortest path of the vertex 1th to the rest of the vertices.

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.