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 form the backbone of countless real-world applications you'll encounter throughout computer science—from GPS navigation and network routing to social network analysis and game AI. When you're tested on these algorithms, you're not just being asked to recall steps; 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. You need to understand how 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 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.
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.
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 an FRQ 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 trade guaranteed optimality (in some cases) for dramatic speed improvements.
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?
An FRQ 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?