The Dijkstra algorithm of ten algorithms:
The shortest path is a classical problem in graph theory algorithm. The graph is divided into directed graphs, non-directed graphs, the path weights have positive values, negative values, and different algorithms need to be selected for different situations. The basic principle of applying different algorithms to different scenarios is given on the wiki: the shortest path problem.
The Dijkstra algorithm is adopted for the non-directed graph and the positive weight path.
For example, is the shortest path to A to B, where the B-node is not qualified, modified to any node path, the problem is exactly the same.
You first need to record the distance from each point to the origin, and this distance is refreshed during each round of traversal. The shortest path to the origin of each node is the shortest path from its previous node (the precursor node) to the origin, plus the distance from the predecessor node to the node. With this principle, the shortest distance per node can be obtained by the N-round calculation.
In the first round, it can be calculated that the distances of 2, 3, 4, 5, 6 to Origin 1 are: [7, 9,-1,-1, 14], respectively. -1 means infinity. Take the smallest of the 7, that is, you can determine the shortest path of 1 is 0,2 for the next round of the precursor node. Also determine the shortest path for 2 nodes is 7, route: 1->2.
The second round, take 2 nodes as the precursor node, according to the shortest distance of the precursor node and the distance from the precursor node to calculate a new shortest distance, you can get 3,4,5,6 node to the origin of the distance is: [17, 22,-1,-1], this time needs to be the results of this round compared with the previous round, 3 nodes: 17 > 9, the shortest path is still 9, 4 nodes: < infinity, the shortest path to refresh 4 nodes is 22, 5 nodes: Unchanged, still infinite, 6 nodes: + < Infinity, take 14, unchanged. You can get the shortest distance for this round: [9, 22,-1, 14], take the shortest path to the smallest node, 3, as the next round of the precursor node. Also determine the shortest path for 3 nodes is 9, route: 1->3.
Third round, ibid, with 3 as the precursor node, the calculated distance of the 4,5,6 is: [20,-1, 11], in accordance with the principle of taking the shortest path, compared with the previous round, refreshed as: [20,–1, 11], selected 6 for the next round of the precursor node. At the same time, the shortest path to 6 is 11, route: 1->3->6.
Fourth round, ibid, with 6 as the precursor node, the calculated distance of 4 and 5 is [20, 20], compared with the previous round, after the refresh for [20, 20], the two are equal to only two nodes, and they want to wait, the rest of the calculation is no longer necessary. The shortest path for the two nodes is 20. End of the entire calculation. The shortest path for 4 is 20, route: 1->3->4. The shortest path for 5 is 20, route: 1->3->6->5.
If they are not equal, then a fifth round is required, the shortest path and route of the two are determined first, and then the remainder is taken. Until the entire 5 cycles are complete.
The code is as follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dijkstra algorithm (Dykstra algorithm)