Slides 15
Slides 15
Barbara Morawska
F (0) = 1
F (n) = (n) ∗ F (n − 1)
Algorithm 2: factorial
Input : n – a non-negative integer
Output: n!
1 if n = 0 then
2 return 1
3 else
4 return n ∗ factorial(n − 1)
Iterative and recursive algorithms
Algorithm to compute n’th power of a.
Iterative version
Algorithm 3: power
Input : n – a non-negative integer, a – a non-zero real number
Output: an
1 if n = 0 then
2 return 1
3 else
4 result ← 1
5 for j = 1 to n do
6 value ← a ∗ value
7 return value
Iterative and recursive algorithms
Algorithm to compute n’th power of a.
Recursive version
Recursive version follows the recursive definition:
I Base case: a0 = 1
I Recursive step: an+1 = a ∗ an
Algorithm 4: power
Input : n – a non-negative integer, a – a non-zero real number
Output: an
1 if n = 0 then
2 return 1
3 else
4 return a ∗ power (n − 1, a)
Iterative and recursive algorithms
Algorithm to compute n’th Fibonacci number.
Iterative version
Algorithm 5: fibonacci
Input : n – a non-negative integer
Output: fn – n’th fibonacci number
1 if n = 0 then
2 return 0
3 else
4 previous1 ← 0
5 previous2 ← 1
6 for j = 1 to n − 1 do
7 next ← previous1 + previous2
8 previous1 ← previous2
9 previous2 ← next
10 return previous2
Iterative and recursive algorithms
Algorithm to compute n’th Fibonacci number.
Recursive version
Recursive version follows the recursive definition:
I f0 = 0, f1 = 1
I fn = fn−1 + fn−2
Algorithm 7: fibonacci
Input : n – a non-negative integer
Output: fn – n’th fibonacci number
1 if n = 0 then
2 return 0
3 else if n = 1 then
4 return 1
5 else
6 return fibonacci(n − 1) + fibonacci(n − 2)
Iterative and recursive algorithms
Recursive algorithm for Fibonacci number is not optimal.
Look at the tree of computations for f4 performed by recursive
algorithm:
f4
f3 f2
f2 f1 f1 f0
f1 f0
f2 is computed two times!
Iterative and recursive algorithms
Algorithm to compute gcd of two integers a, b.
Iterative version
Algorithm 8: gcd
Input : a – a positive integer, b– a positive integer
Output: gcd(a, b)
1 while a 6= b do
2 if a > b then
3 a ←a−b
4 else
5 b ←b−a
6 return a
Iterative and recursive algorithms
Algorithm to compute gcd of two integers a, b.
Recursive version
Algorithm 9: gcd
Input : a – a positive integer, b– a positive integer
Output: gcd(a, b)
1 //base case:
2 if a = b then
3 return a
4 //recursive step:
5 if a > b then
6 return gcd(a − b, b)
7 else
8 return gcd(a, b − a)
a>b
We use definition of gcd:
Definition:
g = gcd(a, b) iff g > 0 ∧ g|a ∧ g|b ∧ ∀x (x |a ∧ x |b → x ≤ g)
1. factorial(5)
2. 5 ∗ factorial(4)
3. 5 ∗ 4 ∗ factorial(3)
4. 5 ∗ 4 ∗ 3 ∗ factorial(2)
5. 5 ∗ 4 ∗ 3 ∗ 2 ∗ factorial(1)
6. 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 ∗ factorial(0)
7. 5∗4∗3∗2∗1∗1
8. 5∗4∗3∗2∗1
9. 5∗4∗3∗2
10. 5∗4∗6
11. 5 ∗ 24
12. 120
Factorial
Tail-recursive version
Algorithm 14: factTR
Input : n – a non-negative integer, result = 1
Output: n!
1 if n = 0 then
2 return result
3 else
4 return factTR(n − 1, result ∗ n)
Computation of factTR(5, 1)
1. factTR(5, 1)
2. factTR(4, 5)
3. factTR(3, 20)
4. factTR(2, 60)
5. factTR(1, 120)
6. factTR(0, 120)
7. 120
Power
Recursive version of power is not tail-recursive:
Algorithm 4: power
Input : n – a non-negative integer, a – a non-zero real number
Output: an
1 if n = 0 then
2 return 1
3 else
4 return a ∗ power (n − 1, a)
Notice: f0 = 0, f1 = 1, f2 = 1, f3 = 2, f4 = 3, f5 = 5, . . .
gcd
The recursive version of gcd is tail-recursive:
Algorithm 9: gcd
Input : a – a positive integer, b– a positive integer
Output: gcd(a, b)
1 //base case:
2 if a = b then
3 return a
4 //recursive step:
5 if a > b then
6 return gcd(a − b, b)
7 else
8 return gcd(a, b − a)
Linear search
The recursive version of linear search is tail-recursive:
Algorithm 11: linear search
Input : x , i = 1, j = n – integers, a1 , a2 , . . . , an – distinct integers
Output: i the index of ai , where ai = x , or 0
1 if x = ai then
2 return i
3 else if i = j then
4 return 0
5 else
6 search(x , i + 1, j, a1 , . . . , an )
Binary search
The recursive version of binary search is tail-recursive:
Algorithm 13: binary search
Input : x – an integer, a1 , a2 , . . . , an – increasing integers
Output: i if ai = x , 0 otherwise
1 m ← b(i + j)/2c
2 if x = am then
3 return m
4 else if x < am and i < m then
5 return binary serach(x , i, m − 1, a1 , a2 , . . . , an )
6 else if x < am and j > m then
7 return binary serach(x , m + 1, j, a1 , . . . , an )