Sub topic 15
Beyond Divide and Conquer: Linear Recurrences and Characteristic Equations
This chapter assumes you can read recursive code, are comfortable with Big-Theta notation, and can solve a quadratic equation. Some recursive algorithms do not cut the input by a constant factor. Naive Fibonacci calls itself on n - 1 and n - 2, so its subproblem sizes are n minus a constant rather than n/b. The master theorem needs the aT(n/b) form, so it cannot handle these. For this family the tool is the characteristic equation.
Linear Homogeneous Recurrences
A linear homogeneous recurrence with constant coefficients has this form:
T(n) = c1·T(n−1) + c2·T(n−2) + … + ck·T(n−k)
It is linear because each T term appears to the first power. It is homogeneous because there is no extra term such as + n or + 1.
The method rests on one guess. Try T(n) = rⁿ, substitute, and divide by rⁿ⁻ᵏ. That leaves the characteristic equation:
rᵏ = c1·rᵏ⁻¹ + c2·rᵏ⁻² + … + ck
Every root of this equation gives a solution, and any linear combination of solutions is also a solution. The initial values T(0), T(1), … fix the coefficients.
| Roots of the characteristic equation | General solution |
|---|---|
| Distinct roots r1, …, rk | α1·r1ⁿ + … + αk·rkⁿ |
| Root r repeated m times | (β0 + β1·n + … + βm−1·nᵐ⁻¹)·rⁿ |
The term with the largest root in absolute value, provided its coefficient is nonzero, gives the asymptotic growth.
Worked Example: Fibonacci
For T(n) = T(n−1) + T(n−2), the characteristic equation is r² = r + 1. Its roots are:
- φ = (1 + √5)/2 ≈ 1.618
- ψ = (1 − √5)/2 ≈ −0.618
So T(n) = α·φⁿ + β·ψⁿ. Because |ψ| < 1, the ψⁿ term shrinks toward 0, and T(n) = Θ(φⁿ).
The running time of naive Fibonacci has an extra constant term: C(n) = C(n−1) + C(n−2) + 1. To remove it, substitute U(n) = C(n) + 1. Then U(n) = U(n−1) + U(n−2), which is homogeneous again. The running time is therefore Θ(φⁿ), about 1.618ⁿ.
import math
PHI = (1 + math.sqrt(5)) / 2
def fib(n, counter):
counter[0] += 1 # count every call
if n < 2:
return n
return fib(n - 1, counter) + fib(n - 2, counter)
for n in range(5, 31, 5):
counter = [0]
fib(n, counter)
print(n, counter[0], round(counter[0] / PHI ** n, 4))The ratio settles near 2φ/√5 ≈ 1.4472. That is the constant hidden inside Θ(φⁿ).
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 is r² = 5r − 6, so the roots are 2 and 3, and T(n) = α·2ⁿ + β·3ⁿ. The initial values give α + β = 0 and 2α + 3β = 1, so β = 1 and α = −1. Then T(n) = 3ⁿ − 2ⁿ = Θ(3ⁿ).
Exercise
What is the general solution of T(n) = 4T(n−1) − 4T(n−2), and how fast does it grow when the coefficient of the n·2ⁿ term is nonzero?
Show answer
The equation r² − 4r + 4 = 0 has the double root r = 2, so T(n) = (β0 + β1·n)·2ⁿ. When β1 ≠ 0, T(n) = Θ(n·2ⁿ).