Chapter 8: Recursion. March 10, 2008

Similar documents
Chapter 5: Sequences, Mathematic Induction, and Recursion

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

1 Sequences and Summation

Recurrence Relations and Recursion: MATH 180

5. Sequences & Recursion

Every subset of {1, 2,...,n 1} can be extended to a subset of {1, 2, 3,...,n} by either adding or not adding the element n.

Advanced Counting Techniques. Chapter 8

Page references correspond to locations of Extra Examples icons in the textbook.

12 Sequences and Recurrences

Recurrence Relations

Part III, Sequences and Series CS131 Mathematics for Computer Scientists II Note 16 RECURRENCES

Learning Objectives

Advanced Counting Techniques. 7.1 Recurrence Relations

Discrete Structures Lecture Sequences and Summations

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

Advanced Counting Techniques

Definition: A sequence is a function from a subset of the integers (usually either the set

Chapter12. Relations, Functions, and Induction

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Recursion and Induction

cse547, math547 DISCRETE MATHEMATICS Professor Anita Wasilewska

6.042/18.062J Mathematics for Computer Science March 17, 2005 Srini Devadas and Eric Lehman. Recurrences

Recursion: Introduction and Correctness

Lecture 8 : Structural Induction DRAFT

Sec$on Summary. Sequences. Recurrence Relations. Summations. Ex: Geometric Progression, Arithmetic Progression. Ex: Fibonacci Sequence

1 Sequences and Series

Generalization of Fibonacci sequence

SOLVING LINEAR RECURRENCE RELATIONS

Ex. Here's another one. We want to prove that the sum of the cubes of the first n natural numbers is. n = n 2 (n+1) 2 /4.

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

Recurrence Relations

1 Recursive Algorithms

Binomial Coefficient Identities/Complements

Discrete Mathematics -- Chapter 10: Recurrence Relations

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

The Fibonacci Sequence

Reductions, Recursion and Divide and Conquer

Induction and Recursion

At the start of the term, we saw the following formula for computing the sum of the first n integers:

Math Circle: Recursion and Induction

Recurrent Problems. ITT9131 Konkreetne Matemaatika. Chapter One The Tower of Hanoi Lines in the Plane The Josephus Problem

MATH 324 Summer 2011 Elementary Number Theory. Notes on Mathematical Induction. Recall the following axiom for the set of integers.

3.1 Induction: An informal introduction

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

With Question/Answer Animations

Unit 5: Sequences, Series, and Patterns

CHAPTER 4 SOME METHODS OF PROOF

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Sequences and Series. College Algebra

Data Structure Lecture#4: Mathematical Preliminaries U Kang Seoul National University

Induction and recursion. Chapter 5

CSCE 222 Discrete Structures for Computing. Dr. Hyunyoung Lee

Another Proof By Contradiction: 2 is Irrational

Lecture 12 : Recurrences DRAFT

Mathematical Induction

Sequences and Summations ICS 6D. Prof. Sandy Irani

Data Structure Lecture#4: Mathematical Preliminaries U Kang Seoul National University

Linear Recurrence Relations

Cpt S 223. School of EECS, WSU

The Principle of Linearity applications in the areas of algebra and analysis

Discrete Mathematics

4.1 Induction: An informal introduction

Reading 5 : Induction

Chapter 2. Mathematical Reasoning. 2.1 Mathematical Models

Why do we need math in a data structures course?

CSC2100B Data Structures Analysis

Decrypting Fibonacci and Lucas Sequences. Contents

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

Chapter 13 - Inverse Functions

Notes on generating functions in automata theory

MATH 402 : 2017 page 1 of 6

CVO103: Programming Languages. Lecture 2 Inductive Definitions (2)

Example. How to Guess What to Prove

3515ICT: Theory of Computation. Regular languages

Nondeterministic finite automata

Jong C. Park Computer Science Division, KAIST

What can you prove by induction?

Structural Induction

When is a number Fibonacci?

Recursion and Induction

Intermediate Math Circles March 11, 2009 Sequences and Series

CHAPTER 8 Advanced Counting Techniques

Algoritmi di Bioinformatica. Computational efficiency I

(Ordinary) mathematical induction is a way to prove things about natural numbers. We can write it as a rule: φ(0) x. φ(x) φ(succ(x)) x.

The Fibonacci Sequence

The associativity of equivalence and the Towers of Hanoi problem

CSI Mathematical Induction. Many statements assert that a property of the form P(n) is true for all integers n.

Chapter 8. Sequences, Series, and Probability. Selected Applications

Fibonacci Numbers. November 7, Fibonacci's Task: Figure out how many pairs of rabbits there will be at the end of one year, following rules.

Sum of Squares. Defining Functions. Closed-Form Expression for SQ(n)

Fibonacci Numbers. By: Sara Miller Advisor: Dr. Mihai Caragiu

MI 4 Mathematical Induction Name. Mathematical Induction

Arithmetic Series Can you add the first 100 counting numbers in less than 30 seconds? Begin How did he do it so quickly? It is said that he

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 1

Limits and Continuity

CMSC 132, Object-Oriented Programming II Summer Lecture 10:

{ 0! = 1 n! = n(n 1)!, n 1. n! =

CS632 Notes on Relational Query Languages I

Transcription:

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).