Bellman-Ford handles negative weights but pays for that generality. When every edge weight is guaranteed non-negative — true for almost every genuinely physical or economic "cost" (time, distance, a bid-ask spread, a transaction cost) — Dijkstra's algorithm exploits that guarantee with a greedy relaxation order, driven by a min-priority-queue, achieving with a binary heap. This note derives the algorithm, proves its correctness rests entirely on non-negativity, and shows precisely why negative weights break it.
In this lesson
- Learning objectives
- The algorithm
- Correctness: why non-negativity is essential
- Constructing a counterexample with negative weights
- Running time with a binary heap
Before you start
The full lesson, with worked examples and exercise solutions, is for members.
Read the full lessonNew to SelfMadeQuant.com? See membership options or explore the curriculum.