Data Structures and Algorithms - 19
BITS Pilani
Hyderabad Campus
Motivation: Asymptotic Behavior of Recursive
Algorithms
When analyzing algorithms, recall that we only care about the
asymptotic behavior
Recursive algorithms are no different
Rather than solving exactly the recurrence relation associated with
the cost of an algorithm, it is sufficient to give an asymptotic
characterization
The main tool for doing this is the master theorem
BITS Pilani, Hyderabad Campus
Master Theorem
Let T(n) be a monotonically increasing function that
satisfies
T(n) = a T(n/b) + f(n)
T(1) = c
where a 1, b 2, c>0. If f(n) is (nd) where d 0 then
if a < bd
T(n) = If a = bd
if a > bd
BITS Pilani, Hyderabad Campus
Master Theorem: Pitfalls
You cannot use the Master Theorem if
T(n) is not monotone, e.g. T(n) = sin(x)
f(n) is not a polynomial, e.g., T(n)=2T(n/2)+2n
b cannot be expressed as a constant, e.g.
BITS Pilani, Hyderabad Campus
Master Theorem: Example 1
Let T(n) = T(n/2) + ½ n2 + n. What are the parameters?
a=1
b=2
d=2
Therefore, which condition applies?
1 < 22, case 1 applies
We conclude that
T(n) (nd) = (n2)
BITS Pilani, Hyderabad Campus
Master Theorem: Example 2
Let T(n)= 2 T(n/4) + n + 42. What are the parameters?
a=2
b=4
d = 1/2
Therefore, which condition applies?
2 = 41/2, case 2 applies
We conclude that
BITS Pilani, Hyderabad Campus
Master Theorem: Example 3
Let T(n)= 3 T(n/2) + 3/4n + 1. What are the parameters?
a= 3
b=2
d=1
Therefore, which condition applies?
3 > 21, case 3 applies
We conclude that
Note that log231.584…, can we say that T(n) (n1.584)
No, because log231.5849… and n1.584 (n1.5849)
BITS Pilani, Hyderabad Campus
‘Fourth’ Condition
Recall that we cannot use the Master Theorem if f(n), the non-recursive
cost, is not a polynomial
There is a limited 4th condition of the Master Theorem that allows us to
consider polylogarithmic functions
Corollary: If for some k0 then
This final condition is fairly limited and we present it merely for sake of
completeness.. Relax
BITS Pilani, Hyderabad Campus
‘Fourth’ Condition: Example
Say we have the following recurrence relation
T(n)= 2 T(n/2) + n log n
Clearly, a=2, b=2, but f(n) is not a polynomial. However, we have
f(n)(n log n), k=1
Therefore by the 4th condition of the Master Theorem we can say that
BITS Pilani, Hyderabad Campus
Thank You!!
BITS Pilani, Hyderabad Campus