Lecture notes · Algorithms · Lecture 15

Dynamic Programming, Part 1: SRTBOT, Fib, DAGs, Bowling

MIT 6.006 Introduction to Algorithms, lecture 15, 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 dynamic programming as a way to design recursive algorithms, using a six-step checklist called SRTBOT and a first example with Fibonacci numbers.

Key ideas

Words to know

subproblem
A smaller version of the problem you are trying to solve, which you solve first so you can use its answer to build the bigger answer.
recurrence relation
A rule that says how to get the answer to one problem from the answers to smaller problems.
topological order
An order to solve the subproblems so that every subproblem is solved before any problem that depends on it. Think of it as a safe order to do the work.
memoization
Saving the answer to a piece of work the first time you do it, so you can reuse it later instead of repeating it.

Example

Imagine planning a long trip by working out the cost of each leg first, then using those costs to get the total. You never redo a leg you already priced, and you always price the small legs before the whole trip. That is the same idea as solving subproblems in order and reusing their answers.

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

Part 2 · 10:00–20:00

This part shows why the simple recursive way to compute Fibonacci numbers is too slow, and how remembering past answers (memoization) makes it fast.

Key ideas

Words to know

Memoization
Writing down answers to smaller problems the first time you solve them, so you can look them up later instead of solving them again.
Subproblem
A smaller version of the main problem. Solving the smaller ones helps you solve the bigger one.
Base case
The simplest case, where the answer is already known and no further steps are needed.
Exponential growth
A quantity that multiplies by the same amount at each step, so it gets huge very fast. In algorithms, it means the work grows far too quickly as the input gets bigger.

Example

Imagine you are doing a long math homework set where many questions need the same intermediate answer. If you write that answer on a notepad the first time, you can just read it off the notepad every time after that, instead of working it out again.

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

Part 3 · 20:00–30:00

This part shows how saving answers you already computed (memoization) can turn a very slow recursive method into a fast one, using Fibonacci numbers, merge sort, and shortest paths in a graph as examples.

Key ideas

Words to know

memoization
Saving the answer to a problem the first time you solve it, so you can look it up instead of solving it again.
sub-problem
A smaller version of the main problem that you solve as one step on the way to the full answer.
exponential
A running time that grows so fast that each extra input roughly multiplies the work, so it gets huge quickly.
polynomial
A running time that grows like n raised to some fixed power, which grows much more slowly than exponential time.

Example

Imagine working out a long family tree, where you need the same grandparent's details many times. If you write each answer on a sticky note the first time and just read the note when you need it again, you avoid repeating the same work. That sticky-note habit is memoization.

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

Part 4 · 30:00–40:00

This part shows that a shortest-path problem on a directed graph with no cycles can be solved by writing one recursive rule and then computing answers in a safe order, and it introduces a bowling game with a product scoring rule.

Key ideas

Words to know

topological order
A list of the vertices in a graph where each vertex comes before the ones it points to, so you can solve things in a safe order.
DAG
Directed acyclic graph: a set of points joined by one-way arrows, with no loop that ever brings you back to where you started.
memo table
A notebook where you write down each answer after you work it out, so you can look it up instead of working it out again.
relax
To check whether a new route to a point is shorter than the best one found so far, and if it is, to update the record with the shorter route.

Example

Think of a set of one-way roads that lead into a town. To find the quickest way to reach the town, you check each road that leads into it, add the travel time from where you started, and keep the shortest total. This matches the rule that the best path to v is the best of the paths coming into v.

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

Part 5 · 40:00–50:00

This part shows how to break a bowling scoring puzzle into smaller pieces, using suffixes of the pin line as the sub-problems, and how to write the rule that links them.

Key ideas

Words to know

suffix
The part of a list from some position to the very end. For example, the last three items of a list.
subsequence
A list made by picking items from the original in order, where you may skip items anywhere in the middle.
sub-problem
A smaller version of the main puzzle that you solve first, so you can use its answer to solve the bigger one.
recurrence relation
A rule that says how the answer to one sub-problem is worked out from the answers to smaller sub-problems.

Example

Imagine a line of people waiting for a bus, and you must decide what to do with each person in turn. If you only care about the people from the middle of the line to the back, you can solve that smaller line first, then add the front person's choice on top. This is like working with a suffix: cut off the front, solve the rest, and build the answer back up.

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

Part 6 · 50:00–57:10

This part shows how a bowling-pin scoring problem can be solved quickly by working through smaller leftover groups of pins, starting from the end and filling in answers as you go.

Key ideas

Words to know

suffix
The part of a list that is left after you remove some items from the front. For example, pins 3 to 10 are a suffix of pins 1 to 10.
topological order
An order to solve smaller problems so that each one is solved before any problem that depends on it.
memoized
Saving the answer to a smaller problem the first time you compute it, so you can reuse it instead of working it out again.
local brute force
Trying every possible choice for one item, while reusing saved answers to the smaller problems that come after it, so the total work stays small.

Example

Imagine a line of coins, and you can take one coin or two neighboring coins, but you want the most total value. Ask yourself only one question at a time: what is the best you can do with the coins that remain after the first choice? Save those answers and build up from the end of the line, so each small answer is ready when you need it.

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

← Lecture 13: Dijkstra · Lecture 16: Dynamic Programming, Part 2: LCS, LIS, Coins →

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