DSA Notes
Compare all shortest path algorithms (BFS, Dijkstra, Bellman-Ford, Floyd-Warshall, A*) with a decision guide for when to use each one.
The Shortest Path Problem
Finding the shortest path between vertices in a graph is one of the most fundamental problems in computer science. But there is no single "best" algorithm — the right choice depends on your graph's properties: Is it weighted? Are weights negative? Do you need single-source or all-pairs? Is it sparse or dense?
Algorithm Comparison Table
| Algorithm | Weights | Negative Edges | Source | Time | Space |
|---|---|---|---|---|---|
| BFS | Unweighted | N/A | Single | O(V+E) | O(V) |
| Dijkstra | Non-negative | No | Single | O((V+E) log V) | O(V) |
| Bellman-Ford | Any | Yes (detects neg. cycles) | Single | O(VE) | O(V) |
| Floyd-Warshall | Any | Yes (detects neg. cycles) | All pairs | O(V³) | O(V²) |
| A* | Non-negative | No | Single (to target) | O(E) best case | O(V) |
| 0-1 BFS | 0 or 1 | No | Single | O(V+E) | O(V) |
| SPFA | Any | Yes | Single | O(VE) worst, ~O(E) avg | O(V) |
Decision Flowchart
| ├── YES | Use BFS (O(V+E)) |
| └── NO | Are all weights 0 or 1? |
| ├── YES | Use 0-1 BFS with deque (O(V+E)) |
| └── NO | Do you need ALL pairs shortest paths? |
| ├── YES | Is V ≤ 400? |
| │ ├── YES | Floyd-Warshall (O(V³)) |
| │ └── NO | Run Dijkstra from each vertex (O(V(V+E)logV)) |
| └── NO | Are there negative edge weights? |
| ├── YES | Bellman-Ford (O(VE)) |
| └── NO | Do you have a target + heuristic? |
| ├── YES | A* search |
| └── NO | Dijkstra (O((V+E)logV)) |
BFS: The Simplest Case
When: Unweighted graph (or all edges have same weight) Why: BFS explores in order of distance from source — first it finds all vertices at distance 1, then 2, etc. Key idea: Queue-based level-by-level expansion
Dijkstra: The Workhorse
When: Weighted graph with non-negative edges, single source Why: Greedy approach — always process the closest unvisited vertex Key idea: Priority queue (min-heap) ensures we process vertices in distance order
The "relaxation" step: if dist[u] + weight(u,v) < dist[v], update dist[v].
Once a vertex is popped from the heap, its distance is final (greedy guarantee from non-negative weights).
Bellman-Ford: Handling Negative Weights
When: Graph may have negative edges (but ideally no negative cycles) Why: Relaxes ALL edges V-1 times, guaranteed to find shortest paths Key idea: On iteration i, all shortest paths using ≤ i edges are correct
Also detects negative cycles: if any edge can still be relaxed after V-1 iterations, a negative cycle exists.
Floyd-Warshall: All Pairs
When: Need shortest path between every pair of vertices, V is small (≤400) Why: Elegant DP approach — try each vertex as intermediate Key idea: dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) for each k
Three nested loops. Simple to code, but O(V³) limits it to small graphs.
A*: Informed Search
When: Single source to single target, and you have a heuristic function Why: Uses a heuristic h(v) to estimate remaining distance, focusing search toward the target Key idea: Priority = actual distance g(v) + estimated remaining h(v)
The heuristic must be admissible (never overestimate) for A* to find optimal paths.
0-1 BFS: Special Case Optimization
When: All edge weights are either 0 or 1 Why: Use a deque — push weight-0 edges to front, weight-1 to back Advantage: O(V+E) instead of O((V+E) log V) for Dijkstra
Practical Recommendations
- Default choice: Dijkstra with binary heap. It handles 99% of shortest path problems you will encounter.
- Negative weights: Bellman-Ford. Slow but correct.
- Small dense graphs, all pairs: Floyd-Warshall. Simple to code.
- Unweighted: Always BFS. Simpler and faster than Dijkstra.
- Game pathfinding: A* with Manhattan distance (grid) or Euclidean distance heuristic.
- Competitive programming: Know all five. The constraints tell you which to use.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Shortest Path Algorithms: Complete Comparison.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, shortest, path
Related Data Structures & Algorithms Topics