DSA Notes
Master all shortest path algorithms — BFS for unweighted graphs, Dijkstra for non-negative weights, Bellman-Ford for negative weights, Floyd-Warshall for all-pairs, DAG shortest paths, A* heuristic search, and a decision guide for choosing the right algorithm for every scenario.
Overview
Finding shortest paths in graphs is one of the most fundamental problems in computer science. The right algorithm depends on:
- Is the graph weighted or unweighted?
- Are there negative edge weights?
- Do you need one source or all pairs?
- Is the graph a DAG?
Algorithm 2 — Dijkstra (Non-Negative Weights)
Use when: Non-negative edge weights, single source.
Algorithm 3 — Bellman-Ford (Negative Weights)
Use when: Negative edge weights, single source, need negative cycle detection.
Algorithm 4 — Floyd-Warshall (All Pairs)
Use when: Need shortest path between ALL pairs of vertices.
Algorithm 5 — DAG Shortest Path (Topological + DP)
When the graph is a DAG, we can find shortest paths in O(V+E) — faster than Dijkstra:
Use when: Graph is a DAG — handles negative weights in O(V+E).
Algorithm 6 — A* Algorithm (Heuristic Search)
A* is Dijkstra enhanced with a heuristic that estimates remaining distance to the target:
Use when: Single target, admissible heuristic available (GPS navigation, game AI).
Algorithm Selection Guide
| YES | BFS O(V+E) — simple, optimal |
| YES | DAG Shortest Path O(V+E) — handles negative weights too! |
| YES | Bellman-Ford O(VE) — detects and reports negative cycles |
| NO | Bellman-Ford O(VE) — still correct, just slower |
| YES | Dijkstra O((V+E) log V) — fastest for non-negative weights |
| Small graph (V ≤ 1000)? | Floyd-Warshall O(V³) |
| Large sparse graph? | Dijkstra × V times O(V(V+E) log V) |
| YES | A* — often much faster than Dijkstra in practice |
Comparison Table
| Algorithm | Type | Time | Space | Neg Weights | Neg Cycle | All Pairs |
|---|---|---|---|---|---|---|
| BFS | Unweighted | O(V+E) | O(V) | ❌ | ❌ | ❌ |
| Dijkstra | Non-neg weighted | O((V+E)logV) | O(V) | ❌ | ❌ | ❌ |
| Bellman-Ford | Any weighted | O(VE) | O(V) | ✅ | ✅ | ❌ |
| DAG SP | DAG (any weight) | O(V+E) | O(V) | ✅ | N/A | ❌ |
| Floyd-Warshall | Any weighted | O(V³) | O(V²) | ✅ | ✅ | ✅ |
| A* | Non-neg + heuristic | O(E log V) | O(V) | ❌ | ❌ | ❌ |
Summary
Choosing the right shortest path algorithm is critical for efficiency:
| Scenario | Algorithm |
|---|---|
| Unweighted graph | BFS |
| Non-negative, single source | Dijkstra |
| Negative weights, single source | Bellman-Ford |
| DAG with any weights | Topological + DP |
| All pairs | Floyd-Warshall |
| Single target + heuristic | A* |
*Next Lesson: Union-Find DSU*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Shortest Path Algorithms — Complete Comparison and Selection Guide.
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