# Solving recurrence relations in a nutshell

Able to solve recurrence relation is a very important skill when we study data structures and algorithm. This is a ability that I used to be familar with when I took combinatorics class when I was an undergraduate. However, by that time, I didn't realize how important this skill is from computer science point of view. But, thanks to MAW, I do now.

This post is a study summary note on this very important subject. The aim of this note is to help at least me quickly solve any types of recurrence relation in the future. The content closely follows Chapter 7 "Recurrence Relations and Generating Functions" of "Introductory Combinatorics", which is the textbook I used.

Note

This note is practical-oriented. I will skip the proof of the theorem whenever possible.If you are interested in the proof side of the universe, please read the book.

## TOC

The post will be organized in the following format:

- Linear homogeneous recurrence relation with constant coefficients
- Method 1: Characteristic equation
- distinct roots (theorem 7.4.1)
- roots with multiplicities (theorem 7.4.2)

- Method 2: Generating function

- Method 1: Characteristic equation
- Linear nonhomogeneous recurrence relation with constant coefficients
- Method 1: Characteristic equation
- Method 2: Generating functions

## Linear homogeneous recurrence relation with constant coefficients

*Definition:* Let \(h_0, h_1, h_2, \dots, h_n, \dots\) be a sequence of numbers. This sequence is
said to satisfy a **linear recurrence relation of order \(k\)**, provided that there
exist quantities \(a_1, a_2, \dots, a_k,\) with \(a_k \ne 0\), and a quantity \(b_n\)
(each of these quantities \(a_1,a_2,\dots,a_k,b_n\) may depend on \(n\)) such that

*Example:* The Fabonacci sequence \(f_0, f_1, f_2, \dots, f_n, \dots\) satisfies
the linear recurrence relation

of order 2 with \(a_1 = 1, a_2 = 1,\) and \(b_n = 0\).

*Definition:* The linear recurrence relation \ref{eq:1} is called **homogeneous**
provided that \(b_n\) is zero and is said to have **constant coefficients** provided that
\(a_1, a_2, \dots, a_k\) are constants.

### Method 1: Characteristic equation

*Theorem 7.4.1:* Let \(q\) be a nonzero number. Then \(h_n = q^n\) is a solution of the
linear homogeneous recurrence relation

with constant coefficients iff \(q\) is a root of the polynomial equation (called **characteristic equation**)

If the polynomial equation has \(k\) *distinct* roots \(q_1, q_2, \dots, q_k\), then

is the general solution of \ref{eq:2} in the following sense: No matter what initial values for \(h_0, h_1, \dots, h_{k-1}\) are given, there are constants \(c_1, c_2, \dots, c_k\) so that \ref{eq:4} is the unique sequence which satisfies both the recurrence relation \ref{eq:2} and the initial values.

*Example:* Solve the Fabonacci recurrence relation

subject to the initial values \(f_0 = 0\), and \(f_1\) = 1.

We rewrite reccurrence relation into \(f(n) - f(n-1) - f(n-2) = 0\) and the characteristic equation of this recurrence relation is

and its two roots are \(\frac{1+\sqrt 5}{2}\), \(\frac{1-\sqrt 5}{2}\), and by theorem 7.4.1,

is the general solution. We now want constants \(c_1\), and \(c_2\) so that

and we have \(c_1 = \frac{1}{\sqrt 5}\), and \(c_2 = -\frac{1}{\sqrt 5}\). Thus,

is the solution of the Fabonacci recurrence relation.

Note

As you might notice, theorem 7.4.1 explicitly requires that the roots of the characteristic equation have to be distinct. However, that's not always the case and theorem 7.4.1 will not work (see book for an example). That's why we need theorem 7.4.2.

*Theorem 7.4.2:* Let \(q_1, q_2, \dots, q_n\) be the distinct roots of the following characteristic equation of the
linear homogeneous recurrence relation with constant coefficients:

If \(q_i\) is an \(s_i\)-fold root fo the characteristic equation of \ref{eq:5}, the part of the general solution of this recurrence relation corresponding to \(q_i\) is

The general solution of the recurrence relation is

*Example:* Solve the recurrence relation

subject to the initial values \(h_0=1\), \(h_1 = 0\), \(h_2 = 1\), and \(h_3 = 2\).

The characteristic equation of this recurrence relation is \(x^4 + x^3 -3x^2 - 5x - 2 = 0\), which has roots \(-1\), \(-1\), \(-1\), \(-2\). Thus, the part of the general solution corresponding to the root \(-1\) is

while the part of a general solution corresponding to the root \(2\) is \(H_n^{(2)} = c_42^n\). The general solution is

Then we can use initial values to determine \(c1\), \(c2\), \(c3\), \(c4\) and we have \(h_n = \frac{7}{9} (-1)^n - \frac{3}{9}n(-1)^n + \frac{2}{9}2^n\).

### Method 2: Generating function

*Definition:* Let \(h_0, h_1, h_2, \dots, h_n, \dots\) be an infinite sequence of numbers. Its **generating function** is defined to be the infinite
series

The coefficient of \(x^n\) in \(g(x)\) is the general solution to \(h_n\). As you can see, generating functions are Taylor series (power series expansion) of infinitely differentiable functions. If we can find the function (i.e. \(g(x)\)) and its Taylor series, then the coefficients of the Taylor series give the solution to the problem.

Let's illustrate this method using an example.

*Example:* Solve the recurrence relation

subject to the initial values \(h_0 = 1\) and \(h_1 = -2\).

We first rewrite the recurrence relation into \(h_n -5h_{n-1} + 6h_{n-2} = 0 \quad (n \ge 2)\). Let \(g(x) = h_0 + h_1x + h_2x^2 + \dots + h_nx^n + \cdots\) be the generating function for the sequence \(h_0, h_1, \dots, h_n, \dots\). We then form the following system of equations with the multipliers chosen based upon our rewritten recurrence relation initially.

If you look at the coefficients of \(x^n\) term vertically of all these three equations, you can see that they match our recurrence relation exactly. Now, we add these three equations together, we obtain

since \($h_n - 5h_{n-1} + 6h_{n-2} = 0 \quad (n \ge 2)\) and our initial condition, we have

Thus,

Now, we need to expand \(g(x)\) in order to get the coefficient of \(h_n\). Since \(1-5x+6x^2 = (1-2x)(1-3x)\), we can write

for some constants \(c1\) and \(c2\). We can determine \(c1\) and \(c2\) by multiplying both sides of this equation by \(1-5x+6x^2\) to get

We can get \(c_1 = 5\) and \(c_2 = -4\). Since

We have

So

Thus, \(h_n = 5\times2^n - 4\times3^n\).

## Linear nonhomogeneous recurrence relation with constant coefficients

**nonhomogeneous** means \(b_n\) in \ref{eq:1} is no longer zero constant.

### Method 1: Characteristic equation

*Steps:*

1) Find the general solution of the homogeneous relation.

2) Find a particular solution of the nonhomogeneous relation.

- If \(b_n\) is a polynomial of degree \(k\) in \(n\), then look for a particular solution \(h_n\) that is also a polynomial of degree \(k\) in \(n\). Thus, try
- \(h_n = r\) (a constant) if \(b_n = d\) (a constant)
- \(h_n = rn + s\) if \(b_n = dn + e\)
- \(h_n = rn^2 + sn + t\) if \(b_n = dn^2 + en + f\)

- If \(b_n\) is an exponential, then look for a particular solution that is also an exponential. Thus, try \(h_n = pd^n\) if \(b_n = d^n\) or \(h_n = pnd^n\) if the first try doesn't work.

3) Combine the general solution and the particular solution so that the combined solution satisfies the initial conditions.

*Example:* Solve

We first consider corresponding homogeneous recurrence relation \(h_n = 3h_{n-1}\) and its characteristic equation is \(x - 3 = 0\). and thus we have the general solution \(h_n = c3^n, \quad (n \ge 1)\).

Now we seek a particular solution of the nonhomogeneous recurrence relation \(h_n = 3h_{n-1}-4n, \quad (n \ge 1)\). We try to find a solution of the form \(h_n = rn + s\) for some constant number \(r\) and \(s\). We plug in our conjecture into the recurrence relation and get

Thus, \(r = 2\) and \(s = 3\) and \(h_n = 2n + 3\). Now, we combine the general solution of the homogeneous relation with the particular solution of the nonhomogeneous relation to obtain

Now, let's use inital condition to solve for \(c\) and we have \(c = -1\). So, \(h_n = -3^n + 2n + 3\).

Note

As you can see, solving recurrence relation using characteristic equation has strong connection with solving differential equations (both homogeneous and nonhomogeneous).

### Method 2: Generating function

There is nothing difference in using "generating function" method to solve nonhomogeneous than solve homogeneous recurrence relation. That's actually a beauty of this method: nothing needs to tweak in order to work under different situation.

Note

Certainly, not all recurrence relation appeard in computer science can be easily solved by the method described in this post. For instance, inside Josephus problem, recurrence relation may depend on whether \(n\) is odd or even and methods may not apply nicely. This implies another type of technique to solve recurrence relation is to guess the solution and prove it by induction. Also, in the book, solving \(h_n = h_{n-1} + n^3\) on p. 250 is not standard as well.