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.
This week you learn to solve the recurrences (recurrence relations) you set up last week: instead of writing an in terms of earlier terms, you find a formula directly in terms of n. 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:
Characteristic equation: substitute an=rn and solve the equation in r.
Distinct rootsr1=r2: an=αr1n+βr2n.
Double rootr: an=(α+βn)rn. Whoever forgets the factor n loses marks.
Find the constants with a0, a1. Compute one more term and check.
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 k with constant coefficients:
an=c1an−1+c2an−2+⋯+ckan−k,ck=0.
Word by word:
Word
Meaning
Counterexample
linear
the earlier terms are only multiplied by constants and added
an=an−12
homogeneous
no extra term without a
an=2an−1+1
constant coefficients
the ci are numbers, not depending on n
an=nan−1
degree k
it looks back at most k steps
an=an−3 is degree 3
If the degree is k, then k initial conditions determine the sequence uniquely. On the final the degree is almost always 2.
Intuition: why rn?
A first-order recurrence like an=2an−1 multiplies by 2 at each step, and the solution is an=a02n. For second order too we expect the solution to be “exponential”. Write an=rn and put it into the recurrence:
rn=c1rn−1+c2rn−2⟹r2−c1r−c2=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=0 gives two distinct roots r1,r2:
an=α1r1n+α2r2n.
Example.an=an−1+2an−2, a0=2, a1=7.
1) r^2 = r + 2 -> r^2 - r - 2 = 0 -> (r - 2)(r + 1) = 0 -> r = 2, r = -12) a_n = A 2^n + B (-1)^n3) n = 0: A + B = 2 n = 1: 2A - B = 7 add: 3A = 9 -> A = 3, B = -14) a_n = 3 * 2^n - (-1)^n5) check: a_2 = 7 + 2*2 = 11 ; formula 12 - 1 = 11
TIP · always check one term. Compute a2 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 (r0, of multiplicity two), r0n alone is not enough to satisfy two conditions. The second basic solution is nr0n:
an=(α1+α2n)r0n.
Example.an=4an−1−4an−2, a0=1, a1=6.
r^2 - 4r + 4 = (r - 2)^2 = 0 -> r = 2 (double root)a_n = (A + B n) 2^nn = 0: A = 1n = 1: (1 + B) * 2 = 6 -> B = 2a_n = (1 + 2n) 2^ncheck: a_3 = 4*20 - 4*6 = 56 ; formula 7 * 8 = 56
PITFALL · Writing α2n+β2n for a double root. This is really (α+β)2n, a single constant. It cannot satisfy both initial conditions. For a double root the second term must be βnrn.
Verifying a solution by proof
On the final you are sometimes asked “Show that an=… 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)n satisfies an=an−1+2an−2 for all n≥2, with a0=2, a1=7.
The first two terms give 3⋅2n. Since (−1)n−1=−(−1)n−2, the last two terms give (−1)n−2−2(−1)n−2=−(−1)n−2=−(−1)n. Hence an−1+2an−2=3⋅2n−(−1)n=an. Also a0=3−1=2 and a1=6+1=7. ■
In plain words: add the two exponential parts separately. Use the trick 2n−1+2n−1=2n, and the fact that the sign changes when you shift the power of (−1) by one.
Nonhomogeneous recurrences
Of the form an=c1an−1+⋯+ckan−k+F(n), with F(n)=0. The solution:
an=an(h)+an(p)
an(h): the general solution of the homogeneous recurrence you get by deleting F(n). an(p): any particular solution of the recurrence. A guessing table for the particular solution:
F(n)
Try
When does it change?
constant d
C
if 1 is a characteristic root, Cn
dn+e
Cn+D
if 1 is a root, multiply by one n
d⋅sn
Csn
if s is a root, Cnsn (if multiple, nm)
Example.an=3an−1+4, a0=1.
homogeneous: a_n = 3 a_{n-1} -> a_n(h) = A 3^nparticular: a_n(p) = C (try constant) -> C = 3C + 4 -> C = -2general: a_n = A 3^n - 2condition: a_0 = A - 2 = 1 -> A = 3result: 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=1 to α3n and then add −2, it comes out wrong. First write the full general solution (a(h)+a(p)), and apply the condition last.
Divide-and-conquer recurrences
For algorithms that split the problem into a subproblems, each of size n/b, with combining taking g(n) (binary search, merge sort):
Master theorem. If f(n)=af(n/b)+cnd with a≥1, b>1, c>0, d≥0, then:
f(n) is ⎩⎨⎧O(nd)O(ndlogn)O(nlogba)a<bda=bda>bd
How to read it: compare a (the number of branches) with bd (the rate at which the work at one level shrinks).
Recurrence
a
bd
Result
f(n/2)+2 (binary search)
1
20=1
O(logn)
2f(n/2)+n (merge sort)
2
2
O(nlogn)
4f(n/2)+n
4
2
O(n2)
2f(n/2)+n2
2
4
O(n2)
You can also compute small values by hand: if f(n)=2f(n/2)+3, f(1)=1 then f(2)=5, f(4)=13, f(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.
Intuition: xk 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 k”.
Expansions to memorize:
Function
Sequence ak
1−x1
1
1−ax1
ak
(1−x)21
k+1
(1−x)n1
C(n+k−1,k)
1−x1−xm+1
1 for k≤m, then 0
(1+x)n
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?
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=6 example in the questions.
You can also solve recurrences with generating functions: for ak=3ak−1, a0=1, we get G(x)−3xG(x)=1, G(x)=1−3x1, so ak=3k. 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∣. For three sets:
∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩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..100 are divisible by 2, 3 or 7?
If “divisible by none” is asked, it is 100−72=28. Forgetting this complement step is the most common loss.
Common mistakes
Wrong
The truth
Writing r2+c1r+c2=0 instead of r2=c1r+c2
Move the right-hand terms to the left changing signs: r2−c1r−c2=0
αrn+βrn for a double root
(α+βn)rn
Applying the initial condition only to the homogeneous part
First a(h)+a(p), then the condition
Trying C3n when F(n)=3n and 3 is a root
Try Cn3n, otherwise you get a contradiction for C
Mixing up a and b in the master theorem
a = number of subproblems, b = division ratio
The coefficient of xk in (1−x)31 is C(k+3,k)
C(n+k−1,k)=C(k+2,k)
Subtracting the triple intersection in inclusion-exclusion
The triple intersection is added
Writing ∣A∪B∪C∣ for a “none of them” question
Subtract from the total: N−∣A∪B∪C∣
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−2.
Show answer
3 and -2
Substitute an=rn: r2=r+6, that is r2−r−6=0. Factor: (r−3)(r+2)=0. The roots are r=3 and r=−2.
Question 2medium
Solve an=5an−1−6an−2 with a0=1, a1=4. Then compute a5.
Show answer
an=2⋅3n−2n, a5=454
The characteristic equation is r2−5r+6=0, with roots 2 and 3 (distinct). The general solution is an=α2n+β3n.n=0: α+β=1. n=1: 2α+3β=4. Multiply the first by 2 and subtract: β=2, α=−1.an=2⋅3n−2n. a5=2⋅243−32=454. Check: a2=5⋅4−6=14 and 2⋅9−4=14.
Question 3medium
Solve an=6an−1−9an−2 with a0=2, a1=9, and find a4.
Show answer
an=(n+2)3n, a4=486
r2−6r+9=(r−3)2=0: a single root r=3 of multiplicity two (repeated root). The general solution is an=(α+βn)3n.n=0: α=2. n=1: 3(2+β)=9, β=1. So an=(n+2)3n.a4=6⋅81=486.
Question 4medium
Solve an=4an−1−4an−2 with a0=1, a1=6.
Show answer
an=(1+2n)2n
(r−2)2=0, double root 2. an=(α+βn)2n. a0=α=1. a1=2(1+β)=6, β=2. Result an=(1+2n)2n. Check: a2=4⋅6−4⋅1=20, the formula gives 5⋅4=20.
Question 5hard
Solve the nonhomogeneous recurrence an=3an−1+2n with a0=1.
Show answer
an=3n+1−2n+1
The homogeneous part an=3an−1: an(h)=α3n. For the particular solution, F(n)=2n and 2 is not a characteristic root, so try an(p)=C2n:C2n=3C2n−1+2n. Divide by 2n−1: 2C=3C+2, C=−2.an=α3n−2⋅2n. a0=α−2=1, α=3. Result 3n+1−2n+1. Check: a1=3+2=5=9−4.
Question 6easy
Let f(n)=2f(n/2)+3 for n=2k, n≥2, with f(1)=1. Compute f(8).
Show answer
29
Compute from the bottom up: f(2)=2⋅1+3=5, f(4)=2⋅5+3=13, f(8)=2⋅13+3=29. (In general f(n)=4n−3; for n=8 that is 29.)
Question 7medium
Use the master theorem to give a big-O estimate for f(n)=4f(n/2)+n.
Show answer
O(n2)
In the form f(n)=af(n/b)+cnd: a=4, b=2, d=1. bd=2 and a=4>2. In this case f(n) is O(nlogba)=O(nlog24)=O(n2).
Question 8medium
Use the master theorem to give a big-O estimate for f(n)=8f(n/2)+n3.
Show answer
O(n3logn)
a=8, b=2, d=3. bd=23=8=a: the equality case. The result is O(ndlogn)=O(n3logn).
Question 9medium
What is the coefficient of x5 in (1−x)31? What counting problem does it answer?
Show answer
21
In the expansion of (1−x)n1, the coefficient of xk is C(n+k−1,k). Here n=3, k=5: C(7,5)=21. This is the number of nonnegative integer solutions of x1+x2+x3=5: each 1−x1 represents the values 0,1,2,… of one variable.
Question 10hard
Use a generating function to find the number of solutions of e1+e2+e3=6 with 1≤e1≤3, 0≤e2≤2, 0≤e3≤2.
Show answer
3
The generating function is (x+x2+x3)(1+x+x2)2, and we want the coefficient of x6.(1+x+x2)2=1+2x+3x2+2x3+x4. To get x6: x2⋅x4 (coefficient 1⋅1) and x3⋅2x3 (coefficient 1⋅2). x1 would need x5, but there is no x5. Total 1+2=3. (List: (2,2,2), (3,1,2), (3,2,1).)
Question 11medium
Which sequences have generating functions (a) 1−2x1 and (b) (1−x)2x?
Show answer
(a) ak=2k, (b) ak=k
(a) 1−ax1=∑akxk, here a=2: 1,2,4,8,…(b) (1−x)21=∑(k+1)xk. Multiplying by x shifts the sequence one place to the right: ∑kxk, that is 0,1,2,3,…
Question 12hard
How many integers in {1,2,…,200} are divisible by none of 2, 3, 5?
Show answer
54
First count those divisible by at least one (inclusion-exclusion): ⌊200/2⌋+⌊200/3⌋+⌊200/5⌋=100+66+40=206. Pairs: ⌊200/6⌋+⌊200/10⌋+⌊200/15⌋=33+20+13=66. Triple: ⌊200/30⌋=6.∣A∪B∪C∣=206−66+6=146. Divisible by none: 200−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)