Sub topic 15
Beyond Divide and Conquer: Linear Recurrences and Characteristic Equations
The master theorem handles algorithms that split a problem of size n into pieces of size n/b. Many recursive algorithms shrink the input by subtraction instead. They call themselves on n − 1 and n − 2. The naive Fibonacci function is the standard example. Its running time satisfies T(n) = T(n−1) + T(n−2) + Θ(1). This recurrence has no a, b, or f(n) to plug into the master theorem, so you need a different tool: the characteristic equation.
This chapter assumes you can unroll a recurrence, prove a bound by induction, solve a quadratic with the quadratic formula, and read Big-Theta notation.
What Counts as a Linear Recurrence
A linear recurrence with constant coefficients of order k has this form:
T(n) = c₁T(n−1) + c₂T(n−2) + … + c_kT(n−k) + g(n)
The coefficients c₁ … ck are fixed numbers, and ck ≠ 0. The term g(n) is called the forcing term. When g(n) = 0 the recurrence is homogeneous. Otherwise it is non-homogeneous. You also need k initial values, such as T(0) and T(1), to pin down a unique sequence.
| Recurrence | Order | Homogeneous? | Typical source |
|---|---|---|---|
| T(n) = T(n−1) + T(n−2) | 2 | yes | Fibonacci numbers |
| T(n) = T(n−1) + T(n−2) + 1 | 2 | no | call count of naive Fibonacci |
| T(n) = 2T(n−1) + 1 | 1 | no | Tower of Hanoi moves |
| T(n) = T(n−1) + n | 1 | no | selection sort comparisons |
First-order recurrences can usually be unrolled directly. The characteristic equation is most useful at order 2 and above, where unrolling branches and gets out of hand.
The Characteristic Equation
Guess that the homogeneous recurrence has a solution of the form T(n) = rⁿ for some nonzero r. Substitute it in:
rⁿ = c₁rⁿ⁻¹ + c₂rⁿ⁻² + … + c_krⁿ⁻ᵏ
Divide both sides by rⁿ⁻ᵏ:
rᵏ − c₁rᵏ⁻¹ − c₂rᵏ⁻² − … − c_k = 0
This polynomial equation is the characteristic equation. Each root r gives one solution rⁿ. The recurrence is linear, so any linear combination of solutions is also a solution. That fact gives the general solution.
Distinct roots
If the k roots r₁, …, r_k are all different, every solution has the form
T(n) = α₁r₁ⁿ + α₂r₂ⁿ + … + αkrkⁿ
You find the constants αᵢ by plugging in the initial values and solving a small linear system.
For Fibonacci, F(n) = F(n−1) + F(n−2) with F(0) = 0 and F(1) = 1. The characteristic equation is r² − r − 1 = 0, with roots
φ = (1 + √5)/2 ≈ 1.618 and ψ = (1 − √5)/2 ≈ −0.618.
So F(n) = α φⁿ + β ψⁿ. The initial values give α + β = 0 and αφ + βψ = 1. Since φ − ψ = √5, the solution is α = 1/√5 and β = −1/√5:
F(n) = (φⁿ − ψⁿ)/√5
Because |ψ| < 1, the ψⁿ term shrinks to zero, and F(n) = Θ(φⁿ).
Repeated roots
If a root r has multiplicity m, it contributes m independent solutions: rⁿ, n·rⁿ, …, nᵐ⁻¹·rⁿ.
Take T(n) = 4T(n−1) − 4T(n−2) with T(0) = 1 and T(1) = 4. The characteristic equation is r² − 4r + 4 = (r − 2)² = 0, so 2 is a double root. The general solution is T(n) = (α + βn)·2ⁿ. From T(0) = 1 we get α = 1. From T(1) = 4 we get 2(1 + β) = 4, so β = 1. The result is T(n) = (n + 1)·2ⁿ = Θ(n·2ⁿ). Check it at n = 2: the recurrence gives 16 − 4 = 12, and the formula gives 3·4 = 12.
If you forget the extra n factor, the initial conditions will produce a system with no solution. That failure is the usual sign that a root is repeated.
Non-Homogeneous Recurrences
For T(n) = (homogeneous part) + g(n), the solution is
T(n) = (general homogeneous solution) + (one particular solution).
You find a particular solution by guessing a form that matches g(n), then solving for its coefficients. Fit the initial values only after adding the two parts together. Fitting them to the homogeneous part alone gives the wrong constants.
| Forcing term g(n) | Guess for the particular solution |
|---|---|
| constant c | constant A |
| polynomial of degree d | polynomial of degree d |
| sⁿ | A·sⁿ |
| any of the above, when the guess's base (1 for polynomials, s for sⁿ) is a root of multiplicity m | multiply the guess by nᵐ |
The last row matters more than it seems. In T(n) = T(n−1) + n, the characteristic equation is r − 1 = 0, so 1 is a root. A polynomial forcing term then needs a guess of degree 2, not degree 1. This is why the sum 1 + 2 + … + n is quadratic.
Worked example: counting calls in naive Fibonacci
This function makes one call, then two recursive calls:
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)Let C(n) be the total number of calls. Then C(0) = C(1) = 1 and C(n) = C(n−1) + C(n−2) + 1.
- Homogeneous part: the characteristic roots are φ and ψ, as before.
- Particular solution: guess the constant A. Then A = A + A + 1, so A = −1.
- General solution: C(n) = αφⁿ + βψⁿ − 1.
A shortcut avoids solving for α and β. Define D(n) = C(n) + 1. Then D satisfies the plain Fibonacci recurrence with D(0) = D(1) = 2. That makes D(n) = 2F(n+1), so
C(n) = 2F(n+1) − 1 = Θ(φⁿ).
The following script counts the calls directly and checks them against the formula.
import math
calls = 0
def fib(n):
global calls
calls += 1
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
def F(n):
# Iterative Fibonacci with F(0) = 0, F(1) = 1
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
phi = (1 + math.sqrt(5)) / 2
for n in range(0, 26, 5):
calls = 0
fib(n)
predicted = 2 * F(n + 1) - 1
print(n, calls, predicted, round(calls / phi ** n, 4))The measured and predicted counts match exactly. The last column approaches 2φ/√5 ≈ 1.4472, which confirms the Θ(φⁿ) growth.
When the forcing term is Θ(1) rather than exactly 1, sandwich the recurrence between two exact versions with constants c₁ and c₂. Both versions solve to Θ(φⁿ) as long as the base values are positive, so the original does too.
The Whole Procedure
Gotchas
- The dominant coefficient can vanish. T(n) = 3T(n−1) − 2T(n−2) has roots 1 and 2, so T(n) = α + β·2ⁿ. With T(0) = T(1) = 1 you get β = 0 and T(n) = 1 for every n. Always fit the constants before claiming a bound from the largest root.
- A wrong particular guess fails silently. If the guessed form clashes with a characteristic root, the coefficient equation reduces to something like 0 = 1. Multiply the guess by n and try again.
- The method needs constant coefficients. T(n) = n·T(n−1) is linear, but its coefficient depends on n. It unrolls to n!, and the characteristic equation does not apply.
- The method doesn't apply to divide and conquer. T(n) = 2T(n/2) + n has no characteristic polynomial in n. You can substitute n = 2ᵏ and S(k) = T(2ᵏ) to get the linear recurrence S(k) = 2S(k−1) + 2ᵏ, but for that form the master theorem is usually faster.
Exercise
Solve T(n) = 5T(n−1) − 6T(n−2) with T(0) = 0 and T(1) = 1, and give its Theta bound.
Show answer
The characteristic equation r² − 5r + 6 = 0 has roots 2 and 3, so T(n) = α·2ⁿ + β·3ⁿ. The initial values give α + β = 0 and 2α + 3β = 1, so β = 1 and α = −1. Therefore T(n) = 3ⁿ − 2ⁿ = Θ(3ⁿ).
Exercise
Solve T(n) = 2T(n−1) + 1 with T(0) = 0 (Tower of Hanoi) using the homogeneous-plus-particular method.
Show answer
The homogeneous root is 2, giving α·2ⁿ. A constant guess A gives A = 2A + 1, so A = −1. Then T(n) = α·2ⁿ − 1, and T(0) = 0 gives α = 1. So T(n) = 2ⁿ − 1 = Θ(2ⁿ).
Exercise
For T(n) = T(n−1) + n with T(0) = 0, why is a linear guess Bn + C for the particular solution wrong, and what is the correct answer?
Show answer
The characteristic root is 1, and the base of a polynomial forcing term is also 1, so the guess must be multiplied by n. Substituting Bn + C gives Bn + C = B(n−1) + C + n, which simplifies to 0 = n − B and cannot hold for all n. Using n(Bn + C) gives 2Bn − B + C = n, so B = 1/2 and C = 1/2. With T(0) = 0 the homogeneous constant is 0, so T(n) = n(n+1)/2 = Θ(n²).