1.6 Mathematical analysis for Recursive algorithms (1)
1.6 Mathematical analysis for Recursive algorithms (1)
UNIT NO 1
INTRODUCTION
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS
(Common to CSE & IT)
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
.
3 Recursion Tree Method
Master’s Method
● The Master Method is used for solving the following types of recurrence.
● T(n) = a T(n)+ f (n/b) with a≥1 and b≥1 be constant & f(n) be a function and n/b can
be interpreted as, Let T (n) is defined on non-negative integers by the recurrence.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
.
General Plan
Example 1:
Computing factorial of some n numbers
If (n=0)
Return 1
Else
Return Factorial (n-1)*n
Mathematical Analysis:
1. The factorial algorithm works for input size n.
2. The basic operation in computing factorial is multiplication.
3. The recursive function can be formulated as F(n) = F(n-1) * n where n >0.
Then basic operation multiplication is given as M(n).
M(n) = M(n-1) + 1
M(n-1) → these multiplications are required to compute factorial (n-1)
+1 → to multiply factorial (n-1) by n.
4. In step 3 the recurrence relation is obtained. M(n) = M(n-1) + 1.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
• Thus, the recurrence relation and initial condition for the algorithm’s number of
multiplications .
M(n):
M(n) = M(n − 1) + 1 for n > 0,
M(0) = 0 for n = 0.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
= M(n − n) + n
= n
Therefore M(n)=n
Thus the time complexity of factorial function is Θ(n).
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
Example 2:
Towers of Hanoi
• Classic example of recursive function.
• There are 3 pegs named A, B and C. 5 disks of different sizes are placed on
peg A.
• The arrangements of the disks is such that every smaller disk is placed on
the larger disk.
• The Problem of “Towers of Hanoi” states that move the 5 disks from peg A to
peg C using peg B as an auxiliary.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
Mathematical Analysis:
Step 2: The basic operation in this problem is moving the disk from one peg to
another.
• When n>1, then to move these disks from peg A to peg C using
auxiliary peg B, we first move recursively n-1 disks from peg A to peg B using
auxiliary peg C.
• Then we move the largest disk directly from peg A to peg C and
finally move n-1 disks from peg B to peg C.
• If n=1, then we simply move the disk from peg A to peg C.
Step 3: The move of disks are denoted by M(n). M(n) depends on the number of
disks n. The recursive relation can then setup as M(1)=1.
• If n>1 then we need two recursive calls plus one move.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
Hence
M(n) = M(n-1) + 1 + M(n-1)
Here M(n-1) → to move (n-1) disks from peg A to B.
1 → To move largest disk from peg A to C.
M(n-1) → to move (n-1) disks from peg B to C.
M(n) = 2M(n-1) + 1
Put M(n-1) = 2M(n-2)+1
=2[2M(n-2)+1]+1
=4M(n-2)+3
Put M(n-2) = 2M(n-3)+1
=4[2M(n-3)+1]+3
=8M(n-3)+7
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
• From this we can conclude the time complexity of the Towers of Hanoi
problem as Θ(2n- 1) = Θ(2n).
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec( n 2 )+ 1
Algorithm Analysis
Since the recursive calls end when n is equal to 1 and there are no additions
made then, the initial condition is A(1) = 0.
The standard approach to solving such a recurrence is to solve it only for n
= 2k A(2k) = A(2k−1) + 1 for k > 0,
A(20) = 0.
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
Backward substitutions
A(2k) = A(2k−1) + 1
substitute A(2k−1) = A(2k−2) + 1
= [A(2k−2) + 1]+ 1
= A(2k−2) + 2
substitute A(2k−2) = A(2k−3) + 1
= [A(2k−3) + 1]+ 2
= A(2k−3) + 3
..…
= A(2k−i) + i
...
= A(2k−k) + k.
Thus, we end up with A(2k) = A(1) + k = k, or, after returning to the original
variable n = 2k and hence k = log2 n,
A(n) = log2 n ϵ Θ (log2 n).
CS8451
DESIGN AND ANALYSIS OF ALGORITHMS (Common to CSE & IT)
Video Link
https://www.youtube.com/watch?v=XTs1H2LfY9g