07- 22 jan - divide and conquer
TRANSCRIPT
CS 321. Algorithm Analysis & Design Lecture 7
Divide and ConquerPart II - Master’s Theorem
Analysing Recurrences
Analysing Recurrences
Base Case: T(n) is constant for all sufficiently small n.
T (n) T (n/2) +O(1)
Analysing Recurrences
Base Case: T(n) is constant for all sufficiently small n.
T (n) 2T (n/2) +O(n)
T (n) T (n/2) +O(1)
Analysing Recurrences
Base Case: T(n) is constant for all sufficiently small n.
T (n) 2T (n/2) +O(n)
T (n) T (n/2) +O(1)
Analysing Recurrences
Binary Search
Base Case: T(n) is constant for all sufficiently small n.
T (n) 2T (n/2) +O(n)
T (n) T (n/2) +O(1)
Analysing Recurrences
Binary Search
MergeSort and C losest Pair
Base Case: T(n) is constant for all sufficiently small n.
T (n) aT (n/b) +O(nd)
T (n) aT (n/b) +O(nd)
#of subproblems solved
T (n) aT (n/b) +O(nd)
#of subproblems solved
size of each subproblem
T (n) aT (n/b) +O(nd)
#of subproblems solved
size of each subproblem time to combine
T (n) aT (n/b) +O(nd)
#of subproblems solved
size of each subproblem time to combine
T (n) aT (n/b) +O(nd)
integer that could be as smal l as one.
#of subproblems solved
size of each subproblem time to combine
T (n) aT (n/b) +O(nd)
b should be a constant greater than one.
integer that could be as smal l as one.
T (n) aT (n/b) +O(nd)
T (n) = O(ndlog n)
T (n) = O(nd)
T (n) = O(nlogb a)
T (n) aT (n/b) +O(nd)
T (n) = O(ndlog n)
T (n) = O(nd)
T (n) = O(nlogb a)
if a = bd
T (n) aT (n/b) +O(nd)
T (n) = O(ndlog n)
T (n) = O(nd)
T (n) = O(nlogb a)
if a = bd
if a < bd
T (n) aT (n/b) +O(nd)
T (n) = O(ndlog n)
T (n) = O(nd)
T (n) = O(nlogb a)
if a = bd
if a < bd
if a > bd
a (# of
subproblems)
b (n/b = size of subproblems)
d (nd = running time of
combine step)
Running Time
Binary Search
1 2 0 log n
Merge Sort
2 2 1 n log n
a (# of
subproblems)
b (n/b = size of subproblems)
d (nd = running time of
combine step)
Running Time
Binary Search
1 2 0 log n
Merge Sort
2 2 1 n log n
a (# of
subproblems)
b (n/b = size of subproblems)
d (nd = running time of
combine step)
Running Time
Binary Search
1 2 0 log n
Merge Sort
2 2 1 n log n
T (n) aT (n/b) +O(nd)
T (n) = O(ndlog n)
T (n) = O(nd)
T (n) = O(nlogb a)
if a = bd
if a < bd
if a > bd
#of subproblems at level j = aj
size of subproblems at level j = n/bj
#of subproblems at level j = aj
size of subproblems at level j = n/bj
Amount of work done at level j?
#of subproblems at level j = aj
size of subproblems at level j = n/bj
Amount of work done at level j?
aj · c(n/bj)d
#of subproblems at level j = aj
size of subproblems at level j = n/bj
Amount of work done at level j?
cnd(a/bd)j
#of subproblems at level j = aj
size of subproblems at level j = n/bj
Amount of work done at level j?
cnd(a/bd)j
Total work?
#of subproblems at level j = aj
size of subproblems at level j = n/bj
Amount of work done at level j?
cnd(a/bd)j
Total work?
cnd
logb nX
j=0
(a/bd)j
#of subproblems at level j = aj
size of subproblems at level j = n/bj
Amount of work done at level j?
cnd(a/bd)j
Total work?
cnd
logb nX
j=0
(a/bd)j
a = rate of proliferation
bd = rate of work shrinkage per subproblem
Amount of work done at level j?
cnd(a/bd)j
Total work?
cnd
logb nX
j=0
(a/bd)j
a = rate of proliferation
bd = rate of work shrinkage per subproblem
cnd
logb nX
j=0
(a/bd)j
if a = bd
cnd
logb nX
j=0
(a/bd)j
if a = bdT (n) = O(nd
log n)
cnd
logb nX
j=0
(a/bd)j
if a < bd
1 + r + r2 + r3 + · · ·+ rk =rk+1 � 1
r � 1
(r 6= 1)
cnd
logb nX
j=0
(a/bd)j
if a < bd
1 + r + r2 + r3 + · · ·+ rk =rk+1 � 1
r � 1
(r 6= 1)
1
(1� r)
When r < 1
cnd
logb nX
j=0
(a/bd)j
if a < bd
1 + r + r2 + r3 + · · ·+ rk =rk+1 � 1
r � 1
(r 6= 1)
T (n) = O(nd)
1
(1� r)
When r < 1
cnd
logb nX
j=0
(a/bd)j
if a > bd
1 + r + r2 + r3 + · · ·+ rk =rk+1 � 1
r � 1
(r 6= 1)
cnd
logb nX
j=0
(a/bd)j
if a > bd
1 + r + r2 + r3 + · · ·+ rk =rk+1 � 1
r � 1
(r 6= 1)When r > 1
rk✓1 +
1
(r � 1)
◆
cnd
logb nX
j=0
(a/bd)j
if a > bd
1 + r + r2 + r3 + · · ·+ rk =rk+1 � 1
r � 1
(r 6= 1)When r > 1
rk✓1 +
1
(r � 1)
◆
T (n) = O(alogb n)
T (n) aT (n/b) +O(nd)
T (n) = O(ndlog n)
T (n) = O(nd)
T (n) = O(nlogb a)
if a = bd
if a < bd
if a > bd