Find the shortest path between two nodes in a graph. Β β± Unweighted:
O(V+E)Β· Weighted (Dijkstra):O((V+E) log V)Β πΎ SpaceO(V)
- Unweighted graph β BFS guarantees the shortest path (fewest hops) because it explores nodes level by level.
- Weighted graph (non-negative weights) β Dijkstra's algorithm uses a min-heap to always expand the cheapest known node.
- Negative weights β Bellman-Ford relaxes all edges V-1 times.
BFS shortest path:
queue = [(start, [start])] β (node, path_so_far)
while queue:
node, path = dequeue
if node == target: return path
for neighbour in graph[node]:
if not visited: enqueue (neighbour, path + [neighbour])
Graph (unweighted): BFS from A to F:
A Level 0: A
/ \ Level 1: B, C
B C Level 2: D, E, F β found F!
/ \ \
D E F Shortest path: A β C β F (2 hops)
Weighted graph (Dijkstra):
A --1-- B --4-- D
| |
2 1
| |
C ------3------ D
Dijkstra expands cheapest node first using heap
- BFS for shortest path: Konrad Zuse (1945), Moore (1959)
- Dijkstra's algorithm: Edsger W. Dijkstra (1956, published 1959) β one of the most elegant algorithms in CS
- Bellman-Ford: Richard Bellman (1958), Lester Ford Jr. (1956)
- A*: Hart, Nilsson, Raphael (1968) β adds a heuristic to guide Dijkstra toward the goal
| Graph Type | Algorithm | Time |
|---|---|---|
| Unweighted | BFS | O(V+E) |
| Non-negative weights | Dijkstra | O((V+E) log V) |
| Negative weights | Bellman-Ford | O(VE) |
| Grid (unit cost) | BFS | O(mΒ·n) |
| All-pairs | Floyd-Warshall | O(VΒ³) |
- For grids, BFS is almost always the answer
- Dijkstra requires a min-heap (
heapq) β each node is(cost, node_id)
- GPS turn-by-turn navigation
- Network routing protocols (OSPF uses Dijkstra)
- Game pathfinding (A* is standard in game engines)
- Social network "shortest connection path"
- Internet packet routing
- BFS β BFS for unweighted shortest path
- Grid Traversal β shortest path in a maze
- Topo Sort β longest path in DAG via topological sort
- Heap β min-heap used in Dijkstra