← chapter

Dijkstra's shortest path

Cheapest-cost paths over non-negative weighted edges. BFS with a priority queue instead of a plain queue.

Weights break BFS

Watch distances settle

Numbers in nodes = tentative distance (∞ → falls as edges relax). Green = settled · blue = frontier · grey numbers = edge weights.

The direct edge is a trap

Relaxation + non-negativity

Takeaway

The heaps chapter pays off here: the priority queue is the whole trick. networkx.single_source_dijkstra in production; A* and Prim are this + one idea. Next: Bellman-Ford, for negative weights.