Вы находитесь на странице: 1из 14

Recurrence Relations

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

4.1

Introduction

The following are examples of recurrence relations : an = 2an1 + 1 , n 2 an = 5an1 6an2 , n 3 (1) (2)

For (1) we would need one initial value to nd an . For example, if a1 = 1 then a2 = 3 and a3 = 7 . For (2) we would need two initial values to nd an . For example, if a1 = 1 and a2 = 5 then a3 = 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 2an1 + 1 using the given formula and show that it is equal to an . Now an1 = 2n1 1 so 2an1 + 1 = 2 2n1 1 + 1 = 2n 1 so an = 2an1 + 1 . Recurrence relations have many applications. Suppose that you put 100 into a savings account yielding 4% compounded annually. Let an be the amount (in pounds) in the account after n years. Then an is equal to the amount in the account after n 1 years plus the interest for the nth year. For example, a1 is equal to 100 plus the interest which is 4. Hence a1 = 104. In general, an = an1 + (0.04)an1 so that an = (1.04)an1 , n 1 with a0 = 100. Solving this we obtain a1 = 100(1.04) a2 = (1.04)a1 = 100(1.04)2 a3 = (1.04)a2 = 100(1.04)3 and in general an = 100(1.04)n

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

Fig 1

A B C

Peg 1

Peg 2

Peg 3

The object is to transfer the tower to one of the other pegs, moving only one disc at a time and 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. Clearly a1 = 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 rst move the top n 1 discs to peg 2. This takes an1 moves. We transfer the largest disc to peg 3 ; this takes one move. Another an1 moves transfers the n 1 discs on peg 2 to peg 3. This shows that an = 2an1 + 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. Let an 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 nd a7 . Solution Clearly a1 = 1 and a2 = 2 .

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 an1 ways of reaching the (n1) th stair and an2 ways of reaching the (n2) th stair. Hence an = an1 + an2 . Using a1 = 1 and a2 = 2, we obtain a7 = 21.

Exercises 4.1 1. 2. Let an = 2an1 + an2 with a1 = 1 and a2 = 1 . Find a3 , a4 , a5 and a6 . Verify that the solution of the recurrence relation an = 3an1 with a1 = 4 is an = 4(3)n1 . 3. Verify that the solution of an = 5an1 12 with a1 = 13 is an = 2(5)n + 3 . 4. Let xn satisfy the recurrence relation xn = with x1 = 1 . (a) Let an = (n + 1) xn . Show that an = 2an1 with a1 = 2. (b) Solve the recurrence relation for an . Hence nd xn . 5. The double tower of Hanoi puzzle contains 2n discs. There are n dierent sizes, two of each size. The usual rules apply. Discs of the same size are identical. You are allowed to place discs of the same size on top of each other. Let an be the minimum number of moves need to solve the puzzle. Show that an = 2an1 + 2. 6. A robot can move forward in steps of size 10 cm, 20 cm, or 30 cm. Let an be the number of ways the robot can move 10n cm. Note that moving 10 cm and then 20 cm is dierent 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. 2n xn1 n+1

4.2

Linear Homogeneous Recurrence Relations

The following are examples of linear homogeneous recurrence relations : an = 5an1 an = 7an1 12an2 (3) (4)

They are linear since terms such as a2 n1 are not present. They are homogeneous since all the terms are multiples of aj . The general solution of an = p an1 is an = c p where c is a constant. Example 4.3 Find the solution of (3) with a1 = 20. Solution The general solution is an = c 5n . 5c = 20 Hence c = 4 and an = 4 (5) . Suppose an = rn is a solution of (4). Then rn = 7rn1 12rn2 Dividing by rn2 , we obtain r2 = 7r 12
n n

Since a1 = 20 ,

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) is where c and d are constants. an = c 3n + d 4n

The same method is used to solve the general recurrence relation an = pan1 + qan2 We rst write down the characteristic equation r2 = pr + q If this quadratic equation has two distinct real solutions r = r1 and r = r2 then the general solution of (5) is n n an = c r1 + d r2 where c and d are constants. (5)

6 Example 4.4 Find the general solution of an = an1 + 2an2 . 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 is an = c 2n + d (1)n

Example 4.5 Find the solution of an = 4an1 3an2 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 = an1 + an2 with a0 = 0 and a1 = 1 . Solution The characteristic equation is r2 r 1 = 0 with solutions 1+ 5 2 The general solution is an = c Using the initial conditions c+d=0 1+ 5 2
n

and

1 5 2 1 5 2
n

+d

7 1+ 5 2 1 5 2

c Solving these gives

+d

=1

1 c= 5 so 1 an = 5 1+ 5 2
n

1 d = 5 1 5 2
n

1 5

The above method only works when the characteristic equation has two distinct real solun tions. If the characteristic equation has a repeated solution r = r1 then an = n r1 is a solution of the recurrence relation . The general solution is
n n an = c r1 + d n r1

Example 4.7 Find the general solution of an = 4an1 4an2 . 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. 2. 3. 4. 5. Find the solution of an = 4an1 with a1 = 20 . Find the solution of an = 5an1 6an2 with a1 = 1 and a2 = 5 . Find the solution of an = 6an1 5an2 with a1 = 1 and a2 = 41 . Find the solution of an = an1 + 2an2 with a1 = 3 and a2 = 9 . Find the solution of an = 6an1 9an2 with a0 = 2 and a1 = 6 .

4.3

Solving Inhomogeneous Recurrence Relations

The following are examples of inhomogeneous recurrence relations : an = 4an1 15 an = 7an1 12an2 + 5 (7)n (6) (7)

We begin by nding a particular solution to an inhomogeneous recurrence relation. The simplest way to do this is to look for a solution of the same form as the inhomogeneous term. Example 4.8 Find a particular solution of an = 4an1 15 . Solution Let an = p where p is a constant. We have to nd p. 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 = 3an1 + 9 2n . Solution Let an = pn + q where p and q are a constants. We have to nd 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 nd a particular solution of an = 4an1 3an2 + 5 (2)n we would look for a solution an = p (2)n . This method of nding a particular solution works in most cases. It would not work for an = 4an1 3an2 + 5 (3)n . In this case we would look for a solution an = pn (3)n . To nd the general solution of an inhomogeneous recurrence relation we add a particular solution to the general solution of the associated homogeneous recurrence relation.

Putting an = p into an = 4an1 15 gives

9 Example 4.10 Find the general solution of solution of an = 3an1 + 9 2n . Solution By Example 4.9 , a particular solution is an = n 3. an = 3an1 is an = c 3n . The general solution of an = 3an1 + 9 2n is an = c 3n + n 3

The general solution of

Example 4.11 Find the solution of an = (0.5) an1 + 3 with a0 = 8. Solution To nd 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) an1 is an = c (0.5)n . The general solution of an = (0.5) an1 + 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 . Hence an = 2 (0.5)n + 6 .

Example 4.12 The average number of comparisons xn made by the quick sort algorithm when sorting n pieces of data satises n xn = (n + 1) xn1 + 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 nd xn . Solution If xn = (n + 1) an then n(n + 1) an = n(n + 1) an1 + 2n so that

10

an = an1 + Also, since x0 = 0 we have that a0 = 0 . From (8), a1 = 1 , a2 = 1 + an = 1 + Using xn = (n + 1) an , xn = (n + 1) 1 + 2 , 3

2 n+1

(8)

a3 = 1 +

2 2 + 3 4

2 2 2 + + + 3 4 n+1 2 2 2 + + + 3 4 n+1

Exercises 4.3 1. 2. Find the solution of an = 2 an1 + 1 with a1 = 1. Find the solution of an = 5 an1 8 with a1 = 7.

3. Find the solution of an = (0.4) an1 + 12 with a0 = 8. What is the behaviour of an for large n ? 4. 5. Find the general solution of solution of an = 2an1 + 3n 13 . Let xn satisfy the recurrence relation (n + 1) xn = 5(n + 2) xn1 + 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 nd 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 search in a list of size m/2. The method assumes that the list has terms in order of increasing size; for example numbers in increasing size or words in alphabetical order. We show how the method works for nding the number 9 in the list of eight numbers 1 2 3 4 5 6 9 10 We rst divide the list in two : 1 2 3 4 5 6 9 10

11 We then compare 9 with the largest number in the rst list. Since 4 < 9 we keep the second 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 comparisons needed to nd 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 list of size 2n1 . This reduction requires two comparisons; one to nd out which half of the list to use and the other to check if any terms in the list remain. Hence an = an1 + 2 A bit has two possible values, 0 and 1. A bit string is a sequence of bits. The length of a 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 two consecutive 0s. Find a1 and a2 . Find a recurrence relation for an and hence nd hence nd a6 . Solution We call a bit string valid if it does not have two consecutive 0s.

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 an1 of type (i) and an2 of type (ii) so an = an1 + an2 Using the above recurrence relation we obtain a6 = 21 . Example 4.14 A computer system considers a string of decimal digits a valid codeword if it contains an even number of 0 digits. Thus 07903 and 68433 are valid codewords of length ve 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 nd the solution of the 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 9an1 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 length n 1. Since there are a total of 10n1 strings of length n 1 and an1 of these are valid, there are 10n1 an1 invalid strings of length n 1. Hence an = 9an1 + 10n1 an1 an = 8an1 + 10n1 We have to solve the above recurrence relation with a1 = 9. We rst nd a particular solution of the form an = p 10n1 . p 10n1 = 8p 10n2 + 10n1 0 = 10n2 (10p 8p 10) so that p = 5. The general solution of an = 8an1 is an = c 8n1 . The general solution of an = 8an1 + 10n1 is an = c 8n1 + 5 (10)n1

Using a1 = 9 , 9=c+5 Hence c = 4 and an = 4 (8)n1 + 5 (10)n1

13 Exercises 4.4 1. 0s. Let an be the number of bit strings of length n that do not have three consecutive

(a) Find a1 , a2 and a3 . (b) Find a recurrence relation for an . Use the recurrence relation to nd a7 . 2. A ternary string is a string that only contains the symbols 0, 1 and 2. Let an be the number of ternary strings of length n that do not have two consecutive 0s. Find a recurrence relation for an and use the recurrence relation to nd a6 . 3. Find a recurrence relation for the number of ternary strings of length n that contain an even number of 0s. 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 divided into by n lines, if no two of the lines are parallel and no three of the lines go through the same point.

4.5

Nonlinear Recurrence Relations

The recurrence relation an = 3an1 (1 an1 ) (9)

is an example of a nonlinear recurrence relation. Even though it looks very simple, solutions to it can be very complicated. More generally we discuss an = f (an1 ) where f is a given function. For (9) the function f is f (x) = 3x(1 x). A xed point of (10) is a solution an that does not depend on n. If x is a xed point of (10) then putting an = x we nd that x = f (x) . To nd the xed points of (9) we have to solve x = 3x(1 x) so x = 0 or 1 = 3 3x. Hence the xed points of (9) are 0 and 2/3. A xed point x of (10) is said to be stable if when we solve (10) with a0 close to x the solution an tends towards x as n increases. (10)

14 A xed point x of (10) that is not stable is said to be unstable. The solution of an = (0.6) an1 is an = a0 (0.6)n1 . Hence the xed point 0 is stable. The solution of an = (1.4) an1 is an = a0 (1.4)n1 . Hence the xed point 0 is unstable. For a xed point x of an = f (an1 ) we have that if |f (x)| < 1 then x is stable. if |f (x)| > 1 then x is unstable. Example 4.15 Find the xed points of an = (1.5)an1 a2 and examine their stability. n1 Solution f (x) = (1.5)x x2 and xed 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 xed point 0 is unstable and the xed point 0.5 is stable.

Exercises 4.5 1. Find the xed points of an = and examine their stability. 2. Find the xed points of an = p an1 where p is a positive parameter. Find the values of p for which the xed points are stable or unstable. 1+p an1 p 1 an1 + 2 an1

Вам также может понравиться