Graph Theory37 sections · 1633 units
Open in Course

SPFA Improvement

(Queue-based relaxation)

Standard Bellman-Ford relaxes all edges every iteration, even if most distances did not change. This wastes time when only a few nodes are updated. SPFA (Shortest Path Faster Algorithm) uses a queue to track only nodes whose distances changed. You only relax edges from nodes in the queue.

If a node's distance improves, add it to the queue (if not already there). On random graphs, SPFA runs in O(E)O(E) average case. Worst case is still O(VE)O(VE), but practical performance is much better. SPFA is Bellman-Ford with a queue improvement.

Space complexity is O(V)O(V) for the data structures used.