Shortest path faster algorithm

From HandWiki
Revision as of 20:46, 6 February 2024 by CodeMe (talk | contribs) (fix)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Short description: Graph algorithm


The Shortest Path Faster Algorithm (SPFA) is an improved version of the Bellman–Ford algorithm that computes single-source shortest paths in a weighted directed graph. The algorithm works well on sparse random graphs, especially those that contain negative-weight edges.[1][2][3] The worst-case complexity of SPFA is the same as that of Bellman–Ford, therefore Dijkstra's algorithm is preferred for graphs with nonnegative edge weights.

SPFA was first published by Edward F. Moore in 1959, as a generalization of breadth first search;[4] 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.[5]

Algorithm

Given a weighted directed graph [math]\displaystyle{ G=(V,E) }[/math] and a source vertex [math]\displaystyle{ s }[/math], SPFA finds the shortest path from [math]\displaystyle{ s }[/math] to each vertex [math]\displaystyle{ v }[/math] in the graph. The length of the shortest path from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ v }[/math] is stored in [math]\displaystyle{ d(v) }[/math] for each vertex [math]\displaystyle{ v }[/math].

SPFA is similar to the Bellman-Ford algorithm in that each vertex is used as a candidate to relax its adjacent vertices. However, 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, repeating the process until no more vertices can be relaxed.

Below is the pseudo-code of the algorithm.[6] Here [math]\displaystyle{ Q }[/math] is a first-in, first-out queue of candidate vertices, and [math]\displaystyle{ w(u,v) }[/math] is the edge weight of [math]\displaystyle{ (u,v) }[/math].

A demo of SPFA based on Euclidean distance. Red lines are the shortest path covering (so far observed). Blue lines indicate where relaxing happens, i.e., connecting [math]\displaystyle{ v }[/math] with a node [math]\displaystyle{ u }[/math] in [math]\displaystyle{ Q }[/math], which gives a shorter path from the source to [math]\displaystyle{ v }[/math].
 procedure Shortest-Path-Faster-Algorithm(G, s)
  1    for each vertex vs in V(G)
  2        d(v) := ∞
  3    d(s) := 0
  4    push s into Q
  5    while Q is not empty do
  6        u := poll Q
  7        for each edge (u, v) in E(G) do
  8            if d(u) + w(u, v) < d(v) then
  9                d(v) := d(u) + w(u, v)
 10                if v is not in Q then
 11                    push v into Q

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

Proof of correctness

We can prove that the algorithm always finds the shortest path.

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

Complexity

Experiments show that the average time complexity of SPFA is [math]\displaystyle{ O(|E|) }[/math] on random graphs, and the worst-case time complexity is [math]\displaystyle{ \Omega(|V|\cdot|E|) }[/math], which is equal to that of Bellman-Ford algorithm.[1][8]

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 [math]\displaystyle{ Q }[/math] is a priority queue, then the algorithm 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 [math]\displaystyle{ Q }[/math] so that vertices closer to the source are processed first. Therefore, when implementing these techniques, [math]\displaystyle{ Q }[/math] is no longer a first-in, first-out (FIFO) 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 [math]\displaystyle{ v }[/math] to the end of the queue, we compare [math]\displaystyle{ d(v) }[/math] to [math]\displaystyle{ d\big(\text{front}(Q)\big) }[/math], and insert [math]\displaystyle{ v }[/math] to the front of the queue if [math]\displaystyle{ d(v) }[/math] is smaller. The pseudo-code for this technique is (after pushing [math]\displaystyle{ v }[/math] to the end of the queue in line 11):

procedure Small-Label-First(G, Q)
    if d(back(Q)) < d(front(Q)) then
        u := pop back of Q
        push u into front of Q

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:

procedure Large-Label-Last(G, Q)
    x := average of d(v) for all v in Q
    while d(front(Q)) > x
        u := pop front of Q
        push u to back of Q

References