Skip to content
Data Structures and Algorithms / Shortest Paths, Spanning Trees, and Weighted Graphs

Dijkstra's Algorithm

Bellman-Ford handles negative weights but pays O(VE)O(VE) 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 O((V+E)log⁡V)O((V+E)\log V) 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

  1. Learning objectives
  2. The algorithm
  3. Correctness: why non-negativity is essential
  4. Constructing a counterexample with negative weights
  5. Running time with a binary heap

Before you start

The full lesson, with worked examples and exercise solutions, is for members.

Read the full lesson

New to SelfMadeQuant.com? See membership options or explore the curriculum.