Shortest Path Faster Algorithm
# Shortest Path Faster Algorithm

The Shortest Path Faster Algorithm (SPFA) 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; SPFA is Moore's “Algorithm D.” The name, “Shortest Path Faster Algorithm (SPFA),” was given by FanDing Duan, a Chinese researcher who rediscovered the algorithm in 1994.

## Algorithm

Given a weighted directed graph $G=(V,E)$ and a source vertex $s$, the SPFA algorithm finds the shortest path from $s$, to each vertex $v$, in the graph. The length of the shortest path from $s$ to $v$ is stored in $d(v)$ for each vertex $v$.

The basic idea of SPFA is the same as the 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 $Q$ is a first-in, first-out queue of candidate vertices, and $w(u,v)$ is the edge weight of $(u,v)$.

The algorithm can also be applied to an undirected graph by replacing each undirected edge with two directed edges of opposite directions.

## Proof of correctness

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 $dist[w]>dist[u]+w(u,w)$ for any two vertices $u$ and $w$ at the time the condition is checked,$u$ 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 $u\not =s$, then relaxation is not possible; relaxation is possible from $u=s$ , and this is added to the queue immediately before the while loop is entered. Now, consider what happens inside the loop. A vertex $u$ is popped, and is used to relax all its neighbors, if possible. Therefore, immediately after that iteration of the loop, $u$ is not capable of causing any more relaxations (and does not have to be in the queue anymore). However, the relaxation by $x$ might cause some other vertices to become capable of causing relaxation. If there exists some vertex $x$ such that $dist[x]>dist[w]+w(w,x)$ before the current loop iteration, then $w$ is already in the queue. If this condition becomes true during the current loop iteration, then either $dist[x]$ increased, which is impossible, or $dist[w]$ decreased, implying that $w$ was relaxed. But after $w$ 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.

## Running time

The worst-case running time of the algorithm is $O(|V|\cdot |E|)$, just like the standard Bellman-Ford algorithm. Experiments suggest that the average running time is $O(|E|)$, and indeed this is true on random graphs, but it is possible to construct sparse graphs where SPFA runs in time $\Omega (|V|^{2})$ 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 $Q$ 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 (but not the worst-case performance). Both techniques rearrange the order of elements in $Q$ so that vertices closer to the source are processed first. Therefore, when implementing these techniques, $Q$ is no longer a first-in, first-out queue, but rather a normal doubly linked list or double-ended queue.

Small Label First (SLF) technique. In line 11, instead of always pushing vertex $v$ to the end of the queue, we compare $d(v)$ to $d{\big{\text{front}}(Q){\big )}$, and insert $v$ to the front of the queue if $d(v)$ is smaller. The pseudo-code for this technique is (after pushing $v$ to the end of the queue in line 11):

Large Label Last (LLL) 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:

