Quiz: the DP principle

Short questions to test your understanding. Try each on paper first, then open the answer. Paths go from the top row to the bottom row of a cost image, one row down per step and at most one column sideways; M(i, j) is the cost of the cheapest path to pixel (i, j).


Q1. State the principle of optimality for minimal paths in one sentence. Why does it let us keep only one number per pixel?

What could you do if a part of the best path were not the best way to its end pixel?

Every part of an optimal path is itself optimal: if the best path to the bottom passes through p, its part up to p is the cheapest path to p (otherwise we could swap in the cheaper part). So for continuing paths only the cheapest way to reach p matters, and one number per pixel, M(p), is enough.


Q2. Three cells above pixel (i, j) have M = 12, 9, 15, and C(i, j) = 4. What is M(i, j), and where does its pointer point?

M(i, j) = 4 + \min(12, 9, 15) = 13; the pointer points to the middle cell (straight up), the one with 9.


Q3. Why does the greedy path (cheapest first pixel, then always the cheapest pixel below) usually fail?

It decides each step by looking only at the next pixel. A cheap step can lead into an expensive region that cannot be left in time, because a path moves at most one column per row. DP avoids this by not committing to any step before the whole table is filled: the path is chosen from the end.


Q4. An image gets twice as many rows (same width). Roughly what happens to the number of paths, and to the work of DP?

About m \cdot 3^{n-1} paths, about 3nm operations.

The number of paths is roughly squared (divided by m): 3^{2n-1} instead of 3^{n-1}, so astronomically more. The DP work only doubles: it grows linearly with the number of pixels.


Q5. You need only the cost of the best path, not the path. How much memory does DP need? And if you need the path?

For the cost, one row of M is enough: each row is computed from the previous one only. For the path, you also need the pointers of every cell (or the whole table M), to backtrack from the end.


Q6. In the two-label chain of the graph-cut day (E = \sum_p D_p(L_p) + \lambda\,\#\{\text{label changes}\}), what are the stages, the states and the cost of a step? What is the “path”?

Stages: the pixels along the line. States: the two labels. Each pixel-and-label pays its data cost D_p(\ell), and a step that changes the label pays \lambda. The “path” is the labelling: one label per pixel. The same table, with 2 columns instead of m.


Q7. Why is one raster-order pass of the distance transform not enough, while one row-by-row pass is enough for monotone paths?

A monotone path only ever comes from above, so when row i is filled, everything it needs (row i-1) is final. For the distance transform, the shortest way to the nearest background pixel may come from below or from the right, which a top-to-bottom, left-to-right pass has not seen yet. Each pass is DP with a fixed order; neither order covers all paths.


Q8. (math) Paths go from the top-left to the bottom-right cell with steps right, down, or diagonally right-down. Write the recursion for M, and say in which order the table can be filled.

M(0, 0) = C(0, 0) and M(i, j) = C(i, j) + \min\big(M(i-1, j),\ M(i, j-1),\ M(i-1, j-1)\big), with M = \infty outside the grid. Row by row, left to right (or column by column): all three cells on the right-hand side come earlier. This is the table that aligns two signals (dynamic time warping).