Zain Dana Harpernight school / A27

Dynamic programming. Remember what you already worked out.

How many single-letter edits turn one word into another? Rerun tries every way and counts the calls, then writes each answer down once in a table, the idea Richard Bellman named dynamic programming.

The film

About 4:15, hosted by Rerun. It asks you one question and waits for your answer before it shows you. The guide's voice is not recorded yet, so for now the film plays with captions; the full words are also below.

Nothing loads until you press play.

The lab: the tide table

Type two strings and fill the table square by square, then trace the edits back from the corner; or try every way, with every call counted, for strings short enough to finish. In the film, kitten and sitting took 29,737 calls the slow way and 56 squares in the table; at 13 characters, 2,114,900,428 calls against 196 squares.

Everything is computed on your computer, in this page. Nothing is sent anywhere.

Everything the film says

Dynamic programming

Dynamic programming: remember what you already worked out.

Some problems are made of smaller problems that keep coming back. Tonight Rerun learns to solve each one once and write the answer down.

How different are two words

How different are two words? Count the fewest single-letter edits that turn one into the other: put a letter in, take one out, or change one.

Kitten becomes sitting in 3: change the k to an s, the e to an i, and add a g. This count is called the edit distance.

Try every way

The obvious way is to try everything. Look at the last letters: either delete one, insert one, or match or change them, and work out the rest of each case the same way.

That works, but the same smaller questions come up again and again. For kitten and sitting, the recursion made 29,737 calls. For intention and execution, 2,193,844.

Two phrases of thirteen characters. How many calls does trying every way make? (the film pauses here)

Too many calls

For two phrases of 13 characters, it made 2,114,900,428 calls and took 11,327 milliseconds on this computer. Each extra letter multiplies the work by about five and a half, and the factor keeps rising.

Yet there are only 196 different questions in the whole problem, the big one included. The rest is repetition.

The tide table

So write each answer down once, in a table: one row for each letter of the first word plus one for the empty start, and the same for the columns.

Each square needs only three others: the one above, the one to its left, and the one diagonally above and left. Fill them in order and the bottom corner is the answer. For kitten and sitting, 56 squares instead of 29,737 calls.

Robert Wagner and Michael Fischer published this table for strings in 1974, and others found it independently.

Reading the way back

The table also says which edits to make. From the bottom corner, step back to whichever neighbour gave its value, and each step is one edit or one match.

Halve the beach and half the beach are 2 apart: drop the v, and change the e to an f.

A shortcut that fails

Why not just walk along both strings and fix each difference as you meet it? We tried that. It got both classic word pairs right.

It was wrong on all 6 of our guide sentences, and too high on 576 of 1,000 random pairs. A test with only the textbook examples would have passed it.

The idea

Richard Bellman chose the name dynamic programming around 1950, for problems solved in stages. Today it means: when a big problem is built from smaller ones that overlap, solve each smaller one once and keep the answer.

The same idea finds shortest routes and decodes speech.

Goodnight

When the same question keeps coming back, write the answer down.

Goodnight.

Sources

Every claim in the film, with where it comes from and what that source does not prove. The numbers about the model are computed from its weights, which you can load and check: facts.json.

What this lesson leaves out

Unit costs: inserting, deleting or changing a letter each costs 1.

Call counts for long strings come from the recursion's own formula, checked against the running counter up to 13 characters.

Wagner and Fischer's paper and Bellman's book were checked by their bibliographic records; the naming date comes from Dreyfus's 2002 account.

Before this: Search and sorting (A26).