USTH Algorithm Recursion
USTH Algorithm Recursion
Method of thinking:
Imagine that someone else is going to solve the simpler problems
Your only task is to simplify the original problem, and
The Recursion Fairy will magically take care of the simpler subproblems
Rules:
(1) Can only move one disk at a time.
(2) A larger disk can never be placed on top of a
smaller disk.
(3) May use third post for temporary storage.
Solution for n = 2
1 2 3
How is about n>2 ?
1 2
17-11 3
Task Decomposition (cont.)
n-1 n-1
1 1 1 1 1 1 1 1
When will the world vanish?
When you receive the solution for the problem of size n − 1, size n
− 2, etc..., you will then use these solutions to solve
the original problem.
Recursive problem
Now, suppose that you know (e.g., someone told you) that:
Because we used factorial(n−1), we will need the solution for 1 base case:
Computing n! algorithm
int factorial( int n ) {
if ( n <= 1 ) {
return 1;
} else {
return n * factorial( n – 1 );
}
}
Θ(1) n 1
T! ( n )
T! ( n 1) Θ(1) n 1
The analysis of the run time of this function yields a recurrence relation:
T!(n) = T!(n – 1) + Q(1) T!(1) = Q(1)
If k = n – 1 then
T!(n) = T!(n – (n – 1)) + n – 1
= T!(1) + n – 1
=1+n–1=n
7 2 9 4 2 4 7 9
7 2 2 7 9 4 4 9
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
1 2 n/2
i 2i n/2i
… … …
Compute Sum of 1…n
n
Problem: Running sum ( i)
1
Recursive solution:
RunningSum(1) = 1
RunningSum(n) = n + RunningSum(n-1)
Recursive algorithm:
int RunningSum(int n) {
declare sum
if (n equal to 1)
return 1;
else
sum = n + RunningSum(n-1)
return sum
}
Executing RunningSum
res = RunningSum(4);
return 4 + RunningSum(3);
return 3 + RunningSum(2);
return 2 + RunningSum(1);
return 1;
Now you analyze the algorithm
Compute power
Recursive definition of bn :
bn = 1 if n = 0
bn = b bn–1 if n > 0
Simple recursive power algorithm:
Easy case: solved directly.
To compute bn:
1. If n = 0:
1.1. Terminate with answer 1.
2. If n > 0:
2.1. Terminate with answer b bn–1.
than n to 0.
Design the algoritm
non-recursive recursive
Simple power algorithm O(n) O(n)
Smart power algorithm O(log n) O(log n)
Recursive Squaring
We can derive a more efficient linearly
recursive algorithm by using repeated
squaring:
1 if x 0
p ( x, n) x p ( x, ( n 1) / 2) 2 if x 0 is odd
p ( x, n / 2) 2 if x 0 is even
For example,
24 = 2(4/2)2 = (24/2)2 = (22)2 = 42 = 16
25 = 21+(4/2)2 = 2(24/2)2 = 2(22)2 = 2(42) = 32
26 = 2(6/ 2)2 = (26/2)2 = (23)2 = 82 = 64
27 = 21+(6/2)2 = 2(26/2)2 = 2(23)2 = 2(82) = 128.
56
A Recursive Squaring Method
Algorithm Power(x, n):
Input: A number x and integer n = 0
Output: The value xn
if n = 0 then
return 1
if n is odd then
y = Power(x, (n - 1)/ 2)
return x · y ·y
else
y = Power(x, n/ 2)
return y · y
Analyzing the algorithm
Algorithm Power(x, n):
Input: A number x and integer n
=0
Output: The value xn
if n = 0 then Each time we make a
recursive call we halve the
return 1 value of n; hence, we make
if n is odd then log n recursive calls. That
y = Power(x, (n - 1)/ 2) is, this method runs in
O(log n) time.
return x · y · y
else
It is important that we
y = Power(x, n/ 2) used a variable twice here
return y · y rather than calling the
method twice.
Exercises
Fibonacci sequence problem
f ( n ) f ( n 1) f ( n 2)
f (1) 1
f (0 ) 1
In other words, the n-th Fibonacci number is the sum of the previous two
Fibonacci numbers.
Design a recursive algorithm
x, if y 0
gcd( x, y )
gcd( y, x mod y ), otherwise
Design a recursive algorithm