Sub topic 4
Defining the state, recurrence, and base cases
This chapter assumes you can write recursive functions in Python, know what a base case is, and can read Big-O notation. It also assumes you have seen the previous two chapters, which covered overlapping subproblems and optimal substructure.
Every dynamic programming solution rests on three decisions. You choose the state, which is what one subproblem is. You write the recurrence, which says how a state's answer comes from smaller states. You set the base cases, which are the states you can answer directly. If these three are right, memoization and tabulation become almost mechanical. If any of them is wrong, no caching trick will fix it.
The state: what one subproblem means
A state is a small set of parameters that fully identifies a subproblem. Write it as a sentence before you write code:
fib(n): the n-th Fibonacci number.ways(n): the number of distinct ways to climb a staircase ofnsteps, taking 1 or 2 steps at a time.
A good state has two properties:
- It is complete. The answer for the state must depend only on its parameters. It must not depend on how you reached the state. If
ways(n)depended on which step you took last,nalone would not be enough. - It is small. Every extra parameter multiplies the number of states. Include only what the recurrence actually needs.
The recurrence: answers from smaller answers
The recurrence expresses a state in terms of other states. To find it, ask what the last decision was. For the staircase, the final move onto step n was either a 1-step from n - 1 or a 2-step from n - 2. Those two cases do not overlap, and together they cover every way to reach n, so their counts add:
ways(n) = ways(n - 1) + ways(n - 2)
Look at the combining operator. Counting problems add the cases. Optimization problems take min or max over the choices. Feasibility problems use or. Every recurrence must move toward smaller states, or the computation will never finish.
Base cases: where the recursion stops
Base cases are the states the recurrence cannot reduce any further. Pick them by asking which inputs the recurrence would turn into invalid states:
ways(0) = 1: there is exactly one way to climb zero steps, which is to do nothing.ways(1) = 1: a single 1-step.
If ways(1) were missing, the call ways(1) would compute ways(0) + ways(-1) and reach a negative step count. A common mistake is setting ways(0) = 0. That makes ways(2) = 1 when the correct answer is 2.
The specification as plain code
Before you optimize, translate the three decisions directly into a function. The result is slow, but it is an executable specification that you can test against small inputs.
def ways(n):
# ways(n) = number of ways to climb exactly n steps using 1- or 2-steps
if n == 0:
return 1
if n == 1:
return 1
return ways(n - 1) + ways(n - 2)
for n in range(6):
print(n, ways(n)) # 1, 1, 2, 3, 5, 8Check the small cases by hand. For n = 3 the ways are 1+1+1, 1+2, and 2+1, which is 3 and matches the output. The next two chapters take this same recurrence and remove the repeated work, first top-down and then bottom-up.
Common errors
| Error | Symptom | Fix |
|---|---|---|
| State missing a parameter | Wrong answers that depend on call order | Add the information the future choices need |
| Overlapping cases in the recurrence | Counts too large | Split on a decision whose cases cannot overlap |
| Missing base case | Infinite recursion or negative indices | Cover every state the recurrence cannot reduce |
| Wrong base value | Every answer off in the same way | Check the smallest cases by hand |
Exercise
You may climb 1, 2, or 3 steps at a time. Give the state, the recurrence, and the base cases for the number of ways to climb n steps, and compute the answer for n = 4.
Show answer
State: ways(n) = number of ways to climb exactly n steps. Recurrence: ways(n) = ways(n-1) + ways(n-2) + ways(n-3). Base cases: ways(0) = 1, and ways(n) = 0 for n < 0. Alternatively, list ways(0)=1, ways(1)=1, ways(2)=2 explicitly. Then ways(3) = 2+1+1 = 4 and ways(4) = 4+2+1 = 7.
Exercise
A student defines ways(0) = 0 and ways(1) = 1 for the 1-or-2-step staircase. What does their function return for n = 2, and why is that wrong?
Show answer
It returns ways(1) + ways(0) = 1. The correct answer is 2 (1+1 and 2). Climbing zero steps has exactly one way, doing nothing, so ways(0) must be 1.