Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion...

45
Chapter 8: Recursion March 10, 2008

Transcript of Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion...

Page 1: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Chapter 8: Recursion

March 10, 2008

Page 2: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Outline

1 8.1 Recursively Defined Sequences

2 8.2 Solving Recurrence Relations by Iteration

3 8.4 General Recursive Definitions

Page 3: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Recursively Defined Sequences

• As mentioned in 4.1, a sequence can be defined usingrecursion. This means that the k -th terms is defined usingan equation involving previous terms in the sequence. Forexample,

ak = ak−1 + 2ak−2, k ≥ 2

anda0 = −1, a1 = 2

• The values for a0 and a1 in this example are called theinitial values of the sequence.

• The formula

ak = ak−1 + 2ak−2, k ≥ 2

which relates the k -th term to some of its predecessors iscalled the recurrence relation for this sequence.

Page 4: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleFor the recursively defined sequence

ak = ak−1 + 2ak−2, k ≥ 2

with initial conditions

a0 = −1, a1 = 2

compute the terms a2, a3, and a4.Solution:

a2 = a1 + 2a0 = 2 + 2(−1) = 0a3 = a2 + 2a1 = 0 + 2 · 2 = 4a4 = a3 + 2a2 = 4 + 2 · 0 = 4

Page 5: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleShow that the sequence

0, 1, 5, 19, . . . , 3n − 2n, . . .

satisfies the recurrence relation

dk = 5dk−1 − 6dk−2, k ≥ 2

Solution: Substitute

dk−1 = 3k−1 − 2k−1

dk−2 = 3k−2 − 2k−2

into the recurrence relation:

Page 6: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

dk = 5 · (3k−1 − 2k−1)− 6 · (3k−2 − 2k−2)

= 5 · 3k−1 − 5 · 2k−1 − 6 · 3k−2 + 6 · 2k−2

= 5 · 3k−1 − 5 · 2k−1 − 2 · 3k−1 + 3 · 2k−1

= 3 · 3k−1 − 2 · 2k−1

= 3k − 2k

Page 7: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Basic Problem of Recursion

Given a recursively defined sequence an, find an analyticalformula an = f (n) for the sequence.In general, this may be a difficult problem. In this course, wewill look at relatively simple instances of recursively definedsequences.Our approach will generally consist of the following two steps:

1 Guess an explicit formula for an = f (n). [Often, thisconsists of writing out the first several terms and trying todiscover a pattern.]

2 Prove that this formula is correct using induction.

Page 8: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

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 onone pole in a row of three poles. The task is to transferthese disks from the pole where they lie originally to one ofthe others, never placing a larger disk on top of a smallerone.

Figure: The Tower of Hanoi

Page 9: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

• 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, formany, many years Brahmins have been moving a tower ofsixty-four golden disks from one pole to another, one byone, never placing a larger on top of a smaller. When alldisks have been transferred, the Tower and the Brahminswill fall, and it will be the end of the world.”

• Assuming this legend is true, is there anything to worryabout?

Page 10: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Suppose that the poles are labelled as A, B, and C. We willapproach this problem in a general way and assume that, at thebeginning, we have k disks piled on the pole A.What is the most efficient way of transferring these k disks fromA to C? In other words, what is the minimum number of steps(moves) mk that are necessary in order to move the disks fromA 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 fromA to B, in order to get them out of the way.

Page 11: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

The most efficient algorithm for transferring all the disks from Ato C will then require these three stages:

1 Transfer the top k − 1 disks from A to B, using C forintermediate 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 intermediatemoves.

Page 12: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Suppose mn is the minimum number of steps necessary tomove n disks from one pole to another.Then

mk = mk−1 + 1 + mk−1

= 2mk−1 + 1

and, of course, m1 = 1.Therefore, the number of steps required to move k disks fromone pole to another is an example of a recursively definedsequence.

Page 13: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

The first few terms of this sequence are

m1 = 1m2 = 2m1 + 1 = 3m3 = 2m2 + 1 = 7m4 = 2m3 + 1 = 15m5 = 2m4 + 1 = 31m6 = 2m5 + 1 = 63

Page 14: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

In 8.2, we will see that the formula for the k -th term is:

mk = 2k − 1

So, for k = 64 disks, the minimum number of moves necessaryto transfer all the disks is

m64 = 264 − 1 ≈ 1.845× 1019

Even if the Brahmins were able to transfer the disks veryquickly, with one move taking one second, it would take around584 billion years to finish the task!

Page 15: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Example: Fibonacci Numbers

• In 1202, an Italian mathematician Leonardo di Pisa (alsoknown as Fibonacci) posed the following problem:A single pair of rabbits (male and female) is born at thebeginning 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 theend of every month.

2 No rabbits die.

How many rabbits will there be at the end of the first year?

Page 16: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Question: If we know the number of rabbit pairs at the ends ofprevious months, how can we calculate the number of rabbitpairs at the end of the k -th month?Observation: The number of pairs born at the end of the k -thmonth is the number of pairs alive at the end of the (k − 2)-thmonth (they are the pairs that will be fertile at the beginning ofthe k -th month). But, the number of rabbit pairs alive at the endof the k -th month is the sum of the pairs that were alive at theend of the (k − 1)-th month and those pairs that were born atthe end of the k -th month.Therefore, the number of pairs alive at the end of the month k isthe number of pairs alive at the end of the month k − 1 plus thenumber of pairs that were alive at the end of the month k − 2.

Page 17: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

If we denote the number of pairs alive at the end of the n-thmonth Fn, we have the recurrence relation

Fk = Fk−1 + Fk−2

with the initial conditions

F0 = 1, F1 = 1

[F1 = 1, since the original pair does not procreate during thefirst month.]Then,

F2 = F1 + F0 = 2F3 = F2 + F1 = 3

...F11 = F10 + F9 = 144F12 = F11 + F10 = 233

So, after one year, there will be 233 rabbit pairs.

Page 18: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

It can be shown (which will be too complicated for this course)that the general formula for Fn is:

Fn =1√5

(1 +√

52

)n+1

(1−

√5

2

)n+1 .

Page 19: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Example: Interest CompoundedSeveral Times A Year

When an annual interest rate of i is compounded m times ayear, the interest rate paid per period is i/m. For example, if theannual interest rate is 3% = 0.03 is compounded quarterly, theinterest rate paid per quarter is 0.03/4 = 0.0075.

Let Pk represent the amount on the deposit at the end of thek -th compounding period, for k ≥ 1, and P0 being the originaldeposit into the account.

Page 20: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

The interest earned during the k -th period is

Pk−1

(im

)The amount in the account at the end of the k -th period will be

Pk = Pk−1 + Pk−1

(im

)= Pk−1

(1 +

im

)

Page 21: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

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 percentageincrease in the value of an account over one year. What isthe APR for this account?

Page 22: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Solution: (a) The recurrence relation is

Pk = Pk−1

(1 +

im

)= 1.0075 · Pk−1

with the initial condition P0 = $10, 000.

P1 = 1.0075 · $10, 000 = $10, 075.00P2 = 1.0075 · $10, 075.00 = $10, 150.56P3 = 1.0075 · $10, 150.56 = $10, 226.69P4 = 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− 1000010000

= 0.03034 = 3.034%

Page 23: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Method of Iteration

• Iteration is the most basic method for findingan explicitformula for a sequence defined recursively.

• Suppose we are given a sequence a0, a1, a2, . . . defined bysome recurrence relation and initial conditions.

• We start from the initial conditions and compute successiveterms of the sequence until we discover a pattern.

Page 24: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleLet a0, a1, a2, . . . be the sequence defined recursively in thefollowing way: for all integers k ≥ 1

ak = ak−1 + 2a0 = 1

Use iteration to guess an explicit formula for the sequence.Solution:

a0 = 1a1 = a0 + 2 = 1 + 2a2 = a1 + 2 = (1 + 2) + 2 = 1 + 2 + 2a3 = a2 + 2 = (1 + 2 + 2) + 2 = 1 + 2 + 2 + 2a4 = a3 + 2 = (1 + 2 + 2 + 2) + 2 = 1 + 2 + 2 + 2 + 2

...

A good guess would be that an = 1 + n · 2 = 1 + 2n

Page 25: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

DefinitionA sequence a0, a1, a2, . . . is called an arithmetic sequence if,and only if, there is a constant d such that

ak = ak−1 + d , for k ≥ 1

Equivalently,an = a0 + n · d , n ≥ 0.

Page 26: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleLet r be a nonzero constant, and suppose a sequencea0, a1, a2, . . . is defined recursively as

ak = rak−1, k ≥ 1a0 = a

Use iteration to guess an explicit formula for this sequence.Solution:

a0 = aa1 = ra0 = ra

a2 = ra1 = r(ra) = r2a

a3 = ra2 = r(r2a) = r3a

a4 = ra3 = r(r3a) = r4a...

Guess: an = rna for n ≥ 0

Page 27: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

DefinitionA sequence a0, a1, a2, . . . is called a geometric sequence if, andonly if, there is a constant r 6= 0 such that

ak = rak−1, k ≥ 1

Equivalently,an = a0rn, n ≥ 0

Page 28: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

• In previous examples, we were able to guess an explicitformula for a recursively defined sequence using iteration.

• At this point, a formula is just a mere guess; in order to beable to convince ourselves that it indeed describes therecursively defined sequence, we need to prove it.

• To prove that a guessed formula is correct, we generallyuse mathematical induction (either weak or strong,depending on the problem)

Page 29: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleIn 8.1, we saw that The Tower of Hanoi problem leads to thefollowing recursively defined sequence:

mk = 2mk−1 + 1, k ≥ 2m1 = 1

Let’s try to find an explicit formula for mn.Solution: First, we’ll guess a formula using iteration:

m1 = 1m2 = 2m1 + 1 = 2 · 1 + 1 = 2 + 1

m3 = 2m2 + 1 = 2 · (2 + 1) + 1 = 22 + 2 + 1

m4 = 2m3 + 1 = 2 · (22 + 2 + 1) + 1 = 23 + 22 + 2 + 1...

Page 30: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

It seems that the formula for mn will be

mn = 2n−1 + 2n−2 + . . . + 22 + 2 + 1

Using the formula for the sum of the geometric sequence withr = 2, we get

mn =2n − 12− 1

= 2n − 1

Next, we’ll try to confirm this conjecture using induction:When n = 1, we see that:

m1 = 21 − 1 = 1

which is true.Suppose the formula is true for some n = k ≥ 1:

mk = 2k − 1

Page 31: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Finally, let’s prove that the formula is also true for n = k + 1:

mk+1 = 2mk + 1 (by definition)

= 2(2k − 1) + 1 (by ind. hypothesis)

= 2k+1 − 2 + 1

= 2k+1 − 1

which finishes our proof that

mn = 2n − 1

Page 32: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleConsider the following recursively defined sequence:

bk =bk−1

1 + bk−1, k ≥ 1

b0 = 1

Guess an explicit formula for bn and then use induction to proveit.Solution:

b0 = 1

b1 =b0

1 + b0=

12

b2 =b1

1 + b1=

13

b3 =b2

1 + b2=

14

...

Page 33: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Guess: bn = 1n+1 , n ≥ 0.

Now, let’s try to prove the conjecture by induction:Obviously,

b0 =1

0 + 1= 1

Suppose that the formula is true for some n = k ≥ 1:

bk =1

k + 1

Then, for n = k + 1:

bk+1 =bk

1 + bk(by definition)

=1/(k + 1)

1 + 1/(k + 1)(ind.hypothesis)

=1

k + 2

Page 34: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

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 buildnew (“more complex”) set elements from old (“simpler”)ones.

Page 35: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

A recursive definition of a set consists of the followingcomponents:

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 belongto the set.

3 RESTRICTION: A statement that no objects belong to theset other than those coming from (1) or (2).

Page 36: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleRecursive Definition of Boolean ExpressionsBoolean expressions were introduced in Chapter 1 asstatements in propositional logic which are built from thealphabet p, q, r ,. . . , logical symbols ∼, ∧, and ∨, along withparentheses (, and ).

1 BASE: Each symbol of the alphabet is a Booleanexpression.

2 RECURSION: If P and Q are Boolean expressions, thenso are

(a) (P ∧Q), (b) (P ∨Q), (c) ∼ P

3 RESTRICTION: There are no Boolean expressions overthe alphabet other than those obtained from (1) and (2).

Page 37: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Derive the fact that

((p ∨ q)∨ ∼ ((p∧ ∼ s) ∧ r))

is a Boolean expression.Solution:

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.

Page 38: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleSet of Strings over an Alphabet Let S be the set of all stringsover the alphabet {a, b}. S can be defined recursively in thefollowing 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 theletters a and b to the string s (This is also called theconcatenation of s with a or b)

3 RESTRICTION: Nothing is in S other than strings definedin (1) and (2).

Page 39: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Derive the fact that baa ∈ S.Solution:

1 By (1), ε ∈ S.2 By Step 1 and (2b), b = εb ∈ S3 By Step 2 and (2a), ba ∈ S4 By Step 3 and (2a), baa ∈ S.

Page 40: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleParenthesis Structures Some configurations of parentheses inalgebraic expressions are “legal” [e.g. (( )( )), and (( ))( )] whileothers are not [e.g. ( )) or ((( ))( )] A recursive definition of theset P of legal configurations of parenthesis can be given asfollows:

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.

Page 41: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

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.

Page 42: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

Proving Properties of RecursivelyDefined Sets

Structural Induction for Recursively Defined SetsSuppose S is a set that has been defined recursively, andconsider 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 theobject constructed by the rule also satisfies the property.

Since the only objects in S are those obtained through BASEand RECURSION, then every object in S has the assertedproperty.

Page 43: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleIf P is the set of all legal configurations of parentheses from theprevious example, prove that every configuration in P containsan 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 propertyis true.RECURSION: Suppose that all previously constructedconfigurations of parentheses have the same number of leftand right parenthesis.

Page 44: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

If we use the rule (2a) to construct a new object, then it is of theform

(E),

where E is a previously constructed configuration. By inductivehypothesis, E contains the same number of left and rightparentheses, so the same will be true of (E).If we use the rule (2b) to get a new configuration, then it is ofthe form

EF ,

where E and F are two previously constructed configurations.By inductive hypothesis, both E and F have the equal numberof left and right parentheses, so the same must be true of EF .So, using structural induction, we were able to prove that everyconfiguration from P has an equal number of left and rightparentheses.

Page 45: Chapter 8: Recursion - Ryerson Universitymth314/W08/Slides/Chapter8.pdf · Chapter 8: Recursion March 10, 2008. Outline 1 8.1 Recursively Defined Sequences 2 8.2 Solving Recurrence

ExampleGive a recursive definition for the set of all strings over thealphabet {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 thosecoming from (1) and (2).