Lecture notes · Algorithms · Lecture 16

Dynamic Programming, Part 2: LCS, LIS, Coins

MIT 6.006 Introduction to Algorithms, lecture 16, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part introduces the longest common subsequence problem and shows how to set up its subproblems for two sequences at once.

Key ideas

Words to know

subsequence
A list of letters or items taken from a sequence in the same order, where you may skip some of them.
substring
One unbroken piece of a sequence, with no skipped letters.
memoize
Save the answer to each smaller problem the first time you solve it, so you never solve the same one twice.
topological order
An order to solve the subproblems so that every smaller problem is finished before any problem that depends on it.

Example

Think of the word hello hidden inside the longer words hieroglyphology and Michelangelo. You can find its letters in both words, in order, while skipping other letters in between. That is the idea of a subsequence: the letters keep their order, but gaps are allowed.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part shows how to find the longest shared sequence of letters in two words by comparing them one letter at a time, from the end toward the start.

Key ideas

Words to know

subsequence
A list of letters taken in order from a longer string. The letters do not have to sit next to each other, but they must keep their original order.
suffix
The part of a string from some position to the end. For example, the suffix starting at position 2 is everything from position 2 onward.
subproblem
A smaller version of the original problem that we solve on the way to solving the big one. Here, each one asks for the answer on a pair of suffixes.
parent pointers
Arrows drawn from each answer back to the smaller answer it came from, so you can trace which choices built the final result.

Example

Imagine two friends each writing down a list of their favorite songs, and you want the longest run of songs that appear in both lists in the same order. At each step you compare the next song on each list: if they match, you keep it, and if not, you try skipping one list's song and then the other, and keep whichever path gives the longer shared list.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part shows how to get the actual answer, not just its length, from a dynamic programming table, and then sets up the longest increasing subsequence problem.

Key ideas

Words to know

subsequence
A list you make by keeping some items from a sequence, in their original order, and dropping the others. You do not have to keep items next to each other.
suffix
The part of a sequence from some position to the end. For example, the suffix starting at position 3 is everything from position 3 onward.
parent pointers
Notes saved in each table cell that say which neighboring cell its best value came from. Following them backward retraces the best choices.
topological order
An order for doing the work so that every smaller piece is finished before any bigger piece that depends on it.

Example

Imagine picking a group of kids from a line so that their heights get taller each time, going from left to right. You can skip kids, but you cannot reorder them, and each pick must be strictly taller than the last. The task is to find the biggest group you can pick that way.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part shows how to find the longest increasing subsequence by asking, for each starting letter, what comes next, and trying every choice.

Key ideas

Words to know

subproblem
A smaller version of the original problem that we solve on its way to the full answer.
brute force
Trying every possible choice instead of guessing or using a clever shortcut, so that you are sure to find the best one.
base case
The simplest starting answer that we know for sure, which the rest of the answers are built on.
strictly increasing
Each letter in the sequence must be bigger than the one before it, with no ties allowed.

Example

Imagine a line of people standing in a row, each with a height, and you want the longest group you can pick who get taller as they go. You try each person as the first pick, then for each one you check every person after them who is taller, and you keep the longest group you find.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

This part shows that some dynamic programming problems can also be solved as shortest path problems, and then works through a two-player coin game using substrings and whose turn it is.

Key ideas

Words to know

DAG
A directed acyclic graph: a set of dots joined by one-way arrows, with no loops that let you go around in a circle.
shortest path
The route between two points that adds up to the smallest total cost, such as the least distance or the lowest weight.
subproblem
A smaller version of the main problem that you solve first, so you can use its answer to solve the bigger one.
substring
A continuous piece taken from the middle or ends of a list, with no gaps.

Example

Picture a row of bread rolls on a table, and two friends take turns grabbing a roll from either end of the row. After a few grabs, the rolls left over form a single block in the middle, so you need to think about that middle block, not just the rolls at one end.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–58:36

This part explains how to write a recurrence (a step-by-step rule for solving a problem from smaller pieces) for a two-player coin game, where each player takes a coin from either end of a row.

Key ideas

Words to know

subproblem
a smaller version of the original problem that you solve first, so you can use its answer to solve the bigger one.
zero-sum game
a game where whatever one player gains, the other player loses, so the two scores always add up to the same total.
recurrence relation
a rule that says how to find the answer to a problem from the answers to smaller problems.
parent pointers
arrows that record which choice gave the best answer at each step, so you can trace back the best strategy.

Example

Imagine two friends taking cookies from either end of a row, each wanting as many as possible. Once one friend takes a cookie, the other friend is the one to choose next, and they will try to take the best cookie for themselves. Thinking about what the other friend will do is what leads to using min in the rule.

3 checked practice questions for this part are in the app.

← Lecture 15: Dynamic Programming, Part 1: SRTBOT, Fib, DAGs, Bowling

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York