Five Shortest PathsAlgorithmComparison
July and July February 12, 2011.
For more information, see Wikipedia.
-----------------------------------
Comparison of several Shortest Path Algorithms:
I, Floyd: Find the shortest path of multiple sources without negative weight edges. Use a matrix to record graphs. Poor timeliness, time complexity O (V ^ 3 ).
Floyd-warshall algorithm (Floyd-warshall algorithm) is an algorithm that solves the shortest path between any two points,
The shortest path of a directed graph or a negative weight can be correctly handled.
The time complexity of the Floyd-warshall algorithm is O (n ^ 3) and the space complexity is O (n ^ 2 ).
The principle of Floyd-warshall is dynamic planning:
Set Di, J, and K to the shortest path length of the intermediate node from I to J.
If the shortest path goes through K, Di, j, k = Di, K, K-1 + DK, J, K-1;
If the shortest path does not pass through K, Di, j, k = Di, J, K-1.
Therefore, Di, j, k = min (Di, K, K-1 + DK, J, K-1, Di, J, k-1 ).
In actual algorithms, iteration can be performed directly in the original space to save space, so that the space can be reduced to two-dimensional.
The Floyd-warshall algorithm is described as follows:
For k between 1 to n do
For I do 1 to n do
For J between 1 to n do
If (Di, K + DK, j <Di, j) then
Di, J ← Di, K + DK, J;
Where di and J represent the cost from point I to Point J. When Di and J are ∞, there is no connection between two points.
Ii. Dijkstra: The shortest path of a single source with no negative weight. The timeliness is good, and the time complexity is O (V * V + E ).
If the source is reachable, O (V * LGV + E * LGV) => O (E * LGV ).
When it is a sparse graph, E = V * V/LGV at this time, so the time complexity of the algorithm can beO (V ^ 2).
If the Fibonacci heap is used as the priority queue, the algorithm time complexity is O (V * LGV + E ).
For more information, see: classical algorithm research series: II. Dijkstra Algorithm
Http://blog.csdn.net/v_JULY_v/archive/2010/12/24/6096981.aspx
Iii. Bellman-Ford: Find the shortest path of a single source, and check whether there is a negative weight loop (If yes, there is no Shortest Path ),
Good timeliness, time complexity O (VE ).This algorithm will be elaborated in this blog in the future.
The bellman-Ford algorithm is an algorithm used to solve the single-source shortest path problem.
The shortest path of a single source point refers:
Given a weighted directed graph G and the Source Vertex S, find the shortest path from S to V for any vertex v in the graph G.
Unlike the Dijkstra algorithm, In the Bellman-Ford algorithm, the edge weight can be negative.
Suppose we can find a loop (starting from V and returning to V after several points) and the sum of the weights of all edges in the loop is negative.
Through this loop, the shortest path of any two points in the loop can be infinitely small. If you do not process this negative loop,ProgramIt will run forever. The bellman-Ford algorithm is capable of distinguishing this negative loop.
Iv. spfa: It is the queue Optimization of Bellman-Ford, with relatively good timeliness and time complexity O (KE ). (K <v ).
Similar to the Bellman-Ford algorithm, the spfa algorithm uses a series of relaxation operations to get the shortest path from a node to all other nodes in the graph. The difference is that the spfa algorithm usesMaintain a queueSo that it is not necessary to update other nodes immediately after the current shortest path of a node is updated, which greatly reduces the number of repeated operations.
The spfa algorithm can be used for graphs with negative edge weights, which is different from the Dijkstra algorithm.
And Dijkstra algorithm and Bellman-Ford AlgorithmBothDifferent,
The time efficiency of the spfa algorithm is unstable, that is, the time required for different graphs varies greatly.
In the best case, if each node only enters the queue once, the algorithm actually changes to breadth-first traversal, and the time complexity is only O (e ). On the other hand, there is such an example that every node is queued (V-1) times, at this time the algorithm degrades to the Bellman-Ford algorithm, its time complexity is O (VE ).
The spfa algorithm can completely replace the Bellman-Ford algorithm in the negative edge weight graph. It also performs well in the sparse graph. However, in the non-negative edge weight graph, to avoid the worst case, the more efficient Dijkstra Algorithm and Its heap optimized version are usually used. The performance of common spfa algorithms in a class of grid graphs is not satisfactory.
V. Wide search: The single source does not have the maximum short circuit. Matrix record method time complexity O (V ^ 2); edge table record method time complexity O (KE ).
I am July and have allArticleContent and materials are copyrighted.
You must indicate the author and the source, and notify me of the reprinting. July and July February 12, 2011.