MCA 301: Design and Analysis of Algorithms: Instructor Neelima Gupta Ngupta@cs - Du.ac - in
MCA 301: Design and Analysis of Algorithms: Instructor Neelima Gupta Ngupta@cs - Du.ac - in
Analysis of Algorithms
Instructor
Neelima Gupta
ngupta@cs.du.ac.in
Table Of Contents
Solving Recurrences
The Master Theorem
Recurrence Relations
Recurrences
• The expression:
c n 1
T ( n)
2T cn n 1
n
2
is a recurrence.
– Recurrence: an equation that describes a function in
terms of its value on smaller functions
Recurrence Examples
0 n0 0 n0
s ( n) s ( n)
c s (n 1) n 0 n s (n 1) n 0
n 1
c c n 1
T ( n) T ( n)
2T c n 1
n n
2 aT cn n 1
b
Solving Recurrences
• Substitution method
• Iteration method
• Master method
Solving Recurrences
• The substitution method (CLR 4.1)
– “Making a good guess” method
– Guess the form of the answer, then use
induction to find the constants and show that
solution works
– Examples:
• T(n) = 2T(n/2) + (n) T(n) = (n lg n)
• T(n) = 2T(n/2) + n ???
Solving Recurrences
• The substitution method (CLR 4.1)
– “Making a good guess” method
– Guess the form of the answer, then use
induction to find the constants and show that
solution works
– Examples:
• T(n) = 2T(n/2) + (n) T(n) = (n lg n)
• T(n) = 2T(n/2) + n T(n) = (n lg n)
• T(n) = 2T(n/2 )+ 17) + n ???
Substitution method
We require T(2)≤ c 2 lg 2
Hence proved.
Solving Recurrences using Recursion Tree Method
For e.g., T(n) = a T(n/b) + f(n) where a > 1 ,b > 1 and f(n) is a given
function .
T n/b T n/b
n
n
T n/2 T n/2
n/2 n/2 n
1 1 1 1 1 1 1
T(n) = Ɵ (n log n)
1 1 2
log n 4
:
:
:
n
n
n/3 2n/3
n/3 2n/3 n
n 2 n 1 2n n .
log3 n (3/2)2
log3/2 n
32 3 3 3 3
n
1 n/3i
:
:
:
we have 1 when n = 1
3i
=> n = 3i
Taking log₃ on both the sides
=> log₃ n = i
or n = 1
(3/2)k
=> k = log3/2 n
applies:
T ( n) n logb a
when f (n) O n log b a
– Thus the solution is T(n) = (n2)
More Examples of Master’s
Theorem
• T(n) = 3T(n/5) + n
• T(n) = 2T(n/2) + n
• T(n) = 2T(n/2) + 1
• T(n) = T(n/2) + n
• T(n) = T(n/2) + 1
When Master’s Theorem cannot
be applied
• T(n) = 2T(n/2) + n logn
• T(n) = 2T(n/2) + n/ logn
Up Next