Shortest Path Algorithms
Introduction
Finding the shortest path between two points is one of the most fundamental problems in graph theory and computer science. GPS navigation systems find shortest routes through road networks. Internet routers find shortest paths through communication networks. Logistics companies optimize delivery routes.
The shortest path problem comes in several variants. Single-source shortest path finds the shortest paths from one vertex to all others. Single-pair shortest path finds the path between two specific vertices. All-pairs shortest path finds paths between every pair of vertices.
Different algorithms are optimal for different scenarios. Dijkstra's algorithm handles non-negative weights efficiently. Bellman–Ford handles negative weights and detects negative cycles. Floyd–Warshall computes all-pairs shortest paths. Understanding these algorithms and their trade-offs is essential for solving real-world optimization problems.
This page presents the major shortest path algorithms, analyzing their correctness, complexity, and appropriate use cases.
Problem Formulation
Weighted Graphs
Consider a directed graph
For vertices
Negative Weights and Cycles
Negative edge weights are allowed in some algorithms but complicate the problem. A negative-weight cycle is a cycle whose edges sum to a negative weight. If such a cycle is reachable from the source and reaches the destination, the shortest path is undefined (negative infinity).
Dijkstra algorithm requires non-negative weights. Bellman-Ford handles negative weights and detects negative cycles. Detecting and handling negative cycles is important in financial arbitrage detection.
Dijkstra Algorithm
Overview
Dijkstra algorithm, published by Edsger Dijkstra in
The algorithm maintains a set
The Algorithm
Initialize: Set
Main loop: While the priority queue is not empty, extract the vertex
The relaxation step checks if going through
Correctness
Dijkstra algorithm is correct because when a vertex
This greedy choice is safe: the minimum-distance vertex in the queue cannot have its distance improved by paths through other unprocessed vertices, because those paths would have greater or equal length.
Complexity
The complexity depends on the priority queue implementation. With a binary heap, extract-min takes
With a Fibonacci heap, decrease-key is amortized
Bellman-Ford Algorithm
Overview
The Bellman-Ford algorithm solves single-source shortest paths with negative edge weights and can detect negative-weight cycles. It is slower than Dijkstra but more general.
The Algorithm
Initialize: Set
Main loop: Repeat
Negative cycle detection: After
Correctness
After
Complexity
The algorithm performs
Floyd-Warshall Algorithm
Overview
Floyd-Warshall computes shortest paths between all pairs of vertices simultaneously. It uses dynamic programming and handles negative edge weights (but not negative cycles).
The Algorithm
Let d[i][j] denote the shortest path from i to j using only intermediate vertices from {1, 2, ..., k}. Initialize d[i][j] = w(i,j) if edge exists, 0 if i = j, infinity otherwise.
Let
The recurrence is: for
This considers whether the shortest path from
Complexity
The three nested loops each run
Negative cycles are detected if any diagonal entry
Breadth-First Search for Unweighted Graphs
When all edges have equal weight (or weight
The algorithm uses a queue: enqueue the source with distance
BFS is optimal for unweighted graphs and is the basis for more sophisticated algorithms. Many graph algorithms are variations or extensions of BFS and DFS.
A* Algorithm
A* is a heuristic search algorithm that extends Dijkstra by using an admissible heuristic
An admissible heuristic never overestimates the true distance. With such a heuristic, A* finds optimal paths while often exploring far fewer vertices than Dijkstra. For geographic routing, the Euclidean distance is a common heuristic.
A* is widely used in games, robotics, and navigation systems. The choice of heuristic trades off between optimality guarantees and computational efficiency.
Applications
Navigation and Routing
GPS systems and mapping applications use shortest path algorithms to compute driving directions. Modern systems use A* with geographic heuristics and precomputed auxiliary data structures for near-instant responses.
Network Routing
Internet routing protocols use shortest path algorithms. OSPF (Open Shortest Path First) uses Dijkstra algorithm. Each router maintains a map of the network and computes shortest paths to all destinations.
Social Network Analysis
The shortest path between users in a social network measures degrees of separation. Betweenness centrality, measuring how often a vertex lies on shortest paths between others, identifies influential network nodes.
Arbitrage Detection
In currency exchange, taking logarithms of exchange rates converts multiplication to addition. A negative cycle in this transformed graph represents an arbitrage opportunity. Bellman-Ford detects such opportunities.
Algorithm Selection Guide
Use BFS for unweighted graphs
O*(n+m) .Use Dijkstra for non-negative weights
O*(m*(log_)(n)) .Use Bellman-Ford for negative weights or cycle detection
O*(n*m) .Use Floyd-Warshall for all-pairs queries
O(n3) .Use A* when a good heuristic is available.
Summary
Shortest path algorithms find paths minimizing total edge weight in weighted graphs. The main variants are single-source (one source to all vertices), single-pair (specific source and target), and all-pairs (between every pair).
Dijkstra algorithm is optimal for single-source shortest paths with non-negative weights. It uses a priority queue to always process the vertex with minimum distance estimate, achieving
Bellman-Ford algorithm handles negative edge weights by relaxing all edges
Floyd-Warshall computes all-pairs shortest paths using dynamic programming in
A* extends Dijkstra with heuristic guidance, often finding optimal paths much faster by prioritizing promising directions. BFS is optimal for unweighted graphs.
These algorithms are fundamental to navigation systems, network routing, social network analysis, and many optimization problems. Choosing the right algorithm depends on edge weight properties, graph density, and query type.