TExcelTAD
Home › Discrete Mathematics › Linear Recurrences

Linear Recurrences

Fibonacci numbers, the number of ways to tile a strip, the population of a model ecosystem and the cost of some recursive programs all satisfy linear recurrences with constant coefficients:

f(n)=a1f(n−1)+a2f(n−2)+⋯+adf(n−d)+g(n)f(n) = a_1 f(n-1) + a_2 f(n-2) + \cdots + a_d f(n-d) + g(n)

There is a complete recipe for solving them, based on the roots of a polynomial. (In matrix language this is diagonalization; see Recurrences in the linear algebra course.)

Homogeneous recurrences (g(n)=0g(n) = 0)

Try f(n)=rnf(n) = r^n. Substituting and dividing by rn−dr^{n-d} gives the characteristic equation

rd=a1rd−1+a2rd−2+⋯+adr^d = a_1 r^{d-1} + a_2 r^{d-2} + \cdots + a_d
  1. If its roots r1,…,rdr_1, \ldots, r_d are distinct, the general solution is f(n)=c1r1n+⋯+cdrdnf(n) = c_1 r_1^n + \cdots + c_d r_d^n.
  2. A root rr of multiplicity mm contributes rn,nrn,…,nm−1rnr^n, n r^n, \ldots, n^{m-1} r^n.
  3. The constants cic_i come from the dd initial values (a linear system).

Fibonacci. f(n)=f(n−1)+f(n−2)f(n) = f(n-1) + f(n-2) gives r2=r+1r^2 = r + 1, roots φ=1+52\varphi = \frac{1 + \sqrt5}{2} and φ^=1−52\hat\varphi = \frac{1 - \sqrt5}{2}. With f(0)=0f(0) = 0, f(1)=1f(1) = 1: c1+c2=0c_1 + c_2 = 0 and c1φ+c2φ^=1c_1 \varphi + c_2 \hat\varphi = 1, so c1=15=−c2c_1 = \frac{1}{\sqrt5} = -c_2, giving Binet's formula again (Generating Functions). Since ∣φ^∣<1\lvert \hat\varphi \rvert < 1, f(n)f(n) is the integer nearest to φn5\frac{\varphi^n}{\sqrt5}.

SymPy's rsolve follows this recipe:

python · try it yourself
from sympy import Function, rsolve, symbols, simplify

n = symbols("n", integer=True)
f = Function("f")
fib = rsolve(f(n) - f(n - 1) - f(n - 2), f(n), {f(0): 0, f(1): 1})
print("Fibonacci:", fib)
print([simplify(fib.subs(n, k)) for k in range(10)])
rep = rsolve(f(n) - 4 * f(n - 1) + 4 * f(n - 2), f(n), {f(0): 1, f(1): 4})
print("repeated root r = 2:", rep)
Fibonacci: -sqrt(5)*(1/2 - sqrt(5)/2)**n/5 + sqrt(5)*(1/2 + sqrt(5)/2)**n/5
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
repeated root r = 2: 2**n + 2*2**(n - 1)*n

The second recurrence has r2−4r+4=(r−2)2r^2 - 4r + 4 = (r - 2)^2, a double root, so its solution has the form (c1+c2n)2n(c_1 + c_2 n)2^n.

Tiling a strip

How many ways can a 2×n2 \times n strip be tiled with 1×21 \times 2 dominoes? The leftmost column is either one vertical domino (leaving 2×(n−1)2 \times (n-1)) or two horizontal dominoes stacked (leaving 2×(n−2)2 \times (n-2)):

t(n)=t(n−1)+t(n−2),t(1)=1, t(2)=2t(n) = t(n-1) + t(n-2), \qquad t(1) = 1,\ t(2) = 2

Fibonacci again, shifted: t(n)=Fn+1t(n) = F_{n+1}. Many counting problems reduce to a linear recurrence once you split on what happens at one end.

python · try it yourself
from functools import lru_cache

@lru_cache(None)
def tilings(n):
    if n <= 1:
        return 1
    return tilings(n - 1) + tilings(n - 2)

def brute(n):
    """Count tilings of a 2 x n strip by placing dominoes cell by cell."""
    def go(filled):
        if all(filled.values()):
            return 1
        c, r = min((c, r) for (r, c), v in filled.items() if not v)   # leftmost empty cell
        total = 0
        for cells in ([(r, c), (r + 1, c)], [(r, c), (r, c + 1)]):
            if all(filled.get(x) is False for x in cells):
                for x in cells:
                    filled[x] = True
                total += go(filled)
                for x in cells:
                    filled[x] = False
        return total
    return go({(r, c): False for r in range(2) for c in range(n)})

print([tilings(n) for n in range(1, 11)])
print([brute(n) for n in range(1, 11)])
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

Inhomogeneous recurrences

If g(n)≠0g(n) \ne 0: find one particular solution fpf_p by guessing a function of the same shape as gg (constant, polynomial, exponential), then add the general homogeneous solution, and fit the constants to the initial values.

Example. f(n)=2f(n−1)+1f(n) = 2f(n-1) + 1, f(0)=0f(0) = 0 (Towers of Hanoi). Homogeneous part: c⋅2nc \cdot 2^n. Particular: try a constant fp=af_p = a: a=2a+1a = 2a + 1 gives a=−1a = -1. So f(n)=c 2n−1f(n) = c\,2^n - 1, and f(0)=0f(0) = 0 gives c=1c = 1: f(n)=2n−1f(n) = 2^n - 1.

If the guess has the same shape as a homogeneous solution (e.g. g(n)=2ng(n) = 2^n with root 2), multiply the guess by nn.

In software

Linear recurrences describe running times (T(n)=T(n−1)+T(n−2)+1T(n) = T(n-1) + T(n-2) + 1 for naive Fibonacci is Θ(φn)\Theta(\varphi^n)), loan balances, digital filters (IIR filters are linear recurrences on signals) and random number generators (linear congruential and linear-feedback shift registers). They can be evaluated in O(log⁡n)O(\log n) steps with matrix powers by repeated squaring.

Exercises

Exercise 1: Distinct roots

Solve f(n)=5f(n−1)−6f(n−2)f(n) = 5f(n-1) - 6f(n-2), f(0)=2f(0) = 2, f(1)=5f(1) = 5.

Answer

f(n)=2n+3nf(n) = 2^n + 3^n.

Solution

r2−5r+6=(r−2)(r−3)r^2 - 5r + 6 = (r - 2)(r - 3), so f(n)=c12n+c23nf(n) = c_1 2^n + c_2 3^n. Then c1+c2=2c_1 + c_2 = 2 and 2c1+3c2=52c_1 + 3c_2 = 5, giving c1=c2=1c_1 = c_2 = 1.

Exercise 2: Particular solution

Solve f(n)=3f(n−1)+4f(n) = 3f(n-1) + 4, f(0)=1f(0) = 1.

Answer

f(n)=3n+1−2f(n) = 3^{n+1} - 2.

Solution

Particular constant: a=3a+4a = 3a + 4, so a=−2a = -2. General: c 3n−2c\,3^n - 2; f(0)=c−2=1f(0) = c - 2 = 1 gives c=3c = 3. Check: f(1)=7=3⋅1+4f(1) = 7 = 3 \cdot 1 + 4.

Exercise 3: Bit strings without "11" (code)

Count binary strings of length nn with no two consecutive 1s. Find the recurrence and check it by brute force.

Answer

b(n)=b(n−1)+b(n−2)b(n) = b(n-1) + b(n-2) with b(1)=2b(1) = 2, b(2)=3b(2) = 3, so b(n)=Fn+2b(n) = F_{n+2}.

Solution

A valid string ends in 0 (any valid string of length n−1n - 1 before it) or in 01 (any valid string of length n−2n - 2 before it).

python · try it yourself
from itertools import product

brute = [sum(1 for s in product("01", repeat=n) if "11" not in "".join(s)) for n in range(1, 13)]
b = [2, 3]
while len(b) < 12:
    b.append(b[-1] + b[-2])
print(brute)
print(b)
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]

Test yourself

What is the characteristic equation of f(n)=6f(n−1)−9f(n−2)f(n) = 6f(n-1) - 9f(n-2)?

  1. r2−6r+9=0r^2 - 6r + 9 = 0
  2. r2+6r−9=0r^2 + 6r - 9 = 0
  3. 6r−9=06r - 9 = 0
  4. r2=15r^2 = 15

A double root r=3r = 3 contributes which terms to the general solution?

  1. 3n3^n only
  2. 3n3^n and n⋅3nn \cdot 3^n
  3. 3n3^n and 32n3^{2n}
  4. n3n^3

How many ways can a 2×52 \times 5 strip be tiled with dominoes?

  1. 5
  2. 8
  3. 13
  4. 10