Document

advertisement
Lecture 3.3: Recursion
CS 250, Discrete Structures, Fall 2011
Nitesh Saxena
*Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag
10/04/2011
Lecture 3.3 -- Recursion
1
Course Admin

Mid-Term 1 Graded




Scores posted
To be distributed at the end of today’s lecture
Again, take a careful look, and in case of any
questions, please contact the TA
HW2



Being graded
Expected to have the results by coming weekend
Solution to be posted soon
10/04/2011
Lecture 3.3 -- Recursion
2
Course Admin

Overall grades




Recall: they will be relative, based on overall
performance of the class
Further improvement possible in the upcoming
HWs and two exams
Please continue to work hard. It will pay off.
Don’t hesitate to ask for extra help
10/04/2011
Lecture 3.3 -- Recursion
3
Outline



Some practice: strong induction
Recursion
Recursive Functions and Definitions
10/04/2011
Lecture 3.3 -- Recursion
4
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]
10/04/2011
Lecture 3.3 -- Recursion
5
Recursively Defined Sequences
Often it is difficult to express the members of an
object or numerical sequence explicitly.
EG: The Fibonacci sequence:
{fn } = 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 {fn }:
INITIALIZATION: f0 = 0, f1 = 1
RECURSION:
fn = fn-1+fn-2 for n > 1.
10/04/2011
Lecture 3.3 -- Recursion
6
Recursive Functions
It is possible to think of any function with domain N as
a sequence of numbers, and vice-versa.
Simply set:
fn =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.
10/04/2011
Lecture 3.3 -- Recursion
7
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/04/2011
Lecture 3.3 -- Recursion
8
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! =
10/04/2011
Lecture 3.3 -- Recursion
9
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
10/04/2011
Lecture 3.3 -- Recursion
10
Recursive Functions: gcd
Euclid’s algorithm makes use of the fact that
gcd(x,y ) = gcd(y, x mod y)
 x, if y  0
gcd( x, y )  
gcd( y, x mod y ), otherwise
(here we assume that x > 0)
10/04/2011
Lecture 3.3 -- Recursion
11
Recursive Definitions: Mathematical
Notation
Definition of summation notation:
0, if n  0
 n 1
ai  

i 1
 ai  an , if n  0
 i 1
n
There is also a general product notation :
n
a
i
 a1  a2  an 1  an
i 1
10/04/2011
Lecture 3.3 -- Recursion
12
Recursive Definitions: Mathematical
Notation
Q: Find a recursive definition for the product
notation
n
a
i
i 1
10/04/2011
Lecture 3.3 -- Recursion
13
Recursive Definitions:
Mathematical Notation
A: This is very similar to definition of
summation notation.
1, if n  0

n
 n 1
ai  


i 1
  ai   an , if n  0
 i 1 
Note: Initialization is argument for “product
of nothing” being 1, not 0.
10/04/2011
Lecture 3.3 -- Recursion
14
Recursively Defined Sets
Our examples so far have been inductively
defined functions.
Sets can be defined inductively, too.
Give an inductive definition of
S = {x: x is a multiple of 3}
Base Case
1. 3  S
Recursive Case
2. x,y  S  x + y  S
10/04/2011
Lecture 3.3 -- Recursion
15
Strings
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.
Example: Let  = {a, b, c}. Then
* = {, a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc, aaa, aab,…}
How big is *?
10/04/2011
Countably infinite
Lecture 3.3 -- Recursion
16
Strings
Recursive definition of the length of
strings (the length of string w is |w|.):
 || = 0
 If x  , and w  *, then |wx| = |w| + 1
10/04/2011
Lecture 3.3 -- Recursion
17
Well Formed Formulae (WFF) for
Propositions
A set of wff is defined as follows:
1.
2.
3.
4.
5.
6.
This just describes fully
T is a wff
parenthesized propositions.
F is a wff
p is a wff for any propositional variable p
If p is a wff, then (p) is a wff
If p and q are wffs, then (p  q), is a wff
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.
10/04/2011
Lecture 3.3 -- Recursion
18
Today’s Reading

Rosen 5.3
10/04/2011
Lecture 3.3 -- Recursion
19
Download