Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

MA 515: Introduction to Algorithms &

MA353 : Design and Analysis of Algorithms


[3-0-0-6]
Lecture 5
http://www.iitg.ernet.in/psm/indexing_ma353/y09/index.html

Partha Sarathi Manal


psm@iitg.ernet.in
Dept. of Mathematics, IIT Guwahati
Mon 10:00-10:55 Tue 11:00-11:55 Fri 9:00-9:55
Class Room : 2101
Solving recurrences

Substitution method
Recursion tree method
The master method
Substitution method
It has been explained in Lecture 4
Few Exercises:
1. T(n) = T(n/2) + T(n/2) +O(n)
2. T(n) =2T(sqrt(n)) +lg n
3. T(n) = T(2n/3) + T(n/3) + O(n)
4. T(n) = 3T (n/4) + (n2)
Recursion tree
Recursion tree
Recursion tree
Recursion tree
Recursion tree
Compression
(nlgn) grows more slowly than (n2).
Therefore, merge sort asymptotically beats
insertion sort in the worst case.
In practice, merge sort beats insertion sort for
n> 30 or so.
Go test it out for yourself!
The master method
The master method applies to recurrences of
the form
T(n) = aT(n/b) + f(n) ,

where a1, b> 1, and f is asymptotically


positive.
Three common cases
T(n) = aT(n/b) + f(n) where a1, b> 1
Compare f(n) with nlogba :
1. f(n) = O(nlogba )for some constant > 0.
f(n) grows polynomially slower than nlogba
(by an n factor).
Solution: T(n) = (nlogba).
Three common cases
T(n) = aT(n/b) + f(n) where a1, b> 1

2. f(n) = (nlogba lgkn) for some constant k0.


f(n) and nlogba grow at similar rates.
Solution: T(n) = (nlogba lgk+1n).
Three common cases
T(n) = aT(n/b) + f(n) where a1, b> 1
Compare f(n)with nlogba :
3. f(n) = (nlogba + ) for some constant > 0.
f(n) grows polynomially faster than nlogba
(by an n factor), and
f(n) satisfies the regularity condition that
a f(n/b) cf(n) for some constant c < 1.
Solution: T(n) = (f(n)).
Example
Idea of master theorem
T(n) = aT(n/b) + f(n) where a1, b> 1
Idea of master theorem
T(n) = aT(n/b) + f(n) where a1, b> 1
Idea of master theorem
T(n) = aT(n/b) + f(n) where a1, b> 1
Idea of master theorem
T(n) = aT(n/b) + f(n) where a1, b> 1
Idea of master theorem
T(n) = aT(n/b) + f(n) where a1, b> 1
Idea of master theorem
T(n) = aT(n/b) + f(n) where a1, b> 1
Exercises
T(n) = 2T(n/2) + n3
T(n) = T(9n/10) + n
T(n) = 16T(n/4) + n2
T (n) = 7T(n/3) + n2
T(n) = 7T(n/2) + n2
Assume T(n) is constant for n 2.

You might also like