Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 3.3: Recursion CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag.

Similar presentations


Presentation on theme: "1 Lecture 3.3: Recursion CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag."— Presentation transcript:

1 1 Lecture 3.3: Recursion CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag

2 11/26/2015Lecture 3.3 -- Recursion2 Course Admin Mid-Term 1 Graded Pick’em up if not already done. HW2 Graded

3 11/26/2015Lecture 3.3 -- Recursion3 Course Admin HW3 to be posted soon Covers “Induction and Recursion” (Chapter 5) Due in 2 weeks – Tues, Oct 30

4 11/26/2015Lecture 3.3 -- Recursion4 Course Admin Mid-Term 2 Exam Thu, Oct 25, in class, 11-12:15. Covers Chapter 5 (lecture 3.*) Review on Oct 23

5 11/26/2015Lecture 3.3 -- Recursion5 Outline Some practice: strong induction Recursion Recursive Functions and Definitions

6 11/26/2015Lecture 3.3 -- Recursion6 Strong Induction Example(Rosen) Prove that every integer > 1 can be expressed as a product of prime numbers [This is referred to as the fundamental theorem of arithmetic]

7 11/26/2015Lecture 3.3 -- Recursion7 Recursively Defined Sequences Often it is difficult to express the members of an object or numerical sequence explicitly. EG: The Fibonacci sequence: {f n } = 0,1,1,2,3,5,8,13,21,34,55,… There may, however, be some “local” connections that can give rise to a recursive definition –a formula that expresses higher terms in the sequence, in terms of lower terms. EG: Recursive definition for {f n }: INITIALIZATION:f 0 = 0, f 1 = 1 RECURSION: f n = f n-1 +f n-2 for n > 1.

8 11/26/2015Lecture 3.3 -- Recursion8 Recursive Functions It is possible to think of any function with domain N as a sequence of numbers, and vice-versa. Simply set: f n =f (n) For example, our Fibonacci sequence becomes the Fibonacci function as follows: f (0) = 0, f (1) = 1, f (2) = 1, f (3) = 2,… Such functions can then be defined recursively by using recursive sequence definition. EG: INITIALIZATION:f (0) = 0, f (1) = 1 RECURSION: f (n) = f (n -1) +f (n -2), for n > 1.

9 11/26/2015Lecture 3.3 -- Recursion9 Recursive Functions: Factorial A simple example of a recursively defined function is the factorial function: n! = 1· 2· 3· 4 ···(n –2)·(n –1)·n i.e., the product of the first n positive numbers (by convention, the product of nothing is 1, so that 0! = 1). Q: Find a recursive definition for n!

10 11/26/2015Lecture 3.3 -- Recursion10 Recursive Functions: Factorial A:INITIALIZATION:0!= 1 RECURSION: n != n · (n -1)! To compute the value of a recursive function, e.g. 5!, one plugs into the recursive definition obtaining expressions involving lower and lower values of the function, until arriving at the base case. EG: 5! =

11 11/26/2015Lecture 3.3 -- Recursion11 Recursive Functions: Factorial A:INITIALIZATION:0!= 1 RECURSION: n != n · (n -1)! To compute the value of a recursive function, e.g. 5!, one plugs into the recursive definition obtaining expressions involving lower and lower values of the function, until arriving at the base case. EG: 5! = 5 · 4! = 5 · 4 · 3! = 5 · 4 · 3 · 2! = 5 · 4 · 3 · 2 · 1! = 5 · 4 · 3 · 2 · 1 · 0! = 120

12 11/26/2015Lecture 3.3 -- Recursion12 Recursive Functions: gcd Euclid’s algorithm makes use of the fact that gcd(x,y ) = gcd(y, x mod y) (here we assume that x > 0)

13 11/26/2015Lecture 3.3 -- Recursion13 Recursive Definitions: Mathematical Notation Definition of summation notation: There is also a general product notation :

14 11/26/2015Lecture 3.3 -- Recursion14 Recursive Definitions: Mathematical Notation Q: Find a recursive definition for the product notation

15 11/26/2015Lecture 3.3 -- Recursion15 Recursive Definitions: Mathematical Notation A: This is very similar to definition of summation notation. Note: Initialization is argument for “product of nothing” being 1, not 0.

16 11/26/2015Lecture 3.3 -- Recursion16 Recursively Defined Sets Our examples so far have been inductively defined functions. Sets can be defined inductively, too. Recursive CaseBase Case Give an inductive definition of S = {x: x is a multiple of 3} 1. 3  S 2. x,y  S  x + y  S

17 11/26/2015Lecture 3.3 -- Recursion17 Strings: Recursive Definition Let  be a finite set called an alphabet. The set of strings on , denoted  * is defined as:   *, where denotes the null or empty string. If x  , and w   *, then wx   *, where wx is the concatenation of string w with symbol x. Countably infinite Example: Let  = {a, b, c}. Then  * = {, a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc, aaa, aab,…} How big is  *?

18 11/26/2015Lecture 3.3 -- Recursion18 Length of a String: Recursive Definition Recursive definition of the length of strings (the length of string w is |w|.): | | = 0 If x  , and w   *, then |wx| = |w| + 1

19 11/26/2015Lecture 3.3 -- Recursion19 Well Formed Formulae (WFF) for Propositions A set of wff is defined as follows: 1. T is a wff 2. F is a wff 3. p is a wff for any propositional variable p 4. If p is a wff, then (  p) is a wff 5. If p and q are wffs, then (p  q), is a wff 6. If p and q are wffs, then (p  q) is a wff For example, a statement like ((  r)  (p  r)) can be proven to be a wff by arguing that (  r) and (p  r) are wffs by recursion and then applying rule 5.

20 11/26/2015Lecture 3.3 -- Recursion20 Today’s Reading Rosen 5.3


Download ppt "1 Lecture 3.3: Recursion CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag."

Similar presentations


Ads by Google