Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 3
Chapter 3, 4.1, 4.2, 4.3
Ladan Tahvildari, PEng
Dept. of Electrical and Computer Engineering
University of Waterloo
Asymptotic - Overview
A way to describe behaviour of functions in the limit.
Ο − notation ⎯
⎯→ Ο ≈≤ (big-oh, upper-bound)
Ω − notation ⎯
⎯→ Ω ≈≥ (big-omega, lower bound)
Θ − notation ⎯
⎯→ Θ ≈= (theta, tight bound)
ο − notation ⎯
⎯→ ο ≈p (little-oh, upper-bound not tight)
ω − notation ⎯
⎯→ ω ≈f (little-omega, lower-bound but not tight)
Example:
2 n = Ο( n )
2 3
with c = 1, n0 = 2
Example:
n = Ω(log n)
with c = 1, n0 = 16
Merge Sort
Θ(1) if n = 1;
T(n) =
2T(n/2) + Θ(n) if n > 1.
Recursion-Tree Method
Master Method
Substitution Method
Θ(1)
Θ(1)
Θ(1)
…
…
Θ(1)
…
…
Θ(1) Total = n
2
( ( ) + ( ) +L
1 + 16 + 16
5 5 2 5 3
16
)
= Θ(n2) Geometric Series
May 12, 2009 SE240 17
Appendix A: Geometric Series
n +1
1 − x
1 + x + x2 + L + xn = for x ≠ 1
1− x
1
1+ x + x +L =
2
for |x| < 1
1− x
…
…
#leaves = ah
= alogbn nlogbaΤ (1)
Τ (1)
= nlogba
May 12, 2009 SE240 20
Master Method Intuition
…
…
C ASE 1:
CASE 1: The
The weight
weight increases
increases
geometrically
geometrically from
from the
the root
root to the nlogbaΤ (1)
to the
Τ (1) leaves.
leaves. The
The leaves
leaves hold
hold aa constant
constant
fraction
fraction of
of the
the total
total weight.
weight. Θ(nlogba)
May 12, 2009 SE240 23
Three Common Cases (cont.)
2. f (n) = Θ(nlogba ).
• f (n) and nlogba grow at similar rates.
Solution: T(n) = Θ(nlogba lgn) .
…
…
C ASE 2:
CASE 2: The
The weight isis
weight nlogbaΤ (1)
Τ (1) approximately
approximately the the same
same on
on
each
each of
of the
the log
logbbnn levels.
levels.
Θ(nlogbalg n)
May 12, 2009 SE240 25
Three Common Cases (cont.)
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) ≤ c f (n) for some constant c < 1.
Solution: T(n) = Θ( f (n)) .
Running time dominated by cost at the root
May 12, 2009 SE240 26
Idea of Master Theorem
Recursion tree:
f (n) f (n)
a
f (n/b) f (n/b) … f (n/b) a f (n/b)
h = logbn a
f (n/b2) f (n/b2) … f (n/b2) a2 f (n/b2)
…
…
C ASE 3:
CASE 3: The
The weight
weight decreases
decreases
geometrically
geometrically from
from the
the root
root to the nlogbaΤ (1)
to the
Τ (1) leaves.
leaves. The
The root
root holds
holds aa constant
constant
fraction
fraction of
of the
the total
total weight.
weight. Θ( f (n))
May 12, 2009 SE240 27
Master Method Strategy
logb a
2. Determine n
logb a
3. Compare f(n) and n asymptotically
T(n) = 4T(n/2) + n
a = 4, b = 2 ⇒ nlogba = n2; f (n) = n.
CASE 1: f (n) = O(n2 – ε) for ε = 1.
∴ T(n) = Θ(n2).
T(n) = 4T(n/2) + n2
a = 4, b = 2 ⇒ nlogba = n2; f (n) = n2.
CASE 2: f (n) = Θ(n2).
∴ T(n) = Θ(n2lg n).
May 12, 2009 SE240 29
Examples
T(n) = 4T(n/2) + n3
a = 4, b = 2 ⇒ nlogba = n2; f (n) = n3.
CASE 3: f (n) = Ω(n2 + ε) for ε = 1
and 4(n/2)3 ≤ cn3 (reg. cond.) for c = 1/2.
∴ T(n) = Θ(n3).