Shortest Path Faster Algorithm


The Shortest Path Faster Algorithm is an improvement of the Bellman–Ford algorithm which computes single-source shortest paths in a weighted directed graph. The algorithm is believed to work well on random sparse graphs and is particularly suitable for graphs that contain negative-weight edges. However, the worst-case complexity of SPFA is the same as that of Bellman–Ford, so for graphs with nonnegative edge weights Dijkstra's algorithm is preferred. The SPFA algorithm was first published by Edward F. Moore in 1959, as a generalization of breadth first search; the same algorithm was rediscovered in 1994 by Fanding Duan.

Algorithm

Given a weighted directed graph and a source vertex, the SPFA algorithm finds the shortest path from, to each vertex, in the graph. The length of the shortest path from, to is stored in for each vertex.
The basic idea of SPFA is the same as Bellman–Ford algorithm in that each vertex is used as a candidate to relax its adjacent vertices. The improvement over the latter is that instead of trying all vertices blindly, SPFA maintains a queue of candidate vertices and adds a vertex to the queue only if that vertex is relaxed. This process repeats until no more vertex can be relaxed.
Below is the pseudo-code of the algorithm. Here is a first-in, first-out queue of candidate vertices, and is the edge weight of.
procedure Shortest-Path-Faster-Algorithm
1 for each vertex vs in V
2 d := ∞
3 d := 0
4 push s into Q
5 while Q is not empty do
6 u := poll Q
7 for each edge in E do
8 if d + w < d then
9 d := d + w
10 if v is not in Q then
11 push v into Q
The algorithm can also be applied to an undirected graph by replacing each undirected edge with two directed edge of opposite directions.

Proof of correctness

We will prove that the algorithm never computes incorrect shortest path lengths.
The algorithm fails to terminate if negative-weight cycles are reachable from the source. See 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. Therefore in graphs containing no cycles of negative weight, the algorithm will never terminate with incorrect shortest path lengths.

Running time

The worst-case running time of the algorithm is, just like the standard Bellman-Ford algorithm. Experiments suggest that the average running time is, and indeed this is true on random graphs, but it is possible to construct sparse graphs where SPFA runs in time like the usual Bellman-Ford algorithm.

Optimization techniques

The performance of the algorithm is strongly determined by the order in which candidate vertices are used to relax other vertices. In fact, if is a priority queue, then the algorithm pretty much resembles Dijkstra's. However, since a priority queue is not used here, two techniques are sometimes employed to improve the quality of the queue, which in turn improves the average-case performance. Both techniques rearranges the order of elements in so that vertices closer to the source are processed first. Therefore, when implementing these techniques, is no longer a first-in, first-out queue, but rather a normal doubly linked list or double-ended queue.
Small Label First technique. In line 11, instead of always pushing vertex to the end of the queue, we compare to, and insert to the front of the queue if is smaller. The pseudo-code for this technique is :
procedure Small-Label-First
if d < d then
u := pop back of Q
push u into front of Q
Large Label Last technique. After line 11, we update the queue so that the first element is smaller than the average, and any element larger than the average is moved to the end of the queue. The pseudo-code is:
procedure Large-Label-Last
x := average of d for all v in Q
while d > x
u := pop front of Q
push u to back of Q