Find shortest paths using BFS (unweighted), Dijkstra (weighted), and Bellman-Ford.
Shortest path algorithms find the minimum cost path between nodes. BFS finds the shortest path in unweighted graphs in O(V+E). Dijkstra uses a priority queue for weighted graphs with non-negative edges in O((V+E) log V). Bellman-Ford handles negative weights in O(VE).
BFS O(V+E) · Dijkstra O((V+E) log V) · Bellman-Ford O(VE)
O(V) for distance array
Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.
works for unweighted graphs; each level = one step
greedy algorithm; always expand the cheapest unvisited node
use a priority queue keyed by distance for Dijkstra
update dist[v] if dist[u] + weight(u,v) < dist[v]
relax all edges V-1 times; detects negative cycles
all-pairs shortest path in O(V³)