Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
5. Recurrence Relations We have already encountered a number of recurrence relations when we talked about recursion. Now we look at more situations in which they arise and techniques for getting explicit formulas for the sequence they define. 5.1 Modeling with Recurrence Relations A recurrence relation is a recursive definition of a sequence of numbers. Recall a sequence of numbers is just a list of numbers a0, a1, a2, a3, …, an, Two examples that we have encountered are the following. (1) The sequence of factorials of the positive integers. 1, 1, 2, 6, 24, 120, …, n!, Here an = n!. (2) The Fibonacci sequence 1, 1, 2, 3, 5, 8, …, fn, Here each element is the sum of the two previous. We have seen recursive definitions of each of these sequences. For the sequence of factorials one has n! = n (n-1)! 1 if n 1 if n = 0 For the Fibonacci sequence one has fn = fn-1 1 + fn-2 if n 2 if n = 0 or n = 1 In general, a recurrence relation is a recursive definition of a sequence a0, a1, a2, …, an, …where we are given a formula for the an in terms of some or all of the aj for j n – 1. We also need one or more starting values that define the first one or several aj. For example, for n! we only needed one starting value was 0! = 1, since n! was defined in terms of (n-1)!. However for the Fibonacci sequence we needed two 5.1 - 1 starting values since fn was defined in terms of fn-1 and fn-2. A recurrence relation is also called a difference equation. In this section we look at various situation that can be modeled in terms of recurrence relations. Many situations that involved permutations and combinations can also be modeled with recurrence relations if we take the right point of view. Example 1. Let Sn be the number of n bit strings that don't contain two consecutive 1's. Find a recurrence relation for Sn. Note that S1 = 2 since neither of the one bit strings 0 or 1 contain two consecutive 1's. However S2 = 3 since only the three two bit sequences 00, 01 and 10 do not contain two consecutive 1's. To get a recurrence relation for Sn consider an n bit string b1b2bn. We can divide these in two parts. The first case is when b1 = 0, i.e. the string is of the form 0b2bn. In this case b2bn can be any n-1 bit string that does not contain two consecutive 1's. There are Sn-1 of these. The other case is when b1 = 1, i.e. the string is of the form 1b2bn. In this case one must have b2 = 0 since if b2 were equal to 1 there would be two consecutive 1's. So the string is of the form 10b3bn. In this case b3bn can be any n-2 bit string that does not contain two consecutive 1's. There are Sn-2 of these. Thus Sn Sn-1 + Sn-2 = 2 3 if n 3 if n = 1 if n = 2 Note that the basic recurrence relation Sn = Sn-1 + Sn-2 is the same as the Fibonacci sequence, but the starting values are different. Another situation where recurrence relations arise is when counting statements in a recursive algorithm Example 2. Let Sn be the number of statements executed when the following recursive algorithm for computing the Fibonacci numbers is called with the value n. Fibonacci(n) If n = 0 or n = 1 then return 1 else return Fibonacci(n – 1) + Fibonacci(n-2) Find a recurrence relation for Sn. 5.1 - 2 If n = 0 or n = 1 then the statements "If n = 0 or n = 1 then" and "return 1" are executed, so S0 = 2 and S1 = 2. If n 2 then the statements "If n = 0 or n = 1 then" and "return Fibonacci(n – 1) + Fibonacci(n-2)" are executed which is two statements to start with. However the statement "return Fibonacci(n – 1) + Fibonacci(n-2)" calls the Fibonacci algorithm twice. Once with the value n-1 and once with the value n-2. The call with n-1 requires Sn-1 statements to be executed while the call with n-2 requires Sn-2 statements to be executed. So Sn = 2 + Sn-1 + Sn-2. Thus Sn = 2 2 if n 2 if n = 0 or n = 1 + Sn-1 + Sn-2 Note that the basic recurrence relation Sn = 2 + Sn-1 + Sn-2 is similar to the Fibonacci sequence fn = fn-1 + fn-2 , but there is an additional 2 in the formula. Example 3 – A Home Mortgage. You get a loan to buy a house. Let a0 = initial amount of the loan For example, it might be a0 = $ 100,000 You payoff the loan as follows. Each month the mortgage company charges you a certain interest rate on the amount you still owe. Let r = monthly interest rate on the amount you still owe For example, it might be r = ½% = 0.005 (This would be an annual rate of 6%.) If the initial amount of the loan was a0 = 100000 and r = ½% then the interest for the first month would be ra0 = (0.005)(100000) = 500 This amount is added to the loan, so the amount owed becomes (1) a0 + ra0 = (1 + r)a0 In the example, it would be a0 + ra0 = 100000 + 500 = 100500 Each month you make a monthly payment. Let 5.1 - 3 P = monthly payment For example, it might be P = $ 700 This is subtracted from the amount you owe. So the amount you owe after the first month is a1 = (1 + r)a0 - P In our example this would be a1 = 100500 - 700 - 99800 The formula (1) applies to every month. If an = amount owed after month n Then an = (1 + r)an-1 - P This is a recurrence relation. If we couple it with the initial amount of the loan then if n 1 (1 + r)an-1 - P an = initial amount of loan if n = 0 In our example, it is if n 1 1.05an-1 - 700 an = 100000 if n = 0 Example 4 – Cobweb models in Economics. We are modeling how the price of some commodity changes over time. Let n = time (measured in some units) p = pn = price of some commodity at time n 5.1 - 4