MAT2033 · Week 11 · Solving linear recurrences, generating functions

Linear recurrences, generating functions and inclusion-exclusion

Solving recurrences with the characteristic equation, adding a particular solution, divide-and-conquer recurrences, counting with generating functions, and inclusion-exclusion. A full question on the final comes from here.

recurrence relationlinear homogeneous recurrencecharacteristic equationcharacteristic rootrepeated rootparticular solutionnonhomogeneous recurrencedivide-and-conquer recurrencemaster theoremgenerating functioninclusion-exclusion

This week you learn to solve the recurrences (recurrence relations) you set up last week: instead of writing ana_n in terms of earlier terms, you find a formula directly in terms of nn. This topic is not on the midterm, it is a final exam topic. The final question is usually like this: “Solve the recurrence … with initial conditions …”. Then comes a generating function or inclusion-exclusion question.

Golden rules:

  1. Characteristic equation: substitute an=rna_n = r^n and solve the equation in rr.
  2. Distinct roots r1≠r2r_1 \neq r_2: an=αr1n+βr2na_n = \alpha r_1^n + \beta r_2^n.
  3. Double root rr: an=(α+βn)rna_n = (\alpha + \beta n) r^n. Whoever forgets the factor nn loses marks.
  4. Find the constants with a0a_0, a1a_1. Compute one more term and check.
  5. If it is not homogeneous: general solution = homogeneous solution + particular solution. Apply the initial condition last.

What is a linear homogeneous recurrence?

A linear homogeneous recurrence relation of degree kk with constant coefficients:

an=c1an−1+c2an−2+⋯+ckan−k,ck≠0.a_n = c_1 a_{n-1} + c_2 a_{n-2} + \dots + c_k a_{n-k}, \qquad c_k \neq 0.

Word by word:

WordMeaningCounterexample
linearthe earlier terms are only multiplied by constants and addedan=an−12a_n = a_{n-1}^2
homogeneousno extra term without aaan=2an−1+1a_n = 2a_{n-1} + 1
constant coefficientsthe cic_i are numbers, not depending on nnan=n an−1a_n = n\,a_{n-1}
degree kkit looks back at most kk stepsan=an−3a_n = a_{n-3} is degree 3

If the degree is kk, then kk initial conditions determine the sequence uniquely. On the final the degree is almost always 2.

Intuition: why rnr^n?

A first-order recurrence like an=2an−1a_n = 2a_{n-1} multiplies by 2 at each step, and the solution is an=a02na_n = a_0 2^n. For second order too we expect the solution to be “exponential”. Write an=rna_n = r^n and put it into the recurrence:

rn=c1rn−1+c2rn−2  ⟹  r2−c1r−c2=0.r^n = c_1 r^{n-1} + c_2 r^{n-2} \;\Longrightarrow\; r^2 - c_1 r - c_2 = 0.

This is the characteristic equation, and its roots are the characteristic roots. Each root gives a basic solution; because the recurrence is linear, every linear combination of the basic solutions is also a solution.

Rule 1: distinct roots

If r2−c1r−c2=0r^2 - c_1 r - c_2 = 0 gives two distinct roots r1,r2r_1, r_2:

an=α1r1n+α2r2n.a_n = \alpha_1 r_1^n + \alpha_2 r_2^n.

Example. an=an−1+2an−2a_n = a_{n-1} + 2a_{n-2}, a0=2a_0 = 2, a1=7a_1 = 7.

1) r^2 = r + 2  ->  r^2 - r - 2 = 0  ->  (r - 2)(r + 1) = 0  ->  r = 2, r = -1
2) a_n = A 2^n + B (-1)^n
3) n = 0:  A + B = 2
   n = 1:  2A - B = 7
   add:    3A = 9  ->  A = 3,  B = -1
4) a_n = 3 * 2^n - (-1)^n
5) check: a_2 = 7 + 2*2 = 11 ;  formula 12 - 1 = 11

TIP · always check one term. Compute a2a_2 from the recurrence and from the formula. If they are not equal, there is a sign error in the system. On the exam it takes 20 seconds and saves your marks.

Rule 2: double root

If the characteristic equation has a single root (r0r_0, of multiplicity two), r0nr_0^n alone is not enough to satisfy two conditions. The second basic solution is nr0nn r_0^n:

an=(α1+α2n) r0n.a_n = (\alpha_1 + \alpha_2 n)\, r_0^n.

Example. an=4an−1−4an−2a_n = 4a_{n-1} - 4a_{n-2}, a0=1a_0 = 1, a1=6a_1 = 6.

r^2 - 4r + 4 = (r - 2)^2 = 0   ->  r = 2 (double root)
a_n = (A + B n) 2^n
n = 0:  A = 1
n = 1:  (1 + B) * 2 = 6  ->  B = 2
a_n = (1 + 2n) 2^n
check: a_3 = 4*20 - 4*6 = 56 ;  formula 7 * 8 = 56

PITFALL · Writing α2n+β2n\alpha 2^n + \beta 2^n for a double root. This is really (α+β)2n(\alpha+\beta)2^n, a single constant. It cannot satisfy both initial conditions. For a double root the second term must be βnrn\beta n r^n.

Verifying a solution by proof

On the final you are sometimes asked “Show that an=…a_n = \dots is a solution”. This is a direct proof: plug the formula into the right side and obtain the left side.

Claim. an=3⋅2n−(−1)na_n = 3\cdot 2^n - (-1)^n satisfies an=an−1+2an−2a_n = a_{n-1} + 2a_{n-2} for all n≥2n \ge 2, with a0=2a_0 = 2, a1=7a_1 = 7.

Proof. For n≥2n \ge 2,

an−1+2an−2=3⋅2n−1−(−1)n−1+2(3⋅2n−2−(−1)n−2)=3⋅2n−1+3⋅2n−1−(−1)n−1−2(−1)n−2.a_{n-1} + 2a_{n-2} = 3\cdot 2^{n-1} - (-1)^{n-1} + 2\left(3\cdot 2^{n-2} - (-1)^{n-2}\right) = 3\cdot 2^{n-1} + 3\cdot 2^{n-1} - (-1)^{n-1} - 2(-1)^{n-2}.

The first two terms give 3⋅2n3\cdot 2^n. Since (−1)n−1=−(−1)n−2(-1)^{n-1} = -(-1)^{n-2}, the last two terms give (−1)n−2−2(−1)n−2=−(−1)n−2=−(−1)n(-1)^{n-2} - 2(-1)^{n-2} = -(-1)^{n-2} = -(-1)^n. Hence an−1+2an−2=3⋅2n−(−1)n=ana_{n-1} + 2a_{n-2} = 3\cdot 2^n - (-1)^n = a_n. Also a0=3−1=2a_0 = 3 - 1 = 2 and a1=6+1=7a_1 = 6 + 1 = 7. ■\blacksquare

In plain words: add the two exponential parts separately. Use the trick 2n−1+2n−1=2n2^{n-1} + 2^{n-1} = 2^n, and the fact that the sign changes when you shift the power of (−1)(-1) by one.

Nonhomogeneous recurrences

Of the form an=c1an−1+⋯+ckan−k+F(n)a_n = c_1 a_{n-1} + \dots + c_k a_{n-k} + F(n), with F(n)≠0F(n) \neq 0. The solution:

an=an(h)+an(p)a_n = a_n^{(h)} + a_n^{(p)}

an(h)a_n^{(h)}: the general solution of the homogeneous recurrence you get by deleting F(n)F(n). an(p)a_n^{(p)}: any particular solution of the recurrence. A guessing table for the particular solution:

F(n)F(n)TryWhen does it change?
constant ddCCif 1 is a characteristic root, CnCn
dn+edn + eCn+DCn + Dif 1 is a root, multiply by one nn
d⋅snd\cdot s^nCsnC s^nif ss is a root, CnsnC n s^n (if multiple, nmn^m)

Example. an=3an−1+4a_n = 3a_{n-1} + 4, a0=1a_0 = 1.

homogeneous:  a_n = 3 a_{n-1}            ->  a_n(h) = A 3^n
particular:   a_n(p) = C  (try constant) ->  C = 3C + 4  ->  C = -2
general:      a_n = A 3^n - 2
condition:    a_0 = A - 2 = 1  ->  A = 3
result:       a_n = 3^(n+1) - 2      check: a_1 = 3 + 4 = 7 = 9 - 2

PITFALL · applying the initial condition to the homogeneous part. If you apply a0=1a_0 = 1 to α3n\alpha 3^n and then add −2-2, it comes out wrong. First write the full general solution (a(h)+a(p)a^{(h)} + a^{(p)}), and apply the condition last.

Divide-and-conquer recurrences

For algorithms that split the problem into aa subproblems, each of size n/bn/b, with combining taking g(n)g(n) (binary search, merge sort):

f(n)=af(n/b)+g(n).f(n) = a f(n/b) + g(n).

Binary search: f(n)=f(n/2)+2f(n) = f(n/2) + 2. Merge sort: M(n)=2M(n/2)+nM(n) = 2M(n/2) + n.

Master theorem. If f(n)=af(n/b)+cndf(n) = a f(n/b) + cn^d with a≥1a \ge 1, b>1b > 1, c>0c > 0, d≥0d \ge 0, then:

f(n) is {O(nd)a<bdO(ndlog⁡n)a=bdO(nlog⁡ba)a>bdf(n) \text{ is } \begin{cases} O(n^d) & a < b^d \\ O(n^d \log n) & a = b^d \\ O(n^{\log_b a}) & a > b^d \end{cases}

How to read it: compare aa (the number of branches) with bdb^d (the rate at which the work at one level shrinks).

Recurrenceaabdb^dResult
f(n/2)+2f(n/2) + 2 (binary search)120=12^0 = 1O(log⁡n)O(\log n)
2f(n/2)+n2f(n/2) + n (merge sort)22O(nlog⁡n)O(n\log n)
4f(n/2)+n4f(n/2) + n42O(n2)O(n^2)
2f(n/2)+n22f(n/2) + n^224O(n2)O(n^2)

You can also compute small values by hand: if f(n)=2f(n/2)+3f(n) = 2f(n/2) + 3, f(1)=1f(1) = 1 then f(2)=5f(2) = 5, f(4)=13f(4) = 13, f(8)=29f(8) = 29.

Generating functions

The generating function of a sequence is the power series that carries the terms as coefficients:

G(x)=a0+a1x+a2x2+⋯=∑k=0∞akxk.G(x) = a_0 + a_1 x + a_2 x^2 + \dots = \sum_{k=0}^{\infty} a_k x^k.

Intuition: xkx^k is a “shelf label”, and the coefficient says how many things are on that shelf. When you multiply polynomials, the exponents add; this matches exactly the job of “count the selections whose total is kk”.

Expansions to memorize:

FunctionSequence aka_k
11−x\dfrac{1}{1-x}11
11−ax\dfrac{1}{1-ax}aka^k
1(1−x)2\dfrac{1}{(1-x)^2}k+1k+1
1(1−x)n\dfrac{1}{(1-x)^n}C(n+k−1, k)C(n+k-1,\,k)
1−xm+11−x\dfrac{1-x^{m+1}}{1-x}1 for k≤mk \le m, then 0
(1+x)n(1+x)^nC(n,k)C(n,k)

Counting example. You will buy 8 pastries of three kinds, with at least 2 of the cheese kind. How many different selections are there?

cheese:      x^2 + x^3 + ...  = x^2 / (1 - x)
other two:   1 + x + x^2 + ... = 1 / (1 - x)   (each)
G(x) = x^2 / (1 - x)^3
coefficient of x^8 = coefficient of x^6 in 1/(1-x)^3 = C(3+6-1, 6) = C(8, 6) = 28

For bounded variables (when there is an upper bound), write the series as a finite polynomial and find the coefficient by multiplying. This is what is done in the e1+e2+e3=6e_1 + e_2 + e_3 = 6 example in the questions.

You can also solve recurrences with generating functions: for ak=3ak−1a_k = 3a_{k-1}, a0=1a_0 = 1, we get G(x)−3xG(x)=1G(x) - 3xG(x) = 1, G(x)=11−3xG(x) = \frac{1}{1-3x}, so ak=3ka_k = 3^k. On the final this method is rarer; the characteristic root method is what is mainly expected.

Inclusion-exclusion

For two sets ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A\cap B|. For three sets:

∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣.|A\cup B\cup C| = |A| + |B| + |C| - |A\cap B| - |A\cap C| - |B\cap C| + |A\cap B\cap C|.

Intuition: when you add them one by one, you counted those in two sets twice, so subtract. But those in all three were added 3 times and then subtracted 3 times, so not counted at all: add them back once.

Example. How many numbers from 1..1001..100 are divisible by 2, 3 or 7?

|A| = floor(100/2) = 50     |B| = floor(100/3) = 33     |C| = floor(100/7) = 14
|AB| = floor(100/6) = 16    |AC| = floor(100/14) = 7    |BC| = floor(100/21) = 4
|ABC| = floor(100/42) = 2
|A u B u C| = 50 + 33 + 14 - 16 - 7 - 4 + 2 = 72

If “divisible by none” is asked, it is 100−72=28100 - 72 = 28. Forgetting this complement step is the most common loss.

Common mistakes

WrongThe truth
Writing r2+c1r+c2=0r^2 + c_1 r + c_2 = 0 instead of r2=c1r+c2r^2 = c_1 r + c_2Move the right-hand terms to the left changing signs: r2−c1r−c2=0r^2 - c_1 r - c_2 = 0
αrn+βrn\alpha r^n + \beta r^n for a double root(α+βn)rn(\alpha + \beta n) r^n
Applying the initial condition only to the homogeneous partFirst a(h)+a(p)a^{(h)} + a^{(p)}, then the condition
Trying C3nC 3^n when F(n)=3nF(n) = 3^n and 3 is a rootTry Cn3nC n 3^n, otherwise you get a contradiction for CC
Mixing up aa and bb in the master theoremaa = number of subproblems, bb = division ratio
The coefficient of xkx^k in 1(1−x)3\frac{1}{(1-x)^3} is C(k+3,k)C(k+3, k)C(n+k−1,k)=C(k+2,k)C(n+k-1, k) = C(k+2, k)
Subtracting the triple intersection in inclusion-exclusionThe triple intersection is added
Writing ∣A∪B∪C∣\lvert A\cup B\cup C\rvert for a “none of them” questionSubtract from the total: N−∣A∪B∪C∣N - \lvert A\cup B \cup C\rvert

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Find the characteristic roots of the recurrence an=an−1+6an−2a_n = a_{n-1} + 6a_{n-2}.

Show answer
3 and -2

Substitute an=rna_n = r^n: r2=r+6r^2 = r + 6, that is r2−r−6=0r^2 - r - 6 = 0. Factor: (r−3)(r+2)=0(r-3)(r+2)=0. The roots are r=3r = 3 and r=−2r = -2.

Question 2medium

Solve an=5an−1−6an−2a_n = 5a_{n-1} - 6a_{n-2} with a0=1a_0 = 1, a1=4a_1 = 4. Then compute a5a_5.

Show answer
an=2⋅3n−2na_n = 2\cdot 3^n - 2^n, a5=454a_5 = 454

The characteristic equation is r2−5r+6=0r^2 - 5r + 6 = 0, with roots 2 and 3 (distinct). The general solution is an=α2n+β3na_n = \alpha 2^n + \beta 3^n.n=0n=0: α+β=1\alpha + \beta = 1. n=1n=1: 2α+3β=42\alpha + 3\beta = 4. Multiply the first by 2 and subtract: β=2\beta = 2, α=−1\alpha = -1.an=2⋅3n−2na_n = 2\cdot 3^n - 2^n. a5=2⋅243−32=454a_5 = 2\cdot 243 - 32 = 454. Check: a2=5⋅4−6=14a_2 = 5\cdot 4 - 6 = 14 and 2⋅9−4=142\cdot 9 - 4 = 14.

Question 3medium

Solve an=6an−1−9an−2a_n = 6a_{n-1} - 9a_{n-2} with a0=2a_0 = 2, a1=9a_1 = 9, and find a4a_4.

Show answer
an=(n+2)3na_n = (n+2)3^n, a4=486a_4 = 486

r2−6r+9=(r−3)2=0r^2 - 6r + 9 = (r-3)^2 = 0: a single root r=3r = 3 of multiplicity two (repeated root). The general solution is an=(α+βn)3na_n = (\alpha + \beta n)3^n.n=0n=0: α=2\alpha = 2. n=1n=1: 3(2+β)=93(2 + \beta) = 9, β=1\beta = 1. So an=(n+2)3na_n = (n+2)3^n.a4=6⋅81=486a_4 = 6 \cdot 81 = 486.

Question 4medium

Solve an=4an−1−4an−2a_n = 4a_{n-1} - 4a_{n-2} with a0=1a_0 = 1, a1=6a_1 = 6.

Show answer
an=(1+2n)2na_n = (1+2n)2^n

(r−2)2=0(r-2)^2 = 0, double root 2. an=(α+βn)2na_n = (\alpha + \beta n)2^n. a0=α=1a_0 = \alpha = 1. a1=2(1+β)=6a_1 = 2(1+\beta) = 6, β=2\beta = 2.
Result an=(1+2n)2na_n = (1+2n)2^n. Check: a2=4⋅6−4⋅1=20a_2 = 4\cdot 6 - 4\cdot 1 = 20, the formula gives 5⋅4=205 \cdot 4 = 20.

Question 5hard

Solve the nonhomogeneous recurrence an=3an−1+2na_n = 3a_{n-1} + 2^n with a0=1a_0 = 1.

Show answer
an=3n+1−2n+1a_n = 3^{n+1} - 2^{n+1}

The homogeneous part an=3an−1a_n = 3a_{n-1}: an(h)=α3na_n^{(h)} = \alpha 3^n.
For the particular solution, F(n)=2nF(n) = 2^n and 2 is not a characteristic root, so try an(p)=C2na_n^{(p)} = C 2^n:C2n=3C2n−1+2nC2^n = 3C2^{n-1} + 2^n. Divide by 2n−12^{n-1}: 2C=3C+22C = 3C + 2, C=−2C = -2.an=α3n−2⋅2na_n = \alpha 3^n - 2\cdot 2^n. a0=α−2=1a_0 = \alpha - 2 = 1, α=3\alpha = 3. Result 3n+1−2n+13^{n+1} - 2^{n+1}. Check: a1=3+2=5=9−4a_1 = 3 + 2 = 5 = 9 - 4.

Question 6easy

Let f(n)=2f(n/2)+3f(n) = 2f(n/2) + 3 for n=2kn = 2^k, n≥2n \ge 2, with f(1)=1f(1) = 1. Compute f(8)f(8).

Show answer
29

Compute from the bottom up: f(2)=2⋅1+3=5f(2) = 2\cdot 1 + 3 = 5, f(4)=2⋅5+3=13f(4) = 2\cdot 5 + 3 = 13, f(8)=2⋅13+3=29f(8) = 2 \cdot 13 + 3 = 29.
(In general f(n)=4n−3f(n) = 4n - 3; for n=8n = 8 that is 2929.)

Question 7medium

Use the master theorem to give a big-O estimate for f(n)=4f(n/2)+nf(n) = 4f(n/2) + n.

Show answer
O(n2)O(n^2)

In the form f(n)=af(n/b)+cndf(n) = af(n/b) + cn^d: a=4a = 4, b=2b = 2, d=1d = 1. bd=2b^d = 2 and a=4>2a = 4 > 2.
In this case f(n)f(n) is O(nlog⁡ba)=O(nlog⁡24)=O(n2)O(n^{\log_b a}) = O(n^{\log_2 4}) = O(n^2).

Question 8medium

Use the master theorem to give a big-O estimate for f(n)=8f(n/2)+n3f(n) = 8f(n/2) + n^3.

Show answer
O(n3log⁡n)O(n^3 \log n)

a=8a = 8, b=2b = 2, d=3d = 3. bd=23=8=ab^d = 2^3 = 8 = a: the equality case. The result is O(ndlog⁡n)=O(n3log⁡n)O(n^d \log n) = O(n^3\log n).

Question 9medium

What is the coefficient of x5x^5 in 1(1−x)3\dfrac{1}{(1-x)^3}? What counting problem does it answer?

Show answer
21

In the expansion of 1(1−x)n\frac{1}{(1-x)^n}, the coefficient of xkx^k is C(n+k−1,k)C(n+k-1, k). Here n=3n=3, k=5k=5: C(7,5)=21C(7,5) = 21.
This is the number of nonnegative integer solutions of x1+x2+x3=5x_1 + x_2 + x_3 = 5: each 11−x\frac{1}{1-x} represents the values 0,1,2,…0,1,2,\dots of one variable.

Question 10hard

Use a generating function to find the number of solutions of e1+e2+e3=6e_1 + e_2 + e_3 = 6 with 1≤e1≤31 \le e_1 \le 3, 0≤e2≤20 \le e_2 \le 2, 0≤e3≤20 \le e_3 \le 2.

Show answer
3

The generating function is (x+x2+x3)(1+x+x2)2(x + x^2 + x^3)(1 + x + x^2)^2, and we want the coefficient of x6x^6.(1+x+x2)2=1+2x+3x2+2x3+x4(1+x+x^2)^2 = 1 + 2x + 3x^2 + 2x^3 + x^4. To get x6x^6: x2⋅x4x^2 \cdot x^4 (coefficient 1⋅11\cdot 1) and x3⋅2x3x^3 \cdot 2x^3 (coefficient 1⋅21 \cdot 2). x1x^1 would need x5x^5, but there is no x5x^5.
Total 1+2=31 + 2 = 3. (List: (2,2,2)(2,2,2), (3,1,2)(3,1,2), (3,2,1)(3,2,1).)

Question 11medium

Which sequences have generating functions (a) 11−2x\dfrac{1}{1-2x} and (b) x(1−x)2\dfrac{x}{(1-x)^2}?

Show answer
(a) ak=2ka_k = 2^k, (b) ak=ka_k = k

(a) 11−ax=∑akxk\frac{1}{1-ax} = \sum a^k x^k, here a=2a = 2: 1,2,4,8,…1, 2, 4, 8, \dots(b) 1(1−x)2=∑(k+1)xk\frac{1}{(1-x)^2} = \sum (k+1)x^k. Multiplying by xx shifts the sequence one place to the right: ∑kxk\sum k x^k, that is 0,1,2,3,…0, 1, 2, 3, \dots

Question 12hard

How many integers in {1,2,…,200}\{1, 2, \dots, 200\} are divisible by none of 22, 33, 55?

Show answer
54

First count those divisible by at least one (inclusion-exclusion): ⌊200/2⌋+⌊200/3⌋+⌊200/5⌋=100+66+40=206\lfloor 200/2\rfloor + \lfloor 200/3\rfloor + \lfloor 200/5\rfloor = 100 + 66 + 40 = 206.
Pairs: ⌊200/6⌋+⌊200/10⌋+⌊200/15⌋=33+20+13=66\lfloor 200/6 \rfloor + \lfloor 200/10\rfloor + \lfloor 200/15\rfloor = 33 + 20 + 13 = 66. Triple: ⌊200/30⌋=6\lfloor 200/30\rfloor = 6.∣A∪B∪C∣=206−66+6=146|A\cup B\cup C| = 206 - 66 + 6 = 146. Divisible by none: 200−146=54200 - 146 = 54.

Verification: All recurrences were compared with the closed form over 20 iterated terms in node; generating function coefficients were verified by polynomial multiplication and exact counting, and inclusion-exclusion results by counting 1..N one by one (verify/MAT2033/11.mjs).

Review card

Homogeneous: a_n = c1 a_{n-1} + c2 a_{n-2} -> r^2 - c1 r - c2 = 0 Distinct roots r1, r2: a_n = A r1^n + B r2^n Double root r: a_n = (A + B n) r^n (don't forget the n!) A, B: plug in a0 and a1, a system of 2 unknowns Nonhomogeneous: a_n = a_n(h) + a_n(p); find p first, THEN apply the initial condition F(n) = constant -> p = C; F(n) = s^n -> p = C s^n (if s is a root, C n s^n) Master: f(n) = a f(n/b) + c n^d a < b^d -> O(n^d); a = b^d -> O(n^d log n); a > b^d -> O(n^{log_b a}) 1/(1-x) = 1+x+x^2+... 1/(1-ax) -> a^k 1/(1-x)^n -> C(n+k-1, k) Bounded variable: a factor (x^low + ... + x^high), read off the desired coefficient IE (3 sets): |A|+|B|+|C| - |AB|-|AC|-|BC| + |ABC| "none" = total - |A u B u C|; the count divisible by k = floor(N/k)