57
1 Recursive Recursive Definitions and Definitions and Structural Structural Induction Induction CS/APMA 202 CS/APMA 202 Rosen section 3.4 Rosen section 3.4 Aaron Bloomfield Aaron Bloomfield

1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

Embed Size (px)

Citation preview

Page 1: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

11

Recursive Definitions Recursive Definitions and Structural Inductionand Structural Induction

CS/APMA 202CS/APMA 202

Rosen section 3.4Rosen section 3.4

Aaron BloomfieldAaron Bloomfield

Page 2: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

22

RecursionRecursion

Recursion means defining something, Recursion means defining something, such as a function, in terms of itselfsuch as a function, in terms of itself For example, let For example, let ff((xx) = ) = xx!! We can define We can define ff((xx) as ) as ff((xx) = ) = xx * f( * f(xx-1)-1)

Page 3: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

33

Recursion exampleRecursion example

Rosen, section 3.4, question 1Rosen, section 3.4, question 1 Find Find ff(1), (1), ff(2), (2), ff(3), and (3), and ff(4), where (4), where ff(0) = 1(0) = 1 Let Let ff((nn+1) = +1) = ff((nn) + 2) + 2

ff(1) = (1) = ff(0) + 2 = 1 + 2 = 3(0) + 2 = 1 + 2 = 3

a)a) ff(2) = (2) = ff(1) + 2 = 3 + 2 = 5(1) + 2 = 3 + 2 = 5

ff(3) = (3) = ff(2) + 2 = 5 + 2 = 7(2) + 2 = 5 + 2 = 7

ff(4) = (4) = ff(3) + 2 = 7 + 2 = 9(3) + 2 = 7 + 2 = 9

a)a) Let Let ff((nn+1) = 3+1) = 3ff((nn))ff(1) = 3 * (1) = 3 * ff(0) = 3*1 = 3(0) = 3*1 = 3

ff(2) = 3 * (2) = 3 * ff(1) = 3*3 = 9(1) = 3*3 = 9

ff(3) = 3 * (3) = 3 * ff(2) = 3*9 = 27(2) = 3*9 = 27

a)a) ff(4) = 3 * (4) = 3 * ff(3) = 3*27 = 81(3) = 3*27 = 81

Page 4: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

44

Recursion exampleRecursion example

Rosen, section 3.4, question 1Rosen, section 3.4, question 1 Find Find ff(1), (1), ff(2), (2), ff(3), and (3), and ff(4), where (4), where ff(0) = 1(0) = 1 Let Let ff((nn+1) = 2+1) = 2ff((nn))

ff(1) = 2(1) = 2ff(0)(0) = 2 = 211 = 2 = 2

ff(2) = 2(2) = 2ff(1)(1) = 2 = 222 = 4 = 4

ff(3) = 2(3) = 2ff(2)(2) = 2 = 244 = 16 = 16

ff(4) = 2(4) = 2ff(3)(3) = 2 = 21616 = 65536 = 65536

c)c) Let Let ff((nn+1) = +1) = ff((nn))22 + + ff((nn) + 1) + 1ff(1) = (1) = ff(0)(0)22 + + ff(0) + 1 = 1(0) + 1 = 122 + 1 + 1 = 3 + 1 + 1 = 3

ff(2) = (2) = ff(1)(1)22 + + ff(0) + 1 = 3(0) + 1 = 322 + 3 + 1 = 13 + 3 + 1 = 13

c)c) ff(3) = (3) = ff(2)(2)22 + + ff(0) + 1 = 13(0) + 1 = 1322 + 13 + 1 = 183 + 13 + 1 = 183

ff(4) = (4) = ff(3)(3)22 + + ff(0) + 1 = 183(0) + 1 = 18322 + 183 + 1 = 33673 + 183 + 1 = 33673

Page 5: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

55

FractalsFractals

A fractal is a pattern that uses recursionA fractal is a pattern that uses recursion The pattern itself repeats indefinitelyThe pattern itself repeats indefinitely

Page 6: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

66

FractalsFractals

Page 7: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

77

Fibonacci sequenceFibonacci sequence

Definition of the Fibonacci sequenceDefinition of the Fibonacci sequence

Non-recursive:Non-recursive:

Recursive:Recursive: FF((nn) = ) = FF((nn-1) + -1) + FF((nn-2)-2)

or:or: FF((nn+1) = +1) = FF((nn) + ) + FF((nn-1)-1)

Must always specify base case(s)!Must always specify base case(s)! FF(1) = 1, (1) = 1, FF(2) = 1(2) = 1 Note that some will use Note that some will use FF(0) = 1, (0) = 1, FF(1) = 1(1) = 1

n

nn

nF25

5151)(

Page 8: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

88

Fibonacci sequence in JavaFibonacci sequence in Java

long Fibonacci (int n) {long Fibonacci (int n) {

if ( (n == 1) || (n == 2) )if ( (n == 1) || (n == 2) )

return 1;return 1;

elseelse

return Fibonacci (n-1) + Fibonacci (n-2);return Fibonacci (n-1) + Fibonacci (n-2);

}}

long Fibonacci2 (int n) {long Fibonacci2 (int n) {

return (long) ((Math.pow((1.0+Math.sqrt(5.0)),n)-return (long) ((Math.pow((1.0+Math.sqrt(5.0)),n)-

Math.pow((1.0-Math.sqrt(5.0)),n)) /Math.pow((1.0-Math.sqrt(5.0)),n)) /

(Math.sqrt(5) * Math.pow(2,n)));(Math.sqrt(5) * Math.pow(2,n)));

}}

Page 9: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

99

Recursion definitionRecursion definition

From From ““The HackerThe Hacker’’s Dictionarys Dictionary””::

recursion recursion nn. See . See recursionrecursion. See also . See also tail recursiontail recursion..

Page 10: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1010

Bad recursive definitionsBad recursive definitions

Consider:Consider: ff(0) = 1(0) = 1 ff((nn) = 1 + ) = 1 + ff((nn-2)-2) What is What is ff(1)?(1)?

Consider:Consider: ff(0) = 1(0) = 1 ff(n) = 1+(n) = 1+ff(-n)(-n) What is f(1)?What is f(1)?

Page 11: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1111

Defining sets via recursionDefining sets via recursion

Same two parts:Same two parts: Base case (or basis step)Base case (or basis step) Recursive stepRecursive step

Example: the set of positive integersExample: the set of positive integersBasis stepBasis step: 1 : 1 SS

Recursive stepRecursive step: if : if xx SS, then , then xx+1 +1 SS

Page 12: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1212

Defining sets via recursionDefining sets via recursion

Rosen, section 3.4, question 24: give recursive Rosen, section 3.4, question 24: give recursive definitions for:definitions for:

a)a) The set of odd positive integersThe set of odd positive integers 1 1 SS If If x x SS, then , then xx+2 +2 SS

b)b) The set of positive integer powers of 3The set of positive integer powers of 3 3 3 SS If If xx SS, then 3*, then 3*xx SS

The set of polynomials with integer coefficientsThe set of polynomials with integer coefficients 0 0 SS If If pp((xx) ) SS, then , then pp((xx) + cx) + cxnn SS

cc ZZ, , nn ZZ and and nn ≥ 0≥ 0

Page 13: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1313

Defining strings via recursionDefining strings via recursion

TerminologyTerminology is the empty string: is the empty string: “”“” is the set of all letters: { a, b, c, …, z }is the set of all letters: { a, b, c, …, z }

The set of letters can change depending on the problemThe set of letters can change depending on the problem

We can define a set of strings We can define a set of strings * as follows* as follows Base step: Base step: ** If If ww * and * and x x , then wx , then wx ** Thus, Thus, * s the set of all the possible strings that can * s the set of all the possible strings that can

be generated with the alphabetbe generated with the alphabet Is this countably infinite or uncountably infinite?Is this countably infinite or uncountably infinite?

Page 14: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1414

Defining strings via recursionDefining strings via recursion

Let Let = { 0, 1 } = { 0, 1 }

Thus, Thus, * is the set of all binary numbers* is the set of all binary numbers Or all binary stringsOr all binary strings Or all possible computer filesOr all possible computer files

Page 15: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1515

String length via recursionString length via recursion

How to define string length recursively?How to define string length recursively? Basis step: Basis step: ll(() = 0) = 0 Recursive step: Recursive step: ll((wxwx) = ) = ll((ww) + 1 if ) + 1 if ww * and * and x x

Example: Example: ll((““aaaaaa””)) ll((““aaaaaa””) = ) = ll((““aaaa””) + 1) + 1 ll((““aaaa””) = ) = ll((““aa””) + 1) + 1 ll((““aa””) = ) = ll((“”“”) + 1) + 1 ll((“”“”) = 0) = 0 Result: 3Result: 3

Page 16: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1616

TodayToday’’s demotivatorss demotivators

Page 17: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1717

Strings via recursion exampleStrings via recursion example

Rosen, section 3.4, question 38: Give a Rosen, section 3.4, question 38: Give a recursive definition for the set of string that recursive definition for the set of string that are palindromesare palindromes We will define set We will define set PP, which is the set of all , which is the set of all

palindromespalindromes

Basis stepBasis step: : PP Second basis step: Second basis step: xx PP when when xx

Recursive stepRecursive step: : xpxxpx PP if if xx and and pp PP

Page 18: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1818

Strings and induction exampleStrings and induction example

This requires structural induction, which This requires structural induction, which will be covered later in this slide setwill be covered later in this slide set

Page 19: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

1919

Recursion prosRecursion pros

Easy to programEasy to program

Easy to understandEasy to understand

Page 20: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2020

Recursion consRecursion cons

Consider the recursive Fibonacci generatorConsider the recursive Fibonacci generatorHow many recursive calls does it make?How many recursive calls does it make?

FF(1): 1(1): 1 FF(2): 1(2): 1 FF(3): 3(3): 3 FF(4): 5(4): 5 FF(5): 9(5): 9 FF(10): 109(10): 109 FF(20): 13,529(20): 13,529 FF(30): 1,664,079(30): 1,664,079 FF(40): 204,668,309(40): 204,668,309 FF(50): 25,172,538,049(50): 25,172,538,049 FF(100): 708,449,696,358,523,830,149 (100): 708,449,696,358,523,830,149 7 * 10 7 * 102020

At 1 billion recursive calls per second (generous), this would take At 1 billion recursive calls per second (generous), this would take over 22,000 yearsover 22,000 yearsBut that would also take well over 10But that would also take well over 101212 Gb of memory! Gb of memory!

Page 21: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2121

TreesTrees

Rooted trees:Rooted trees: A graph containing nodes and edgesA graph containing nodes and edges

Cannot contain a cycle!Cannot contain a cycle!

Cycle not allowed in a tree

Page 22: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2222

Rooted treesRooted trees

Recursive definition:Recursive definition: Basis stepBasis step: A single vertex : A single vertex rr is a rooted tree is a rooted tree Recursive stepRecursive step: :

Let Let TT11, , TT22, …, , …, TTnn be rooted trees be rooted trees

Form a new tree with a new root Form a new tree with a new root rr that contains an edge that contains an edge to the root of each of the trees to the root of each of the trees TT11, , TT22, …, , …, TTnn

Page 23: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2323

(Extended) Binary trees(Extended) Binary trees

Recursive definitionRecursive definition Basis stepBasis step: : The empty set is an extended binary treeThe empty set is an extended binary tree Recursive stepRecursive step: :

Let Let TT11, and , and TT22 be extended binary trees be extended binary trees

Form a new tree with a new root Form a new tree with a new root rr

Form a new tree such that Form a new tree such that TT11 is the left subtree, is the left subtree,

and and TT22 is the right subtree is the right subtree

Page 24: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2424

Full binary treesFull binary trees

Recursive definitionRecursive definition Basis step: Basis step: A full binary tree consisting only of the vertex A full binary tree consisting only of the vertex rr Recursive stepRecursive step: :

Let Let TT11, and , and TT22 be extended binary trees be extended binary trees

Form a new tree with a new root Form a new tree with a new root rr

Form a new tree Form a new tree TT such that such that TT11 is the left subtree, is the left subtree,

and and TT22 is the right subtree is the right subtree

This is denoted by This is denoted by TT = = TT11∙∙TT22

Note the only difference between a regular Note the only difference between a regular binary tree and a full one is the basis stepbinary tree and a full one is the basis step

Page 25: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2525

Binary tree heightBinary tree height

hh((TT) denotes the height of tree ) denotes the height of tree TTRecursive definition:Recursive definition: Basis stepBasis step: The height of a tree with : The height of a tree with

only one node only one node rr is 0 is 0 Recursive stepRecursive step::

Let Let TT11 and and TT22 be binary trees be binary trees

The binary tree The binary tree TT = = TT11∙∙TT22 has height has heighthh((TT) = 1 + max ( ) = 1 + max ( hh((TT11), ), hh((TT22) )) )

This definition can be generalized to This definition can be generalized to non-binary treesnon-binary trees

Page 26: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2626

Binary tree sizeBinary tree size

nn((TT) denotes the number of vertices in tree ) denotes the number of vertices in tree TTRecursive definition:Recursive definition: Basis stepBasis step: The number of vertices of an : The number of vertices of an

empty tree is 0empty tree is 0 Basis stepBasis step: The number of vertices of a tree : The number of vertices of a tree

with only one node with only one node rr is 1 is 1 Recursive stepRecursive step::

Let Let TT11 and and TT22 be binary trees be binary trees

The number of vertices in binary tree The number of vertices in binary tree TT = = TT11∙∙TT22 is: is:n(n(TT) = 1 + ) = 1 + nn((TT11) + ) + nn((TT22))

This definition can be generalized to non-This definition can be generalized to non-binary treesbinary trees

Page 27: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2727

A bit of humor: Computer A bit of humor: Computer terminologyterminology

Page 28: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2828

Recursion vs. inductionRecursion vs. induction

Consider the recursive definition for Consider the recursive definition for factorial:factorial:

ff(0) = 1(0) = 1

ff(n) = n * (n) = n * ff(n-1)(n-1)

Sort of like inductionSort of like induction

Base case

The “step”

Page 29: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

2929

Recursion vs. inductionRecursion vs. induction

Rosen, section 3.4, example 7 (page 262)Rosen, section 3.4, example 7 (page 262)

Consider the set of all integers that are multiples Consider the set of all integers that are multiples of 3of 3

{ 3, 6, 9, 12, 15, … }{ 3, 6, 9, 12, 15, … }

{ { xx | | xx = 3 = 3kk and and kk ZZ++ } }

Recursive definition:Recursive definition: Basis stepBasis step: 3 : 3 SS Recursive stepRecursive step: If : If xx SS and and yy SS, then , then xx++yy SS

Page 30: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

3030

Recursion vs. inductionRecursion vs. induction

Proof via induction: prove that Proof via induction: prove that SS contains all the integers contains all the integers that are divisible by 3that are divisible by 3

Let Let AA be the set of all ints divisible by 3 be the set of all ints divisible by 3 Show that Show that SS = = AA

Two parts:Two parts:Show that Show that SS AA

Let Let PP((nn) = 3) = 3nn SSBase caseBase case: : PP(1) = 3*1 (1) = 3*1 SS

By the basis step of the recursive definitionBy the basis step of the recursive definitionInductive hypothesisInductive hypothesis: assume : assume PP((kk) = 3*) = 3*kk SS is true is trueInductive stepInductive step: show that : show that PP((kk+1) = 3*(+1) = 3*(kk+1) is true+1) is true

3*(3*(kk+1) = 3+1) = 3kk+3+3 33kk SS by the inductive hypothesis by the inductive hypothesis 3 3 SS by the base case by the base case Thus, 3Thus, 3kk+3 +3 SS by the recursive definition by the recursive definition

Show that Show that AA SSDone in the text, page 267 (not reproduced here)Done in the text, page 267 (not reproduced here)

Page 31: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

3131

What did we just do?What did we just do?

Notice what we did:Notice what we did: Showed the base caseShowed the base case Assumed the inductive hypothesisAssumed the inductive hypothesis For the inductive step, we:For the inductive step, we:

Showed that each of the Showed that each of the ““partsparts”” were in were in SS The parts being 3The parts being 3kk and 3 and 3

Showed that since both parts were in Showed that since both parts were in SS, by the , by the recursive definition, the combination of those parts recursive definition, the combination of those parts is in is in SS

i.e., 3i.e., 3kk+3 +3 SS

This is called This is called structural inductionstructural induction

Page 32: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

3232

Structural inductionStructural induction

A more convenient form of induction for A more convenient form of induction for recursively defined recursively defined ““thingsthings““Used in conjunction with the recursive definitionUsed in conjunction with the recursive definitionThree parts:Three parts: Basis stepBasis step: Show the result holds for the elements in : Show the result holds for the elements in

the basis step of the recursive definitionthe basis step of the recursive definition Inductive hypothesisInductive hypothesis: Assume that the statement is : Assume that the statement is

true for some existing elementstrue for some existing elementsUsually, this just means assuming the statement is trueUsually, this just means assuming the statement is true

Recursive stepRecursive step: Show that the recursive definition : Show that the recursive definition allows the creation of a new element using the allows the creation of a new element using the existing elementsexisting elements

Page 33: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

3434

Tree structural induction exampleTree structural induction example

Rosen, section 3.4, question 43Rosen, section 3.4, question 43

Show that Show that nn((TT) ) ≥≥ 2 2hh((TT) + 1) + 1

Basis stepBasis step: Let : Let TT be the full binary be the full binary tree of just one node tree of just one node rr

hh((TT) = 0) = 0

nn((TT) = 1) = 1

nn((TT) ) ≥≥ 2 2hh((TT) + 1) + 1

1 1 ≥≥ 2*0 + 1 2*0 + 1

1 1 ≥≥ 1 1

Page 34: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

3535

Tree structural induction exampleTree structural induction example

Show that Show that nn((TT) ) ≥≥ 2 2hh((TT) + 1) + 1Inductive hypothesisInductive hypothesis::

Let Let TT11 and and TT22 be full binary trees be full binary treesAssume that Assume that nn((TT11) ) ≥≥ 2 2hh((TT11) + 1 for some tree ) + 1 for some tree TT11

Assume that Assume that nn((TT22) ) ≥≥ 2 2hh((TT22) + 1 for some tree ) + 1 for some tree TT22

Recursive stepRecursive step::Let Let TT = = TT11 ∙ ∙ TT22

Here the ∙ operator means creating a new tree with a root note Here the ∙ operator means creating a new tree with a root note rr and subtrees and subtrees TT11 and and TT22

New element is New element is TTBy the definition of height and size, we know:By the definition of height and size, we know:

nn((TT) = 1 + ) = 1 + nn((TT11) + ) + nn((TT22))hh((TT) = 1 + max ( ) = 1 + max ( hh((TT11), ), hh((TT22) )) )

Therefore:Therefore:nn((TT) = 1 + ) = 1 + nn((TT11) + ) + nn((TT22))≥≥ 1 + 21 + 2hh((TT11) + 1 + 2) + 1 + 2hh((TT22) + 1) + 1≥≥ 1 + 2*max ( 1 + 2*max ( hh((TT11), ), hh((TT22) )) ) the sum of two non-neg #the sum of two non-neg #’’s is at least s is at least

as large as the larger of the as large as the larger of the twotwo= 1 + 2*= 1 + 2*hh((TT))

Thus, Thus, nn((TT) ) ≥≥ 2 2hh((TT) + 1) + 1

Page 35: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

3636

String structural induction exampleString structural induction example

Rosen, section 3.4, question 32Rosen, section 3.4, question 32

Part (a): Give the definition for Part (a): Give the definition for ones(s)ones(s), which counts the , which counts the number of ones in a bit string number of ones in a bit string ss

Let Let = { 0, 1 } = { 0, 1 }

Basis stepBasis step: : onesones(()) = = 00

Recursive stepRecursive step: : ones(wx) = ones(w) + xones(wx) = ones(w) + xWhere Where xx and and ww **Note that Note that xx is a bit: either 0 or 1 is a bit: either 0 or 1

Page 36: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

3737

String structural induction exampleString structural induction example

Part (b): Use structural induction to prove that Part (b): Use structural induction to prove that ones(st) = ones(s) + ones(st) = ones(s) + ones(t)ones(t)

Basis stepBasis step: : tt = = ones (sones (s∙∙) = ones(s) = ones(s)+0 = ones(s) + ones() = ones(s) = ones(s)+0 = ones(s) + ones())

Inductive hypothesisInductive hypothesis: Assume : Assume ones(s∙t) = ones(s) + ones(t)ones(s∙t) = ones(s) + ones(t)

Recursive stepRecursive step: Want to show that : Want to show that ones(s∙t∙x) = ones(s) + ones(t∙x)ones(s∙t∙x) = ones(s) + ones(t∙x) Where Where ss, , t t * and * and xx New element is New element is ones(s∙t∙x)ones(s∙t∙x) ones (s∙t∙x) = ones ((s∙t)∙x))ones (s∙t∙x) = ones ((s∙t)∙x)) by associativity of by associativity of

concatenationconcatenation = x+ones(s∙t)= x+ones(s∙t) by recursive definitionby recursive definition = x + ones(s) + ones(t)= x + ones(s) + ones(t) by inductive hypothesisby inductive hypothesis = ones(s) + (x + ones(t))= ones(s) + (x + ones(t)) by commutativity and assoc. of +by commutativity and assoc. of + = ones(s) + ones(t∙x)= ones(s) + ones(t∙x) by recursive definitionby recursive definition Proven!Proven!

Page 37: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

38

Quick surveyQuick survey

I feel I understand structural I feel I understand structural induction…induction…

a)a) Very wellVery well

b)b) With some review, IWith some review, I’’ll be goodll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 38: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

3939

Human stupidityHuman stupidity

Page 39: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4040

Induction methods comparedInduction methods comparedWeak Weak

mathematicalmathematicalStrongStrong

MathematicalMathematical StructuralStructural

Used forUsed forUsually Usually

formulaeformulaeUsually formulae not Usually formulae not

provable via provable via mathematical inductionmathematical induction

Only things defined Only things defined via recursionvia recursion

AssumptionAssumption Assume P(k)Assume P(k) Assume P(1), P(2), …, Assume P(1), P(2), …, P(k)P(k)

Assume statement is Assume statement is true for some "old" true for some "old"

elementselements

What to What to proveprove

True for True for P(k+1)P(k+1) True for P(k+1)True for P(k+1)

Statement is true for Statement is true for some "new" elements some "new" elements

created with "old" created with "old" elementselements

Step 1 Step 1 calledcalled

Base caseBase case Base caseBase case Basis stepBasis step

Step 3 Step 3 calledcalled

Inductive stepInductive step Inductive stepInductive step Recursive stepRecursive step

Page 40: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4141

Induction types comparedInduction types compared

Show that Show that FF((nn) < 2) < 2nn

Where Where FF((nn) is the ) is the nnthth Fibonacci number Fibonacci number Actually Actually FF((nn) < 2) < 20.7*0.7*nn, but we won, but we won’’t prove that heret prove that here

Fibonacci definition:Fibonacci definition: Basis stepBasis step: : FF(1) = 1 and (1) = 1 and FF(2) = 1(2) = 1 Recursive stepRecursive step: : FF((nn) = ) = FF((nn-1) + -1) + FF((nn-2)-2)

Base case (or basis step)Base case (or basis step): Show true for : Show true for FF(1) (1) and and FF(2)(2) F(1) = 1 < 2F(1) = 1 < 211 = 2 = 2 F(2) = 1 < 2F(2) = 1 < 222 = 4 = 4

Page 41: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4242

Via weak mathematical inductionVia weak mathematical induction

Inductive hypothesisInductive hypothesis: Assume : Assume FF((kk) < 2) < 2kk

Inductive stepInductive step: Prove : Prove FF((kk+1) < 2+1) < 2kk+1+1

FF((kk+1) = +1) = FF((kk) + ) + FF((kk-1)-1) We know We know FF((kk) < 2) < 2kk by the inductive hypothesis by the inductive hypothesis Each term is less than the next, therefore Each term is less than the next, therefore

FF((kk) > ) > FF((kk-1)-1)Thus, Thus, FF((kk-1) < -1) < FF((kk) < 2) < 2kk

Therefore, Therefore, FF((kk+1) = +1) = FF((kk) + ) + FF((kk-1) < 2-1) < 2kk + 2 + 2kk = 2 = 2kk+1+1

Proven!Proven!

Page 42: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4343

Via strong mathematical inductionVia strong mathematical induction

Inductive hypothesisInductive hypothesis: Assume : Assume FF(1) < 2(1) < 211, , FF(2) < 2(2) < 222, …, , …, FF((k-k-1) < 21) < 2kk-1-1, , FF((kk) < 2) < 2kk

Inductive stepInductive step: Prove : Prove FF((kk+1) < 2+1) < 2kk+1+1

FF((kk+1) = +1) = FF((kk) + ) + FF((kk-1)-1) We know We know FF((kk) < 2) < 2kk by the inductive hypothesis by the inductive hypothesis We know We know FF((kk-1) < 2-1) < 2kk-1-1 by the inductive by the inductive

hypothesishypothesis Therefore, Therefore, FF((kk) + ) + FF((kk-1) < 2-1) < 2kk + 2 + 2kk-1-1 < 2 < 2kk+1+1

Proven!Proven!

Page 43: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4444

Via structural inductionVia structural induction

Inductive hypothesisInductive hypothesis: Assume : Assume FF((nn) < 2) < 2nn

Recursive stepRecursive step: : Show true for Show true for ““new elementnew element””: : FF((nn+1)+1) We know We know FF((nn) < 2) < 2nn by the inductive by the inductive

hypothesishypothesis Each term is less than the next, therefore Each term is less than the next, therefore

FF((nn) > ) > FF((nn-1)-1)Thus, Thus, FF((nn-1) < -1) < FF((nn) < 2) < 2nn

Therefore, Therefore, FF((nn) + ) + FF((nn-1) < 2-1) < 2nn + 2 + 2nn = 2 = 2nn+1+1

Proven!Proven!

Page 44: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4545

Another way via structural inductionAnother way via structural induction

Inductive hypothesisInductive hypothesis: Assume : Assume FF((nn) < 2) < 2nn and and FF((nn-1) < 2-1) < 2nn-1-1

The difference here is we are using two The difference here is we are using two ““oldold”” elements versus one, as in the last slideelements versus one, as in the last slide

Recursive stepRecursive step: : Show true for Show true for ““new elementnew element””: : FF((nn+1)+1) FF((nn+1) = +1) = FF((nn) + ) + FF((nn-1)-1) We know We know FF((nn) < 2) < 2nn by the inductive hypothesis by the inductive hypothesis We know We know FF((nn-1) < 2-1) < 2nn-1-1 by the inductive hypothesis by the inductive hypothesis Therefore, Therefore, FF((nn) + ) + FF((nn-1) < 2-1) < 2kk + 2 + 2kk-1-1 < 2 < 2kk+1+1

Proven!Proven!

Page 45: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4646

But wait!But wait!

In this example, the structural induction proof In this example, the structural induction proof was essentially the same as the weak or strong was essentially the same as the weak or strong mathematical induction proofmathematical induction proof ItIt’’s hard to find an example that works well for all of s hard to find an example that works well for all of

the induction typesthe induction types

Structural induction will work on some recursive Structural induction will work on some recursive problems which weak or strong mathematical problems which weak or strong mathematical induction will notinduction will not Trees, strings, etc.Trees, strings, etc.

Page 46: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4747

A bit of humor…A bit of humor…

Page 47: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4848

Section 3.4, question 8Section 3.4, question 8 Give the recursive definition of the following sequencesGive the recursive definition of the following sequences

Note that many answers are possible!Note that many answers are possible!

a)a) aann = 4 = 4nn – 2 – 2 Terms: 2, 6, 10, 14, 16, etc.Terms: 2, 6, 10, 14, 16, etc. aa11 = 2 = 2 aann = = aan-1n-1 + 4 + 4

b)b) aann = 1 + (-1) = 1 + (-1)nn

Terms: 0, 2, 0, 2, 0, 2, etc.Terms: 0, 2, 0, 2, 0, 2, etc. aa11 = 0, = 0, aa22 = 2 = 2 aann = = aan-2n-2

aann = = nn((nn+1)+1)a)a) Terms: 2, 6, 12, 20, 30, 42, etc.Terms: 2, 6, 12, 20, 30, 42, etc.b)b) aa11 = 2 = 2c)c) aann = = aan-1n-1 + 2*n + 2*n

aann = = nn22

a)a) Terms: 1, 4, 9, 16, 25, 36, 49, etc.Terms: 1, 4, 9, 16, 25, 36, 49, etc.b)b) aa11 = 1 = 1c)c) aann = = aan-1n-1 + 2 + 2nn - 1 - 1

Page 48: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

4949

Section 3.4, question 12Section 3.4, question 12

Show that Show that ff1122 + + ff22

22 + + ff3322 + … + + … + ffnn

22 = = ffnnffnn+1+1

Base caseBase case: : nn = 1 = 1 ff11

22 = = ff11ff22

1122 = 1*1 = 1*1Inductive hypothesisInductive hypothesis: Assume: Assume

ff1122 + + ff22

22 + + ff3322 + … + + … + ffkk

22 = = ffkkffkk+1+1

Inductive stepInductive step: Prove: Prove ff11

22 + + ff2222 + + ff33

22 + … + + … + ffkk22 + + ffk+1k+1

22 = = ffk+1k+1ffkk+2+2

Page 49: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

5050

Section 3.4, question 12Section 3.4, question 12

Inductive hypothesisInductive hypothesis: Assume: Assume ff11

22 + + ff2222 + + ff33

22 + … + + … + ffkk22 = = ffkkffkk+1+1

Inductive stepInductive step: Prove: Prove ff11

22 + + ff2222 + + ff33

22 + … + + … + ffkk22 + + ffk+1k+1

22 = = ffk+1k+1ffkk+2+2

ffkkffkk+1+1 + + ffk+1k+122 = = ffk+1k+1ffkk+2+2

ffkkffkk+1+1 + + ffk+1k+122 = = ffk+1 k+1 ((ffkk + + ffkk+1+1))

ffkkffkk+1+1 + + ffk+1k+122 = = ffkkffkk+1+1 + + ffk+1k+1

22

Page 50: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

5151

Section 3.4, question 13Section 3.4, question 13

Show that Show that ff11 + + ff22 + + ff33 + … + + … + ff22nn-1-1 = = ff22nn

Base caseBase case: : nn = 1 = 1 ff11 = = ff2*12*1

1 = 11 = 1

Inductive hypothesisInductive hypothesis: Assume: Assume ff11 + + ff22 + + ff33 + … + + … + ff22kk-1-1 = = ff22kk

Inductive stepInductive step: Prove: Prove ff11 + + ff22 + + ff33 + … + + … + ff22kk-1-1 + + ff2(2(kk+1)-1+1)-1 = = ff2(2(kk+1)+1)

ff11 + + ff22 + + ff33 + … + + … + ff22kk-1-1 + + ff22kk+1+1 = = ff22kk+2+2

Page 51: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

5252

Section 3.4, question 13Section 3.4, question 13

Inductive hypothesisInductive hypothesis: Assume: Assume ff11 + + ff22 + + ff33 + … + + … + ff22kk-1-1 = = ff22kk

Inductive stepInductive step: Prove: Prove ff11 + + ff22 + + ff33 + … + + … + ff22kk-1-1 + + ff22kk+1+1 = = ff22kk+2+2

ff22kk + + ff22kk+1+1 = = ff22kk+2+2

True by definition of True by definition of ff22kk+2+2

Page 52: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

5353

Section 3.4, question 22Section 3.4, question 22 Show that the set Show that the set SS defined by defined by

Basis stepBasis step: 1 : 1 SS Recursive stepRecursive step: : ss + + tt SS when when ss SS and and tt SS

is the set of positive integers:is the set of positive integers: ZZ++ = { 1, 2, 3, … } = { 1, 2, 3, … }

Note the (somewhat recursive) definition of the positive integers:Note the (somewhat recursive) definition of the positive integers: 1 is a positive integer1 is a positive integer For any arbitrary For any arbitrary nn that is a positive integer, that is a positive integer, nn+1 is also a positive +1 is also a positive

integerinteger

Proof by structural inductionProof by structural inductionBasis stepBasis step: 1 : 1 SS and 1 and 1 ZZ++

Inductive hypothesisInductive hypothesis: Assume : Assume kk SSRecursive stepRecursive step: Show : Show kk+1 +1 SS

kk SS by the inductive hypothesis by the inductive hypothesis 1 1 S S by the base caseby the base case kk+1 +1 SS by the recursive step of the recursive definition above by the recursive step of the recursive definition above

Page 53: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

5454

Section 3.4, question 35Section 3.4, question 35

Give a recursive definition of the reversal of a Give a recursive definition of the reversal of a stringstring

Basis stepBasis step: : RR = = Note that the superscripted R means reversal of a Note that the superscripted R means reversal of a

stringstringRecursive stepRecursive step: Consider a string : Consider a string ww **

Rewrite Rewrite ww as as vyvy where where vv * and * and y y vv is the first is the first nn-1 characters in -1 characters in ww

yy is the last character in is the last character in ww wwRR = = yy((vvRR))

Parentheses are for our benefitParentheses are for our benefit

Page 54: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

55

Quick surveyQuick survey

I felt I understood the material in this I felt I understood the material in this slide set…slide set…

a)a) Very wellVery well

b)b) With some review, IWith some review, I’’ll be goodll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 55: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

56

Quick surveyQuick survey

The pace of the lecture for this The pace of the lecture for this slide set was…slide set was…

a)a) FastFast

b)b) About rightAbout right

c)c) A little slowA little slow

d)d) Too slowToo slow

Page 56: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

57

Quick surveyQuick survey

How interesting was the material How interesting was the material in this slide set? Be honest!in this slide set? Be honest!

a)a) Wow! That was SOOOOOO cool!Wow! That was SOOOOOO cool!

b)b) Somewhat interestingSomewhat interesting

c)c) Rather bortingRather borting

d)d) ZzzzzzzzzzzZzzzzzzzzzz

Page 57: 1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield

5858

TodayToday’’s demotivatorss demotivators