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

Single-Source Shortest Paths: Bellman-Ford and DAG Relaxation

BFS finds shortest paths by edge count on unweighted graphs; this note generalises to weighted edges, where the cost of a path is the sum of its edge weights, not just the number of hops. The unifying technique is relaxation: maintain an upper-bound estimate on each vertex's shortest distance, and repeatedly tighten it whenever a cheaper path is discovered. This note builds the relaxation template, then two specialisations: Bellman-Ford, which handles negative edge weights and detects when no shortest path even exists (a negative-weight cycle), and the much faster DAG algorithm, which reuses topological sort directly.

In this lesson

  1. Learning objectives
  2. Relaxation: the shared primitive
  3. Bellman-Ford
  4. The DAG special case

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.