Recurrences Handout Mathematics
Recurrences Handout Mathematics
Session 1: Recurrences
Shardul Chiplunkar
AHS Math Club, 2017-03-24
Introduction
“If you already know what recursion is, just remember the answer. Otherwise, find
someone who is standing closer to Douglas Hofstadter than you are; then ask him or her
what recursion is.” – Andrew Plotkin
Though I do not expect that you will appreciate the cultured humor of that quote, I do expect
that you are quite excited about recursion and recurrence relations. Recurrence! A concept that is
heavily used in discrete mathematics, combinatorics, computer science, and elsewhere in mathemat-
ics. At its core, recursion is a thing that refers to itself; you may be familiar with recursive acronyms
such as ‘GNU’ (which stands for ‘GNU is Not Unix’), recursive sorting algorithms, perhaps even
recursive number theory if you are familiar with Gödel’s famous theorems.
Recurrences may be beautiful, elegant, and such, but are no use unless we can actually solve
problems with them! The journey from a cleverly-spotted recurrence relation to a closed-form answer
to the problem we were trying to solve is called ‘solving’ a recurrence (no, this session is not going
to be this trivial throughout). In this session, we will explore two methods of solving recurrence
relations: characteristic equations (also known as ansatz, German for ‘approach’) and generating
functions. There will be exercises meant for each method, though of course, you can use any method
to solve them as long as you know what you are doing.
Big Words
Big words are essential to the academic discussion of a complex topic. Our first big word, ‘recur-
rence’, is one we have already used but not defined. A recurrence is an infinite sequence of numbers
{an } such that an = f (an−1 , an−2 , . . . , an−k ) for some function f : Rk → R and some positive integer
k. Then we have our second big word: k is the ‘order’ of a recurrence.
We call a recurrence ‘linear’ if all the ai terms in f are of degree 1 and none have a variable
coefficients; otherwise we use the big word ‘nonlinear’. If all the terms in f involve a member of the
sequence a, then the recurrence is ‘homogeneous’, otherwise it is ‘nonhomogeneous’, which is truly
a big word.
Here are some examples:
1
order 3 an = 3an−2 + an−3 + n + 4
order 1 an = 3an−1 + n + 4
Pn
nonlinear, nonhomogeneous, order n an = 2n + i=1 ian−i (yeah, these are tricky)
Finally, you may be wondering that if a sequence is defined in terms of itself, and those terms
are also defined in terms or previous terms, then where does it all end? Or equivalently, where
does it all start? All recurrences have ‘initial conditions’, and since an order k recurrence needs k
previous terms in its definition, it must have k initial conditions, usually defined as a0 , a1 , . . . , ak−1 .
For example, the recurrence an = 3an−2 + an−3 + n + 4 may have initial conditions a0 = 3, a1 = −1,
and a2 = 2, and that will be enough to get the infinite sequence going.
Something Simple
This method is best illustrated by an example. Let us consider the famous Fibonacci recurrence:
a0 = 1, a1 = 1, and an = an−1 + an−2 for all n ≥ 2. This is linear, homogeneous, and of order 2.
A key step in this method is that we assume an is exponential. Indeed, a graph of the Fibonacci
numbers appears to rise exponentially. This explains the term ansatz because we take a guessed
‘approach’ at the recurrence, and find that we are right when we are done. Thus, let us write
an ≈ αn for some α 6= 0. We have
an = an−1 + an−2
=⇒ αn = αn−1 + αn−2
=⇒ 0 = α2 − α − 1 (1)
Equation (1) is called the ‘characteristic equation’ of the recurrence. We find that it has more
than one valid α; we will write an as a linear combination of all of them. Note that this is another
guessed ansatz step that turns
√ out to be right
√ in the end.
1+ 5 1− 5
We then have α1 = 2 and α2 = 2 , which give an = c1 α1n + c2 α2n for some c1 and c2 . For
specific values, we use our initial conditions to write
a0 = c1 α10 + c2 α20
=⇒ 0 = c1 + c2
a1 = c1 α11 + c2 α21
√ √
1+ 5 1− 5
=⇒ 1 = c1 · + c2 ·
2 2
This system of linear equations gives c1 = √15 and c2 = − √15 . Finally we have a solution to the
Fibonacci recurrence: √ √ !
1 1+ 5 n 1− 5 n
an = √ −
5 2 2
2
A Little Harder
To solve a nonhomogeneous recurrence, we first convert it to a homogeneous recurrence by removing
the nonhomogeneous part and solve that recurrence. Then, we yet again guess a form for the
nonhomogeneous part and plug that in to the original nonhomogeneous recurrence to obtain a
solution for the nonhomogeneous part. We add the homogeneous and nonhomogeneous solutions
together to get our final solution.
Before we can apply this method to nonhomogeneous recurrences, there are two caveats we must
consider (whose proofs are beyond the scope of this session): firstly, if in solving the characteristic
equation we obtain a repeated root α with multiplicity m, then for the linear combination of roots,
we must use c0 αn , c1 nαn , c2 n2 αn , . . . , cm−1 nm−1 αn . Other non-repeated roots are used as usual.
Secondly, if the nonhomogeneous part of the recurrence is P (n)sn for some number s and polynomial
P , then the corresponding nonhomogeneous part in the solution will be Q(n)nm sn for a polynomial
Q with the same degree as P , where m is the multiplicity of s in the characteristic equation; m can
be zero if s is not a root of the characteristic equation.
Let’s see both of these in practice. We want to solve
with initial conditions a0 = 1, a1 = 1, a2 = 0. Like Equation (1), we have the characteristic equation
α3 − 3α2 + 4 = n3n
For the homogeneous part, we remove the n3n and we are left with α3 − 3α2 + 4 = 0, which has
a double root at α = 2 and a single root at α = −1. The homogeneous part of the solution will then
be c0 2n + c1 n2n + c2 (−1)n .
For the nonhomogeneous part, our guessed ansatz is (pn + q)3n according to the second caveat
mentioned above. This should satisfy the original recurrence in Equation (2), so
Recall that this must hold for all n. This means that both parenthesized terms must be zero, and
we have a system of linear equations which gives p = 27 405
4 and q = − 16 . The nonhomogeneous part
27n 405 n
of the solution will be ( 4 − 16 )3 .
Adding the homogeneous and nonhomogeneous parts,
27n 405 n
an = c0 2n + c1 n2n + c2 (−1)n + − 3
4 16
As with the Fibonacci sequence, we use our initial conditions to obtain a system of linear equations:
405
1 = c0 + c2 −
c0 = 28
16
1
27 405
1 = 2c0 + 2c1 − c2 + 3
4
−
16 =⇒ c1 = − 2
27
27 · 2 405
c2 = −
0 = 4c0 + 8c1 + c2 + 9 −
16
4 16
3
We finally have the solution to the recurrence in Equation (2) as
n 27 27n 405 n
n
an = 28 · 2 − · 2n − · (−1)n + − 3
2 16 4 16
Problems
For each of these problems, try to first come up with a recurrence that models the situation, then
try to solve that recurrence using whatever technique (ansatz, induction, guess-and-check) you see
fit. Remember that you must be able to explain and prove your solution semi-rigorously.
1. (inspired by INMO 2015 #4) The employees of FailingProduct, Inc. are playing “Pass the
Blame”. The boss has the blame by default. He blames one of his 6 employees. That employee
blames another employee (maybe the boss again). After the blame has been passed 8 times,
the boss finds himself with the blame. In how many ways can this game of “Pass the Blame”
have been played?
2. (Singapore IMO Practice Set) In how many ways can you tile a 2×n rectangle by 1×1 squares
and L trominoes? (An L tromino is an L-shaped piece with three squares.)
3. (Hong Kong PSC) Find the number of 10-digit positive integers such that each digit is either
1 or 2, and there are two consecutive 1s.
√ 2014
4. (Mercer County Math Circle) Find the units digit of 3 + 7 .
Generating Functions
Generating functions are a very powerful tool. In the words of Herbert S. Wilf who wrote an entire
230-page book1 about generating functions:
“Generating functions are a bridge between discrete mathematics, on the one hand,
and continuous analysis (particularly complex variable theory) on the other. It is possible
to study them solely as tools for solving discrete problems. As such there is much that is
powerful and magical in the way generating functions give unified methods for handling
such problems. . . . The full beauty of the subject of generating functions emerges from
[both] the discrete and the continuous [aspects]. See how they make the solution of
difference equations into child’s play. Then see how the theory of functions of a complex
variable gives, virtually by inspection, the approximate size of the solution.”
The book is, in fact, recommended reading if you want to go beyond what we will cover here.
What is a generating function? A generating function is the sum of a power series whose
coefficients are terms of the sequence we are interested in. For example, if our sequence an was
1, 1, 2, 3, 5, 8, . . . then the power series would be 1x0 , 1x1 , 2x2 , 3x3 , 5x4 , 8x5 , . . . and the sum would
be ∞ n
P
n=0 an x . Note that this is just a formal, abstract, algebraic sum; we do not require it to
converge or even to actually have some x plugged in. This means we can add, multiply, index-shift,
differentiate (!), or do a lot of other things with generating functions without considering their
‘values’ at all.
1
Generatingfunctionology, available at https://www.math.upenn.edu/~wilf/DownldGF.html.
4
We introduce two lemmas regarding simple generating functions. The first is merely the geo-
metric sequence sum formula
1
= 1 + rx + r2 x2 + r3 x3 + · · · (3)
1 − rx
Note that while Equation (3) holds only for |x| < 1, we do not really care about x in a generating
function. The second lemma is
! ! !
1 m m+1 2 m+2 3
=1+ x+ x + x + ··· (4)
(1 − x)m 1 2 3
n(n−1)(n−2)···(n−k+1)
To see why Equation (4) holds, consider the definition of nk as
k! . Nothing really
prevents us from using a negative value of n, so we can write
!
−n (−n)(−n − 1)(−n − 2) · · · (−n − k + 1)
=
k k!
n(n + 1)(n + 2) · · · (n + k − 1)
= (−1)k ·
! k!
n+k−1
= (−1)k
k
In a similar fashion, we abuse the binomial formula to extend it to negative exponents; as the upper
limit of the sum would be lower than the lower limit, we merely sum to infinity (a rigorous proof of
why this works is beyond the scope of this session). Then
1
= (1 − x)−m
(1 − x)m
∞
!
X −m
= (−x)n
n=0
n
∞
!
X m+n−1 n
= x
n=0
n
5
and using our initial conditions
∞
X
= −3x2 − 2x + 1 + n3n xn
n=3
Combining the results in Equation (3) with r = 3 and Equation (4) with m = 2, and performing
a little algebra to get our indexes right, we can write ∞ n n 3x 2
n=3 n3 x = (1−3x)2 − 3x − 18x . We now
P
write
3x
G(x)(1 − 3x + 4x3 ) = −21x2 − 5x + 1 +
(1 − 3x)2
1 − 5x − 21x2 3x
=⇒ G(x) = 3
+
1 − 3x + 4x (1 − 3x) (1 − 3x + 4x3 )
2
1 − 5x − 21x2 3x
= +
(2x − 1)2 (x + 1) (1 − 3x)2 (2x − 1)2 (x + 1)
Already we can see the roots of the characteristic equation of Equation (2) appear in the denom-
inators as components of the generating function. We can use partial fractions to get (courtesy
WolframAlpha)
57 513 1 27 27
G(x) = − − 2
+ 2
+
2(1 − 2x) 16(1 − 3x) 2(1 − 2x) 4(1 − 3x) 16(1 + x)
Our two lemmas can convert this partial fraction expansion back into coefficients (remember to
get the exponent indices correct!) to give
57 1 n 1 27 27n 513 27 n
an = − 2 − · n2n + (−1)n + − + 3
2 2 2 16 4 16 4
n n 27 27n 405 n
n n
= 28 · 2 − · 2 − · (−1) + − 3
2 16 4 16
which is the same result we obtained from the previous method.
Problems
For each of these problems, remember that you must be able to explain and prove your solution
semi-rigorously. These problems are much more difficult than the previous section. Do not feel
ashamed to ask for hints multiple times.
2. What is the probability of rolling 4 6-sided dice and having the results add up to 18?
3. (from a CMU CS course handout2 ) Suppose that we want to fill a basket with fruit, but we
impose on ourselves some very quirky constraints:
2
http://www.cs.cmu.edu/afs/cs.cmu.edu/academic/class/15251-s09/Site/Materials/Handouts/
generatingfunctions.pdf
6
(a) The number of apples must be a multiple of five (an apple a [week]day...)
(b) The number of bananas must be even (eaten before 15-2513 on Tues/Thurs...)
(c) We can take at most four oranges (too acidic...).
(d) There can be at most one pear (get mushy too fast...)
4. Suppose the generating function for the sequence an is A(x), and for bn , it is B(x). What is
the sequence behind the generating function A(x)B(x) (the product of the A(x) and B(x)) in
terms of an and bn ? Multiplying two generating functions like this is called a ‘convolution’.
Use convolutions to prove that
k
! ! !
r+s X r s
=
k i=0
i k−i
Afterthoughts
Honestly, generating functions are a topic so vast that I have no hope of covering everything in my
lifetime, but I hope I have inspired you with some of their magic. Many people do a vastly better
job of explaining generating functions than I do, including:
• Qiaochu Yuan, the Olympiad math god and Math.SE superpower, in “Topics in generating
functions”, which are notes from the Worldwide Online Olympiad Training (WOOT) program,
available at https://math.berkeley.edu/~qchu/TopicsInGF.pdf
• Luis von Ahn and Anupam Gupta of the famous CMU CS course 15-251, titled “Great Theo-
retical Ideas in Computer Science”, of which the notes on generating functions are available at
http://www.cs.cmu.edu/afs/cs.cmu.edu/academic/class/15251-s09/Site/Materials/
Handouts/generatingfunctions.pdf
3
The CMU CS class. All the lecture slides, handouts, etc. from Spring 2009 are available online and it is a really
cool course if you want to go through it.
4
You will have derived the Catalan numbers, which are immensely important in combinatorics, to the point where
the book “Enumerative Combinatorics: Volume II” contains 66 combinatorial interpretations of the Catalan numbers.