Design and Analysis of Algorithms: CSE 5311 Lecture 3 Divide-and-Conquer
Design and Analysis of Algorithms: CSE 5311 Lecture 3 Divide-and-Conquer
Design and Analysis of Algorithms: CSE 5311 Lecture 3 Divide-and-Conquer
CSE 5311
Lecture 3 Divide-and-Conquer
Definition:
Θ(g(n)) = { f (n) : there exist positive constants c1, c2, and
n0 such that 0 ≤ c1 g(n) ≤ f (n) ≤ c2 g(n)
for all n ≥ n0 }
Basic Manipulations:
• Drop low-order terms; ignore leading constants.
• Example: 3n3 + 90n2 – 5n + 6046 = Q(n3)
Θ(1) if n = 1;
T(n) =
2T(n/2) + Θ(n) if n > 1.
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn
cn/4 cn/4
Θ(1) #leaves = n Θ(n)
Total = Θ(n lg n)
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 5
Solving Recurrences
• Recurrence
– The analysis of integer multiplication from last lecture required us to solve a
recurrence
– Recurrences are a major tool for analysis of algorithms
– Divide and Conquer algorithms which are analyzable by recurrences.
T ( n ) = 4 T ( n / 2 ) + Θ(n)
≤ 4 c ( n / 2 )3 + Θ(n)
= ( c / 2 ) n 3 + Θ(n)
= cn 3 − (( c / 2 ) n 3 − Θ(n) ) desired – residual
≤ cn 3 desired
We can imagine Θ(n)=100n. Then, whenever (c/2)n3 – 100n ≥
0, for example, if c ≥ 200 and n ≥ 1.
residual
T ( n ) = 4 T ( n / 2 ) + 100n
≤ cn 2 + 100n
≤ cn 2
T ( n ) = 4 T ( n / 2 ) + 100n
≤ 4 ( c ( n / 2 ) 2 − c ( n / 2 ))+ 100n
1 2
= c n 2 − 2 c n + 100n
1 2
= c n 2 − c n − ( c2 n −100n )
1 2
≤ c n 2 − c n if c > 100.
1 2 2
T(n)
T(n/4) T(n/2)
(n/4)2 (n/2)2
(n/4)2 (n/2)2
Θ(1)
Θ(1)
Θ(1)
Θ(1)
Θ(1)
Total = 25 ( 5 2
n 1+16 + 16 + 16 +L5 3
( ) ( ) )
= Θ(n2)
geometric series
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 22
Appendix: Geometric Series
n+1
1− x
1+ x + x2 +L+ xn = for x ≠ 1
1− x
Recursion tree:
f (n) f (n)
a
f (n/b) f (n/b) / f (n/b) a f (n/b)
a
h = logbn
f (n/b2) f (n/b2) / f (n/b2) a2 f (n/b2)
#leaves = ah
nlogbaΤ (1)
Τ (1) = alogbn
= nlogba
Recursion tree:
f (n) f (n)
a
f (n/b) f (n/b) / f (n/b) a f (n/b)
a
h = logbn
f (n/b2) f (n/b2) / f (n/b2) a2 f (n/b2)
geometrically from the root to the leaves.
The leaves hold a constant fraction of nlogbaΤ (1)
Τ (1) the total weight.
Θ(nlogba)
f(n)= nlogba – ε and a f(n/b)=a (n/b)logba – ε = b ε nlogba – ε
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 27
Case (II)
Recursion tree:
f (n) f (n)
a
f (n/b) f (n/b) / f (n/b) a f (n/b)
a
h = logbn
f (n/b2) f (n/b2) / f (n/b2) a2 f (n/b2)
CASE 2: 2 (k = 0) The weight is nlogbaΤ (1)
Τ (1) approximately the same on each of
the logbn levels.
Θ(nlogbalg n)
f(n)=nlogba and af(n/b)=a (n/b)logba= nlogba
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 29
Case (III)
Recursion tree:
f (n) f (n)
a
f (n/b) f (n/b) / f (n/b) a f (n/b)
a
h = logbn
f (n/b2) f (n/b2) / f (n/b2) a2 f (n/b2)
CASE 3:3 The weight decreases
geometrically from the root to the leaves.
The root holds a constant fraction of the nlogbaΤ (1)
Τ (1) total weight.
Θ( f (n))
f(n)= nlogba + ε and a f(n/b)=a (n/b)logba + ε = b -ε nlogba + ε
Dept. CSE, UT Arlington CSE5311 Design and Analysis of Algorithms 31
Examples