Week 04 (Reccurence02)
Week 04 (Reccurence02)
T (n) = T (n / 4) + T (n / 2) + n 2
Height and leaves of a recursion
tree
• Height
The top of the tree begins with n and for every step down it is divided by b. So it
goes n, n/b, n/b2,...,1. To find the height we need to find a k such that n / bk = 1
or bk = n, which gives k = logbn.
Verify our guess [substitution]
Master Method
The idea is to solve a class of recurrences that have the
form
T(n) = aT(n/b) + f(n)
Assumptions: a 1 and b > 1, and f(n) is asymptotically
positive.
Abstractly speaking, T(n) is the runtime for an algorithm
and we know that
a number of subproblems of size n/b are solved recursively, each
in time T(n/b).
f(n) is the cost of dividing the problem and combining the results.
e.g., In merge-sort .
T (n) = 2T (n / 2) + (n)
…Master Method
alogb n
alogb n
alogb n
log b a +
If f ( n ) = ( n ) for some constant 0 then
Inverse of Case 1
logb a
f(n) grows polynomially faster than n
Also need a “regularity” condition
c 1 and n0 0 such that af (n / b) cf (n) n n0
The work at the root dominates
division/recombination cost
Master Theorem, Summarized
(
1. f (n) = O nlogb a − )
(
T (n) = nlogb a )
(
2. f (n) = nlogb a )
(
T (n) = nlogb a lg n )
( )
3. f (n) = nlogb a + and af (n / b) cf (n), for some c 1, n n0
T ( n) = ( f ( n) )
Strategy
1. Extract a, b, and f(n) from a given recurrence
2. Determine nlogb a
3. Compare f(n) and nlogb a asymptotically
4. Determine appropriate MT case and apply it
BinarySearch(A, l, r, q):
m := (l+r)/2
if A[m]=q then return m
else if A[m]>q then
BinarySearch(A, l, m-1, q)
else BinarySearch(A, m+1, r, q)
T(n) = T(n/2) + 1
1. a=1, b=2, f(n) = 1
2. nlog21 = 1
3. 1 = (1)
➔ Case 2: T(n) = (nlogbalog n) =(log n)
...Examples of Master
Method
T(n) = 9T(n/3) + n
1. a=9, b=3, f(n) = n
2. nlog39 = n2
3. n = (nlog39 - ) with = 1
➔ Case 1: T(n) = (n2)
...Examples of Master
Method
T(n) = 3T(n/4) + n log n
1. a=3, b=4, f(n) = n log n
2. nlog43 = n0.792
➔ Case 3:
CLRS
Chapter: 4 (4.1-4.3)
4.4 for bedtime reading
Exercises
♦ 4.1, 4.2, 4.3
Problems
♦ 4-1, 4-4
HSR
Chapter: 3 (3.1-3.6)
Examples: 3.1-3.5
Exercises: 3.1 (1, 2), 3.2 (1, 3-6),