Recursive Definitions and Structural Induction CSAPMA 202 Rosen

  • Slides: 57
Download presentation
Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3. 4 Aaron Bloomfield 1

Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3. 4 Aaron Bloomfield 1

Recursion means defining something, such as a function, in terms of itself n n

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

Recursion example Rosen, section 3. 4, question 1 n a) Find f(1), f(2), f(3),

Recursion example Rosen, section 3. 4, question 1 n a) Find f(1), f(2), f(3), and f(4), where f(0) = 1 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) = 3 f(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 3

Recursion example Rosen, section 3. 4, question 1 n c) Find f(1), f(2), f(3),

Recursion example Rosen, section 3. 4, question 1 n c) Find f(1), f(2), f(3), and f(4), where f(0) = 1 Let f(n+1) = 2 f(n) f(1) = 2 f(0) = 21 = 2 f(2) = 2 f(1) = 22 = 4 f(3) = 2 f(2) = 24 = 16 f(4) = 2 f(3) = 216 = 65536 d) Let f(n+1) = f(n)2 + f(n) + 1 f(1) = f(0)2 + f(0) + 1 = 12 + 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 4

Fractals A fractal is a pattern that uses recursion n The pattern itself repeats

Fractals A fractal is a pattern that uses recursion n The pattern itself repeats indefinitely 5

Fractals 6

Fractals 6

Fibonacci sequence Definition of the Fibonacci sequence n n Non-recursive: Recursive: or: F(n) =

Fibonacci sequence Definition of the Fibonacci sequence n n Non-recursive: Recursive: or: F(n) = F(n-1) + F(n-2) F(n+1) = F(n) + F(n-1) Must always specify base case(s)! n n F(1) = 1, F(2) = 1 Note that some will use F(0) = 1, F(1) = 1 7

Fibonacci sequence in Java long Fibonacci if ( (n == return else return }

Fibonacci sequence in Java long Fibonacci if ( (n == return else return } (int n) { 1) || (n == 2) ) 1; Fibonacci (n-1) + Fibonacci (n-2); long Fibonacci 2 (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))); } 8

Recursion definition From “The Hacker’s Dictionary”: recursion n. See recursion. See also tail recursion.

Recursion definition From “The Hacker’s Dictionary”: recursion n. See recursion. See also tail recursion. 9

Bad recursive definitions Consider: n n n f(0) = 1 f(n) = 1 +

Bad recursive definitions Consider: n n n f(0) = 1 f(n) = 1 + f(n-2) What is f(1)? Consider: n n n f(0) = 1 f(n) = 1+f(-n) What is f(1)? 10

Defining sets via recursion Same two parts: n n Base case (or basis step)

Defining sets via recursion Same two parts: n n Base case (or basis step) Recursive step Example: the set of positive integers n n Basis step: 1 S Recursive step: if x S, then x+1 S 11

Defining sets via recursion Rosen, section 3. 4, question 24: give recursive definitions for:

Defining sets via recursion Rosen, section 3. 4, question 24: give recursive definitions for: a) The set of odd positive integers n n b) The set of positive integer powers of 3 n n c) 1 S If x S, then x+2 S 3 S If x S, then 3*x S The set of polynomials with integer coefficients n n 0 S If p(x) S, then p(x) + cxn S n c Z, n Z and n ≥ 0 12

Defining strings via recursion Terminology n n is the empty string: “” is the

Defining strings via recursion Terminology n n 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 n n 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? 13

Defining strings via recursion Let = { 0, 1 } Thus, * is the

Defining strings via recursion Let = { 0, 1 } Thus, * is the set of all binary numbers n n Or all binary strings Or all possible computer files 14

String length via recursion How to define string length recursively? n n Basis step:

String length via recursion How to define string length recursively? n n Basis step: l( ) = 0 Recursive step: l(wx) = l(w) + 1 if w * and x Example: l(“aaa”) n n n l(“aaa”) = l(“aa”) + 1 l(“aa”) = l(“a”) + 1 l(“a”) = l(“”) + 1 l(“”) = 0 Result: 3 15

Today’s demotivators 16

Today’s demotivators 16

Strings via recursion example Rosen, section 3. 4, question 38: Give a recursive definition

Strings via recursion example Rosen, section 3. 4, question 38: Give a recursive definition for the set of string that are palindromes n We will define set P, which is the set of all palindromes Basis step: P n Second basis step: x P when x Recursive step: xpx P if x and p P 17

Strings and induction example This requires structural induction, which will be covered later in

Strings and induction example This requires structural induction, which will be covered later in this slide set 18

Recursion pros Easy to program Easy to understand 19

Recursion pros Easy to program Easy to understand 19

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

Recursion cons Consider the recursive Fibonacci generator How many recursive calls does it make? n n n 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! 20

Trees Rooted trees: n A graph containing nodes and edges Cannot contain a cycle!

Trees Rooted trees: n A graph containing nodes and edges Cannot contain a cycle! Cycle not allowed in a tree 21

Rooted trees Recursive definition: n n Basis step: A single vertex r is a

Rooted trees Recursive definition: n n Basis step: A single vertex r is a rooted tree Recursive step: Let T 1, T 2, …, 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 T 1, T 2, …, Tn 22

(Extended) Binary trees Recursive definition n n Basis step: The empty set is an

(Extended) Binary trees Recursive definition n n Basis step: The empty set is an extended binary tree Recursive step: Let T 1, and T 2 be extended binary trees Form a new tree with a new root r Form a new tree such that T 1 is the left subtree, and T 2 is the right subtree 23

Full binary trees Recursive definition n n Basis step: A full binary tree consisting

Full binary trees Recursive definition n n Basis step: A full binary tree consisting only of the vertex r Recursive step: Let T 1, and T 2 be extended binary trees Form a new tree with a new root r Form a new tree T such that T 1 is the left subtree, and T 2 is the right subtree n This is denoted by T = T 1∙T 2 Note the only difference between a regular binary tree and a full one is the basis step 24

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

Binary tree height h(T) denotes the height of tree T Recursive definition: n n Basis step: The height of a tree with only one node r is 0 Recursive step: Let T 1 and T 2 be binary trees The binary tree T = T 1∙T 2 has height h(T) = 1 + max ( h(T 1), h(T 2) ) This definition can be generalized to non-binary trees 25

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

Binary tree size n(T) denotes the number of vertices in tree T Recursive definition: n n n 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 T 1 and T 2 be binary trees The number of vertices in binary tree T = T 1∙T 2 is: n(T) = 1 + n(T 1) + n(T 2) This definition can be generalized to nonbinary trees 26

A bit of humor: Computer terminology 27

A bit of humor: Computer terminology 27

Recursion vs. induction Consider the recursive definition for factorial: n f(0) = 1 Base

Recursion vs. induction Consider the recursive definition for factorial: n f(0) = 1 Base case n f(n) = n * f(n-1) The “step” Sort of like induction 28

Recursion vs. induction Rosen, section 3. 4, example 7 (page 262) Consider the set

Recursion vs. induction Rosen, section 3. 4, example 7 (page 262) Consider the set of all integers that are multiples of 3 n n { 3, 6, 9, 12, 15, … } { x | x = 3 k and k Z+ } Recursive definition: n n Basis step: 3 S Recursive step: If x S and y S, then x+y S 29

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

Recursion vs. induction Proof via induction: prove that S contains all the integers that are divisible by 3 n n Let A be the set of all ints divisible by 3 Show that S = A Two parts: n Show that S A Let P(n) = 3 n S Base case: P(1) = 3*1 S n 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 n n n 3*(k+1) = 3 k+3 3 k S by the inductive hypothesis 3 S by the base case Thus, 3 k+3 S by the recursive definition Show that A S Done in the text, page 267 (not reproduced here) 30

What did we just do? Notice what we did: n n n Showed the

What did we just do? Notice what we did: n n n Showed the base case Assumed the inductive hypothesis For the inductive step, we: Showed that each of the “parts” were in S n The parts being 3 k and 3 Showed that since both parts were in S, by the recursive definition, the combination of those parts is in S n i. e. , 3 k+3 S This is called structural induction 31

Structural induction A more convenient form of induction for recursively defined “things“ Used in

Structural induction A more convenient form of induction for recursively defined “things“ Used in conjunction with the recursive definition Three parts: n n 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 n Recursive step: Show that the recursive definition allows the creation of a new element using the existing elements 32

Tree structural induction example Rosen, section 3. 4, question 43 Show that n(T) ≥

Tree structural induction example Rosen, section 3. 4, question 43 Show that n(T) ≥ 2 h(T) + 1 Basis step: Let T be the full binary tree of just one node r n n n h (T ) = 0 n (T ) = 1 n (T ) ≥ 2 h (T ) + 1 1 ≥ 2*0 + 1 1≥ 1 34

Tree structural induction example Show that n(T) ≥ 2 h(T) + 1 Inductive hypothesis:

Tree structural induction example Show that n(T) ≥ 2 h(T) + 1 Inductive hypothesis: n Let T 1 and T 2 be full binary trees Assume that n(T 1) ≥ 2 h(T 1) + 1 for some tree T 1 Assume that n(T 2) ≥ 2 h(T 2) + 1 for some tree T 2 Recursive step: n Let T = T 1 ∙ T 2 Here the ∙ operator means creating a new tree with a root note r and subtrees T 1 and T 2 New element is T n By the definition of height and size, we know: n (T ) = 1 + n (T 1 ) + n (T 2 ) h(T) = 1 + max ( h(T 1), h(T 2) ) n Therefore: n (T ) = 1 + n (T 1 ) + n (T 2 ) ≥ 1 + 2 h (T 1 ) + 1 + 2 h (T 2 ) + 1 ≥ 1 + 2*max ( h(T 1), h(T 2) ) = 1 + 2*h(T) n Thus, n(T) ≥ 2 h(T) + 1 the sum of two non-neg #’s is at least as large as the larger of the two 35

String structural induction example Rosen, section 3. 4, question 32 Part (a): Give the

String structural induction example Rosen, section 3. 4, question 32 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 n n Where x and w * Note that x is a bit: either 0 or 1 36

String structural induction example Part (b): Use structural induction to prove that ones(st) =

String structural induction example Part (b): Use structural induction to prove that ones(st) = ones(s) + ones(t) Basis step: t = n 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) n n n n Where s, t * and x New element is ones(s∙t∙x) ones (s∙t∙x) = ones ((s∙t)∙x)) = x+ones(s∙t) = x + ones(s) + ones(t) = ones(s) + (x + ones(t)) = ones(s) + ones(t∙x) Proven! by associativity of concatenation by recursive definition by inductive hypothesis by commutativity and assoc. of + by recursive definition 37

Quick survey n a) b) c) d) I feel I understand structural induction… Very

Quick survey n a) b) c) d) I feel I understand structural induction… Very well With some review, I’ll be good Not really Not at all 38

Human stupidity 39

Human stupidity 39

Induction methods compared Used for Assumption Weak mathematical Strong Mathematical Structural Usually formulae not

Induction methods compared Used for Assumption Weak mathematical Strong Mathematical Structural Usually formulae not provable via mathematical induction Only things defined via recursion Assume P(1), P(2), …, P(k) Assume statement is true for some "old" elements Assume P(k) Statement is true for some "new" elements created with "old" elements What to prove True for P(k+1) Step 1 called Base case Basis step Step 3 called Inductive step Recursive step 40

Induction types compared Show that F(n) < 2 n n n Where F(n) is

Induction types compared Show that F(n) < 2 n n n Where F(n) is the nth Fibonacci number Actually F(n) < 20. 7*n, but we won’t prove that here Fibonacci definition: n n 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) n n F(1) = 1 < 21 = 2 F(2) = 1 < 22 = 4 41

Via weak mathematical induction Inductive hypothesis: Assume F(k) < 2 k Inductive step: Prove

Via weak mathematical induction Inductive hypothesis: Assume F(k) < 2 k Inductive step: Prove F(k+1) < 2 k+1 n n n F(k+1) = F(k) + F(k-1) We know F(k) < 2 k by the inductive hypothesis Each term is less than the next, therefore F(k) > F(k-1) Thus, F(k-1) < F(k) < 2 k n n Therefore, F(k+1) = F(k) + F(k-1) < 2 k + 2 k = 2 k+1 Proven! 42

Via strong mathematical induction Inductive hypothesis: Assume F(1) < 21, F(2) < 22, …,

Via strong mathematical induction Inductive hypothesis: Assume F(1) < 21, F(2) < 22, …, F(k-1) < 2 k-1, F(k) < 2 k Inductive step: Prove F(k+1) < 2 k+1 n n n F(k+1) = F(k) + F(k-1) We know F(k) < 2 k by the inductive hypothesis We know F(k-1) < 2 k-1 by the inductive hypothesis Therefore, F(k) + F(k-1) < 2 k + 2 k-1 < 2 k+1 Proven! 43

Via structural induction Inductive hypothesis: Assume F(n) < 2 n Recursive step: n n

Via structural induction Inductive hypothesis: Assume F(n) < 2 n Recursive step: n n n Show true for “new element”: F(n+1) We know F(n) < 2 n by the inductive hypothesis Each term is less than the next, therefore F(n) > F(n-1) Thus, F(n-1) < F(n) < 2 n n n Therefore, F(n) + F(n-1) < 2 n + 2 n = 2 n+1 Proven! 44

Another way via structural induction Inductive hypothesis: Assume F(n) < 2 n and F(n-1)

Another way via structural induction Inductive hypothesis: Assume F(n) < 2 n and F(n-1) < 2 n-1 n The difference here is we are using two “old” elements versus one, as in the last slide Recursive step: n n n Show true for “new element”: F(n+1) = F(n) + F(n-1) We know F(n) < 2 n by the inductive hypothesis We know F(n-1) < 2 n-1 by the inductive hypothesis Therefore, F(n) + F(n-1) < 2 k + 2 k-1 < 2 k+1 Proven! 45

But wait! In this example, the structural induction proof was essentially the same as

But wait! In this example, the structural induction proof was essentially the same as the weak or strong mathematical induction proof n 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 n Trees, strings, etc. 46

A bit of humor… 47

A bit of humor… 47

Section 3. 4, question 8 Give the recursive definition of the following sequences n

Section 3. 4, question 8 Give the recursive definition of the following sequences n Note that many answers are possible! a) an = 4 n – 2 n n n Terms: 2, 6, 10, 14, 16, etc. a 1 = 2 an = an-1 + 4 b) an = 1 + (-1)n n Terms: 0, 2, etc. a 1 = 0, a 2 = 2 an = an-2 c) an = n(n+1) n n n Terms: 2, 6, 12, 20, 30, 42, etc. a 1 = 2 an = an-1 + 2*n d) an = n 2 n n n Terms: 1, 4, 9, 16, 25, 36, 49, etc. a 1 = 1 an = an-1 + 2 n - 1 48

Section 3. 4, question 12 Show that f 12 + f 22 + f

Section 3. 4, question 12 Show that f 12 + f 22 + f 32 + … + fn 2 = fnfn+1 Base case: n = 1 n n f 12 = f 1 f 2 12 = 1*1 Inductive hypothesis: Assume n f 12 + f 22 + f 32 + … + fk 2 = fkfk+1 Inductive step: Prove n f 12 + f 22 + f 32 + … + fk 2 + fk+12 = fk+1 fk+2 49

Section 3. 4, question 12 Inductive hypothesis: Assume n f 12 + f 22

Section 3. 4, question 12 Inductive hypothesis: Assume n f 12 + f 22 + f 32 + … + fk 2 = fkfk+1 Inductive step: Prove n f 12 + f 22 + f 32 + … + fk 2 + fk+12 = fk+1 fk+2 n fkfk+1 + fk+12 = fk+1 fk+2 n n fkfk+1 + fk+12 = fk+1 (fk + fk+1) fkfk+1 + fk+12 = fkfk+1 + fk+12 50

Section 3. 4, question 13 Show that f 1 + f 2 + f

Section 3. 4, question 13 Show that f 1 + f 2 + f 3 + … + f 2 n-1 = f 2 n Base case: n = 1 n n f 1 = f 2*1 1=1 Inductive hypothesis: Assume n f 1 + f 2 + f 3 + … + f 2 k-1 = f 2 k Inductive step: Prove n n f 1 + f 2 + f 3 + … + f 2 k-1 + f 2(k+1)-1 = f 2(k+1) f 1 + f 2 + f 3 + … + f 2 k-1 + f 2 k+1 = f 2 k+2 51

Section 3. 4, question 13 Inductive hypothesis: Assume n f 1 + f 2

Section 3. 4, question 13 Inductive hypothesis: Assume n f 1 + f 2 + f 3 + … + f 2 k-1 = f 2 k Inductive step: Prove n f 1 + f 2 + f 3 + … + f 2 k-1 + f 2 k+1 = f 2 k+2 n f 2 k + f 2 k+1 = f 2 k+2 n True by definition of f 2 k+2 52

Section 3. 4, question 22 Show that the set S defined by n n

Section 3. 4, question 22 Show that the set S defined by n n Basis step: 1 S Recursive step: s + t S when s S and t S is the set of positive integers: n Z+ = { 1, 2, 3, … } Note the (somewhat recursive) definition of the positive integers: n n 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 n n n k S by the inductive hypothesis 1 S by the base case k+1 S by the recursive step of the recursive definition above 53

Section 3. 4, question 35 Give a recursive definition of the reversal of a

Section 3. 4, question 35 Give a recursive definition of the reversal of a string Basis step: R = n Note that the superscripted R means reversal of a string Recursive step: Consider a string w * n Rewrite w as vy where v * and y v is the first n-1 characters in w y is the last character in w n w. R = y(v. R) Parentheses are for our benefit 54

Quick survey n a) b) c) d) I felt I understood the material in

Quick survey n a) b) c) d) I felt I understood the material in this slide set… Very well With some review, I’ll be good Not really Not at all 55

Quick survey n a) b) c) d) The pace of the lecture for this

Quick survey n a) b) c) d) The pace of the lecture for this slide set was… Fast About right A little slow Too slow 56

Quick survey n a) b) c) d) How interesting was the material in this

Quick survey n a) b) c) d) How interesting was the material in this slide set? Be honest! Wow! That was SOOOOOO cool! Somewhat interesting Rather borting Zzzzzz 57

Today’s demotivators 58

Today’s demotivators 58