Lecture 30: Solving Recurrences

We looked at deriving recurrences for counting last lecture. We also started looking into solving the very simplest of recurrences. This lecture, we will look at some more interesting ways to tackle recurrences. Especially, we will introduce generating functions as an interesting way of solving recurrences.

Some Basic Summation Facts

We recall the following basic summation facts:

 begin{array}{rclcl} sum_{i=0}^n i &=& 0+1 + 2 + 3 + cdots + n &=& frac{n(n+1)}{2}  sum_{i=0}^n a^i &=& a^0 + a^1 + a^2 + cdots + a^n &=& frac{a^{n+1} -1 }{a-1}, mbox{provided} a not= 1 sum_{i=0}^{infty} a^i &=& 1 + a+ a^2 + a^3 + cdots &=& frac{1}{1 -a }, mbox{provided} -1 < a < 1  end{array}

Given a knowledge of the closed forms of the summations above, we can calculate other kinds of summations. Let us take an example.

Example-1

Find the value of sum_{i=0}^n (i+1) a^i = 1. a^0 + 2 . a^1 + 3. a^2 + ldots + (n+1) . a^{n} for some constant a, where a not= 1.

Answer

Let S represent the value of the summation above.

 begin{array}{r|lclclclclclclcl} (1) rightarrow & S   &=& 1 &+& 2 a &+& 3 a^2 &+& 4 a^3 &+& cdots &+& (n+1) a^n  (2) rightarrow & a S &=&   & &     a &+& 2 a^2 &+& 3 a^3 &+& cdots &+&  n a^n &+& (n+1) a^{n+1}  hline mbox{Subtract (2) from (1)} rightarrow  &S - aS &=& 1 &+& a &+& a^2 &+& a^3 &+& cdots &+& a^n &-& (n+1) a^{n+1}  end{array}

From the reasoning above, we get

(1-a) S =  frac{1 - a^{n+1} }{1 - a } - (n+1) a^{n+1} ,.

Since a not= 1, we conclude

 S = frac{1 - a^{n+1} }{(1-a)^2} - frac{ (n+1) a^{n+1} }{ 1 - a } ,.

Solving Recurrences: Basics

We consider recurrences of the form: a_n = K a_{n-1} + L, n > 0 with base case a_0=C fixed. K,L,C are constants.

Example-1

Take the recurrence: a_n = 2 a_{n-1} +1, n > 0 and a_0 = 1. Here is the derivation for the closed form.

Answer

 begin{array}{rclcl} a_n &=& 2 a_{n-1} +1  &=& 2 (2 a_{n-2} +1) + 1 &=& 4 a_{n-2} + 2 + 1 &=& 4 (2 a_{n-3} +1) + 2 + 1  &=& 8 a_{n-3} + 4 + 2 + 1 & =& cdots  &vdots &  &=& 2^n a_{n-n} + 2^{n-1} + cdots + 2 + 1 &=& 2^n a_0 + sum_{i=1}^{n-1} 2^{n-1}  &=& 2^n + frac{(2^n -1)}{2-1}  &=& 2^{n+1} -1  end{array}

Observation

From the example above, we can proceed to solve recurrences of the form a_n = K a_{n-1} + L, n > 0 as follows:

Answer

 begin{array}{rclclcl} a_n &=& K a_{n-1} + L &=& K( K a_{n-2} + L) + L &=& K^2 a_{n-2} + KL + L  &=& K^2 (K a_{n-3} + L) + KL + L & =& K^3 a_{n-3} + L( K^2 + K + 1)  &=& K^4 a_{n-4} + L (K^3 + K^2 + K + 1)  &vdots &  &=& K^n a_0 + L ( K^{n-1} + cdots + K + 1)  &=& K^n a_0 + L frac{K^n -1}{K -1 }  end{array}

Second-Order Recurrences

We will now consider recurrences that are second-order of the form:

 a_n = K a_{n-1} + L a_{n-2}, n geq 2

with base cases a_0, a_1.

Well-Known examples of such recurrences include:

  • Fibonacci numbers: K=L=1 and a_0 = a_1 = 1.

  • Lucas Numbers: K=L=1 and a_0 = 1, a_1 = -1.

  • Neganacci numbers: K=1, L=-1 and a_0 = a_1 = 1.

Quadratic Equation

For the second order recurrence

 a_n = K a_{n-1} + L a_{n-2}, n geq 2

We setup a quadratic equation

 x^2 - K x - L = 0

Let r,s be two different roots to the quadratic equation above. Then the closed form solution to the recurrence has the form

 a_n = A r^n + B s^n

for some coefficients A,B.

If the equation has a single repeated root r=s, then the solution is given by

 a_n = (A+Bn) r^n

Example # 1

Consider the recurrence:

G_0 = 1, G_1 = 2, G_n = - G_{n-1} + 2 G_{n-2}, n geq 2

Answer

We write out some terms of this recurrence

 G_2 = 0, G_3 = 4, G_4 = -4, G_5 = 12, cdots .

Here we have K = -1 and L = 2. Therefore we solve the quadratic equation:

 x^2 + x - 2 = 0

Its roots are: x = frac{-1 pm sqrt{1 + 8}}{2} = frac{-1 pm 3}{2} = 1, -2 .

Therefore, the solution is of the form

 G_n = A 1^n + B ( -2 )^n

Given G_0 = 1, we obtain  A + B = 1, and G_1 = 2 =  A - 2 B . Solving, we obtain the solution

 A = frac{4}{3}, B = - frac{1}{3}

Therefore,

G_n = frac{4}{3} - frac{1}{3} left( -2 right)^n = frac{4 + (-1)^{n+1} 2^n}{3} .

Example # 2

Consider the Fibonacci recurrence

 F_{0} =  0,  F_1= 1, F_{n} = F_{n-1} + F_{n-2}, n geq 2.

Answer

Here  K = L = 1. The characteristic polynomial is

 x^2 - K x - L = 0 ;; x^2 -x -1 = 0.

Solving this equation, we obtain the roots

 x = frac{1 pm sqrt{ 1 + 4} }{2} = frac{1 + sqrt{5}}{2}, frac{1 - sqrt{5}}{2} .

Therefore, the closed form is

 F_n = A left( frac{1 + sqrt{5}}{2} right)^n + B left(frac{1 - sqrt{5}}{2} right)^n.

Plugging, in n = 0, we obtain,

 A + B = F_0 = 0.

For n=1,

 A frac{1 + sqrt{5}}{2} + B frac{1 - sqrt{5}}{2}  = F_1 = 1.

Writing phi = frac{1 + sqrt{5}}{2} and psi = frac{1 - sqrt{5}}{2} , we obtain the equations:

 A + B = 0, phi A + psi B = 1

Solving them we obtain,  (psi - phi) A = -1 or A = frac{-1}{psi - phi} =  frac{1}{sqrt{5}}. Likewise, B = -frac{1}{sqrt{5}}. The overall closed form is

 F_n = frac{1}{sqrt{5}}left( phi^n - psi^n right) .

A special note: usually, we have Fibonacci numbers starting as F_0 = 1, F_1 = 1. The closed form for the sequence with this base case will be

 tilde{F_n} = frac{1}{sqrt{5}}left( phi^{n+1} - psi^{n+1} right) .