Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield."— Presentation transcript:

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

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

3 3 Recursion example Rosen, section 3.4, question 1 – 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

4 4 Recursion example Rosen, section 3.4, question 1 – Find f(1), f(2), f(3), and f(4), where f(0) = 1 c) Let f(n+1) = 2 f(n) f(1) = 2 f(0) = 2 1 = 2 f(2) = 2 f(1) = 2 2 = 4 f(3) = 2 f(2) = 2 4 = 16 f(4) = 2 f(3) = 2 16 = 65536 d) Let f(n+1) = f(n) 2 + f(n) + 1 f(1) = f(0) 2 + f(0) + 1 = 1 2 + 1 + 1 = 3 f(2) = f(1) 2 + f(0) + 1 = 3 2 + 3 + 1 = 13 f(3) = f(2) 2 + f(0) + 1 = 13 2 + 13 + 1 = 183 f(4) = f(3) 2 + f(0) + 1 = 183 2 + 183 + 1 = 33673

5 5 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

6 6 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))); }

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

8 8 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)?

9 9 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

10 10 Defining sets via recursion Rosen, section 3.4, question 24: 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) + cx n  S – c  Z, n  Z and n ≥ 0

11 11 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?

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

13 13 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

14 14 Today ’ s demotivators

15 15 Recursion pros Easy to program Easy to understand

16 16 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 * 10 20 At 1 billion recursive calls per second (generous), this would take over 22,000 years But that would also take well over 10 12 Gb of memory!

17 17 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 ”

18 18 Recursion vs. induction Rosen, section 3.4, example 7 (page 262) 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

19 19 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 Done in the text, page 267 (not reproduced here)

20 20 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

21 21 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

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


Download ppt "1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield."

Similar presentations


Ads by Google