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

Slides:



Advertisements
Similar presentations
Mathematical Induction
Advertisements

22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Discrete Mathematics Math 6A Homework 9 Solution.
Recursive Definitions and Structural Induction
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Induction and recursion
CSE115/ENGR160 Discrete Mathematics 04/03/12 Ming-Hsuan Yang UC Merced 1.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
CSE115/ENGR160 Discrete Mathematics 03/31/11
1 Recitation 10. Induction Introduction. Induction is useful in proving properties of recursive algorithms, like their execution times. It is also the.
Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
1 Section 3.4 Recursive Definitions. 2 Recursion Recursion is the process of defining an object in terms of itself Technique can be used to define sequences,
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
Discrete Mathematics Chapter 4 Induction and Recursion 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
Recursive Definitions and Structural Induction
1 Sequences and Summations CS/APMA 202 Rosen section 3.2 Aaron Bloomfield.
Discrete Mathematics CS 2610 March 26, 2009 Skip: structural induction generalized induction Skip section 4.5.
Lecture 9. Arithmetic and geometric series and mathematical induction
Induction and recursion
Chapter 4: Induction and Recursion
CSE 311 Foundations of Computing I Lecture 15 Recursive Definitions and Structural Induction Autumn 2011 CSE 3111.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
CSE 311 Foundations of Computing I Lecture 16 Recursively Defined Sets and Structural Induction Spring
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Section 2.4. Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations.
Induction and recursion
MA/CSSE 474 Theory of Computation Functions, Closure, Decision Problems.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Chapter 5 With Question/Answer Animations. Section 5.1.
4.3 Recursive Definitions and Structural Induction Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
1 Lecture 3.3: Recursion CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Chapter 1: Introduction
Discrete Structure Li Tak Sing( 李德成 ) Lecture 13 1.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield.
Inductive Proofs and Inductive Definitions Jim Skon.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Quiz 7 The way I expected it.. How to do it! 1. Let P(n) be the statement that n 2 = n(n + 1)(2n + 1)/6 for the positive integer n. Be.
Chapter 5 Kenneth Rosen, Discrete Mathematics and its Applications, 7th edition, McGraw Hill Instructor: Longin Jan Latecki, Copyright.
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CSE 311 Foundations of Computing I Lecture 15 Strong Induction and Recursive Definitions Spring
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
(Proof By) Induction Recursion
Chapter 4: Induction and Recursion
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
CSE 311: Foundations of Computing
Mathematical Induction Recursion
Discrete Structures for Computer Science
Lecture Outline for Recurrences
Discrete Structures for Computer Science
Applied Discrete Mathematics Week 9: Integer Properties
CS 250, Discrete Structures, Fall 2015 Nitesh Saxena
Mathematical Induction
Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011
Induction and recursion
Chapter 4 (Part 2): Mathematical Reasoning, Induction & Recursion
CSE 311 Foundations of Computing I
Recursion.
Presentation transcript:

1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 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)

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 = = 3 f(2) = f(1) + 2 = = 5 f(3) = f(2) + 2 = = 7 f(4) = f(3) + 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 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 = d) Let f(n+1) = f(n) 2 + f(n) + 1 f(1) = f(0) 2 + f(0) + 1 = = 3 f(2) = f(1) 2 + f(0) + 1 = = 13 f(3) = f(2) 2 + f(0) + 1 = = 183 f(4) = f(3) 2 + f(0) + 1 = = 33673

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 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 Recursion definition From “ The Hacker ’ s Dictionary ” : recursion n. See recursion. See also tail recursion.

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 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 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 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 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 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 Today ’ s demotivators

15 Recursion pros Easy to program Easy to understand

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

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