Recurrence Relation
Recurrence Relation
Topperworld.in
Recurrence realation
❖ Substitution Method
• Guess the form of the solution.
• Use mathematical induction to prove that the guess is correct.
For example consider the recurrence
T (n) = T +n
©Topperworld
Design and Analysis of Algorithm
Solution:
For T (n) = O (log n)
We have to show that for some constant c
T (n) ≤c logn.
Now,
T (n) ≤c log +1
T (n) = 2T + n n>1
Solution:
Now,
≤cnlogn-cnlog2+n
©Topperworld
Design and Analysis of Algorithm
cn2
/ \
T(n/4) T(n/2)
©Topperworld
Design and Analysis of Algorithm
Solution:
cn2
/ \
c(n2)/16 c(n2)/4
/ \ / \
T(n/16) T(n/8) T(n/8) T(n/4)
cn2
/ \
c(n2)/16 c(n2)/4
/ \ / \
c(n2)/256 c(n2)/64 c(n2)/64 c(n2)/16
/ \ / \ / \ / \
©Topperworld
Design and Analysis of Algorithm
So ,
Time Complexity is 0(n).
Space Complexity is log n.
©Topperworld
Design and Analysis of Algorithm
©Topperworld
❖ Iteration Methods:
• It means to expand the recurrence and express it as a summation of
terms of n and initial condition.
Example1:
Consider the Recurrence T (n) = 1 if n=1
= 2T (n-1) if n>1
Solution:
T (n) = 2T (n-1)
= 2[2T (n-2)] = 22T (n-2)
= 4[2T (n-3)] = 23T (n-3)
= 8[2T (n-4)] = 24T (n-4) ….(Eq.1)
Repeat the procedure for i times
T (n) = 2i T (n-i)
Put n-I =1 or I = n-1 in …(Eq.1)
T (n) = 2n-1 T (1)
= 2n-1 .1 {T (1) =1 .....given}
= 2n-1
❖ Master Theorem:
• The Master Theorem is a tool used in the analysis of algorithms,
particularly those that follow a divide-and-conquer approach.
• It provides a straightforward way to determine the time complexity of
recursive algorithms by expressing their time recurrence relations in a
specific form.
The general form of a recurrence relation that can be solved using the
Master Theorem is: T(n) = aT(n/b) + f(n)
©Topperworld
Design and Analysis of Algorithm
Here:
o T(n) is the time complexity of the algorithm for a problem of size n,
o a is the number of subproblems at each recursion level,
o b is the factor by which the problem size is reduced in each recursive
call,
o f(n) is the cost of dividing the problem and combining the results.
o The Master Theorem has three cases, each of which provides a solution
for the time complexity T(n)
Case1:
Case2:
Case 3:
©Topperworld