fbpx
Wikipedia

Shortest path faster 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.[4]

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

Algorithm edit

Given a weighted directed graph   and a source vertex  , SPFA 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  .

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.[7] Here   is a first-in, first-out queue of candidate vertices, and   is the edge weight of  .

 
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   with a node   in  , which gives a shorter path from the source to  .
 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 edit

It is possible to 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: 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 holds before the loop is entered: İf  , 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 vertex   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.[8]

Complexity edit

Experiments show that the average time complexity of SPFA is   on random graphs, and the worst-case time complexity is  , which is equal to that of the Bellman-Ford algorithm.[1][9]

Optimization techniques edit

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 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   so that vertices closer to the source are processed first. Therefore, when implementing these techniques,   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 of the above pseudocode, instead of always pushing the 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 (after pushing   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 edit

  1. ^ a b "Detail of message". poj.org. Retrieved 2023-12-11.
  2. ^ Pape, U. (1974-12-01). "Implementation and efficiency of Moore-algorithms for the shortest route problem". Mathematical Programming. 7 (1): 212–222. doi:10.1007/BF01585517. ISSN 1436-4646.
  3. ^ Schrijver, Alexander (2012-01-01). "On the history of the shortest path problem". ems.press. Retrieved 2023-12-13.
  4. ^ Zhang, Wei; Chen, Hao; Jiang, Chong; Zhu, Lin. "Improvement And Experimental Evaluation Bellman-Ford Algorithm". Proceedings of the 2013 International Conference on Advanced ICT and Education.
  5. ^ Moore, Edward F. (1959). "The shortest path through a maze". Proceedings of the International Symposium on the Theory of Switching. Harvard University Press. pp. 285–292.
  6. ^ Duan, Fanding (1994). "关于最短路径的SPFA快速算法 [About the SPFA algorithm]". Journal of Southwest Jiaotong University. 29 (2): 207–212.
  7. ^ "Algorithm Gym :: Graph Algorithms".
  8. ^ "Shortest Path Faster Algorithm". wcipeg.
  9. ^ "Worst test case for SPFA". Retrieved 2023-05-14.

shortest, path, faster, algorithm, been, suggested, that, this, article, merged, into, bellman, ford, algorithm, discuss, proposed, since, april, 2024, this, article, multiple, issues, please, help, improve, discuss, these, issues, talk, page, learn, when, rem. It has been suggested that this article be merged into Bellman Ford algorithm Discuss Proposed since April 2024 This article has multiple issues Please help improve it or discuss these issues on the talk page Learn how and when to remove these template messages This article s tone or style may not reflect the encyclopedic tone used on Wikipedia See Wikipedia s guide to writing better articles for suggestions November 2023 Learn how and when to remove this template message This article needs additional citations for verification Please help improve this article by adding citations to reliable sources Unsourced material may be challenged and removed Find sources Shortest path faster algorithm news newspapers books scholar JSTOR November 2023 Learn how and when to remove this template message Learn how and when to remove this template message 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 4 SPFA was first published by Edward F Moore in 1959 as a generalization of breadth first search 5 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 6 Contents 1 Algorithm 2 Proof of correctness 3 Complexity 4 Optimization techniques 5 ReferencesAlgorithm editGiven a weighted directed graph G V E displaystyle G V E nbsp and a source vertex s displaystyle s nbsp SPFA finds the shortest path from s displaystyle s nbsp to each vertex v displaystyle v nbsp in the graph The length of the shortest path from s displaystyle s nbsp to v displaystyle v nbsp is stored in d v displaystyle d v nbsp for each vertex v displaystyle v nbsp 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 7 Here Q displaystyle Q nbsp is a first in first out queue of candidate vertices and w u v displaystyle w u v nbsp is the edge weight of u v displaystyle u v nbsp nbsp 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 v displaystyle v nbsp with a node u displaystyle u nbsp in Q displaystyle Q nbsp which gives a shorter path from the source to v displaystyle v nbsp procedure Shortest Path Faster Algorithm G s 1 for each vertex v s 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 lt 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 editIt is possible to 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 To show that if dist w gt dist u w u w displaystyle dist w gt dist u w u w nbsp for any two vertices u displaystyle u nbsp and w displaystyle w nbsp at the time the condition is checked u displaystyle u nbsp 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 If u s displaystyle u not s nbsp then relaxation is not possible relaxation is possible from u s displaystyle u s nbsp and this is added to the queue immediately before the while loop is entered Now consider what happens inside the loop A vertex u displaystyle u nbsp is popped and is used to relax all its neighbors if possible Therefore immediately after that iteration of the loop u displaystyle u nbsp is not capable of causing any more relaxations and does not have to be in the queue anymore However the relaxation by x displaystyle x nbsp might cause some other vertices to become capable of causing relaxation If there exists some vertex x displaystyle x nbsp such that dist x gt dist w w w x displaystyle dist x gt dist w w w x nbsp before the current loop iteration then w displaystyle w nbsp is already in the queue If this condition becomes true during the current loop iteration then either dist x displaystyle dist x nbsp increased which is impossible or dist w displaystyle dist w nbsp decreased implying that w displaystyle w nbsp was relaxed But after w displaystyle w nbsp 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 8 Complexity editExperiments show that the average time complexity of SPFA is O E displaystyle O E nbsp on random graphs and the worst case time complexity is W V E displaystyle Omega V cdot E nbsp which is equal to that of the Bellman Ford algorithm 1 9 Optimization techniques editThe performance of the algorithm is strongly determined by the order in which candidate vertices are used to relax other vertices In fact if Q displaystyle Q nbsp 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 Q displaystyle Q nbsp so that vertices closer to the source are processed first Therefore when implementing these techniques Q displaystyle Q nbsp 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 of the above pseudocode instead of always pushing the vertex v displaystyle v nbsp to the end of the queue we compare d v displaystyle d v nbsp to d front Q displaystyle d big text front Q big nbsp and insert v displaystyle v nbsp to the front of the queue if d v displaystyle d v nbsp is smaller The pseudo code for this technique is after pushing v displaystyle v nbsp to the end of the queue in line 11 procedure Small Label First G Q if d back Q lt 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 gt x u pop front of Q push u to back of QReferences edit a b Detail of message poj org Retrieved 2023 12 11 Pape U 1974 12 01 Implementation and efficiency of Moore algorithms for the shortest route problem Mathematical Programming 7 1 212 222 doi 10 1007 BF01585517 ISSN 1436 4646 Schrijver Alexander 2012 01 01 On the history of the shortest path problem ems press Retrieved 2023 12 13 Zhang Wei Chen Hao Jiang Chong Zhu Lin Improvement And Experimental Evaluation Bellman Ford Algorithm Proceedings of the 2013 International Conference on Advanced ICT and Education Moore Edward F 1959 The shortest path through a maze Proceedings of the International Symposium on the Theory of Switching Harvard University Press pp 285 292 Duan Fanding 1994 关于最短路径的SPFA快速算法 About the SPFA algorithm Journal of Southwest Jiaotong University 29 2 207 212 Algorithm Gym Graph Algorithms Shortest Path Faster Algorithm wcipeg Worst test case for SPFA Retrieved 2023 05 14 Retrieved from https en wikipedia org w index php title Shortest path faster algorithm amp oldid 1218487336, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.