19-recursion-3
19-recursion-3
Ind.&Rec.: Recursion
(Ch 5.3/5.4)*
2
Motivation
Consider the following sequence:
S = {1, 5, 9, 13, etc…}
Si = 1 + 4i
Si = Si-12 +4 & S0 = 1
3
Recursively Defined Functions
A recursive or inductive definition of a
function consists of two steps
4
Recursively Defined Functions
Example: Suppose f is defined by:
f(0) = 3,
f(n + 1) = 2f(n) + 3
Find f(1), f(2), f(3), f(4)
Solution:
f(1) 2f(0) + 3 = 2∙3 + 3 = 9
=
f(2) = 2f(1)+ 3 = 2∙9 + 3 = 21
Solution:
f(0) = 1
f(n) = n ∙ f(n-1)
5
Recursively Defined Functions
Example: Give a recursive definition of:
Solution:
The first part of the definition is
never die?
7
Modeling the Rabbits Population Growth
8
Rabbits Solution
Solution: Let fn be the the number of pairs of rabbits after n
months
9
Leonardo of
Pisa Fibonacci
(1170- 1250)
Fibonacci Numbers
Example : The Fibonacci numbers are defined
as follows:
f0 = 0
f1 = 1
fn = fn−1 + fn−2
Find f2, f3 , f4 , f5
f2 = f1 + f0 = 1 + 0 = 1
f3 = f2 + f1 = 1 + 1 = 2
f4 = f3 + f2 = 2 + 1 = 3
f5 = f4 + f3 = 3 + 2 = 5
10
Recursive Algorithms (Ch.
5.4)
11
Recursive Algorithms
An algorithm is called recursive if it solves a
problem by reducing it to an instance of the
12
Recursive Factorial Algorithm
Give a recursive algorithm for computing n!,
where n is a nonnegative integer
Basis step: 0! = 1
Recursive step: n! = n.(n-1)!
factorial(2)
if n = 0 then return 1
else return n∙factorial(n − 1)
{output is n!}
factorial(1)
if n = 0 then return 1
else return n∙factorial(n − 1)
{output is n!}
factorial(0)
if n = 0 then return 1
else return n∙factorial(n − 1)
{output is n!}
14
Recursive Factorial Algorithm
factorial(4) (n = 4) procedure factorial(n: nonnegative
if n = 0 then return 1 integer)
else return n∙factorial(n − 1)
{output is n!} if n = 0 then
return 1
factorial(3) else
if n = 0 then return 1 return n∙factorial(n − 1){output
else return n∙factorial(n − 1) is n!}
{output is n!}
factorial(2)
if n = 0 then return 1
else return n∙factorial(n − 1)
{output is n!}
factorial(1)
if n = 0 then return 1
else return n∙factorial(n − 1)
1
{output is n!}
15
Recursive Factorial Algorithm
factorial(4) (n = 4) procedure factorial(n: nonnegative
if n = 0 then return 1 integer)
else return n∙factorial(n − 1)
{output is n!} if n = 0 then
return 1
factorial(3) else
if n = 0 then return 1 return n∙factorial(n − 1){output
else return n∙factorial(n − 1) is n!}
{output is n!}
factorial(2)
if n = 0 then return 1
else return n∙factorial(n − 1)
{output is n!}
factorial(1)
if n = 0 then return 1
else return 1∙1{output is n!}
1
16
Recursive Factorial Algorithm
factorial(4) (n = 4) procedure factorial(n: nonnegative
if n = 0 then return 1 integer)
else return n∙factorial(n − 1)
{output is n!} if n = 0 then
return 1
factorial(3) else
if n = 0 then return 1 return n∙factorial(n − 1){output
else return n∙factorial(n − 1) is n!}
{output is n!}
factorial(2)
if n = 0 then return 1
else return n∙factorial(n − 1)
1
{output is n!}
17
Recursive Factorial Algorithm
factorial(4) (n = 4) procedure factorial(n: nonnegative
if n = 0 then return 1 integer)
else return n∙factorial(n − 1)
{output is n!} if n = 0 then
return 1
factorial(3) else
if n = 0 then return 1 return n∙factorial(n − 1){output
else return n∙factorial(n − 1) is n!}
{output is n!}
factorial(2)
if n = 0 then return 1
else return 2∙1{output is n!}
18
Recursive Factorial Algorithm
factorial(4) (n = 4) procedure factorial(n: nonnegative
if n = 0 then return 1 integer)
else return n∙factorial(n − 1)
{output is n!} if n = 0 then
return 1
factorial(3) else
if n = 0 then return 1 return n∙factorial(n − 1){output
else return n∙factorial(n − 1) is n!}
2
{output is n!}
19
Recursive Factorial Algorithm
factorial(4) (n = 4) procedure factorial(n: nonnegative
if n = 0 then return 1 integer)
else return n∙factorial(n − 1)
{output is n!} if n = 0 then
return 1
factorial(3) else
if n = 0 then return 1 return n∙factorial(n − 1){output
else return 3∙2{output is n!} is n!}
20
Recursive Factorial Algorithm
factorial(4) (n = 4) procedure factorial(n: nonnegative
if n = 0 then return 1 integer)
else return n∙factorial(n − 1)
if n = 0 then
6
{output is n!}
return 1
else
return n∙factorial(n − 1){output
is n!}
21
Recursive Factorial Algorithm
factorial(4) (n = 4) procedure factorial(n: nonnegative
if n = 0 then return 1 integer)
else return 4∙6{output is n!}
if n = 0 then
6
return 1
else
return n∙factorial(n − 1){output
is n!}
22
factorial(4)
Recursive Factorial Algorithm
=24
procedure factorial(n: nonnegative
integer)
if n = 0 then
return 1
else
return n∙factorial(n − 1){output
is n!}
23
Common Mistakes in Iterative Algs.
In iterative algorithms, its important to make
sure that loops terminate eventually, and not
Example:
int result := 1
while (result > 0)
…
result := result + 1
Termination condition won’t be reached
since result is always positive
24
Common Mistakes in Recursive Algs.
Similarly, in recursive algorithms, we must be careful
not to create an infinite chain of function calls:
Example:
procedure fact(int numb)
return numb * fact(numb-1)
No Termination condition! (infinite descent)
Or:
procedure fact(int numb)
if numb<=1 then
return 1;
else
return numb * fact(numb+1);
Infinite ascent since base case wont hold, & the
function will keep being called for larger and larger
values
25
Recursive Exponentiation Algorithm
Give a recursive algorithm for computing an,
where a is a nonzero real number and n is a
nonnegative integer
Solution: Use the recursive definition of an
Basis Step: a0 = 1
Recursive step: an = a . an-1
if n = 0 then
return 1
else
return a∙ power (a, n − 1)
{output is an}
26
Correctness of Recursive Algorithms
Induction is usually used to show that recursive algorithms
always produce the correct output
power(a,0) = 1
INDUCTIVE STEP: The inductive hypothesis is that power(a,k)
= ak, for all a ≠0
Assuming the inductive hypothesis, the algorithm correctly
computes ak+1, since power(a,k + 1) = a∙ power (a, k) = a∙ ak =
27
ak+1
Fibonacci Sequence
Construct a recursive algorithm to compute
the nth Fibonacci's number
{output is f(n)}
28
Recursive Binary Search Algorithm
A divide-and-conquer algorithm
Works by first dividing the problem into smaller
instances of the same problem and then conquering the
29
Merge Sort
Another divide-and-conquer algorithm
Iteratively split the input list into two sublists of
nearly equal size until each sublist has one
element
At each step a pair of sublists is successively
merged into a list with the elements in increasing
order
The process ends when all the sublists have been
merged
continued
Recursive Merge Sort
Subroutine merge, which merges two sorted
lists
35
Recursively Defined Structures
A recursive definition for structures is used to
define a structure that is composed of a
same structure
36
Recursively Defined Sets
An infinite set S may be defined recursively,
by giving:
previously-established elements
What is S?
Since a ∊ Σ* and a ∊ Σ, aa ∊ Σ*
38
Set of Palindromes
A palindrome is a word that can be read from
left-to-right or from right-to-left
wxw Σ*
39
Binary Tree
A binary tree is a data structure defined as
follows:
40
Example B-Trees
41
Recursive Procedures on Binary Trees
How can we recursively find the height of a
tree?
How can we ….
42
Binary Tree: Sum
Write a procedure to sum up all the values in
the nodes of a binary tree of integers
sum(node.left) + sum(node.right)
43
Binary Tree: Sum
roo
sum(root) = 8
t
+ sum(root.left)
+ sum(root.right)
right
left subtree
subtree
sum(root) = 8 sum(right) = 10
sum(left) = 3 +sum(root.left) +sum(right.left)
+sum(left.left) +sum(root.right)
+sum(right.right)
+sum(left.right)
44
Binary Tree: Sum
Basis step: sum(nil) = 0
Recursive Step: sum(node) = node.value +
sum(node.left) + sum(node.right)
45
Binary Tree: Number of Nodes
Write a procedure to counts the number of
nodes in a binary tree
+ count(node.right)
46
Binary Tree: Number of Nodes
Basis step: count(nil) = 0
Recursive Step: count(node) = 1 + count(node.left) +
count(node.right)
47
Binary Tree: Height
Write a procedure to calculate the height of a
binary tree
max(height(node.left), height(node.right))
48
Binary Tree: Height
Basis step: height(nil) = 0
Recursive Step: height(node) = 1 +
max(height(node.left), height(node.right))
50