← Course home

Problem-solving methods · Lesson 46 of 52

Dynamic programming state and transitions

Define dynamic-programming states, transitions, base cases, fill order and cost.

Full lessonAbout 10 min

You will learn to define a state and recurrence and count state, transition and memory costs.

Recommended first: Dynamic programming.

On this page 6 sections

The idea

min_coin_change of coins 1, 3, 4 and amount 6 returns 2.

A dynamic-programming state is the smallest subproblem you are willing to store. The transition says how that state is computed from earlier states. For coin change, the state is the minimum number of coins that sum to an amount. min_coin_change on coins 1, 3, 4 and amount 6 returns 2, for example 3 + 3. The transition tries each coin that does not exceed the amount and adds one to the answer for the remaining amount. Amount 0 takes zero coins. A knapsack state needs two indexes. knapsack_01 on weights 1, 3, values 2, 4, and capacity 4 returns 6. Both items fit, and their values sum to 6. The transition either skips the item or takes it when the remaining capacity allows. Taking it is allowed once, which is why the item index moves forward.

A worked trace

StepStateWhat changed
Amountcoins = 2Amount 2 takes 2 coins.
Amountcoins = 1Amount 4 takes 1 coins.
Amountcoins = 2Amount 6 takes 2 coins.

Why it is correct

The coin table is correct by induction on the amount. The base amount is 0. If every smaller amount is correct, trying each coin reads a correct smaller answer and the minimum plus one is the best for this amount. The fill order matters. You fill smaller amounts first. The knapsack table is correct by induction on the item index and the capacity. The skip choice copies the previous item's answer. The take choice adds the value to a smaller capacity of the previous item. Both choices are available, so the cell keeps the larger one.

A common mistake

Filling a cell before the smaller amounts it reads makes the induction fail.

What it costs

Coin change fills one cell per amount, and each cell tries every coin, so the time is the amount times the number of coins. Extra memory can be the full table or just the previous amounts. The 0-1 knapsack is items times capacity, and a rolling row brings the memory down to the capacity. Count states, then count the work inside one transition, then say whether you store every state. Those are three different numbers. Coin change is Θ(amount times coins) time and can use Θ(amount) extra memory.

Run it

python
from bitsandbytes.dynamic_programming.classic import min_coin_changeprint(min_coin_change([1, 3, 4], 6))# prints: 2 

Practice and next step

Open String search algorithms next. String search finds a pattern without restarting the text index after a partial match.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.dynamic_programming.classic

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Coin change as a table of amounts

min_coin_change of coins 1, 3, 4 and amount 6 returns 2. The 0-1 knapsack on weights 1, 3 and values 2, 4 with capacity 4 returns 6.

coins
2

State

  1. 02chosen
  2. 12chosen

Amount 2 takes 2 coins.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1Coin change stores one number per amount. What is the state?
20-1 knapsack has an item index and a capacity. What dominates the time?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.