01 / 20

Counting Using
Recursion

Learn how to solve complex counting problems by breaking them into smaller subproblems and building solutions from a base case upward.

Discrete Math

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

\(f(n)\)
\(f(n-1)\)
\(g(n)\)
\(f(n-2)\)
... Base Case Recursion tree: each level reduces the problem size

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?

Structure
Many combinatorial objects (strings, trees, paths) are defined recursively: a structure of size \( n \) is built from structures of size \( n-1 \).
Simplicity
A recurrence captures the counting rule in one equation. No need to enumerate every case by hand.
Power
Problems with no simple closed-form (like Fibonacci, Catalan numbers) become tractable through recurrences.

The Formal Framework

A recursive counting formula has two components:

1
Base case(s): The count for the smallest instance(s), e.g. \( f(0) = 1 \) or \( f(1) = 1 \).
2
Recursive step: An equation expressing \( f(n) \) in terms of \( f(k) \) for \( k < n \), e.g. \( f(n) = 2 \cdot f(n-1) \).

Example: Binary Strings of Length \( n \)

Let \( f(n) \) = number of binary strings of length \( n \). Every such string is either:

String of length \(n\)
\(0\) + (\(n-1\) remaining)
\(1\) + (\(n-1\) remaining)
\(f(n) = f(n-1) + f(n-1) = 2f(n-1)\)
Base case: \(f(0) = 1\) (the empty string)

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

1
Define \( f(n) \): State precisely what quantity you are counting and what \( n \) measures (length, size, number of elements).
2
Identify a "first move": Pick a property of the first (or last) element to split on: what are the possible choices?
3
Write the recursive rule: For each choice, express the remaining count in terms of \( f \) at smaller arguments. Sum (or multiply) across choices.
4
Set base case(s): Determine the count for the smallest meaningful input(s) by direct inspection.
5
Verify: Compute a few values by hand and check against independent counting to confirm correctness.

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.

Case 1: Place vertical
\(2 \times (n-1)\)
Leaves \(T(n-1)\) ways
Case 2: Place 2 horizontal
\(2 \times (n-2)\)
Leaves \(T(n-2)\) ways
$$T(n) = T(n-1) + T(n-2)$$
\(T(1) = 1, T(2) = 2\)
This is the Fibonacci sequence!

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:

0
Last bit is 0: The preceding \( n-1 \) bits can be any valid string, giving \( a(n-1) \) options.
1
Last bit is 1: The second-to-last bit must be 0 (to avoid "11"), and the first \( n-2 \) bits form a valid string, giving \( a(n-2) \) options.

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 \).

1 2 3 4
\(n\)
+1 step +2 steps
\(S(n) = S(n-1) + S(n-2)\)
\(S(1) = 1, S(2) = 2\)
Fibonacci again!

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.

Overlap = Double Count
If classes share elements, you count some objects twice. The split must be disjoint.
Missing = Undercount
If classes don't cover all objects, you miss some. The split must be exhaustive.

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:

1
First char is 1 or 2: 2 choices, then any valid string of length \( n-1 \): contributes \( 2 \cdot t(n-1) \).
2
First char is 0: Second char cannot be 0, so it is 1 or 2 (2 choices), then a valid string of length \( n-2 \): contributes \( 2 \cdot t(n-2) \).

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

✕ Wrong Base Case
Using \( f(0) = 0 \) when it should be \( f(0) = 1 \) (e.g., the empty string is one valid object). Always think: "how many objects of size 0 exist?"
✕ Overlapping Cases
Splitting on a property that does not partition objects into disjoint sets, leading to double-counting. Verify cases are mutually exclusive.
✕ Missing Cases
Forgetting a branch in the split (e.g., only considering "starts with 0" but not "starts with 1"). Always check your cases are exhaustive.

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?