Commonly used shortest path algorithms include Dijkstra algorithm, A algorithm, Bellman-Ford algorithm, SPFA algorithm (improved version of Bellman-Ford algorithm), Floyd-Warshall algorithm, Johnson algorithm and bidirectional BFS algorithm. This paper will focus on the principle and implementation of Dijkstra algorithm.
Dijkstra algorithm, translated as dykstra algorithm or Dijkstra algorithm, was proposed by Dutch computer scientist Ezer dykstra in 1956 to solve the single-source shortest path problem of weighted directed graphs. The so-called single-source shortest path problem refers to determining the starting point and finding the shortest path from this node to any node in the graph. This algorithm can be used to find the shortest path between two cities or solve the famous traveling salesman problem.
Problem description: In an undirected graph, it is a set of graph nodes and a connecting edge between a set of nodes. Assuming that the weight of each edge is, find the shortest path from the vertex to other nodes (single source shortest path).
It is a weighted undirected graph, and the vertices in the graph are divided into two groups. The first group is the vertex set that finds the shortest path (represented by). At the beginning, there was only the source point. When the shortest path is found, new vertices are added until all vertices are added, and then the algorithm ends. The second group is the set of vertices with uncertain shortest path (denoted by), and with the increase of intermediate vertices, the intermediate vertices gradually decrease.
The following figure shows the workflow of Dijkstra algorithm (starting from the vertex):
note:
0 1) is the set of vertices for which the shortest path has been calculated;
02) is the vertex set for which the shortest path has not been calculated;
03) indicates that the shortest distance from vertex to vertex is 3.
Step 1: Select vertices and add them.
Step 2: Select vertices and add them to update the shortest distance between vertices.
Step 3: Select vertices and add them to update the shortest distance between vertices.
Step 4: Select vertices and add them to update the shortest distance between vertices.
Step 5: Select vertices and add them to update the shortest distance between vertices.
Step 6: Select vertices and add them to update the shortest distance between vertices.
Step 7: Select vertices and add them to update the shortest distance between vertices.
Example: node number 1-7 stands for a, b, c, d, e, f and g respectively.
(s path & lt the shortest. Paths (g, algorithm = "Dijkstra ")) Output result:
(s path & lt the shortest. Paths (g, 4, algorithm = "Dijkstra ")) output result:
Example:
Find the shortest path from D(4) to G(7):
[1] Wikipedia, the shortest path problem:/yalishadaa/article/details/55827681;
[3]RDocumentation:https://www . RDocumentation . org/packages/RNeo4j/versions/ 1 . 6 . 4/topics/Dijkstra;
[4]r document:https://www . r document . org/packages/I graph/versions/0. 1. 1/topics/shortest . paths;
[5] Pippi: https://pypi.org/project/Dijkstar/