'Shortest' Means 'Cheapest', Not 'Fewest Hops'
Plain BFS finds the path with the fewest edges โ perfect when every step costs the same. The moment edges carry different weights (time, money, effort), the path with the fewest hops is no longer guaranteed to be the cheapest one. A 1-hop route that costs 50 can lose to a 3-hop route that costs 12.
Relaxation is the one operation this whole family is built on: for an edge (u โ v) with weight w, check whether going through u gives a cheaper way to reach v than whatever we currently believe is v's best distance โ if dist[u] + w < dist[v]: dist[v] = dist[u] + w. Every algorithm below is just a different strategy for deciding which order to relax edges in.
A priority queue (min-heap) is what lets Dijkstra always relax the cheapest known frontier node next, instead of processing nodes in arbitrary or arrival order.
Interactive Weighted Grid (Dijkstra)
Every open cell shows its cost (1-9) to enter. Click cells to toggle walls. Watch the priority queue always pop the cheapest known frontier cell next โ not just the nearest one by hop count.
Simulation Status
Click Play or step forward to start.
Which Algorithm, and When
How to choose: unweighted graph โ BFS. Non-negative weights โ Dijkstra. Negative edges allowed โ Bellman-Ford. Need every pair's shortest path โ Floyd-Warshall. Graph is a DAG โ topological order + relax (fastest of all).
| Algorithm | Use When | Time | Space | Why |
|---|---|---|---|---|
| BFS | Unweighted (all edges cost 1) | O(V + E) | O(V) | First visit is always the shortest, by construction. |
| Dijkstra | Non-negative weights | O((V+E) log V) | O(V + E) | Greedily finalizing the cheapest frontier node is provably safe โ as long as nothing can ever get cheaper later. |
| Bellman-Ford | Negative edges (and cycle detection) | O(V ยท E) | O(V) | Relaxes every edge, order-independent, V-1 times โ enough passes for the truth to fully propagate. |
| Floyd-Warshall | All-pairs shortest paths | O(Vยณ) | O(Vยฒ) | DP over "allowed intermediate nodes" โ simple triple loop, no queue needed. |
| Topological + Relax | DAG (no cycles) | O(V + E) | O(V) | Process nodes in dependency order โ one relax pass per edge is enough, no heap needed. |
Golden Templates
1. BFS Distance (Unweighted)
2. Dijkstra (Priority Queue)
3. Bellman-Ford (Negative Edges OK)
How to Spot This Pattern
๐ "Minimum cost / time / effort" to get from A to B.
๐ "Cheapest flights within K stops", "network delay time".
๐ "Path with minimum effort" or "path with maximum probability".
๐ Edge weights are explicitly NOT all equal โ plain BFS would give the wrong answer.
Named problems: Network Delay Time ยท Cheapest Flights Within K Stops ยท Path With Minimum Effort ยท Swim in Rising Water
Common Mistakes
Dijkstra with Negative Edges
Dijkstra assumes a finalized node's distance can never improve later. A negative edge breaks that assumption and produces a wrong answer silently โ no error, just an incorrect result.
Forgetting to Skip Stale PQ Entries
A node can be pushed onto the priority queue multiple times with different distances before it's finalized. Always check if (d > dist[u]) continue; after popping โ the demo above flashes exactly this moment.
BFS on a Weighted Graph
BFS finds the path with the fewest edges, not the cheapest one. The moment weights differ, a plain queue gives the wrong "shortest" path.
O(Vยฒ) Matrix on a Sparse Graph
Scanning a full VรV adjacency matrix to find the next cheapest node works, but wastes time on graphs where E is much smaller than Vยฒ. An adjacency list + priority queue is the faster default.
Ready to Practice?
"Explore the cheapest, trust the order, and never look back once the distance is locked."