14
Recurrence Relations Contents 4.1 Introduction .................................. 2 4.2 Linear Homogeneous Recurrence Relations .................. 5 4.3 Solving Inhomogeneous Recurrence Relations ................. 8 4.4 Further Examples ................................ 10 4.5 Nonlinear Recurrence Relations ........................ 13

Recurrence Relation

Embed Size (px)

DESCRIPTION

Solving Recurrence Relation .with examples.

Citation preview

Page 1: Recurrence Relation

Recurrence Relations

Contents

4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24.2 Linear Homogeneous Recurrence Relations . . . . . . . . . . . . . . . . . . 54.3 Solving Inhomogeneous Recurrence Relations . . . . . . . . . . . . . . . . . 84.4 Further Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104.5 Nonlinear Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . . 13

Page 2: Recurrence Relation

2

4.1 Introduction

The following are examples of recurrence relations :

an = 2an−1 + 1 , n ≥ 2 (1)

an = 5an−1 − 6an−2 , n ≥ 3 (2)

For (1) we would need one initial value to find an. For example, if a1 = 1 then a2 = 3 anda3 = 7 .

For (2) we would need two initial values to find an. For example, if a1 = 1 and a2 = 5 thena3 = 19 and a4 = 65 .

Example 4.1 Verify that the solution of (1) with a1 = 1 is an = 2n − 1 .

Solution We have to do two things

(a) Check that the given formula gives the correct initial value.

(b) Check that the given formula solves (1).

Putting n = 1 in an = 2n − 1 gives a1 = 2− 1 = 1 as required.

To do (b) we evaluate 2an−1 + 1 using the given formula and show that it is equal to an .

Now an−1 = 2n−1 − 1 so

2an−1 + 1 = 2(2n−1 − 1

)+ 1 = 2n − 1

so an = 2an−1 + 1 .

Recurrence relations have many applications. Suppose that you put £100 into a savingsaccount yielding 4% compounded annually. Let an be the amount (in pounds) in theaccount after n years.

Then an is equal to the amount in the account after n − 1 years plus the interest for thenth year. For example, a1 is equal to 100 plus the interest which is 4. Hence a1 = 104.

In general, an = an−1 + (0.04)an−1 so that

an = (1.04)an−1 , n ≥ 1

with a0 = 100.

Solving this we obtaina1 = 100(1.04)

a2 = (1.04)a1 = 100(1.04)2

a3 = (1.04)a2 = 100(1.04)3

and in generalan = 100(1.04)n

Page 3: Recurrence Relation

3

The tower of Hanoi is a puzzle consisting of three pegs mounted on a board and n discs ofdifferent sizes, The picture in Figure 1 shows the n = 3 case.

Fig 1

C

B

A

Peg 3Peg 2Peg 1

The object is to transfer the tower to one of the other pegs, moving only one disc at a timeand never moving a larger disc onto a smaller disc.

Let an be the minimum number of moves needed to solve the puzzle with n discs. Clearlya1 = 1 and a2 = 3.

For the n = 3 case we use the moves

A to peg 2 , B to peg 3 , A to peg 3 , C to peg 2 ,

A to peg 1 , B to peg 2 , A to peg 2 .

This shows that a3 = 7.For the n disc problem we will derive a recurrence relation for an.

We first move the top n− 1 discs to peg 2. This takes an−1 moves.

We transfer the largest disc to peg 3 ; this takes one move.

Another an−1 moves transfers the n− 1 discs on peg 2 to peg 3.

This shows thatan = 2an−1 + 1

The initial condition is a1 = 1.

From Example 4.1, the solution is an = 2n − 1 .

Example 4.2 I can climb up stairs by taking either one stair or two stairs at a time. Letan be the number of ways I can climb n stairs.

(a) Find a1 and a2 .

(b) Find a recurrence relation for an and use it to find a7 .

Solution Clearly a1 = 1 and a2 = 2 .

Page 4: Recurrence Relation

4

I can get to the n th stair by either

• steping 1 stair from the (n− 1) th stair, or by

• steping 2 stairs from the (n− 2) th stair.

There are an−1 ways of reaching the (n−1) th stair and an−2 ways of reaching the (n−2) thstair.

Hence an = an−1 + an−2 .

Using a1 = 1 and a2 = 2, we obtain a7 = 21.

Exercises 4.1

1. Let an = 2an−1 + an−2 with a1 = 1 and a2 = 1 . Find a3, a4, a5 and a6 .

2. Verify that the solution of the recurrence relation

an = 3an−1

with a1 = 4 is an = 4(3)n−1 .

3. Verify that the solution ofan = 5an−1 − 12

with a1 = 13 is an = 2(5)n + 3 .

4. Let xn satisfy the recurrence relation

xn =2nxn−1

n + 1

with x1 = 1 .

(a) Let an = (n + 1) xn . Show that an = 2an−1 with a1 = 2.

(b) Solve the recurrence relation for an . Hence find xn .

5. The double tower of Hanoi puzzle contains 2n discs. There are n different sizes, twoof each size. The usual rules apply. Discs of the same size are identical. You are allowed toplace discs of the same size on top of each other. Let an be the minimum number of movesneed to solve the puzzle. Show that an = 2an−1 + 2.

6. A robot can move forward in steps of size 10 cm, 20 cm, or 30 cm. Let an be thenumber of ways the robot can move 10n cm. Note that moving 10 cm and then 20 cm isdifferent from moving 20 cm and then 10 cm.

(a) Find a recurrence relation for an.

(b) What are the initial conditions?

(c) Find the number of ways the robot can move 70 cm.

Page 5: Recurrence Relation

5

4.2 Linear Homogeneous Recurrence Relations

The following are examples of linear homogeneous recurrence relations :

an = 5an−1 (3)

an = 7an−1 − 12an−2 (4)

They are linear since terms such as a2n−1 are not present. They are homogeneous since all

the terms are multiples of aj .

The general solution ofan = p an−1

is an = c pn where c is a constant.

Example 4.3 Find the solution of (3) with a1 = 20.

Solution The general solution is an = c 5n .

Since a1 = 20 ,5c = 20

Hence c = 4 and an = 4 (5)n .

Suppose an = rn is a solution of (4). Then

rn = 7rn−1 − 12rn−2

Dividing by rn−2 , we obtainr2 = 7r − 12

This quadratic equation has the solutions r = 3 and r = 4 .

Hence an = 3n and an = 4n are solutions of (4).

The general solution of (4) isan = c 3n + d 4n

where c and d are constants.

The same method is used to solve the general recurrence relation

an = pan−1 + qan−2 (5)

We first write down the characteristic equation

r2 = pr + q

If this quadratic equation has two distinct real solutions r = r1 and r = r2 then thegeneral solution of (5) is

an = c rn1 + d rn

2

where c and d are constants.

Page 6: Recurrence Relation

6

Example 4.4 Find the general solution of an = an−1 + 2an−2 .

Solution The characteristic equation is

r2 = r + 2

r2 − r − 2 = (r + 1) (r − 2) = 0

so r = −1 and r = 2 .

The general solution isan = c 2n + d (−1)n

Example 4.5 Find the solution of an = 4an−1 − 3an−2 with a1 = 0 and a2 = 12 .

Solution The characteristic equation is

r2 − 4r + 3 = (r − 3) (r − 1) = 0

so r = 3 and r = 1 . The general solution is

an = c 3n + d

Putting n = 1 and n = 2 in an = c 3n + d and using the initial conditions gives

3c + d = 0

9c + d = 12

Solving these gives c = 2 and d = −6 so

an = 2 (3)n − 6

Example 4.6 Find the solution of an = an−1 + an−2 with a0 = 0 and a1 = 1 .

Solution The characteristic equation is

r2 − r − 1 = 0

with solutions

1 +√

5

2and

1−√5

2

The general solution is

an = c

(1 +

√5

2

)n

+ d

(1−√5

2

)n

Using the initial conditions

c + d = 0

Page 7: Recurrence Relation

7

c

(1 +

√5

2

)+ d

(1−√5

2

)= 1

Solving these gives

c =1√5

d = − 1√5

so

an =1√5

(1 +

√5

2

)n

− 1√5

(1−√5

2

)n

The above method only works when the characteristic equation has two distinct real solu-tions. If the characteristic equation has a repeated solution r = r1 then an = n rn

1 is asolution of the recurrence relation . The general solution is

an = c rn1 + d n rn

1

Example 4.7 Find the general solution of an = 4an−1 − 4an−2 .

Solution The characteristic equation

r2 − 4r + 4 = (r − 2)2 = 0

has only one solution r = 2 .

The general solution of the recurrence relation is

an = c 2n + d n 2n

Exercises 4.2

1. Find the solution of an = 4an−1 with a1 = 20 .

2. Find the solution of an = 5an−1 − 6an−2 with a1 = 1 and a2 = 5 .

3. Find the solution of an = 6an−1 − 5an−2 with a1 = 1 and a2 = 41 .

4. Find the solution of an = an−1 + 2an−2 with a1 = −3 and a2 = 9 .

5. Find the solution of an = 6an−1 − 9an−2 with a0 = −2 and a1 = 6 .

Page 8: Recurrence Relation

8

4.3 Solving Inhomogeneous Recurrence Relations

The following are examples of inhomogeneous recurrence relations :

an = 4an−1 − 15 (6)

an = 7an−1 − 12an−2 + 5 (7)n (7)

We begin by finding a particular solution to an inhomogeneous recurrence relation. Thesimplest way to do this is to look for a solution of the same form as the inhomogeneousterm.

Example 4.8 Find a particular solution of an = 4an−1 − 15 .

Solution Let an = p where p is a constant. We have to find p.

Putting an = p into an = 4an−1 − 15 gives

p = 4p− 15

so that 3p− 15 = 0 and p = 5 .

A particular solution is an = 5 .

Example 4.9 Find a particular solution of an = 3an−1 + 9− 2n .

Solution Let an = pn + q where p and q are a constants. We have to find p and q.

pn + q = 3 [p(n− 1) + q] + 9− 2n

pn + q = 3pn− 3p + 3q + 9− 2n

0 = n(2p− 2)− 3p + 2q + 9

Hence 2p− 2 = 0 so p = 1.

Then 2q + 6 = 0 so q = −3.

A particular solution is an = n− 3 .

To find a particular solution of an = 4an−1 − 3an−2 + 5 (2)n we would look for a solutionan = p (2)n .

This method of finding a particular solution works in most cases. It would not work for

an = 4an−1 − 3an−2 + 5 (3)n .

In this case we would look for a solution an = pn (3)n .

To find the general solution of an inhomogeneous recurrence relation we add a particularsolution to the general solution of the associated homogeneous recurrence relation.

Page 9: Recurrence Relation

9

Example 4.10 Find the general solution of solution of an = 3an−1 + 9− 2n .

Solution By Example 4.9 , a particular solution is an = n− 3.

The general solution ofan = 3an−1

is an = c 3n .

The general solution of an = 3an−1 + 9− 2n is

an = c 3n + n− 3

Example 4.11 Find the solution of an = (0.5) an−1 + 3 with a0 = 8.

Solution To find a particular solution let an = p . Then

p = (0.5) p + 3

so that p = 6 .

Hence a particular solution is an = 6 .

The general solution of an = (0.5) an−1 is an = c (0.5)n .

The general solution of an = (0.5) an−1 + 3 is

an = c (0.5)n + 6

Putting n = 0 in an = c (0.5)n + 6 gives

c + 6 = 8

so that c = 2 . Hencean = 2 (0.5)n + 6 .

Example 4.12 The average number of comparisons xn made by the quick sort algorithmwhen sorting n pieces of data satisfies

nxn = (n + 1) xn−1 + 2n

for n ≥ 1 with x0 = 0.

(a) Let xn = (n + 1) an . Find a recurrence relation for an.

(b) Solve the recurrence relation for an and hence find xn.

Solution If xn = (n + 1) an then

n(n + 1) an = n(n + 1) an−1 + 2n

so that

Page 10: Recurrence Relation

10

an = an−1 +2

n + 1(8)

Also, since x0 = 0 we have that a0 = 0 .

From (8),

a1 = 1 , a2 = 1 +2

3, a3 = 1 +

2

3+

2

4

an = 1 +2

3+

2

4+ · · ·+ 2

n + 1

Using xn = (n + 1) an ,

xn = (n + 1)

(1 +

2

3+

2

4+ · · ·+ 2

n + 1

)

Exercises 4.3

1. Find the solution of an = 2 an−1 + 1 with a1 = 1.

2. Find the solution of an = 5 an−1 − 8 with a1 = 7.

3. Find the solution of an = (0.4) an−1 + 12 with a0 = 8. What is the behaviour of an

for large n ?

4. Find the general solution of solution of an = 2an−1 + 3n− 13 .

5. Let xn satisfy the recurrence relation

(n + 1) xn = 5(n + 2) xn−1 + 8(n + 1)(n + 2) .

for n ≥ 1 with x1 = 24.

(a) Let xn = (n + 2) an . Find a recurrence relation for an.

(b) Solve the recurrence relation for an and hence find xn.

4.4 Further Examples

Many algorithms take a problem and then divide it into a number of smaller problems.The binary search method reduces the search for an element in a list of size m to a searchin a list of size m/2. The method assumes that the list has terms in order of increasingsize; for example numbers in increasing size or words in alphabetical order.

We show how the method works for finding the number 9 in the list of eight numbers

1 2 3 4 5 6 9 10

We first divide the list in two :

1 2 3 4 5 6 9 10

Page 11: Recurrence Relation

11

We then compare 9 with the largest number in the first list. Since 4 < 9 we keep thesecond list and divide it in two :

5 6 9 10

Since 6 < 9 we keep the second list 9 10. After one more application we locate 9.

Suppose that we have m = 2n elements in the list. Let an be the number of comparisonsneeded to find a particular element (in the worst case).

The binary search method reduces the search in a list of size m = 2n to a search in a listof size 2n−1. This reduction requires two comparisons; one to find out which half of thelist to use and the other to check if any terms in the list remain. Hence

an = an−1 + 2

A bit has two possible values, 0 and 1. A bit string is a sequence of bits. The length ofa bit string is the number of bits in the string, for example, 11010110 has length eight.There are 2n bit strings of length n.

Example 4.13 Let an be the number of bit strings of length n that do not have twoconsecutive 0’s.

Find a1 and a2 . Find a recurrence relation for an and hence find hence find a6.

Solution We call a bit string valid if it does not have two consecutive 0’s.

Both 0 and 1 are valid bit strings of length 1 so a1 = 2 .

a2 = 3 . since 01 , 10 and 11 are valid.

We can form a valid bit string of length n by

(i) Adding 1 at the end of a valid bit string of length n− 1.

(ii) Adding 1 0 at the end of a valid bit string of length n− 2.

There are an−1 of type (i) and an−2 of type (ii) so

an = an−1 + an−2

Using the above recurrence relation we obtain a6 = 21 .

Example 4.14 A computer system considers a string of decimal digits a valid codewordif it contains an even number of 0 digits. Thus 07903 and 68433 are valid codewords oflength five while 86031 is not valid.

Let an be the number of valid codewords of length n.

(a) Find a recurrence relation for an.

(b) Find a1 and hence find the solution of the recurrence relation.

Page 12: Recurrence Relation

12

Solution We can form a valid codeword of length n by

(i) Adding a digit other than 0 at the end of a valid string of length n− 1.

(ii) Adding the digit 0 at the end of an invalid string of length n− 1.

For item (i) there are nine digits that we can put in at the end. Hence there are 9an−1

ways of forming a valid codeword of length n in this way.

The number of ways we can do item (ii) is equal to the number of invalid strings of lengthn− 1.

Since there are a total of 10n−1 strings of length n− 1 and an−1 of these are valid, thereare

10n−1 − an−1

invalid strings of length n− 1.

Hencean = 9an−1 + 10n−1 − an−1

an = 8an−1 + 10n−1

We have to solve the above recurrence relation with a1 = 9.

We first find a particular solution of the form an = p 10n−1.

p 10n−1 = 8p 10n−2 + 10n−1

0 = 10n−2 (10p− 8p− 10)

so that p = 5.

The general solution of an = 8an−1 is an = c 8n−1.

The general solution of an = 8an−1 + 10n−1 is

an = c 8n−1 + 5 (10)n−1

Using a1 = 9 ,9 = c + 5

Hence c = 4 andan = 4 (8)n−1 + 5 (10)n−1

Page 13: Recurrence Relation

13

Exercises 4.4

1. Let an be the number of bit strings of length n that do not have three consecutive0’s.

(a) Find a1, a2 and a3.

(b) Find a recurrence relation for an . Use the recurrence relation to find a7.

2. A ternary string is a string that only contains the symbols 0, 1 and 2. Let an be thenumber of ternary strings of length n that do not have two consecutive 0’s.

Find a recurrence relation for an and use the recurrence relation to find a6.

3. Find a recurrence relation for the number of ternary strings of length n that containan even number of 0’s.

4. This question is about the number of regions formed if we draw n lines in the plane.With one line we get 2 regions and with two lines we get 4 regions.

Find a recurrence relation for an , where an is number of regions that a plane is dividedinto by n lines, if no two of the lines are parallel and no three of the lines go through thesame point.

4.5 Nonlinear Recurrence Relations

The recurrence relation

an = 3an−1 (1− an−1) (9)

is an example of a nonlinear recurrence relation. Even though it looks very simple, solutionsto it can be very complicated.

More generally we discuss

an = f(an−1) (10)

where f is a given function.

For (9) the function f is f(x) = 3x(1− x).

A fixed point of (10) is a solution an that does not depend on n.

If x is a fixed point of (10) then putting an = x we find that x = f(x) .

To find the fixed points of (9) we have to solve

x = 3x(1− x)

so x = 0 or 1 = 3− 3x.

Hence the fixed points of (9) are 0 and 2/3.

A fixed point x of (10) is said to be stable if when we solve (10) with a0 close to x thesolution an tends towards x as n increases.

Page 14: Recurrence Relation

14

A fixed point x of (10) that is not stable is said to be unstable.

The solution of

an = (0.6) an−1

is an = a0 (0.6)n−1 . Hence the fixed point 0 is stable.

The solution ofan = (1.4) an−1

is an = a0 (1.4)n−1 . Hence the fixed point 0 is unstable.

For a fixed point x of an = f(an−1) we have that

• if |f ′(x)| < 1 then x is stable.

• if |f ′(x)| > 1 then x is unstable.

Example 4.15 Find the fixed points of an = (1.5)an−1−a2n−1 and examine their stability.

Solution f(x) = (1.5)x− x2 and fixed points x satisfy x = f(x) .

x = (1.5)x− x2

so x = 0 or x = 0.5 .

Also

f ′(x) = 1.5− 2x

f ′(0) = 1.5

f ′(0.5) = 0.5

Hence the fixed point 0 is unstable and the fixed point 0.5 is stable.

Exercises 4.5

1. Find the fixed points of

an =an−1

2+

1

an−1

and examine their stability.

2. Find the fixed points of

an = p an−1

(1 + p

p− an−1

)where p is a positive parameter.

Find the values of p for which the fixed points are stable or unstable.