1.6 Mathematical Analysis For Recursive Algorithms
1.6 Mathematical Analysis For Recursive Algorithms
3. Recursion Tree Method - While substitution method works well for many recurrence
relations, it is not a suitable technique for recurrence relations that model divide
and conquer paradigm based algorithms. Recursion Tree Method is a popular
technique for solving such recurrence relations, in particular for solving
unbalanced recurrence relations. For example, in case of modified merge Sort, to
solve a problem of size n (to sort an array of size n), the problem is divided into
two problems of size n/3 and 2n/3 each. This is done recursively until the
problem size becomes 1. Consider the following recurrence relation. 1. T(n) =
2T(n/2) + 1 Here the number of leaves = 2log n = n and the sum of effort in each
level except leaves is
Master Theorem
Example:
Solution:
f(n) = Θ(1)
Therefore:
a > bd i.e., 2 > 20
Case 3 of master theorem holds good. Therefore:
T(n) Є Θ (nlogba )
Є Θ (nlog22 )
Є Θ (n)
1.6 MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
General plan:
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.
= M(n − n) + n
= n.
Therefore M(n)=n
Thus the time complexity of factorial function is Θ(n).
Example 2:
Towers of Hanoi
Step 4: solving recurrence M(n) = 2M(n-1) + 1 using two substitution methods: Forward
Substitution:
For n > 1,
M(2) = 2M(1) + 1
= 2*1+1
M(2) = 3
M(3) = 2M(2) + 1
= 2*3+1
M(3) = 7
M(4) = 2M(3) + 1
=2 * 7 + 1
M(4) = 15
Backward Substitution:
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
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(2 k−1) + 1 for k > 0,
A(20) = 0.
Backward substitutions
A(2k) = A(2k−1) + 1
= [A(2k−2) + 1]+ 1
= A(2k−2) + 2
= [A(2k−3) + 1]+ 2
= A(2k−3) + 3
...
...
= A(2k−i) + i
...
= A(2k−k) + k.
Thus, we end up with A(2 k) = A(1) + k = k, or, after returning to the original variable n =
2k and hence k = log2 n,
A(n) = log2 n ϵ Θ (log2 n).
We guess at I(n) and then determine the new IC for the homogenous problem for B(n)
There is the Master Theorem that give the asymptotic limit for many common problems.
Iterative algorithm
Algorithm Fib(n)
F[1] ← 0; F[1] ← 1
for i ← 2 to n do