Module #14 - Recursion
Izmir Institute of Technology
CENG 115
Discrete Structures
Slides are based on the Text
Discrete Mathematics & Its Applications (6th Edition)
by Kenneth H. Rosen
12/11/2018 1
Module #14 - Recursion
Module #13:
Recursion
Rosen 6th ed., §§4.3-4.4
12/11/2018 2
Module #14 - Recursion
Recursion
• Recursion is a general term for defining an
object in terms of itself (or of part of itself).
• An inductive proof establishes the truth of
P(n+1) recursively in terms of P(n).
• We can use recursion to define algorithms,
functions, sequences, and sets.
12/11/2018 3
Module #14 - Recursion
§4.3: Recursive Definitions
• In mathematical induction, we prove P for all
members of an infinite set by proving the truth
of larger members in terms of that of smaller
members.
• A recursive definition is writing the
function/predicate value of larger elements in
terms of that of smaller ones provided that a
starting point/element is given.
12/11/2018 4
Module #14 - Recursion
Recursively Defined Functions
• A recursive way to define a function f:NS
(for any set S) or series an=f(n) is to:
– Specify f(0).
– For n>0, define f(n) in terms of f(0),…,f(n−1).
• E.g.: Define the series an :≡ 2n recursively:
– Let a0 :≡ 1.
– For n>0, let an :≡ 2an-1.
12/11/2018 5
Module #14 - Recursion
Recursively Defined Functions
Another example:
• Suppose we define f(n) for all nN
recursively by:
– Let f(0)=3
– For all nN, let f(n+1)=2f(n)+3
• What are the values of the following?
– f(1)= 9 f(2)= 21 f(3)= 45 f(4)= 93
12/11/2018 6
Module #14 - Recursion
Recursive definition of Factorial
• Give a recursive definition of the factorial
function F(n) :≡ n! :≡ 23…n.
– Basis step: F(0) :≡ 1
– Recursive step: F(n) :≡ n F(n-1).
• F(1)=1
• F(2)=2
• F(3)=6
• F(4)=24
12/11/2018 7
Module #14 - Recursion
The Fibonacci Series
• The Fibonacci series fn≥0 is a famous series
defined by:
f0 :≡ 0, f1 :≡ 1, fn≥2 :≡ fn−1 + fn−2
0
1 1
2 3
5 8
13
Leonardo Fibonacci
1170-1250
12/11/2018 8
Module #14 - Recursion
Inductive Proof about Fib. Series
• Theorem: n N fn < 2n
• Proof by induction:
Basis step: f0 = 0 < 20 = 1
f1 = 1 < 21 = 2
Inductive step: Use 2nd principle of induction
Assume k<n, fk < 2k (inductive hypothesis).
Then
fn = fn−1 + fn−2 < 2n−1 + 2n−2 < 2n−1 + 2n−1 = 2n.
inductive hypothesis
12/11/2018 9
Module #14 - Recursion
Recursively Defined Sets
• An infinite set S may be defined
recursively, by giving:
– A small finite set of base elements of S.
– A rule for constructing new elements of S from
previously-established elements.
– Implicitly, S has no other elements but these.
• Example: Let 3S, and let x+yS if x,yS.
What is S?
12/11/2018 10
Module #14 - Recursion
The Set of All Strings
• Given an alphabet Σ, the set Σ* of all strings
over Σ can be recursively defined as:
Basis step:
λ Σ* (λ :≡ “”, the empty string)
Recursive step:
w Σ* x Σ → wx Σ*
12/11/2018 11
Module #14 - Recursion
§4.4: Recursive Algorithms
• A recursive algorithm solves a problem by
reducing it to an instance of the same
problem with smaller input.
• Example: A recursive alg. to compute an.
procedure power(a≠0: real, nN)
if n = 0 then return 1
else return a · power(a, n−1)
12/11/2018 12
Module #14 - Recursion
Efficiency of Recursive Algorithms
• The time complexity of a recursive algorithm
depend critically on the number of recursive
calls it makes.
• Example: Modular exponentiation to a power n
[bn mod m] takes log(n) time if done right, but
linear time if done slightly different.
• Task: Compute bn mod m, where
m≥2, n≥0, and 1≤b<m.
12/11/2018 13
Module #14 - Recursion
Modular Exponentiation Alg. #1
Uses the fact that
bn mod m = (b·(bn−1 mod m)) mod m.
procedure mpower (integers b≥1,n≥0,m ≥ 2)
if n=0 then mpower(b,n,m)=1
else
mpower(b,n,m)=(b·mpower(b,n−1,m)) mod m
{mpower(b,n,m)= bn mod m}
Note: This algorithm’s time complexity is Θ(n)
(assume each mpower call takes a constant time)
12/11/2018 14
Module #14 - Recursion
Modular Exponentiation Alg. #2
Uses the fact that
bn mod m = (bn/2 mod m)2 mod m.
procedure mpower(b,n,m)
if n=0 then mpower(b,n,m)=1
else if 2|n then
mpower(b,n,m)= mpower(b,n/2,m)2 mod m
else
mpower(b,n,m)= (mpower(b,n−1,m)·b) mod m
What is its time complexity? Θ(log n)
12/11/2018 15
Module #14 - Recursion
Recursive Euclid’s Algorithm
procedure gcd(a,bN)
if a = 0 then
gcd(a,b):= b
else
gcd(a,b):= gcd(b mod a, a)
Note that recursive algorithms are often
simpler to code than iterative ones, but they
may require more computation (next slide).
12/11/2018 16
Module #14 - Recursion
Recursive Algorithm for Fib. Series
procedure fibonacci (nN)
if n = 0 then fibonacci(n):= 0
else if n = 1 then fibonacci(n):= 1
else
fibonacci(n):= fibonacci(n-1)+fibonacci(n-2)
The tree (recursive calls)
causes fn+1-1 addition operations
12/11/2018 17
Module #14 - Recursion
Iterative Algorithm for Fib. Series
procedure iterative fibonacci (nN)
if n = 0 then y:= 0
else begin
x:=0 , y:=1
for i:=1 to n-1
z:=x+y , x:=y , y:=z
end {y is the nth fibonacci number}
This algorithm requires n-1 addition operations to
find fn, more efficient than the recursive one.
12/11/2018 18