Dijkstra algorithm: Finding the shortest path to all the points in the map

Source: Internet
Author: User

Dijkstra algorithm is introduced: the Dijkstra algorithm, which is the shortest path algorithm from one vertex to the other, solves the problem of the shortest path in the graph. The main feature of the Dijkstra algorithm is to extend the center of the starting point to the outer layer until it is extended to the end point, which is a breadth-first search method.
Dijkstra Algorithm principle: the optimal sub-path exists. Assuming a shortest path se exists from s→e, and the path passes through point A, you can determine that the SA subpath must be the shortest path to s→a. Proof: Disprove the law. If the sub-path SA is not the shortest, then there must be a shorter ' SA ', thus the SE path is not the shortest, inconsistent with the original hypothesis.
Dijkstra Algorithm Disadvantage: Unlike previously mentioned Viterbi, this algorithm can find the shortest path from the starting point to the rest of each node, so it is necessary to traverse all the paths and nodes, and the computational complexity is relatively large.
Dijkstra Algorithm Example: find the shortest path from node 0 to each node.

  

Step1: first set up two sets s={}: Represents the node where the shortest path has been found; u={}: Represents a node where the shortest path has not been found. Obviously, S and u complement each other, S U u= A set of all nodes, and when U is empty, the algorithm ends, all node shortest paths are found.
Step2: Create an array of dist[i], which holds the shortest path from the starting point 0 to the node I (may need to be updated, which is explained next). Then create a Boolean array s[i] (initial value of 0), to indicate whether the node has found the shortest path, if it has been found, no longer traverse.
Step3: Specific Executive section:
A:
initial point setting.
for node 0, it is first incorporated into the S set, and then the length of the path directly connected to the node 0 is searched and calculated, i.e. dist[1]=100,dist[30]=2,dist[4]=10 (then dist[0]=0). The distance to the node that cannot be reached directly is infinitely large ∞. The dist[3]=99999 is used here to facilitate the program comparison size. The s[0]=1 is then made to indicate that the node has been traversed.
B: Select Minimum Dist[i]. Compare the length of dist[1],dist[3],dist[4], select the shortest length of dist[4], and the node 4 into the S collection, so s[4]=1, indicating that 0 to 4 of the shortest path has been found, and the value is 10. reason: The principle of the existence of optimal sub-path. Because Dist[1],dist[3] is greater than dist[4], so if you choose to go through the junction 1, 3 to reach the node 4 path, in any case, it is impossible to find a less than direct from the node 0 to the node 4 path! This conclusion is very, very important, the key to understanding this algorithm! It is used again and again, and each cycle is compared and selected with the smallest dist[i].
C: Update dist[i]. now, we begin to scale out (breadth first) with node 4 as the center. Now, node 4 can reach node 3, also shows that from node 0 can be reached by node 4 node 3. As for the reasons to update dist[i], such as:
in the first selection, we included the starting point A, and then because of the dist[c]=6<dist[b]=20, we incorporated the C point. At this point, we find that we can also reach point B by C, so we have to compare dist[b] with dist[c]+| The size of the cb|. In this example below, obviously 6+7=13<20, so dist[b] needs to be updated from 20 to 13. So, it's not hard to find out: Each new node is included, we need to compare the length of the path generated by the node that is newly expanded from that node to the original path.

  

STEP4: Repeat steps b, C until the U set is emptied, and all values in S[i] are 1. This indicates that the shortest path is found in a bit of the diagram.

The above example of the steps table, if you can understand the basic understanding of the Dijkstra algorithm ideas.

  

Finally, the shortest path from node 0 to each point is calculated.
Dijkstra algorithm Focus: Understand why you need to select the smallest dist[i]; understand why you need to update dist[i].



June ye refuses to be a learning slag
Links: https://www.jianshu.com/p/c9b27617502e

Dijkstra algorithm: Finding the shortest path to all the points in the map

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.