Defining states and transitions
About this lesson
Every dynamic programming solution has two parts. A state is a small question with a stored answer. A transition builds one state's answer from smaller ones. This lesson takes a row of coins (2, 7, 9, 3, 1): take any coins, but never two that sit side by side, and find the largest total. You name the state, write the transition, fill the table in a safe order, and walk back to recover the coins you took. The Software lesson "Dynamic programming" covers memoisation and tables. This lesson is about how to choose what goes in them.
Coins, never two neighbours
Five coins in a row: 2, 7, 9, 3, 1. Take any you like, but never two side by side. What is the largest total? Checking every pick works but explodes: 13 legal picks here, and about 1.6 times more for each extra coin.
coins v = [2, 7, 9, 3, 1] rule: never take two neighbours subsets of 5 coins: 2^5 = 32 legal picks (no two neighbours): 13 the count for n coins is F(n + 2), a Fibonacci number: n = 5, 10, 20, 30 → 13, 144, 17711, 2178309 it grows about 1.618 times per extra coin
Read the lesson step by step
Coins, never two neighbours
Five coins in a row: 2, 7, 9, 3, 1. Take any you like, but never two side by side. What is the largest total? Checking every pick works but explodes: 13 legal picks here, and about 1.6 times more for each extra coin.
coins v = [2, 7, 9, 3, 1] rule: never take two neighbours subsets of 5 coins: 2^5 = 32 legal picks (no two neighbours): 13 the count for n coins is F(n + 2), a Fibonacci number: n = 5, 10, 20, 30 → 13, 144, 17711, 2178309 it grows about 1.618 times per extra coinName the state
Dynamic programming starts by naming a state: a small question whose answer we store. Here: best(i) = the largest total using only the first i coins. One cell per i, from best(0) to best(5). The answer we want is best(5).
state: best(i) = largest total from coins 1..i with no two neighbours taken states: i = 0, 1, ..., 5 (6 cells) answer: best(5) in Python: best = [0] * (n + 1), coin i is v[i-1] MIT 6.006 calls this step S: subproblem definitionBase cases need no choice
Some states are answered straight away. best(0) = 0: no coins, nothing to take. best(1) = 2: with one coin, take it. These are the base cases; every other cell is built from them.
lines 3 and 4: best[0] = 0 no coins best[1] = v[0] = 2 one coin: take it best = [0, 2, ?, ?, ?, ?] MIT 6.006 step B: base casesThe transition: skip or take
A transition builds one state from smaller ones. At coin i: skip it and keep best(i−1), or take it, which bans coin i−1, giving coin i + best(i−2). Keep the larger. For i = 2: max(2, 7) = 7.
best(i) = max(best(i−1), v[i−1] + best(i−2)) i = 2, coin 2 = v[1] = 7 skip = best[1] = 2 take = best[0] + v[1] = 0 + 7 = 7 best[2] = max(2, 7) = 7 (take wins) best = [0, 2, 7, ?, ?, ?] MIT 6.006 step R: relate subproblemsbest(3): take wins
Coin 3 is worth 9. Skip it: best(2) = 7. Take it: 9 + best(1) = 11. Take wins, so best(3) = 11. Each cell reads only two cells to its left, and both are already filled.
i = 3, coin 3 = v[2] = 9 skip = best[2] = 7 take = best[1] + v[2] = 2 + 9 = 11 best[3] = max(7, 11) = 11 (take wins) best = [0, 2, 7, 11, ?, ?]best(4): skip wins
Coin 4 is worth 3. Take it: 3 + best(2) = 10. Skip it: best(3) = 11. Skip wins, so best(4) = 11. The state does not remember which coins made 11, and it does not need to.
i = 4, coin 4 = v[3] = 3 skip = best[3] = 11 take = best[2] + v[3] = 7 + 3 = 10 best[4] = max(11, 10) = 11 (skip wins) best = [0, 2, 7, 11, 11, ?]best(5) is the answer
Coin 5 is worth 1. Skip: best(4) = 11. Take: 1 + best(3) = 12. So best(5) = 12: the most you can take from all five coins without two neighbours. return best[n] hands it back.
i = 5, coin 5 = v[4] = 1 skip = best[4] = 11 take = best[3] + v[4] = 11 + 1 = 12 best[5] = max(11, 12) = 12 (take wins) best = [0, 2, 7, 11, 11, 12] pick([2, 7, 9, 3, 1]) returns 12 MIT 6.006 step O: original problem = best(n)Order and cost
Every state needs only smaller states, so filling left to right never reads an empty cell: a topological order. Cost = states × work per transition: 6 cells, one max each. O(n) time, not an exponential search.
dependencies: best(i) ← best(i−1), best(i−2) all point left, so i = 0, 1, 2, ... is a valid order time = (number of states) × (work per transition) = (n + 1) × O(1) = O(n) here 6 cells space = n + 1 cells (two would do: only i−1, i−2 are read) MIT 6.006 steps T and T: topological order, timeWalk back to the picks
The table stores totals, not picks, but we can walk back. best(5) ≠ best(4), so coin 5 was taken: jump to best(3). 11 ≠ 7: coin 3 taken, jump to best(1). Coin 1 taken. 2 + 9 + 1 = 12.
i, picks = n, [] while i >= 1: if best[i] != best[i-1]: picks.append(i); i -= 2 else: i -= 1 visits i = 5, 3, 1; picks coins 5, 3, 1 values 2 + 9 + 1 = 12
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In this lesson, what does the state best(i) mean?
A state is a small question with a stored answer. best(i) answers it for the first i coins only, so best(5) answers the whole problem.
02Why does the 'take' option use best(i−2) and not best(i−1)?
Taking coin i means its neighbour, coin i−1, cannot be taken. The best you can add from what is left is best(i−2).
03For coins [2, 7, 9, 3, 1], best(3) = 11 and best(2) = 7. Coin 4 is worth 3. What is best(4)?
Skip: best(3) = 11. Take: 3 + best(2) = 10. max(11, 10) = 11. Adding coin 4 to best(3) is illegal, because best(3) may already use coin 3.
04Why is filling the table from left to right a safe order?
best(i) reads best(i−1) and best(i−2). All its dependencies point left, so left to right is a topological order: no cell is read before it is written.
05How long does the table method take for n coins?
Cost = number of states × work per transition = (n + 1) × O(1) = O(n). Checking every legal pick instead grows about 1.6 times per extra coin.
Glossary
- State
- A small question whose answer is stored, usually one table cell. Here best(i): the largest total from the first i coins.
- Transition
- The rule that builds a state's answer from smaller states. Here best(i) = max(best(i−1), v[i−1] + best(i−2)).
- Base case
- A state answered directly, with no choice to make. Here best(0) = 0 and best(1) = the first coin.
- Topological order
- An order of the states in which every state comes after all the states it reads.
- Bottom up
- Filling the table with a loop, from the smallest states to the answer, instead of recursing from the top.
- Optimal substructure
- The best answer to a problem is built from best answers to smaller problems of the same kind.
- Reconstruction
- Walking back through a filled table to recover which choices produced the best answer.
Notes and sources
- The steps follow the SRT BOT recipe from MIT 6.006 (Spring 2020, lecture 15): Subproblem, Relate, Topological order, Base cases, Original problem, Time. CLRS chapter 14 gives the same plan: characterise an optimal solution, define its value recursively, compute it bottom up, then rebuild the solution.
- A good state holds exactly what the rest of the work needs. Here "how many coins have I looked at" is enough, because the transition looks back at best(i−2) to respect the neighbour rule. Another valid choice is two states per coin, (i, took coin i or not); more information per state, same answer.
- The code is real Python 3: pick([2, 7, 9, 3, 1]) returns 12. It assumes at least one coin; for an empty list, return 0 first. max(skip, take) returns the larger value (docs.python.org).
- The same state and transition also work top down: a recursive best(i) with @functools.cache, as in the Software lesson "Dynamic programming". Only the order of filling changes.
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 15: Recursive Algorithms (SRT BOT)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 14: Dynamic Programming
- Sedgewick and Wayne, Computer Science: An Interdisciplinary Approach, §2.3 Recursion (dynamic programming)
- Python documentation: built-in function max()
- Python documentation: functools.cache
- OEIS A000045: Fibonacci numbers (subsets with no two consecutive integers)
Quiz Unlocks after the last step.