# Difference between revisions of "Shortest Path Faster Algorithm"

(→References) |
|||

(5 intermediate revisions by 2 users not shown) | |||

Line 1: | Line 1: | ||

− | The '''Shortest Path Faster Algorithm''' (SPFA) is a [[ | + | The '''Shortest Path Faster Algorithm''' (SPFA) is a [[Shortest_paths#Single-source_shortest_paths|single-source shortest paths]] algorithm whose origin is unknown<sup>[see references]</sup>. It is similar to [[Dijkstra's algorithm]] in that it performs relaxations on nodes popped from some sort of [[queue]], but, unlike Dijkstra's, it is usable on graphs containing edges of negative weight, like the [[Bellman-Ford algorithm]]. Its value lies in the fact that, in the average case, it is likely to outperform Bellman-Ford (although not Dijkstra's). In theory, this should lead to an improved version of [[Johnson's algorithm]] as well. |

==The algorithm== | ==The algorithm== | ||

Line 23: | Line 23: | ||

====Proof of correctness==== | ====Proof of correctness==== | ||

− | We will prove that the algorithm | + | We will prove that the algorithm never computes incorrect shortest path lengths. |

:''Lemma'': Whenever the queue is checked for emptiness, any vertex currently capable of causing relaxation is in the queue. | :''Lemma'': Whenever the queue is checked for emptiness, any vertex currently capable of causing relaxation is in the queue. | ||

Line 31: | Line 31: | ||

:''Proof'': If no further relaxations are possible, the algorithm continues to remove vertices from the queue, but does not add any more into the queue, because vertices are added only upon successful relaxations. Therefore the queue becomes empty and the algorithm terminates. If any further relaxations are possible, the queue is not empty, and the algorithm continues to run. <math>_{_\blacksquare}</math> | :''Proof'': If no further relaxations are possible, the algorithm continues to remove vertices from the queue, but does not add any more into the queue, because vertices are added only upon successful relaxations. Therefore the queue becomes empty and the algorithm terminates. If any further relaxations are possible, the queue is not empty, and the algorithm continues to run. <math>_{_\blacksquare}</math> | ||

− | The algorithm fails to terminate | + | The algorithm fails to terminate if negative-weight cycles are reachable from the source. See [[Bellman–Ford algorithm#Proof of detection of negative-weight cycles|here]] for a proof that relaxations are always possible when negative-weight cycles exist. In a graph with no cycles of negative weight, when no more relaxations are possible, the correct shortest paths have been computed ([[Shortest path#Relaxation|proof]]). Therefore in graphs containing no cycles of negative weight, the algorithm will never terminate with incorrect shortest path lengths. |

==References== | ==References== | ||

− | + | Although, [http://en.wikipedia.org/wiki/Shortest_Path_Faster_Algorithm Wikipedia] cites [http://wenku.baidu.com/view/3b8c5d778e9951e79a892705.html Duan Fanding] as the originator of the algorithm under this name, this algorithm is first discovered by Yen as his first improvement to Bellman-Ford. | |

+ | |||

+ | |||

+ | The actual details used in this article were deduced from code (privately communicated to the authors) written by Gelin Zhou (University of Waterloo) who himself attributes the algorithm to a slide presented in a computer science class at MIT. The contributors to this article are unable to find a reference to it in the informatics literature. The algorithm was almost certainly popularized by Chinese informatics competitors. | ||

+ | |||

+ | [[Category:Algorithms]] | ||

+ | [[Category:Graph theory]] |

## Latest revision as of 08:56, 11 January 2016

The **Shortest Path Faster Algorithm** (SPFA) is a single-source shortest paths algorithm whose origin is unknown^{[see references]}. It is similar to Dijkstra's algorithm in that it performs relaxations on nodes popped from some sort of queue, but, unlike Dijkstra's, it is usable on graphs containing edges of negative weight, like the Bellman-Ford algorithm. Its value lies in the fact that, in the average case, it is likely to outperform Bellman-Ford (although not Dijkstra's). In theory, this should lead to an improved version of Johnson's algorithm as well.

## The algorithm[edit]

The algorithm works by repeatedly selecting a vertex and using it to relax, if possible, all of its neighbors. If a node was successfully relaxed, then it might in turn be necessary to use it to relax other vertices, and hence it is marked for consideration also. Once there are no vertices left to be considered, the algorithm terminates. Note that a vertex might be considered several times during the course of the algorithm. The usual implementation strategy is to use a queue to hold the vertices that might be considered next.

#### Pseudocode[edit]

input G,v for each u ∈ V(G) let dist[u] = ∞ let dist[v] = 0 let Q be an initially empty queue push(Q,v) while not empty(Q) let u = pop(Q) for each (u,w) ∈ E(G) if dist[w] > dist[u]+wt(u,w) dist[w] = dist[u]+wt(u,w) if w is not in Q push(Q,w)

**Note**: We use an array of boolean flags to keep track of which vertices are currently in the queue.

#### Proof of correctness[edit]

We will prove that the algorithm never computes incorrect shortest path lengths.

*Lemma*: Whenever the queue is checked for emptiness, any vertex currently capable of causing relaxation is in the queue.*Proof*: We want to show that if for any two vertices and at the time the condition is checked, is in the queue. We do so by induction on the number of iterations of the loop that have already occurred. First we note that this certainly holds before the loop is entered: if , then relaxation is not possible; relaxation is possible from , and this is added to the queue immediately before the while loop is entered. Now, consider what happens inside the loop. A vertex is popped, and is used to relax all its neighbors, if possible. Therefore, immediately after that iteration of the loop, is not capable of causing any more relaxations (and does not have to be in the queue anymore). However, the relaxation by might cause some other vertices to become capable of causing relaxation. If there exists some such that before the current loop iteration, then is already in the queue. If this condition becomes true*during*the current loop iteration, then either increased, which is impossible, or decreased, implying that was relaxed. But after is relaxed, it is added to the queue if it is not already present.

*Corollary*: The algorithm terminates when and only when no further relaxations are possible.*Proof*: If no further relaxations are possible, the algorithm continues to remove vertices from the queue, but does not add any more into the queue, because vertices are added only upon successful relaxations. Therefore the queue becomes empty and the algorithm terminates. If any further relaxations are possible, the queue is not empty, and the algorithm continues to run.

The algorithm fails to terminate if negative-weight cycles are reachable from the source. See here for a proof that relaxations are always possible when negative-weight cycles exist. In a graph with no cycles of negative weight, when no more relaxations are possible, the correct shortest paths have been computed (proof). Therefore in graphs containing no cycles of negative weight, the algorithm will never terminate with incorrect shortest path lengths.

## References[edit]

Although, Wikipedia cites Duan Fanding as the originator of the algorithm under this name, this algorithm is first discovered by Yen as his first improvement to Bellman-Ford.

The actual details used in this article were deduced from code (privately communicated to the authors) written by Gelin Zhou (University of Waterloo) who himself attributes the algorithm to a slide presented in a computer science class at MIT. The contributors to this article are unable to find a reference to it in the informatics literature. The algorithm was almost certainly popularized by Chinese informatics competitors.