Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Shortest path algorithms power countless real-world applications: GPS navigation, network routing, social network analysis, and game AI. When you're tested on these algorithms, you're being evaluated on your understanding of algorithmic trade-offs, graph properties, and problem-solving strategies. Each algorithm represents a different approach to the same fundamental question: how do we efficiently find optimal paths through a network?
The key to mastering this topic is recognizing when to use which algorithm. Graph properties like edge weights, density, and the presence of negative cycles determine which algorithm is appropriate. Don't just memorize time complexities. Know what constraints each algorithm handles, what data structures it leverages, and why those choices matter. When an exam question describes a graph scenario, you should immediately know which algorithm fits.
These algorithms find shortest paths from one starting node to all other nodes, but they require that edge weights be non-negative. The greedy approach works here because once a node is finalized, no shorter path to it can exist.
Compare: Dijkstra's vs. BFS โ both find single-source shortest paths, but BFS only works on unweighted graphs while Dijkstra's handles varying non-negative weights. If an exam gives you an unweighted graph, BFS is simpler and faster; don't overcomplicate it with Dijkstra's.
When graphs contain negative edge weights, greedy approaches fail. These algorithms use iterative relaxation: repeatedly updating distance estimates until no improvement is possible.
The core idea is straightforward: relax every edge in the graph, and repeat that process times. Why ? Because the longest possible shortest path (without cycles) visits at most edges. Each iteration guarantees that paths using one more edge are correctly computed.
Compare: Bellman-Ford vs. SPFA โ both handle negative weights with the same worst-case complexity, but SPFA uses a queue to skip unnecessary work. Choose Bellman-Ford when you need guaranteed performance; use SPFA when average-case speed matters more.
Sometimes you need shortest paths between every pair of nodes, not just from one source. These algorithms compute an entire distance matrix, trading higher complexity for complete information.
This algorithm uses dynamic programming over intermediate vertices. The idea: for each vertex , check whether routing the path from to through produces a shorter distance. The recurrence is:
You iterate from 1 to as the outermost loop, with and as the inner loops. The order matters: must be outermost so that when you consider vertex as an intermediate, all paths using vertices through are already computed.
Johnson's is designed for sparse graphs with negative weights. It works in two phases:
Compare: Floyd-Warshall vs. Johnson's โ both compute all-pairs shortest paths, but Floyd-Warshall is simpler with always, while Johnson's is faster on sparse graphs. If a question asks about all-pairs paths on a sparse graph with negative weights, Johnson's is your answer.
When you have additional information about the graph structure (like geometric positions of nodes), you can guide the search more intelligently. Heuristics can dramatically reduce the number of nodes explored.
A* is a best-first search that evaluates nodes using:
where is the actual cost from the start to , and is a heuristic estimate of the remaining cost from to the goal.
Compare: A* vs. Bidirectional Search โ both optimize point-to-point pathfinding, but A* uses domain knowledge (heuristics) while bidirectional search exploits problem structure (known target). In practice, bidirectional A* combines both for maximum efficiency.
| Concept | Best Examples |
|---|---|
| Non-negative weights, single source | Dijkstra's, BFS (unweighted only) |
| Negative weights allowed | Bellman-Ford, SPFA, Johnson's |
| Negative cycle detection | Bellman-Ford |
| All-pairs shortest paths | Floyd-Warshall, Johnson's |
| Sparse graph optimization | Johnson's, SPFA, Dijkstra's with adjacency list |
| Dense graph efficiency | Floyd-Warshall, Dijkstra's with array |
| Heuristic-guided search | A*, Bidirectional Search |
| Unweighted graphs | BFS |
You have a sparse graph with some negative edge weights and need all-pairs shortest paths. Which algorithm should you choose, and why is it better than the alternative?
Compare Dijkstra's Algorithm and Bellman-Ford: what graph property determines which one you can use, and what's the trade-off in time complexity?
A game developer needs to find the shortest path between two points on a grid map. Which two algorithms would you recommend they consider, and what information would help them choose between them?
Why does BFS guarantee shortest paths in unweighted graphs but fail on weighted graphs? What data structure change does Dijkstra's make to handle weights?
A question describes a dense graph with 500 vertices and asks for shortest paths between all pairs. Which algorithm would you use, what is its time complexity, and what technique does it employ?