m01s06 Daa-unit-1-Recursive Algorithms - Analysis - Substitution Method
m01s06 Daa-unit-1-Recursive Algorithms - Analysis - Substitution Method
Module 1
Substitution method
,
• We have to show that it is asymptotically bound by O (log n)
Iterative method
int factorial(int n)
{
if(n==0 || n==1)
return 1;
return (n * factorial(n - 1));
}
• Input size: n
• Basic operation: Multiplication
• M(n): Number of multiplications
factorial(1) 1
factorial(2) 2*factorial(1) 2
factorial(3) 3*factorial(2) 6
factorial(4) 4*factorial(3) 24
factorial(5) 5*factorial(4) 120
int factorial(int n)
{
if(n==0 || n==1)
return 1;
return(n * factorial(n - 1));
}
• Initial condition tells the value with which the sequence starts
• Condition that makes the algorithm stop its recursive calls: if n = 0 return 1
• When n = 0, the algorithm performs no multiplications. The initial condition
is M(0)=0
There are 2 recursively defined functions
• The first is the factorial function F(n) itself; it is defined by the recurrence
F(n) = n. F(n-1) for every n > 0, F(0)=1, F(1)=1
• The second is the number of multiplications M(n) needed to compute F(n)
by the recursive algorithm.
M(n) = M(n- 1) + 1 for n > 0, M(0)=0, M(1)=0
M(0)=0, M(1)=0
M(n) = M(n-1) + 1 substitute M(n-1) = M(n-2) + 1
= [M(n-2) + 1] + 1 = M(n-2) + 2 substitute M(n-2)= M(n-3) + 1
= [M(n-3) + 1] + 2 = M(n-3) + 3
1. How will you find the basic operation in the recursive algorithms?
2. What is the basic operation of the algorithm written for generating the Fibonacci
series?
3. Write the recursive equations for finding the factorial.
4. Identify the basic operation in Towers of Hanoi problem.
5. Write the recursive algorithms and analyse the same using Backward
substitution.
Finding factorial of ‘n’
Counting the number of digits in the binary representation of a number