1 recursive definitions and structural induction cs 202 epp section ??? aaron bloomfield

47
1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

Upload: spencer-watkins

Post on 29-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

1

Recursive Definitions and Structural Induction

CS 202

Epp section ???

Aaron Bloomfield

Page 2: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

2

Recursion

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

Page 3: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

3

Recursion example

– Find f(1), f(2), f(3), and f(4), where f(0) = 1

a) Let f(n+1) = f(n) + 2• f(1) = f(0) + 2 = 1 + 2 = 3• f(2) = f(1) + 2 = 3 + 2 = 5• f(3) = f(2) + 2 = 5 + 2 = 7• f(4) = f(3) + 2 = 7 + 2 = 9

b) Let f(n+1) = 3f(n)• f(1) = 3 * f(0) = 3*1 = 3• f(2) = 3 * f(1) = 3*3 = 9• f(3) = 3 * f(2) = 3*9 = 27• f(4) = 3 * f(3) = 3*27 = 81

Page 4: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

4

Recursion example

– Find f(1), f(2), f(3), and f(4), where f(0) = 1

c) Let f(n+1) = 2f(n)

• f(1) = 2f(0) = 21 = 2• f(2) = 2f(1) = 22 = 4• f(3) = 2f(2) = 24 = 16• f(4) = 2f(3) = 216 = 65536

d) Let f(n+1) = f(n)2 + f(n) + 1• f(1) = f(0)2 + f(0) + 1 = 12 + 1 + 1 = 3• f(2) = f(1)2 + f(0) + 1 = 32 + 3 + 1 = 13• f(3) = f(2)2 + f(0) + 1 = 132 + 13 + 1 = 183• f(4) = f(3)2 + f(0) + 1 = 1832 + 183 + 1 = 33673

Page 5: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

5

Fractals

• A fractal is a pattern that uses recursion– The pattern itself repeats indefinitely

Page 6: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

6

Fractals

Page 7: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

7

Fibonacci sequence

• Definition of the Fibonacci sequence

– Non-recursive:

– Recursive: F(n) = F(n-1) + F(n-2)

or: F(n+1) = F(n) + F(n-1)

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

n

nn

nF25

5151)(

Page 8: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

8

Fibonacci sequence in Java

long Fibonacci (int n) {if ( (n == 1) || (n == 2) ) return 1;else return Fibonacci (n-1) + Fibonacci (n-2);

}

long Fibonacci2 (int n) {return (long) ((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)));}

Page 9: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

9

Recursion definition

• From “The Hacker’s Dictionary”:

• recursion n. See recursion. See also tail recursion.

Page 10: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

10

Bad recursive definitions

• Consider:– f(0) = 1– f(n) = 1 + f(n-2)– What is f(1)?

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

Page 11: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

11

Defining sets via recursion

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

• Example: the set of positive integers– Basis step: 1 S– Recursive step: if x S, then x+1 S

Page 12: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

12

Defining sets via recursion

• Give recursive definitions for:a) The set of odd positive integers

1 S If x S, then x+2 S

b) The set of positive integer powers of 3 3 S If x S, then 3*x S

c) The set of polynomials with integer coefficients 0 S If p(x) S, then p(x) + cxn S

– c Z, n Z and n ≥ 0

Page 13: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

13

Defining strings via recursion

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

• The set of letters can change depending on the problem

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

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

Page 14: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

14

Defining strings via recursion

• Let = { 0, 1 }• Thus, * is the set of all binary numbers

– Or all binary strings– Or all possible computer files

Page 15: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

15

String length via recursion

• How to define string length recursively?– Basis step: l() = 0– Recursive step: l(wx) = l(w) + 1 if w * and x

• Example: l(“aaa”)– l(“aaa”) = l(“aa”) + 1– l(“aa”) = l(“a”) + 1– l(“a”) = l(“”) + 1– l(“”) = 0– Result: 3

Page 16: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

16

Strings via recursion example

• Give a recursive definition for the set of string that are palindromes– We will define set P, which is the set of all

palindromes

• Basis step: P– Second basis step: x P when x

• Recursive step: xpx P if x and p P

Page 17: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

17

Recursion pros

• Easy to program• Easy to understand

Page 18: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

18

Recursion cons• Consider the recursive Fibonacci generator• How many recursive calls does it make?

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

• At 1 billion recursive calls per second (generous), this would take over 22,000 years

• But that would also take well over 1012 Gb of memory!

Page 19: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

19

Trees

• Rooted trees:– A graph containing nodes and edges

• Cannot contain a cycle!

Cycle not allowed in a tree

Page 20: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

20

Rooted trees

• Recursive definition:– Basis step: A single vertex r is a rooted tree– Recursive step:

• Let T1, T2, …, Tn be rooted trees

• Form a new tree with a new root r that contains an edge to the root of each of the trees T1, T2, …, Tn

Page 21: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

21

(Extended) Binary trees

• Recursive definition– Basis step: The empty set is an extended binary tree– Recursive step:

• Let T1, and T2 be extended binary trees

• Form a new tree with a new root r

• Form a new tree such that T1 is the left subtree,and T2 is the right subtree

Page 22: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

22

Full binary trees• Recursive definition

– Basis step: A full binary tree consisting only of the vertex r– Recursive step:

• Let T1, and T2 be extended binary trees• Form a new tree with a new root r• Form a new tree T such that T1 is the left subtree, and T2 is the right subtree

(denoted by T = T1∙T2)

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

Page 23: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

23

Binary tree height

• h(T) denotes the height of tree T• Recursive definition:

– Basis step: The height of a tree with only one node r is 0

– Recursive step:• Let T1 and T2 be binary trees

• The binary tree T = T1∙T2 has heighth(T) = 1 + max ( h(T1), h(T2) )

• This definition can be generalized to non-binary trees

Page 24: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

24

Binary tree size

• n(T) denotes the number of vertices in tree T• Recursive definition:

– Basis step: The number of vertices of an empty tree is 0

– Basis step: The number of vertices of a tree with only one node r is 1

– Recursive step:• Let T1 and T2 be binary trees• The number of vertices in binary tree T = T1∙T2 is:

n(T) = 1 + n(T1) + n(T2)

• This definition can be generalized to non-binary trees

Page 25: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

26

Recursion vs. induction

• Consider the recursive definition for factorial:

– f(0) = 1

– f(n) = n * f(n-1)

• Sort of like induction

Base case

The “step”

Page 26: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

27

Recursion vs. induction

• Consider the set of all integers that are multiples of 3– { 3, 6, 9, 12, 15, … }– { x | x = 3k and k Z+ }

• Recursive definition:– Basis step: 3 S– Recursive step: If x S and y S, then x+y S

Page 27: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

28

Recursion vs. induction• Proof via induction: prove that S contains all the integers that are

divisible by 3– Let A be the set of all ints divisible by 3– Show that S = A

• Two parts:– Show that S A

• Let P(n) = 3n S• Base case: P(1) = 3*1 S

– By the basis step of the recursive definition• Inductive hypothesis: assume P(k) = 3*k S is true• Inductive step: show that P(k+1) = 3*(k+1) is true

– 3*(k+1) = 3k+3– 3k S by the inductive hypothesis– 3 S by the base case– Thus, 3k+3 S by the recursive definition

– Show that A S• Similar steps (not reproduced here)

Page 28: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

29

What did we just do?

• Notice what we did:– Showed the base case– Assumed the inductive hypothesis– For the inductive step, we:

• Showed that each of the “parts” were in S– The parts being 3k and 3

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

– i.e., 3k+3 S

• This is called structural induction

Page 29: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

30

Structural induction

• A more convenient form of induction for recursively defined “things“

• Used in conjunction with the recursive definition• Three parts:

– Basis step: Show the result holds for the elements in the basis step of the recursive definition

– Inductive hypothesis: Assume that the statement is true for some existing elements

• Usually, this just means assuming the statement is true

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

Page 30: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

31

Tree structural induction example

• Show that n(T) ≥ 2h(T) + 1 for full binary trees

• Basis step: Let T be the full binary tree of just one node r– h(T) = 0– n(T) = 1– n(T) ≥ 2h(T) + 1– 1 ≥ 2*0 + 1– 1 ≥ 1

Page 31: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

32

Tree structural induction example

• Show that n(T) ≥ 2h(T) + 1• Inductive hypothesis:

– Let T1 and T2 be full binary trees• Assume that n(T1) ≥ 2h(T1) + 1 for some tree T1

• Assume that n(T2) ≥ 2h(T2) + 1 for some tree T2

• Recursive step:– Let T = T1 ∙ T2

• Here the ∙ operator means creating a new tree with a root note r and subtrees T1 and T2

• New element is T– By the definition of height and size, we know:

• n(T) = 1 + n(T1) + n(T2)• h(T) = 1 + max ( h(T1), h(T2) )

– Therefore:• n(T) = 1 + n(T1) + n(T2)• ≥ 1 + 2h(T1) + 1 + 2h(T2) + 1• ≥ 1 + 2*max ( h(T1), h(T2) ) the sum of two non-neg #’s is at least

as large as the larger of the two

• = 1 + 2*h(T)– Thus, n(T) ≥ 2h(T) + 1

Page 32: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

33

String structural induction example

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

• Let = { 0, 1 }

• Basis step: ones() = 0

• Recursive step: ones(wx) = ones(w) + x– Where x and w *– Note that x is a bit: either 0 or 1

Page 33: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

34

String structural induction example

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

• Basis step: t = – ones (s∙) = ones(s) = ones(s)+0 = ones(s) + ones()

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

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

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

Page 34: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

35

Induction methods comparedWeak

mathematicalStrong

Mathematical Structural

Used forUsually

formulaeUsually formulae not

provable via mathematical induction

Only things defined via recursion

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

Assume statement is true for some "old"

elements

What to prove True for P(k+1) True for P(k+1)

Statement is true for some "new" elements

created with "old" elements

Step 1 calledBase case Base case Basis step

Step 3 calledInductive step Inductive step Recursive step

Page 35: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

36

Induction types compared

• Show that F(n) < 2n

– Where F(n) is the nth Fibonacci number– Actually F(n) < 20.7*n, but we won’t prove that here

• Fibonacci definition:– Basis step: F(1) = 1 and F(2) = 1– Recursive step: F(n) = F(n-1) + F(n-2)

• Base case (or basis step): Show true for F(1) and F(2)– F(1) = 1 < 21 = 2– F(2) = 1 < 22 = 4

Page 36: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

37

Via weak mathematical induction

• Inductive hypothesis: Assume F(k) < 2k

• Inductive step: Prove F(k+1) < 2k+1

– F(k+1) = F(k) + F(k-1)– We know F(k) < 2k by the inductive hypothesis– Each term is less than the next, therefore:

F(k) > F(k-1)• Thus, F(k-1) < F(k) < 2k

– Therefore, F(k+1) = F(k) + F(k-1) < 2k + 2k = 2k+1

– Proven!

Page 37: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

38

Via strong mathematical induction

• Inductive hypothesis: Assume F(1) < 21, F(2) < 22, …, F(k-1) < 2k-1, F(k) < 2k

• Inductive step: Prove F(k+1) < 2k+1

– F(k+1) = F(k) + F(k-1)– We know F(k) < 2k by the inductive hypothesis– We know F(k-1) < 2k-1 by the inductive hypothesis– Therefore, F(k) + F(k-1) < 2k + 2k-1 < 2k+1

– Proven!

Page 38: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

39

Via structural induction

• Inductive hypothesis: Assume F(n) < 2n

• Recursive step: – Show true for “new element”: F(n+1)– We know F(n) < 2n by the inductive hypothesis– Each term is less than the next, therefore

F(n) > F(n-1)• Thus, F(n-1) < F(n) < 2n

– Therefore, F(n) + F(n-1) < 2n + 2n = 2n+1

– Proven!

Page 39: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

40

Another way via structural induction

• Inductive hypothesis: Assume F(n) < 2n and F(n-1) < 2n-1

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

• Recursive step: – Show true for “new element”: F(n+1)– F(n+1) = F(n) + F(n-1)– We know F(n) < 2n by the inductive hypothesis– We know F(n-1) < 2n-1 by the inductive hypothesis– Therefore, F(n) + F(n-1) < 2k + 2k-1 < 2k+1

– Proven!

Page 40: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

41

But wait!

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

the induction types

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

Page 41: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

42

Recursive definition examples• Give the recursive definition of the following sequences

– Note that many answers are possible!

a) an = 4n – 2– Terms: 2, 6, 10, 14, 16, etc.– a1 = 2– an = an-1 + 4

b) an = 1 + (-1)n

– Terms: 0, 2, 0, 2, 0, 2, etc.– a1 = 0, a2 = 2– an = an-2

c) an = n(n+1)– Terms: 2, 6, 12, 20, 30, 42, etc.– a1 = 2– an = an-1 + 2*n

d) an = n2

– Terms: 1, 4, 9, 16, 25, 36, 49, etc.– a1 = 1– an = an-1 + 2n - 1

Page 42: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

43

More more examples

• Show that f12 + f2

2 + f32 + … + fn

2 = fnfn+1

• Base case: n = 1– f1

2 = f1f2

– 12 = 1*1

• Inductive hypothesis: Assume– f1

2 + f22 + f3

2 + … + fk2 = fkfk+1

• Inductive step: Prove– f1

2 + f22 + f3

2 + … + fk2 + fk+1

2 = fk+1fk+2

Page 43: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

44

More more more examples

• Inductive hypothesis: Assume– f1

2 + f22 + f3

2 + … + fk2 = fkfk+1

• Inductive step: Prove– f1

2 + f22 + f3

2 + … + fk2 + fk+1

2 = fk+1fk+2

– fkfk+1 + fk+12 = fk+1fk+2

– fkfk+1 + fk+12 = fk+1 (fk + fk+1)

– fkfk+1 + fk+12 = fkfk+1 + fk+1

2

Page 44: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

45

More^4 examples

• Show that f1 + f2 + f3 + … + f2n-1 = f2n

• Base case: n = 1– f1 = f2*1

– 1 = 1

• Inductive hypothesis: Assume– f1 + f2 + f3 + … + f2k-1 = f2k

• Inductive step: Prove– f1 + f2 + f3 + … + f2k-1 + f2(k+1)-1 = f2(k+1)

– f1 + f2 + f3 + … + f2k-1 + f2k+1 = f2k+2

Page 45: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

46

More^5 examples

• Inductive hypothesis: Assume– f1 + f2 + f3 + … + f2k-1 = f2k

• Inductive step: Prove– f1 + f2 + f3 + … + f2k-1 + f2k+1 = f2k+2

– f2k + f2k+1 = f2k+2

– True by definition of f2k+2

Page 46: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

47

More^6 examples• Show that the set S defined by

– Basis step: 1 S– Recursive step: s + t S when s S and t S

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

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

integer

• Proof by structural induction• Basis step: 1 S and 1 Z+

• Inductive hypothesis: Assume k S• Recursive step: Show k+1 S

– k S by the inductive hypothesis– 1 S by the base case– k+1 S by the recursive step of the recursive definition above

Page 47: 1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield

48

More^7 examples

• Give a recursive definition of the reversal of a string

• Basis step: R = – Note that the superscripted R means reversal of a string

• Recursive step: Consider a string w *– Rewrite w as vy where v * and y

• v is the first n-1 characters in w• y is the last character in w

– wR = y(vR)• Parentheses are for our benefit