Learn
Course · Algorithms

Dynamic Programming and Optimal Substructure

Greedy Algorithms and Matroids proved a rule correct wherever the structure allowed it. This course is what to do where it does not: the same optimal substructure, without the greedy choice property, evaluated by filling a table. Every page runs the table against at least one route that has no table in it — an exhaustive search, a memo-free recursion, or a re-costing of the object that came back — because a filled table is the easiest thing on this path to get confidently wrong.

optimal substructure an optimum contains an optimum of the part overlapping subproblems 22 089 calls, 18 distinct arguments a table, an ORDER, a recurrence the order is the part that fails silently an unwritten cell reads as nothing, and nothing plus a number is a number n²2ⁿ beats (n−1)! only from ten cities up
What this course is about
Subject
Algorithms
Lessons
12
Format
Interactive

Overview

What you will be able to do

By the end you can turn a problem into a table by naming its state, prove the recurrence by substitution, derive the fill order from the reads rather than from habit, and check every object a table hands back by a route that shares nothing with it.

1

Name the state, and know what you have chosen

A subproblem is an index: a remaining amount, a pair of prefixes, an interval, a vertex with a yes-or-no, a subset with an end point. The choice fixes the table's size and the bound, and two correct choices for one problem can differ by a factor of four in cells.

2

Prove the recurrence and derive the order from it

Discharge optimal substructure by substituting a better part and deriving a contradiction, then read the fill order off the cells each cell reads — and check it by counting the reads that landed on cells not yet written, where the only acceptable count is zero.

3

Check what a table hands back without using the table

Re-weigh a set of items against the capacity it claims to fit, re-cost a bracketing from the dimensions, perform an edit script character by character, close a tour before measuring it, and test an independent set against the edge list as it was typed.

4

Put a measured count beside a proved bound and say where they part

Four pages here have the asymptotically better method losing on the instance shown, by factors from 1.2 to more than forty; two have the same recurrence in two loop orders at identical cost answering different questions. Neither number is wrong and neither is the other.

Lessons

12 lessons

The recurrence first, on the smallest problem where a greedy rule already fails; then the memo and the table as two ways of evaluating it; then the fill order, twice, because that is the part that fails without saying so; then two-axis grids and the objects read back out of them; then sequences and counting, where the loop order decides the question rather than the cost; and last, three tables indexed by a tree, by the positions of a game, and by subsets.

Where the Greedy Choice FailsRun the fewest-coins recurrence on a coin system where taking the largest coin first is already wrong, and count all three routes to the answer.Not marked
Overlapping Subproblems and the MemoCount the calls the same recursion makes with and without a memo, and find the number of distinct subproblems that explains the gap.Not marked
The Fill Order Is Part of the AlgorithmFill the matrix-chain table row by row instead of by interval length, and read the cheaper, wrong answer it finishes with.Not marked
Every Bracketing, and the One the Table FoundEnumerate all 132 bracketings of seven matrices, read the table's answer back as a tree, and re-cost that tree from the dimensions alone.Not marked
The Knapsack Table, and What Each Cell ReadsFill the 0/1 knapsack grid, watch the two cells each cell reads, and re-weigh the items the reconstruction hands back.Not marked
One Row, and the Direction of the LoopCollapse the knapsack grid to a single row and watch the loop direction decide whether each item may be taken once or any number of times.Not marked
Edit Distance, and Performing the ScriptFill the edit-distance table for two words, read the edit script back out of it, and carry that script out on the first word character by character.Not marked
The Tails Array Is Not the SubsequenceRun both longest-increasing-subsequence algorithms on one sequence, check what each returns against two properties, and find where the faster one stops being an answer.Not marked
Combinations, or Ordered SequencesSwap the two loops of the coin-counting table and watch the answer change from 541 to a twenty-three-digit number, at identical cost.Not marked
Dynamic Programming on a TreeCompute two numbers per vertex in one postorder pass, and check the chosen set against every subset and against the edge list.Not marked
Won and Lost PositionsLabel every position of a subtraction game from the definition alone, find the period in the labels, and say what the table can and cannot prove about it.Not marked
Subsets as a Table, and Where the Table LosesSolve the travelling salesman exactly by indexing a table with subsets, then compare its work with trying every tour and find the size at which the table finally wins.Not marked

Recommended background

Discrete Mathematics in full, and by name: Dynamic Programming and Greedy Algorithms, where both were met on one example each; Strong Induction, which every correctness proof here uses; Recurrence Relations; Big-O, Big-Omega and Big-Theta; Analysing Iterative Algorithms; and Trees. From this path: Greedy Algorithms and Matroids, whose exchange arguments and fractional knapsack are the thing this course starts by breaking, and Graph Algorithms, for the relaxation step and for shortest paths in a directed acyclic graph, which is a dynamic program in the language of graphs.

Practice suggestions

Read every count as a measurement on the input on screen. The table is not the faster method on four of the twelve instances this course loads by default, and the bounds do not change between the instance where it wins and the instance where it loses. Every page prints both and names the gap.

Write the fill order down beside the recurrence. It is the third of the three parts and the only one that can be wrong while every symptom looks like success: “The Fill Order Is Part of the Algorithm” has a complete table, consistent arithmetic in every cell, and a number 41 per cent below the truth.

Treat the value and the object as two answers. A table can be right about what the optimum costs and wrong about what it is, and every reconstruction on this course is checked by a route that does not read the table — re-costing, re-weighing, or in one case running the answer as a program.

What it does not cover

The Bellman equations, policies, and discounting. Sequential decision under uncertainty is a dynamic program over states and actions, and it belongs to Operations Research rather than here; every problem on this course is deterministic and every table is filled once.

Convex hull and divide-and-conquer optimisations, the Knuth–Yao speedup, and the monotonicity conditions they need. They lower a Θ(n³) fill to Θ(n²) without changing a correctness argument, and none of the bounds this course states depends on them.

Bitmask dynamic programming beyond one example, and profile methods over grid columns. “Subsets as a Table, and Where the Table Loses” shows that a subset is a legitimate index; the catalogue of problems for which that is the right index is a different skill from proving a recurrence.

Space-efficient reconstruction. One row gives the value and cannot give the object, and recovering the object in linear space needs a divide-and-conquer argument that is stated nowhere here — the trade is named and the alternative is not built.