Counting Using
Recursion
Learn how to solve complex counting problems by breaking them into smaller subproblems and building solutions from a base case upward.
What Is a Recurrence Relation?
A recurrence relation defines a sequence where each term is expressed as a function of its preceding terms. Instead of counting everything at once, we describe how a count at size \( n \) relates to counts at smaller sizes.
Breaking a Problem Down
We express \( f(n) \) in terms of smaller instances plus some direct contribution, then unwind down to a base case we can solve by inspection.
Why Use Recursion for Counting?
The Formal Framework
A recursive counting formula has two components:
Example: Binary Strings of Length \( n \)
Let \( f(n) \) = number of binary strings of length \( n \). Every such string is either:
Recursion vs. Closed Form
| Recursive Formula | Closed-Form Formula | |
|---|---|---|
| Definition | Defines \( f(n) \) using \( f(k) \), \( k < n \) | Gives \( f(n) \) directly as an expression in \( n \) |
| Example | \( f(n) = 2f(n-1), \; f(0)=1 \) | \( f(n) = 2^n \) |
| Computation | Iterative or recursive evaluation | Direct substitution |
| Availability | Almost always possible | Not always obtainable |
| Insight | Reveals structure of the problem | Reveals growth rate quickly |
How to Build a Recurrence
Example: Tiling a \( 2 \times n \) Board
How many ways can you tile a \( 2 \times n \) board with \( 2 \times 1 \) dominoes? Let \( T(n) \) be the answer.
Example: Strings with No Consecutive 1s
Let \( a(n) \) = number of binary strings of length \( n \) with no two consecutive 1s. Split on the last bit:
Example: Staircase Climbing
You climb a staircase of \( n \) steps, taking either 1 step or 2 steps at a time. Let \( S(n) \) = number of distinct ways to reach step \( n \).
The Sum Rule Behind Recursion
Every recursive counting argument relies on the rule of sum: if the objects being counted are partitioned into disjoint classes \( C_1, C_2, \ldots, C_k \), then
When you split on the "first move" or "last element," you create these disjoint classes. Each class count becomes a term in your recurrence.
Example: Ternary Strings with No "00"
Let \( t(n) \) = number of strings over \( \{0, 1, 2\} \) of length \( n \) containing no "00" substring. Split on the first character:
Computing Values: Bottom-Up Tables
Once you have a recurrence, build a table starting from the base cases. Here is the tiling recurrence \( T(n) = T(n-1) + T(n-2) \):
| \( n \) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| \( T(n) \) | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 |
Common Mistakes
Summary of Key Recurrences
| Problem | Recurrence | Base Cases |
|---|---|---|
| Binary strings of length \( n \) | \( f(n) = 2f(n-1) \) | \( f(0)=1 \) |
| \( 2 \times n \) tiling | \( T(n) = T(n-1) + T(n-2) \) | \( T(1)=1, T(2)=2 \) |
| No consecutive 1s (binary) | \( a(n) = a(n-1) + a(n-2) \) | \( a(1)=2, a(2)=3 \) |
| No "00" (ternary) | \( t(n) = 2t(n-1) + 2t(n-2) \) | \( t(1)=3, t(2)=8 \) |
| Staircase (\( k \) step sizes) | \( S(n) = \sum_{i=1}^{k} S(n-i) \) | \( S(0)=1 \) |
Question 1 of 5
The recurrence \( f(n) = 3f(n-1) \) with \( f(0) = 1 \) counts the number of strings of length \( n \) over what size alphabet?
Question 2 of 5
For the \( 2 \times n \) domino tiling recurrence \( T(n) = T(n-1) + T(n-2) \) with \( T(1)=1, T(2)=2 \), what is \( T(6) \)?
Question 3 of 5
You can climb stairs taking 1, 2, or 3 steps at a time. Which recurrence counts the ways to reach step \( n \)?
Question 4 of 5
Let \( a(n) \) count binary strings of length \( n \) with no two consecutive 1s. With \( a(1)=2, a(2)=3 \), what is \( a(5) \)?
Question 5 of 5
A student writes the recurrence \( f(n) = f(n-1) + f(n-2) \) with base case \( f(0) = 0, f(1) = 0 \). What goes wrong?