Comparison of several shortest path algorithms Floyd, Dijkstra, Bellman-ford, SPFA

Source: Internet
Author: User

Comparison of several large shortest path algorithms

Comparison of several shortest path algorithms:
Floyd

Multi-source, no negative right side (Error here?) Should be able to have a negative right side) of the shortest. Record a graph with a matrix. Timeliness is poor, time complexity O (v^3).
The Floyd-warshall algorithm (Floyd-warshall algorithm) is an algorithm to solve the shortest path between any two points, which can correctly handle the shortest path problem with the direction graph or 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 programming:
Set Di,j,k to the length of the shortest path from I to J only in the (1..K) set of nodes as intermediate nodes.
If the shortest path passes the point k, then Di,j,k = di,k,k-1 + dk,j,k-1;
If the shortest path does not pass the point k, then 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 the actual algorithm, in order to save space, can be directly in the original space iterative, so that the space can be reduced to two dimensions.
The Floyd-warshall algorithm is described as follows:
For K←1 to N do
For I←1 to N do
For J←1 to N do
if (di,k + dk,j < di,j) Then
Di,j←di,k + dk,j;
Where di,j represents the cost of point I to J, when Di,j is ∞ means there is no connection between two points.

Dijkstra

The shortest path of single source and non-negative power is obtained. Timeliness is good, time complexity is O (v*v+e).
If the source point is reached, O (V*LGV+E*LGV) =>o (E*LGV).
When the case of sparse graphs is E=V*V/LGV at this time, the time complexity of the algorithm can be O (v^2) . If the Fibonacci heaps is a priority queue, the algorithm time complexity is O (V*LGV + E).

For more information, please refer to: two (continued), thorough understanding of the Dijkstra algorithm , and two (re-continuation), Dijkstra algorithm +fibonacci heap of progressive C implementation .

Bellman-ford

Single source The shortest circuit, you can determine whether there is no negative power loop (if there is, there is no shortest),
Good timeliness, time complexity O (VE). This algorithm will be elaborated in this blog in the future.

Bellman-ford algorithm is an algorithm for solving single-source shortest path problem.

The shortest path problem for single source points is:
Given a weighted graph G and the source point s, the shortest path from S to V is obtained for any point of V in Figure G.

Unlike the Dijkstra algorithm, in the Bellman-ford algorithm, the weight of the edge can be a negative number.
Imagine that we can find a loop (that is, from V, after several points and back to V) and that the sum of the weights of all sides in this loop is negative. Then through this loop, the shortest path of any two points in the loop can be infinitely small. If this negative loop is not processed, the program will run indefinitely. The Bellman-ford algorithm has the ability to distinguish this negative loop.


Spfa

Bellman-ford is the queue optimization, timeliness is relatively good, time complexity O (KE). (K<<V).

Similar to the Bellman-ford algorithm, the SPFA algorithm uses a series of relaxation operations to obtain the shortest path from one node to all other nodes in the graph. The difference is that the SPFA algorithm does not need to update the other nodes immediately after the current shortest path of a node is updated by maintaining a queue , which greatly reduces the number of repeated operations.

The SPFA algorithm can be used for graphs with negative edge weights, which are different from the Dijkstra algorithm.

Unlike the Dijkstra algorithm and the Bellman-ford algorithm, the SPFA algorithm time efficiency is not stable, that is, it has a great difference in the time required for different graphs.

In the best case, each node is queued only once, the algorithm actually becomes a breadth-first traversal, with a time complexity of only O (E). On the other hand, there are such examples, so that each node is queued (V-1) times, the algorithm is degraded to the Bellman-ford algorithm, the time complexity of O (VE).

The SPFA algorithm can completely replace the Bellman-ford algorithm on the negative edge weight graph, and it also behaves well in sparse graphs. However, in a non-negative edge graph, in order to avoid the worst case scenario, a more efficient Dijkstra algorithm is often used, along with its use of heap-optimized versions. The usual SPFA algorithm is not satisfactory in a class of grid diagrams.

Finish.

Transferred from: http://www.cnblogs.com/hibernate6/archive/2011/02/12/2522331.html

Comparison of several shortest path algorithms Floyd, Dijkstra, Bellman-ford, SPFA (RPM)

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.