Sub topic 1
Recurrence Relations: Defining a Function in Terms of Itself
A recursive algorithm solves a problem by calling itself on smaller inputs. Its running time has the same structure: the time for an input of size n depends on the time for smaller sizes. A recurrence relation is the mathematical object that records this dependency. This chapter covers what a recurrence is, what makes one well defined, and what it means to "solve" one. The later chapters build on these ideas.
Prerequisites
This chapter assumes you are comfortable with the following:
- Big-O, Big-Omega, and Big-Theta notation, including statements like "f(n) = Θ(n log n)".
- Reading recursive functions in Python and tracing a few calls by hand.
- Logarithm and exponent identities, such as log₂(2ᵏ) = k and a^(logb n) = n^(logb a).
- Summing arithmetic and geometric series.
- Proof by mathematical induction, including strong induction.
What a Recurrence Relation Is
A recurrence relation defines a function on the natural numbers (or a sequence) by giving its value at n in terms of its values at smaller arguments. It has two parts:
- Base cases. These give explicit values for one or more small inputs, for example T(1) = 1.
- A recursive rule. This expresses T(n) for larger n through T at smaller arguments, plus some extra term.
Two standard examples show the pattern:
| Recurrence | Base case(s) | Recursive rule | Where it shows up |
|---|---|---|---|
| T(n) = 2T(n/2) + n | T(1) = 1 | two half-size subproblems plus linear work | merge sort |
| F(n) = F(n−1) + F(n−2) | F(0) = 0, F(1) = 1 | sum of the two previous values | Fibonacci numbers |
| S(n) = S(n−1) + n | S(0) = 0 | previous value plus n | sum 1 + 2 + … + n |
| B(n) = B(n/2) + 1 | B(1) = 1 | one half-size subproblem plus constant work | binary search |
The extra term in the rule, such as the + n in the merge sort recurrence, usually stands for the work a call does itself, outside of its recursive calls. Later chapters call this term f(n).
Well-Defined Recurrences
A recurrence pins down a unique function only when every chain of recursive references reaches a base case after finitely many steps. In practice this needs three things:
- Arguments must strictly decrease. Every argument on the right-hand side has to be smaller than n under some ordering that has no infinite descending chains. On the natural numbers, n−1, n−2, and ⌊n/2⌋ for n ≥ 2 all qualify.
- The base cases must cover every place a chain can stop. F(n) = F(n−1) + F(n−2) needs two base cases because the chains starting at F(2) reach both F(1) and F(0).
- The domain must be stated or implied. Writing T(n/2) suggests n is a power of 2, or that a floor or ceiling is meant. The chapter "Floors, Ceilings, and Base Cases Without Worry" explains why this rarely changes the asymptotic answer.
When these conditions hold, strong induction shows that exactly one function satisfies the recurrence. Each T(n) is determined by values that were already fixed.
The following code evaluates the merge sort and Fibonacci recurrences, then shows what goes wrong when the base case does not cover the domain.
import sys
from functools import lru_cache
@lru_cache(maxsize=None)
def T(n):
# T(1) = 1; T(n) = 2T(floor(n/2)) + n for n >= 2
if n <= 1:
return 1
return 2 * T(n // 2) + n
@lru_cache(maxsize=None)
def F(n):
# F(0) = 0, F(1) = 1; F(n) = F(n-1) + F(n-2)
if n < 2:
return n
return F(n - 1) + F(n - 2)
print([T(n) for n in range(1, 9)]) # [1, 4, 5, 12, 13, 16, 17, 32]
print([F(n) for n in range(10)]) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
def broken(n):
# Base case only at n == 1, but n // 2 can also reach 0, and 0 // 2 == 0
if n == 1:
return 1
return broken(n // 2) + 1
sys.setrecursionlimit(200)
try:
broken(0)
except RecursionError:
print("broken(0) never reaches a base case")broken(5) works, because 5 → 2 → 1. broken(0) recurses forever, because 0 // 2 is 0 again. The argument stops decreasing, so the recurrence is not well defined on all of ℕ.
The Dependency Structure
You can draw a recurrence as a graph: each value points to the values it depends on. The merge sort recurrence at n = 8 gives a single chain. The Fibonacci recurrence branches, and its branches share nodes.
The dependency graph is different from the recursion tree you will build later. The dependency graph shows each distinct value once. The recursion tree shows every call separately, so T(8) has 2 children, 4 grandchildren, and so on. The running time follows the tree, since each call does its own work.
What "Solving" a Recurrence Means
A recurrence lets you compute T(n) one value at a time, but it does not tell you how fast T grows. Solving it means finding one of two things:
- A closed form, which is an explicit formula with no self-reference. For n a power of 2, the merge sort recurrence with T(1) = 1 has the closed form T(n) = n log₂ n + n. For example, T(8) = 8·3 + 8 = 32, which matches the output above.
- An asymptotic bound, such as T(n) = Θ(n log n). Algorithm analysis usually needs only this. Exact closed forms are often messy or do not exist, especially when floors are involved: T(5) = 13 under the floor version, and no simple formula gives that value.
Because asymptotic bounds ignore constant factors, running-time recurrences are usually written loosely. "T(n) = 2T(n/2) + Θ(n)" means the following: there exist constants c₁, c₂ > 0 and n₀ such that the extra work at size n is between c₁n and c₂n, and T(n) = Θ(1) for n below n₀. The constant base case is then left implicit.
You can check a candidate closed form by substituting it into the recurrence. Take T(n) = n log₂ n + n with n = 2ᵏ:
2T(n/2) + n = 2[(n/2)(log₂ n − 1) + n/2] + n = n log₂ n − n + n + n = n log₂ n + n.
The base case also matches: T(1) = 1·0 + 1 = 1. By induction on k, the formula is correct for all powers of 2. This check is a simple form of the substitution method covered later.
A Map of Recurrence Shapes
Different recursion patterns produce different recurrence shapes, and each shape has a preferred solving tool. The table below previews the rest of the tutorial.
| Shape | Example | Typical tool |
|---|---|---|
| Subtract a constant, one call | T(n) = T(n−1) + n | unrolling into a sum |
| Divide by a constant, a equal calls | T(n) = aT(n/b) + f(n) | recursion tree, master theorem |
| Subtract constants, several calls | T(n) = T(n−1) + T(n−2) | characteristic equation |
| Uneven fractional splits | T(n) = T(n/3) + T(2n/3) + n | recursion tree, Akra-Bazzi |
| Anything, once a guess exists | any of the above | substitution (induction) |
Common Errors
- Forgetting the base case or covering only part of the domain. As
broken(0)showed, the base case must catch every place a chain of arguments can stop. - Confusing the number of calls with the size of each call. In T(n) = 2T(n/2) + n, the 2 counts the calls and the /2 gives each call's input size. Swapping them, as in T(n) = T(2n)…, gives a recurrence that does not decrease.
- Dropping the non-recursive term. T(n) = 2T(n/2) with T(1) = 1 solves to n. Adding the linear term makes it n log n. The extra term usually determines the answer.
- Treating a recurrence as a solution. "T(n) = 2T(n/2) + n" describes the running time but does not tell you how it grows. You still have to solve it.
- Mixing exact and asymptotic reasoning. Proving T(n) ≤ cn by induction with a Θ(n) term hidden in the step is a well-known trap. The substitution chapter covers it.
Exercise
Compute S(1) through S(5) for S(0) = 0, S(n) = S(n−1) + n, guess a closed form, and verify the guess satisfies both the base case and the recursive rule.
Show answer
The values are S(1)=1, S(2)=3, S(3)=6, S(4)=10, S(5)=15, which suggests S(n) = n(n+1)/2. Base case: S(0) = 0·1/2 = 0. Rule: S(n−1) + n = (n−1)n/2 + n = (n² − n + 2n)/2 = n(n+1)/2. So S(n) = Θ(n²).
Exercise
The recurrence G(n) = G(n−2) + 1 is given with the single base case G(0) = 0. On which natural numbers is G well defined, and how would you fix it so it is defined on all of ℕ?
Show answer
G is defined only on even n, because odd arguments go 1 → −1 → −3 … and never reach 0. Adding a second base case, such as G(1) = 0 (or any constant), makes every chain end at 0 or 1, so G is defined for all n ≥ 0 and G(n) = ⌊n/2⌋ + G(n mod 2).
Exercise
Using the floor version T(1) = 1, T(n) = 2T(⌊n/2⌋) + n, compute T(16) by hand from T(8) = 32, and check it against the closed form n log₂ n + n.
Show answer
T(16) = 2·T(8) + 16 = 64 + 16 = 80. The closed form gives 16·4 + 16 = 80, so they agree, as expected since 16 is a power of 2.