Comparison of several Shortest Path Algorithms Floyd, Dijkstra, Bellman-Ford, and spfa

Source: Internet
Author: User

Several Shortest PathsAlgorithmComparison

 

July and July February 12, 2011.
-----------------------------------

 

Comparison of several Shortest Path Algorithms:
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 (Floyd-warshall algorithm) is an algorithm used to solve the shortest path between any two points. It can correctly handle the shortest path problem of a directed graph or a negative weight.

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.

 

Dijkstra

The shortest path of a single source with no negative weight is used. 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:Ii. Continue to thoroughly understand the Dijkstra Algorithm, AndII (continued), Dijkstra algorithm + gradual C Implementation of the Fibonacci heap.

 

Bellman-Ford

To find the shortest path of a single source, you can determine 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.

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 AlgorithmBothThe 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.

.

 

Copyright. Repost anyArticleIn the form of a hyperlink.

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.