Comparison of Dijkstra, bellman_ford, spfa, and Floyd Algorithms

Source: Internet
Author: User

Dijkstra: applies to the single-source shortest path of a graph with non-negative weights. It uses the complexity O (E + vlgv) of the Fibonacci heap)

Bellmanford: applies to the single-source shortest path of an image with negative weights, and can detect negative loops. complexity O (VE) spfa: applies to negative weights, the single-source shortest path of a graph without negative loops. In this paper, the complexity O (KE), k is the number of times each node enters the queue, and K is generally <= 2, however, the complexity here proves that there is a problem. In fact, the worst case of spfa should be O (VE ). FLOYD: the shortest path between each pair of nodes.
First, draw a conclusion: (1) when the weight is not negative, use Dijkstra. (2) If the weight value is negative and there is no negative circle, use spfa. spfa can detect negative circles, but cannot output negative circles. (3) When the weight value has a negative value and a negative circle may exist, bellmanford can be used to detect and output a negative circle. (4) spfa negative ring Detection: If a vertex is in the queue for more than or equal to a value of V, a negative ring exists, which is proved later.
This article analyzes the spfa algorithm.

This article solves the following problems: (1) prove the worst complexity of the spfa algorithm. (2) Why does a vertex enter the queue for V times? The graph has a negative ring.
Spfa is Duan fanding of Xi'an Jiaotong University and the Journal of Xi'an Jiaotong University on 1994In the "spfa fast algorithm for shortest paths", he said that spfa is faster than Dijkstra, And the spfa speed for running V times is faster than Floyd, at that time, I was wondering why he was so fast that he never appeared or was mentioned in some classic books. It turns out that the spfa algorithm is limited and does not apply to dense graphs. For dense graphs in special cases, the complexity of spfa is the same as that of bellmanford.
The optimal time complexity is not described first. Below is a proof of spfa Worst time complexity:
Ideas:(1) The complexity of finding out the worst-case to impossible spfa is O (VE ). (2) find out that spfa does have a graph, so that the complexity of running spfa is O (VE ).

I originally wanted to give an example to illustrate the existence of O (VE) in spfa, but it is true that the complexity in the worst case cannot be illustrated by an example. Thank you for pointing out from instructor tianmingbu.

It is proved that if there is a negative loop and only one vertex enters the queue more than or equal to V times.
For a point V, we know that the number of sides of the relaxation path from S to V is a maximum of V-1. The relaxation path I mentioned here refers to: for example, s directly relaxes V, so there is a relaxation path: S-> v. S relaxation A and a relaxation V, then S-> A-> V is a relaxation path.
For all the relaxation paths from S to V, when the number of relaxation path edges is equal, V only enters the queue once. For example, there is a slack path: S-> A-> X-> vs-> B-> X-> vs-> C-> Z-> V, we can see that V only enters the team once. Because s to V the length of the relaxation path can have a maximum of V-1 changes, so v up to the V-1.
For example:
Suppose there is a graph where the vertex set is {S, A, B, C, V}, the most likely relaxation path is: s-> vs-> A-> vs-> B-> vs-> C-> vs-> A-> B-> vs-> A-> C-> -> B-> C-> vs-> A-> B-> C-> V
Then, the number of sides of the relaxation path changes 1, 2, 3, 4, so V into the queue for 4 times, that is, the V-1 times.


So we can say that each point to join the V-1 at most times, so we find the worst case for each point to join the V-1 times, so at this time:

Here is an example of the worst case.


Of course, we may consider how to give a graph when we give a value of V, a value of E, such as E = 2 v, the complexity of running the spfa Algorithm for this image is O (VE ). here we assume that the graph is connected, so E> = V-1.
The method is as follows: (1) first, we will group the graph into a chain, as shown in:


In this way, the V-1 side is used. (2) Add V0 connections to V2, V3 ,.... VK edge, we want to add these edge weight to meet V0 first update VK, V0 after updating the vk-1 vk-1 can also update VK, and so on, as shown in:
(3) To VK, vk-1 ,..... in the order of V1, the weights are added to an infinite self-ring, and the weights are continuously cyclically. This step is to ensure that the outbound degrees from V1 to VK are consistent, so do this, for example:

In this way, we have constructed a graph that allows spfa to run out of O (VE) for the following reasons:
Because the values of E and V are uncertain, we may not be able to complete these structures. We will analyze the above steps when no remaining edge is constructed (2) complexity (that is to say, e <= 2v-3, because the first step to connect to a chain needs to V-1 side, and the second step V0 to connect out of the edge needs to V-2 ), and there are enough edges to construct the above graph.
(1) If e <= 2v-3
Because e <= 2v-3, E = O (V), as long as the complexity is calculated as O (V ^ 2), it can be called O (VE ). we need to calculate the number of queues and the number of access edges.
V0 degree for E-V + 2, V0 into the team 1 time. V1 has an outbound value of 1, and V1 has joined the team once. V2 is 1, V2 is 2 (v0 is relaxed V2, V1 is relaxed V2 ). V3 is 1, and V3 is 3 ..... V (E-V + 2) outbound is 1, the number of queues is (E-V + 2. V (E-V + 3), V (E-V + 4 ),..... V (k-1) of the degree of exit is 1, the number of queues for E-V + 2 times. The number of these points is V-(E-V + 3)-1 = 2v-e-4. VK outbound is 0, VK into the queue for E-V + 2 times.
Therefore, the total number of access edges is as follows:

(2) If E> 2v-3
At this time, the second step of the Construction map has been completed, so you only need to constantly add the remaining sides to the self-protection degree.
V0 degree for the V-1, join 1 time. V1 to VK outbound (E-V + 2)/(V-1) or (E-V + 2)/(V-1) + 1. V1 to VK respectively into 1, 2, 3,... V-1.
Therefore, the total number of access edges is:


Package C24; import Java. util. iterator; import Java. util. using list; import Java. util. list; import c22.graphfactory; import c22.pair; import c22.weighted _ adjacent_list; public class spfa {public int [] spfa (weighted_adjacent_list g, string s) {return spfa (G, G. getvertexindex (s);} public int [] spfa (weighted_adjacent_list g, int s) {// 1. create the data structure int size = G. getsize (); int d [] = new int [size]; // Distance Estimation for (INT I = 0; I <D. Length; I ++) {d [I] = integer. max_value ;}list <integer> q = new queue list <integer> (); Boolean is_in_queue [] = new Boolean [size]; // whether it is in the queue for (INT I = 0; I <is_in_queue.length; I ++) {is_in_queue [I] = false;} // 2. initialize d [s] = 0; q. add (s); is_in_queue [s] = true; // 3. core while (! Q. isempty () {int u = Q. remove (0); is_in_queue [u] = false; List <pair> List = G. getlistbyvertexindex (U); iterator <pair> iter = List. iterator (); While (ITER. hasnext () {pair vstr = ITER. next (); int v = G. getvertexindex (vstr. end); If (d [v]> d [u] + vstr. weight) {d [v] = d [u] + vstr. weight; If (! Is_in_queue [v]) {// If the relaxed vertex is not in the queue, add it to the queue. If it is in the queue, do not move Q. add (V); is_in_queue [v] = true ;}}} return D;} public static void main (string [] ARGs) throws exception {spfa spfa_alg = new spfa (); weighted_adjacent_list G = graphfactory. getweightedadjacentlistinstance ("input \ weighted_graph.txt"); int [] d = spfa_alg.spfa (G, "S"); For (INT I = 0; I <D. length; I ++) {system. out. println (G. getvertexvalue (I) + ":" + d [I]) ;}}

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.