The DP principle: minimal paths on a grid

Image analysis

About this lecture

Many image problems ask for the cheapest path: a boundary along dark pixels, a seam through an image, an alignment of two signals. There are far too many paths to try them all. Dynamic programming (DP) finds the best one exactly, with a table and a few additions.

  • The question: the cheapest path from the top row to the bottom row of a cost image.
  • Why trying all paths fails, and why the greedy choice fails.
  • The key idea: an optimal path is made of optimal sub-paths.
  • Fill a table row by row, then backtrack to read off the path.
  • The general recipe, and two old friends that are the same recursion: the two-label chain of the graph-cut day and the distance transform.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

Builds on: Day 27: Graph cuts.

The question

A small cost image: each pixel has a cost (light = cheap). Find the cheapest path from the top row to the bottom row that moves one row down per step, one column left, straight or right (the three blue arrows).

The cost of a path is the sum of its five pixels.

Try to guess the best path before the next slide.

A 5 by 5 grid of costs drawn as grey squares (light = cheap) with the numbers 7 4 8 8 3 / 2 9 3 8 2 / 3 1 3 6 8 / 5 1 7 3 2 / 9 2 3 9 6; from one cell three arrows point to the cells below-left, below and below-right.

Why not try all paths?

  • From each pixel there are 3 ways down (2 at the image border). A path through n rows has n - 1 steps, so there are about m \cdot 3^{n-1} paths in an image with m columns.
  • 5 \times 5: 259 paths (405 without the border effect). Still possible by hand, barely.
  • 100 \times 100: about 1.5 \cdot 10^{49} paths.
  • 256 \times 256: about 10^{124} paths, more than the roughly 10^{80} atoms in the observable universe.

Trying them all is hopeless. We need a way to reuse work: the paths overlap almost everywhere.

Greedy fails

The obvious shortcut: start at the cheapest pixel of the top row and always step to the cheapest of the three pixels below.

  • In the example: start at 3 (top right), then 2, 6, 2, 6: 19.
  • The best path costs 10: 4, 2, 1, 1, 2. Its first pixel (4) is not the cheapest one.

A cheap first step can lead into an expensive region. To decide the first step well, we must know what comes later. Dynamic programming turns this around: it works out the best way to reach every pixel, so no decision is made too early.

The cost grid with two paths: the best path (orange, solid) through 4, 2, 1, 1, 2 with cost 10, and the greedy path (blue, dashed) down the right edge through 3, 2, 6, 2, 6 with cost 19.

The key idea: optimal sub-paths

Suppose the best path to the bottom passes through pixel p. Then its part from the top to p must be the best path to p: if there were a cheaper way to reach p (dashed), we could swap it in and get a cheaper path to the bottom.

So we only ever need to remember one number per pixel:

M(p) = the cost of the cheapest path from the top row to p.

and, for pixel p in row i, it is known as soon as row i - 1 is known:

M(p) = cost of p + the smallest M among the (up to) three pixels above it.

This is Bellman’s principle of optimality (1957).

Schematic: a path from the top row to the bottom row passes through a pixel p; a dashed alternative route from the top to p is labelled 'cheaper way to p?'.

The math: the Bellman recursion

Cost image C(i, j), rows i = 0, \dots, n-1, columns j = 0, \dots, m-1. A path is j_0, j_1, \dots, j_{n-1} with |j_{i} - j_{i-1}| \le 1, cost \sum_i C(i, j_i).

M(0, j) = C(0, j), \qquad M(i, j) = C(i, j) + \min_{d \in \{-1, 0, 1\}} M(i-1, j+d) \quad (i \ge 1),

with M = \infty outside the image. Store the minimising d as a pointer B(i, j). The optimal cost is \min_j M(n-1, j); the path follows the pointers back from that cell.

Why it is correct (by induction on i): every path to (i, j) comes from one of the three cells above, and its cost is C(i, j) plus the cost of its part up to row i - 1, which is at least M(i-1, j+d). The recursion takes the best of these lower bounds, and it is reached by extending the optimal path to that cell.

Filling the table, then backtracking

Left: the 5 by 5 cost grid. Right: the filled table M with an arrow from each cell to the cell above that gave its minimum; the smallest value in the last row is 10, and the backtracked path through columns 1, 0, 1, 1, 1 is drawn in orange; the greedy path (cost 19) is dashed on the cost grid.
  • Row 0 of M copies the costs. Row 1: M(1,0) = 2 + \min(7, 4) = 6; M(1,1) = 9 + \min(7, 4, 8) = 13; …
  • Each cell also remembers which cell above gave the minimum: a pointer.
  • At the end, the smallest M in the last row is 10. Follow the pointers back up: columns 1, 0, 1, 1, 1.

The DP table as pseudocode

M(0, j) = C(0, j) for every column j
for i = 1 .. n − 1:                          # row by row: the order
    for every column j:
        M(i, j) = C(i, j) + min of M(i − 1, j + d), d in −1, 0, 1
        B(i, j) = the d that gave the minimum   # the pointer
j = the column of the smallest M(n − 1, ·)
for i = n − 1 down to 0:  path(i) = j;  j = j + B(i, j)
  • Two loops fill the table; one loop reads the path back.
  • The only rule about the order: row i - 1 must be finished before row i.

Try it: the table step by step

What it costs

  • Each cell looks at (at most) 3 cells above: one addition and one comparison each. An n \times m image needs about 3nm operations.
  • 5 \times 5: 52 operations instead of 259 paths. 256 \times 256: about 195 000 operations instead of 10^{124} paths.
  • Memory: the whole table and the pointers to recover the path; if only the cost is wanted, the previous row is enough.

The trick is the overlap: the cheapest way to each pixel is computed once and reused by every path that continues from it.

The math: counting paths and operations

Let N(i, j) be the number of paths from row 0 to (i, j). Then N(0, j) = 1 and N(i, j) = \sum_{d} N(i-1, j+d): the same recursion with (+, \text{sum}) in place of (+, \min).

  • Every cell has between 2 and 3 cells above it (for m \ge 2), so m \cdot 2^{n-1} \le \sum_j N(n-1, j) \le m \cdot 3^{n-1}: exponential in n.
  • The table needs one candidate per (cell, step): (n-1)(3m - 2) evaluations (the two border cells of each row have 2 candidates). Linear in the number of pixels.
  • With 2\delta + 1 allowed steps instead of 3: O(nm\delta) work, about m(2\delta+1)^{n-1} paths.

The recipe

Dynamic programming in four steps:

  1. Stages and states: here the rows are the stages and the column of the path is the state.
  2. A recursion: the best value of a state from the best values of the previous stage.
  3. An order in which every right-hand side is known before it is needed: here, row by row.
  4. Backtracking: store where each minimum came from, and follow it back from the best final state.

Step 3 is the condition that matters later: if no such order exists, plain DP does not work (the live-wire unit).

Same recursion, other states: the two-label chain

Recall from the graph-cut day (unit Segmentation as energy minimisation): the best labelling of a 1D profile, E(L) = \sum_p D_p(L_p) + \lambda\,\#\{p : L_p \ne L_{p+1}\}, solved exactly by DP.

  • Stage = pixel p, state = its label 0 or 1. The table has two numbers per pixel.
  • Transition cost \lambda when the label changes: a “path” through a 2-column grid with a step penalty.
  • This is the Viterbi algorithm; the same idea decodes signals and aligns speech.
Five pixels as stages, each with two nodes (label 0 and label 1); a labelling is a path through these nodes, here 0, 0, 1, 1, 1, and the step where the label changes costs λ.

Try it: the two-label chain

Same recursion, other steps

The recursion does not care what the steps are, as long as each one goes back in the filling order.

  • One row down, up to \delta columns sideways, with a penalty \alpha |d| for a step of d columns: smoother paths. The next unit uses this for closed boundaries.
  • Right or down (top-left to bottom-right): M(i, j) = C(i, j) + \min\big(M(i-1, j),\ M(i, j-1)\big). Fill row by row, left to right. In the demo: moves → ↓. Adding the diagonal step gives the table used to align two signals (dynamic time warping).
  • Seams through an image (seam carving) are the ↙↓↘ case on an energy image.
  • Matching two image rows pixel by pixel (scanline stereo) is the same table: stereo, day 53.
Three step sets drawn on small grids: five arrows into a cell from up to two columns left or right in the row above, with penalties +1α and +2α for the sideways steps; arrows from the left and from above; arrows from the left, from above and from the upper-left diagonal.

Same recursion, other order: the distance transform

Recall from the morphology day: the two-pass (chamfer) distance transform. Its first pass is

D(i, j) = \min\big(D(i, j),\ D(i-1, j) + 1,\ D(i, j-1) + 1\big)

in raster order: a Bellman recursion where the “previous stage” is the pixel above and the pixel to the left.

  • One pass is not enough: a pixel whose nearest background lies below or to the right gets the wrong value (shaded). A second pass in the opposite order fixes this case.

Two 6 by 6 grids of city-block distances to two background pixels (black, at the top-left corner and at row 4, column 4). Left, after one forward pass: the cells above and left of the second background pixel are too large (shaded, e.g. 8 instead of 2). Right: the true distances.

  • So what happens when the best path may go up, down, left and right, and no fixed order works at all? That is the live-wire unit: let the algorithm discover the order.

When is DP exact?

Three graphs: a chain of four nodes and a tree of six nodes (DP is exact on both), and a 3 by 3 grid graph with one of its square loops highlighted.
  • DP is exact when the problem is a chain (one state per stage, each stage talks only to the next) or a tree, so that there is an order in which everything needed is already known.
  • A 2D labelling with neighbours in both directions is not a chain: the grid has loops. The graph-cut day solves binary 2D labellings exactly in another way (max-flow, for submodular energies).
  • A boundary that can be written as one value per stage (one column per row, one radius per angle) is a chain, so DP finds the global optimum, however many local minima the image has.

Summary

  • The cheapest path through a cost image cannot be found by trying all paths (\sim 10^{124} for 256 \times 256) or greedily.
  • Principle of optimality: the best path to the end is made of best paths to its intermediate pixels.
  • Fill the table M = cost + the smallest M above, row by row; store pointers; backtrack from the best final cell.
  • About 3nm operations, exact.
  • The recipe (stages, states, recursion, order, backtracking) also gives the two-label chain (Viterbi) and the passes of the distance transform.