Recursive Algorithms- Recurrence relations
When a problem itself is recursively defined using a recurrence relation
then recursive algorithm is appropriate
“Recursive algorithm is which calls itself directly or indirectly”
Recurrence Relation:
“A relation which can be represented by using its previous
results”
We use recurrence relations to find the time complexity of Recursive
algorithms.
We may reperesent the time taken by recursive algorithms using the
equations called recurrence relations , and we find the time complexity
of recursive algorithms by solving the recurrence relations.
Example: Factorial of n
Algorithm factorial(n)
{
if n=0 then
return 1;
else
return(n*fact(n-1));
}
Recurrence relation:
T(n)= 2 when n=0
T(n-1)+2 when n>0
Solving Recurrence Relations
Recurrence relations can be solved by 3 methods
1. Iteration Method
2. Recursion tree Method
3. Mastrer’s Method
1. Iteration Method
We expand the recurrence and express it as a
summation of terms of n and initial condition.
Ex: 1. Solve the recurrence relation
T(n)= 2 when n=1
T(n-1)+2 when n>1
2. Solve the recurrence relation
T(n)= c when n=1
T(n/2)+c when n>1
2. Recursion Tree Method
Recursion Tree is another method for solving the recurrence
relations.
∙ A recursion tree is a tree where each node represents the cost
of a certain recursive sub-problem.
∙ We sum up the values in each node to get the cost of the entire
algorithm.
2. Recursion Tree Method…
Steps to Solve Recurrence Relations Using Recursion Tree Method-
Step-1:
Draw a recursion tree based on the given recurrence relation.
Step-2: Determine-
∙ Cost of each level
∙ Total number of levels in the recursion tree
∙ Number of nodes in the last level
∙ Cost of the last level
Step-3:
Add cost of all the levels of the recursion tree and simplify the
expression so obtained in terms of asymptotic notation.
2. Recursion Tree Method…
Ex: Solve the following recurrence relation using recursion
tree method- T(n) = 2T(n/2) + n
Step-1:
Draw a recursion tree based on the given recurrence relation.
The given recurrence relation shows-
∙ A problem of size n will get divided into 2 sub-problems of size
n/2.
∙ Then, each sub-problem of size n/2 will get divided into 2
sub-problems of size n/4 and so on.
∙ At the bottom most layer, the size of sub-problems will reduce
to 1.
2. Recursion Tree Method…
The given recurrence relation shows-
∙ The cost of dividing a problem of size n into its 2 sub-problems
and then combining its solution is n.
∙ The cost of dividing a problem of size n/2 into its 2
sub-problems and then combining its solution is n/2 and so on.
2. Recursion Tree Method…
recursion tree where each node represents the cost of the
corresponding sub-problem-
2. Recursion Tree Method
Step-2:
Determine cost of each level-
∙ Cost of level-0 = n
∙ Cost of level-1 = n/2 + n/2 = n
∙ Cost of level-2 = n/4 + n/4 + n/4 + n/4 = n and so on.
Step-3: Determine total number of levels in the recursion tree-
∙ Size of sub-problem at level-0 = n/20
∙ Size of sub-problem at level-1 = n/21
∙ Size of sub-problem at level-2 = n/22
Continuing in similar manner, we have-
Size of sub-problem at level-i = n/2i
2. Recursion Tree Method…
Suppose at level-x (last level), size of sub-problem
becomes 1. Then-
n / 2x = 1
2x = n
Taking log on both sides, we get-
xlog2 = logn
x = log2n
∴ Total number of levels in the recursion tree = log2n +
1
2. Recursion Tree Method…
Step-4:
Determine number of nodes in the last level-
∙ Level-0 has 20 nodes i.e. 1 node
∙ Level-1 has 21 nodes i.e. 2 nodes
∙ Level-2 has 22 nodes i.e. 4 nodes
Continuing in similar manner, we have-
Level-log2n has 2log2n nodes i.e. n nodes
2. Recursion Tree Method…
Step-5:
Determine cost of last level-
Cost of last level = n x T(1) = θ(n)
Step-6:
Add costs of all the levels of the recursion tree and simplify the
expression so obtained in terms of asymptotic notation-
= n x log2n + θ (n)
= nlog2n + θ (n)
= θ (nlog2n)
3. Master’s Method
Master’s theorem is another method to solve recurrence relations
Master’s theorem solves recurrence relations of the form-
T(n)=aT(n/b)+θ (nklogpn) , here f(n)= θ (nklogpn)
a>=1 , b>1 ,k>=0 and p is a real number
3. Master’s Method…
To solve recurrence relations using Master’s theorem, we compare a with
bk.
Case-1:
If a > bk, then T(n) = θ (nlogba)
Case-2:
If a = bk and
•If p < -1, then T(n) = θ (nlogba)
•If p = -1, then T(n) = θ (nlogba.log2n)
•If p > -1, then T(n) = θ ([Link]+1n)
Case-3:
If a < bk and
•If p < 0, then T(n) = O (nk)
•If p >= 0, then T(n) = θ (nklogpn)
3. Master’s Method…
Solve the following recurrence relation using Master’s theorem-
T(n) = 3T(n/2) + n2
We compare the given recurrence relation with T(n) = aT(n/b) + θ (nklogpn).
Then, we have- a=3
b=2
k=2
p=0
Now, a = 3 and bk = 22 = 4.
Clearly, a < bk.
So, we follow case-03.
Since p = 0, so we have-
T(n) = θ (nklogpn)
T(n) = θ (n2log0n)
Thus, T(n) = θ (n2)
3. Master’s Method…
Solve the following recurrence relation using Master’s theorem-
T(n) = √2T(n/2) + logn
We compare the given recurrence relation with T(n) = aT(n/b) + θ
(nklogpn).
Then, we have- a = √2
b=2
k=0
p=1
Now, a = √2 = 1.414 and bk = 20 = 1.
Clearly, a > bk.
So, we follow case-1.
So, we have- T(n) = θ (nlogba)
T(n) = θ (nlog2√2)
T(n) = θ (n1/2)
T(n)=θ (√n)
3. Master’s Method…
Form a recurrence relation for the following code and solve it using
Master’s theorem-
fun(n)
{
if(n<=1)
return 1;
else
return fun(√n);
}
•We write a recurrence relation for the given code as T(n) = T(√n) + 1.
•Here 1 = Constant time taken for comparing and returning the value.
•We can not directly apply Master’s Theorem on this recurrence relation.
3. Master’s Method…
•This is because it does not correspond to the general form of Master’s
theorem.
•However, we can modify and bring it in the general form to apply
Master’s theorem.
Let-
n = 2m ……(1)
Then-
T(2m) = T(2m/2) + 1
Now, let T(2m) = S(m), then T(2m/2) = S(m/2)
So, we have-
S(m) = S(m/2) +1
Now, we can easily apply Master’s Theorem.
3. Master’s Method…
We compare the given recurrence relation with S(m) = aS(m/b) + θ (mklogpm).
Then, we have- a=1
b=2
k=0
p=0
Now, a = 1 and bk = 20 = 1. Clearly, a = bk.
So, we follow case-2.
Since p = 0, so we have-
S(m) = θ ([Link]+1m)
S(m) = θ (mlog21.log0+1m)
S(m) = θ (m0.log1m)
Thus, S(m) = θ(logm) ……(2)
3. Master’s Method…
Now,
•From (1), we have n = 2m.
•So, logn = mlog2 which implies m = log2n.
Substituting in (2), we get-
S(m) = θ(loglog2n)