Sub topic 1
Recurrence Relations: Defining a Function in Terms of Itself
A recurrence relation defines a function by giving its value on small inputs directly and its value on larger inputs in terms of its own values on smaller inputs. In algorithm analysis, the function is usually a running time T(n), and the recurrence describes how the cost of a problem of size n depends on the cost of its subproblems.
Prerequisites
This chapter assumes you can:
- Read Big-O, Big-Omega, and Big-Theta notation.
- Follow a recursive function and trace its calls.
- Use basic logarithm and exponent identities, such as 2^(log2 n) = n.
- Sum arithmetic and geometric series.
- Write a proof by mathematical induction.
The Two Parts of a Recurrence
Every well-formed recurrence has two parts:
- A base case fixes the value for one or more small inputs, for example T(1) = 1.
- A recursive rule expresses T(n) for larger n using T at smaller arguments, for example T(n) = 2T(n/2) + n.
The base case is what stops the definition from referring to itself forever. Without it, the rule alone describes a whole family of functions, and you cannot say which one you mean.
| Recurrence | Base case | Typical source |
|---|---|---|
| T(n) = T(n-1) + n | T(1) = 1 | Selection sort, done recursively |
| T(n) = 2T(n/2) + n | T(1) = 1 | Merge sort |
| T(n) = T(n/2) + 1 | T(1) = 1 | Binary search |
| F(n) = F(n-1) + F(n-2) | F(0) = 0, F(1) = 1 | Fibonacci numbers |
Recurrence Versus Closed Form
A recurrence tells you how to compute a value. A closed form tells you the value directly. For example, T(n) = T(n-1) + n with T(1) = 1 has the closed form n(n+1)/2. In analysis we often only need an asymptotic closed form, such as Θ(n²), and that is what the later chapters of this tutorial teach you to find.
Before solving anything, it helps to evaluate a recurrence by computing it directly:
def linear(n):
# T(n) = T(n-1) + n, T(1) = 1
return 1 if n <= 1 else linear(n - 1) + n
def halving(n):
# T(n) = 2T(n/2) + n, T(1) = 1, for n a power of two
return 1 if n <= 1 else 2 * halving(n // 2) + n
print([linear(n) for n in range(1, 6)]) # [1, 3, 6, 10, 15]
print([halving(2 ** p) for p in range(0, 4)]) # [1, 4, 12, 32]The output gives you numbers to check a guessed closed form against. For instance, n log2 n + n gives 32 at n = 8, which matches.
Common Errors
- Missing base case. T(n) = T(n-1) + 1 alone is satisfied by n, by n + 7, and by n + c for any constant c.
- Treating n/2 as exact. For odd n, n/2 is not an integer. The real recurrence uses ⌊n/2⌋ or ⌈n/2⌉. A later chapter shows why this rarely changes the asymptotic answer.
- Ignoring the base case's size. Asymptotic bounds hold for any constant base value, but exact closed forms depend on it.
Exercise
For T(n) = 2T(n/2) + n with T(1) = 1, compute T(16) using the values above, and check whether it equals n log2 n + n.
Show answer
T(16) = 2 T(8) + 16 = 2 32 + 16 = 80. The formula gives 16 * 4 + 16 = 80, so it matches.
Exercise
Why is "T(n) = T(n-1) + 2 for n > 1" not enough to determine T(10)?
Show answer
There is no base case. Any function 2n + c satisfies the rule, so T(10) could be any value 20 + c. Fixing T(1) determines c.