← Course home

Problem-solving methods · Lesson 45 of 52

Dynamic programming

Turn repeated subproblems into bottom-up tables and analyze time and memory separately.

Full lessonAbout 12 min

What you will learn

  • Identify repeated subproblems and state
  • Build a bottom-up recurrence
  • Analyze time and memory separately

Recommended first: Recursion and recurrences.

On this page 6 sections

The idea

fibonacci_modulo(6, 1000) returns 8 and keeps only two running values.

Dynamic programming applies when a problem repeats smaller versions of itself and you can order those versions. So each one is ready before you need it. The Fibonacci recurrence F(n) = F(n - 1) + F(n - 2), with F(0) = 0 and F(1) = 1, is the smallest example. A naive recursion recomputes F(3) many times. A bottom-up table fills each index once. The library function fibonacci_modulo keeps only the last two values and returns F(n) mod m. For n = 6 and m = 1000 the result is 8.

A worked trace

StepStateWhat changed
F(1)current = 1F(1) is 1. The previous value is 0.
F(2)current = 1F(2) is 1. The previous value is 1.
F(3)current = 2F(3) is 2. The previous value is 1.
F(4)current = 3F(4) is 3. The previous value is 2.
F(5)current = 5F(5) is 5. The previous value is 3.
F(6)current = 8F(6) is 8. The previous value is 5.

Why it is correct

The table is correct by induction. The base cells match the definition. If cells n - 1 and n - 2 hold F(n - 1) and F(n - 2). Their sum is F(n) by the recurrence, so writing that sum into cell n preserves the claim. The fill order matters. Cell n is written only after the two cells it reads. A recursion with memoization is the same idea stored in a dictionary instead of an array. The state is the argument n. The transition is the sum. The base cases are the two starting cells.

A common mistake

The naive recursion recomputes the same F(n) many times. The table does not.

What it costs

The table version does Θ(1) work per cell and fills n cells, so the time is Θ(n). The full table uses Θ(n) extra memory. Keeping two running values uses Θ(1) extra memory and the same Θ(n) time, which is what fibonacci_modulo does. The naive recursion without a table does Θ(φ^n) additions because it repeats work. The win is not the recurrence. The win is computing each state once. Larger problems add a second index, and the cost becomes the number of states times the cost of one transition.

Run it

python
from bitsandbytes.dynamic_programming.classic import fibonacci_moduloprint(fibonacci_modulo(6, 1000))# prints: 8 

Practice and next step

Open Dynamic programming state and transitions next. The next lesson names the state and the transition for coin change and knapsack.

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

Bottom-up Fibonacci through F(6)

Each cell is the sum of the two cells before it. The library keeps only the last two values. The table here shows the same numbers.

current
1

State

  1. 00chosen
  2. 11chosen

F(1) is 1. The previous value is 0.

Practice feedback is loading. The lesson explanation remains available above.