Chapter 8: Recursion March 10, 2008
Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence Relations by Iteration 3 8.4 General Recursive Definitions
Recursively Defined Sequences As mentioned in 4.1, a sequence can be defined using recursion. This means that the k-th terms is defined using an equation involving previous terms in the sequence. For example, a k = a k 1 + 2a k 2, k 2 and a 0 = 1, a 1 = 2 The values for a 0 and a 1 in this example are called the initial values of the sequence. The formula a k = a k 1 + 2a k 2, k 2 which relates the k-th term to some of its predecessors is called the recurrence relation for this sequence.
Example For the recursively defined sequence with initial conditions a k = a k 1 + 2a k 2, k 2 a 0 = 1, a 1 = 2 compute the terms a 2, a 3, and a 4. Solution: a 2 = a 1 + 2a 0 = 2 + 2( 1) = 0 a 3 = a 2 + 2a 1 = 0 + 2 2 = 4 a 4 = a 3 + 2a 2 = 4 + 2 0 = 4
Example Show that the sequence satisfies the recurrence relation 0, 1, 5, 19,..., 3 n 2 n,... d k = 5d k 1 6d k 2, k 2 Solution: Substitute d k 1 = 3 k 1 2 k 1 d k 2 = 3 k 2 2 k 2 into the recurrence relation:
d k = 5 (3 k 1 2 k 1 ) 6 (3 k 2 2 k 2 ) = 5 3 k 1 5 2 k 1 6 3 k 2 + 6 2 k 2 = 5 3 k 1 5 2 k 1 2 3 k 1 + 3 2 k 1 = 3 3 k 1 2 2 k 1 = 3 k 2 k
Basic Problem of Recursion Given a recursively defined sequence a n, find an analytical formula a n = f (n) for the sequence. In general, this may be a difficult problem. In this course, we will look at relatively simple instances of recursively defined sequences. Our approach will generally consist of the following two steps: 1 Guess an explicit formula for a n = f (n). [Often, this consists of writing out the first several terms and trying to discover a pattern.] 2 Prove that this formula is correct using induction.
Example: The Tower of Hanoi In 1883., Edouard Lucas invented a mathematical puzzle, which he called The Tower of Hanoi. The set-up of the puzzle consisted of 8 disks of different sizes placed in decreasing size on top of one another on one pole in a row of three poles. The task is to transfer these disks from the pole where they lie originally to one of the others, never placing a larger disk on top of a smaller one. Figure: The Tower of Hanoi
In 1884., De Parville described it in a more colourful way, after slightly changing the statement of the original puzzle: On the steps of the altar in the temple of Benares, for many, many years Brahmins have been moving a tower of sixty-four golden disks from one pole to another, one by one, never placing a larger on top of a smaller. When all disks have been transferred, the Tower and the Brahmins will fall, and it will be the end of the world. Assuming this legend is true, is there anything to worry about?
Suppose that the poles are labelled as A, B, and C. We will approach this problem in a general way and assume that, at the beginning, we have k disks piled on the pole A. What is the most efficient way of transferring these k disks from A to C? In other words, what is the minimum number of steps (moves) m k that are necessary in order to move the disks from A to C? Notice first, that in order to transfer the largest disk from A to C, we first have to move the k 1 smaller disks lying above it from A to B, in order to get them out of the way.
The most efficient algorithm for transferring all the disks from A to C will then require these three stages: 1 Transfer the top k 1 disks from A to B, using C for intermediate moves, until all these disks end up piled on B. 2 Move the largest (bottom) disk from A to C. 3 Finally, move those remaining k 1 disks from B to C, using A (which is now empty) to help us with intermediate moves.
Suppose m n is the minimum number of steps necessary to move n disks from one pole to another. Then m k = m k 1 + 1 + m k 1 = 2m k 1 + 1 and, of course, m 1 = 1. Therefore, the number of steps required to move k disks from one pole to another is an example of a recursively defined sequence.
The first few terms of this sequence are m 1 = 1 m 2 = 2m 1 + 1 = 3 m 3 = 2m 2 + 1 = 7 m 4 = 2m 3 + 1 = 15 m 5 = 2m 4 + 1 = 31 m 6 = 2m 5 + 1 = 63
In 8.2, we will see that the formula for the k-th term is: m k = 2 k 1 So, for k = 64 disks, the minimum number of moves necessary to transfer all the disks is m 64 = 2 64 1 1.845 10 19 Even if the Brahmins were able to transfer the disks very quickly, with one move taking one second, it would take around 584 billion years to finish the task!
Example: Fibonacci Numbers In 1202, an Italian mathematician Leonardo di Pisa (also known as Fibonacci) posed the following problem: A single pair of rabbits (male and female) is born at the beginning of a year. Suppose the following is true: 1 Rabbit pairs are not fertile during the first month of their life, but thereafter give birth to one new male/female pair at the end of every month. 2 No rabbits die. How many rabbits will there be at the end of the first year?
Question: If we know the number of rabbit pairs at the ends of previous months, how can we calculate the number of rabbit pairs at the end of the k-th month? Observation: The number of pairs born at the end of the k-th month is the number of pairs alive at the end of the (k 2)-th month (they are the pairs that will be fertile at the beginning of the k-th month). But, the number of rabbit pairs alive at the end of the k-th month is the sum of the pairs that were alive at the end of the (k 1)-th month and those pairs that were born at the end of the k-th month. Therefore, the number of pairs alive at the end of the month k is the number of pairs alive at the end of the month k 1 plus the number of pairs that were alive at the end of the month k 2.
If we denote the number of pairs alive at the end of the n-th month F n, we have the recurrence relation with the initial conditions F k = F k 1 + F k 2 F 0 = 1, F 1 = 1 [F 1 = 1, since the original pair does not procreate during the first month.] Then, F 2 = F 1 + F 0 = 2 F 3 = F 2 + F 1 = 3. F 11 = F 10 + F 9 = 144 F 12 = F 11 + F 10 = 233 So, after one year, there will be 233 rabbit pairs.
It can be shown (which will be too complicated for this course) that the general formula for F n is: ( F n = 1 5 1 + ) n+1 ( 5 2 1 ) n+1 5. 2
Example: Interest Compounded Several Times A Year When an annual interest rate of i is compounded m times a year, the interest rate paid per period is i/m. For example, if the annual interest rate is 3% = 0.03 is compounded quarterly, the interest rate paid per quarter is 0.03/4 = 0.0075. Let P k represent the amount on the deposit at the end of the k-th compounding period, for k 1, and P 0 being the original deposit into the account.
The interest earned during the k-th period is ( ) i P k 1 m The amount in the account at the end of the k-th period will be ( ) ( i P k = P k 1 + P k 1 = P k 1 1 + i ) m m
Suppose $ 10,000 is deposited at 3% compounded quarterly. (a) How much will the account be worth at the end of one year, assuming no further deposits have been made? (b) The annual percentage rate (APR) is the percentage increase in the value of an account over one year. What is the APR for this account?
Solution: (a) The recurrence relation is ( P k = P k 1 1 + i ) = 1.0075 P k 1 m with the initial condition P 0 = $10, 000. P 1 = 1.0075 $10, 000 = $10, 075.00 P 2 = 1.0075 $10, 075.00 = $10, 150.56 P 3 = 1.0075 $10, 150.56 = $10, 226.69 P 4 = 1.0075 $10, 226.69 = $10, 303.39 The amount in the account after one year will be $10,303.39. (b) The APR will be 10303.39 10000 10000 = 0.03034 = 3.034%
Method of Iteration Iteration is the most basic method for findingan explicit formula for a sequence defined recursively. Suppose we are given a sequence a 0, a 1, a 2,... defined by some recurrence relation and initial conditions. We start from the initial conditions and compute successive terms of the sequence until we discover a pattern.
Example Let a 0, a 1, a 2,... be the sequence defined recursively in the following way: for all integers k 1 a k = a k 1 + 2 a 0 = 1 Use iteration to guess an explicit formula for the sequence. Solution: a 0 = 1 a 1 = a 0 + 2 = 1 + 2 a 2 = a 1 + 2 = (1 + 2) + 2 = 1 + 2 + 2 a 3 = a 2 + 2 = (1 + 2 + 2) + 2 = 1 + 2 + 2 + 2 a 4 = a 3 + 2 = (1 + 2 + 2 + 2) + 2 = 1 + 2 + 2 + 2 + 2. A good guess would be that a n = 1 + n 2 = 1 + 2n
Definition A sequence a 0, a 1, a 2,... is called an arithmetic sequence if, and only if, there is a constant d such that a k = a k 1 + d, for k 1 Equivalently, a n = a 0 + n d, n 0.
Example Let r be a nonzero constant, and suppose a sequence a 0, a 1, a 2,... is defined recursively as a k = ra k 1, k 1 a 0 = a Use iteration to guess an explicit formula for this sequence. Solution: a 0 = a Guess: a n = r n a for n 0 a 1 = ra 0 = ra a 2 = ra 1 = r(ra) = r 2 a a 3 = ra 2 = r(r 2 a) = r 3 a a 4 = ra 3 = r(r 3 a) = r 4 a.
Definition A sequence a 0, a 1, a 2,... is called a geometric sequence if, and only if, there is a constant r 0 such that a k = ra k 1, k 1 Equivalently, a n = a 0 r n, n 0
In previous examples, we were able to guess an explicit formula for a recursively defined sequence using iteration. At this point, a formula is just a mere guess; in order to be able to convince ourselves that it indeed describes the recursively defined sequence, we need to prove it. To prove that a guessed formula is correct, we generally use mathematical induction (either weak or strong, depending on the problem)
Example In 8.1, we saw that The Tower of Hanoi problem leads to the following recursively defined sequence: m k = 2m k 1 + 1, k 2 m 1 = 1 Let s try to find an explicit formula for m n. Solution: First, we ll guess a formula using iteration: m 1 = 1 m 2 = 2m 1 + 1 = 2 1 + 1 = 2 + 1 m 3 = 2m 2 + 1 = 2 (2 + 1) + 1 = 2 2 + 2 + 1 m 4 = 2m 3 + 1 = 2 (2 2 + 2 + 1) + 1 = 2 3 + 2 2 + 2 + 1.
It seems that the formula for m n will be m n = 2 n 1 + 2 n 2 +... + 2 2 + 2 + 1 Using the formula for the sum of the geometric sequence with r = 2, we get m n = 2n 1 2 1 = 2n 1 Next, we ll try to confirm this conjecture using induction: When n = 1, we see that: m 1 = 2 1 1 = 1 which is true. Suppose the formula is true for some n = k 1: m k = 2 k 1
Finally, let s prove that the formula is also true for n = k + 1: m k+1 = 2m k + 1 (by definition) = 2(2 k 1) + 1 (by ind. hypothesis) = 2 k+1 2 + 1 = 2 k+1 1 which finishes our proof that m n = 2 n 1
Example Consider the following recursively defined sequence: b k = b 0 = 1 b k 1 1 + b k 1, k 1 Guess an explicit formula for b n and then use induction to prove it. Solution: b 0 = 1 b 1 = b 0 1 + b 0 = 1 2 b 2 = b 1 1 + b 1 = 1 3 b 3 = b 2 1 + b 2 = 1 4.
Guess: b n = 1 n+1, n 0. Now, let s try to prove the conjecture by induction: Obviously, b 0 = 1 0 + 1 = 1 Suppose that the formula is true for some n = k 1: Then, for n = k + 1: b k b k = 1 k + 1 b k+1 = (by definition) 1 + b k 1/(k + 1) = (ind.hypothesis) 1 + 1/(k + 1) = 1 k + 2
Recursively Defined Sets One way to define a set is to do it recursively. This means that we specify a few core objects that belong to the set and give some rules which show how to build new ( more complex ) set elements from old ( simpler ) ones.
A recursive definition of a set consists of the following components: 1 BASE: A statement that certain objects belong to the set. 2 RECURSION: A collection of rules specifying how to form new objects from those which are known to already belong to the set. 3 RESTRICTION: A statement that no objects belong to the set other than those coming from (1) or (2).
Example Recursive Definition of Boolean Expressions Boolean expressions were introduced in Chapter 1 as statements in propositional logic which are built from the alphabet p, q, r,..., logical symbols,, and, along with parentheses (, and ). 1 BASE: Each symbol of the alphabet is a Boolean expression. 2 RECURSION: If P and Q are Boolean expressions, then so are (a) (P Q), (b) (P Q), (c) P 3 RESTRICTION: There are no Boolean expressions over the alphabet other than those obtained from (1) and (2).
Derive the fact that is a Boolean expression. Solution: ((p q) ((p s) r)) 1 By (1), p, q, r, and s are Boolean expressions. 2 By (2b), (2c) and Step 1, (p q), and s are Boolean expressions. 3 By (2a) and Step 2, (p s) is a Boolean expression. 4 By (2a) and Step 3, ((p s) r) is a Boolean expression. 5 By (2c) and Step 4, ((p s) r) is a Boolean expression. 6 By (2b) and Step 5, ((p q) ((p s) r)) is a Boolean expression.
Example Set of Strings over an Alphabet Let S be the set of all strings over the alphabet {a, b}. S can be defined recursively in the following way: 1 BASE: The empty string ɛ is in S. 2 RECURSION: If s S, then (a) sa S, (b) sb S where sa and sb are strings obtained by appending the letters a and b to the string s (This is also called the concatenation of s with a or b) 3 RESTRICTION: Nothing is in S other than strings defined in (1) and (2).
Derive the fact that baa S. Solution: 1 By (1), ɛ S. 2 By Step 1 and (2b), b = ɛb S 3 By Step 2 and (2a), ba S 4 By Step 3 and (2a), baa S.
Example Parenthesis Structures Some configurations of parentheses in algebraic expressions are legal [e.g. (( )( )), and (( ))( )] while others are not [e.g. ( )) or ((( ))( )] A recursive definition of the set P of legal configurations of parenthesis can be given as follows: 1 BASE: ( ) is in P. 2 RECURSION: (a) If E is in P, so is (E). (b) If E and F are in P, so is EF. 3 RESTRICTION: No configurations of parentheses are in P, other than those derived from (1) and (2) above.
Derive the fact that ( )(( )) is in P. Solution: 1 By (1), ( ) is in P. 2 By (2a) and Step 1, (( )) is in P. 3 By (2b) and Steps 1 and 2, ( )(( )) is in P.
Proving Properties of Recursively Defined Sets Structural Induction for Recursively Defined Sets Suppose S is a set that has been defined recursively, and consider a property that objects in S may or may not possess. To prove that every object in S has this property: 1 Show that each object in the BASE for S has the property. 2 Show that for each rule in RECURSION, if the rule is applied to an object in S that has the property, then the object constructed by the rule also satisfies the property. Since the only objects in S are those obtained through BASE and RECURSION, then every object in S has the asserted property.
Example If P is the set of all legal configurations of parentheses from the previous example, prove that every configuration in P contains an equal number of left and right parentheses. Solution: We will use structural induction to prove this property. BASE: The only object in the base is ( ), for which the property is true. RECURSION: Suppose that all previously constructed configurations of parentheses have the same number of left and right parenthesis.
If we use the rule (2a) to construct a new object, then it is of the form (E), where E is a previously constructed configuration. By inductive hypothesis, E contains the same number of left and right parentheses, so the same will be true of (E). If we use the rule (2b) to get a new configuration, then it is of the form EF, where E and F are two previously constructed configurations. By inductive hypothesis, both E and F have the equal number of left and right parentheses, so the same must be true of EF. So, using structural induction, we were able to prove that every configuration from P has an equal number of left and right parentheses.
Example Give a recursive definition for the set of all strings over the alphabet {0, 1} for which all the 0 s precede all the 1 s. Solution: 1 BASE: The empty string ɛ is in S. 2 RECURSION: If s is a string in S, then so are (a) 0s, (b) s1 3 RESTRICTION: There are no strings in S except those coming from (1) and (2).