Chapter 02
Chapter 02
Content has been extracted from Introduction to Algorithms, Fourth Edition, by Cormen,
Leiserson, Rivest, and Stein. MIT Press. 2022.
Visit [Link]
Original slides from Introduction to Algorithms 6.046J/18.401J, Fall 2005 Class by Prof. Charles
Leiserson and Prof. Erik Demaine. MIT OpenCourseWare Initiative available at
[Link]
Asymptotic Notation
Solving recurrences
Substitution method
Recursion tree
The master method
cg.n/
f
f .n/
c
n
n0 n0
f .n/ D O.g.n// f .n/ D �.g.n
Introduction to Algorithms Lecture 2 5 / 80
O-notation (upper bounds)
Example
2n2 = O(n3 ) (c = 1, n0 = 2).
Example
2n2 = O(n3 ) (c = 1, n0 = 2).
Functions,
not values!
Example
2n2 = O(n3 ) (c = 1, n0 = 2).
Functions,
not values! Funny, “one-way” equality...
Example:
2n2 ∈ O(n3 )
Convention:
A set in a formula represents an anonymous function in the set.
Example:
f (n) = n3 + O(n2 )
means
f (n) = n3 + h(n)
for some h(n) ∈ O(n2 ).
Convention:
A set in a formula represents an anonymous function in the set.
Example:
n2 + O(n) = O(n2 )
means
for any f (n) ∈ O(n):
n2 + f (n) = h(n)
for some h(n) ∈ O(n2 ).
cg.n/ c2 g.n
f .n/
f .n/
f .n/
cg.n/
c1 g.n
n n n
n0 n0 n0
f .n/ D O.g.n// f .n/ D �.g.n// f .n/ D ‚.g.n//
(a) (b) (c)
Introduction to Algorithms Lecture 2 14 / 80
Ω-notation (lower bounds)
Example:
√
n = Ω(lg n) (c = 1, n0 = 16)
c2 g.n/
f .n/
f .n/
cg.n/
c1 g.n/
n n
n0 n0
f .n/ D �.g.n// f .n/ D ‚.g.n//
(b) (c)
Example:
1 2
2n − 2n = Θ(n2 )
Example:
2n2 = o(n3 ) (n0 = 2c )
Example:
√
n = ω(lg n) (n0 = 1 + 1c )
Asymptotic Notation
Solving recurrences
Substitution method
Recursion tree
The master method
Example:
T (n) = 4T ( n2 ) + n
▶ Assume that T (1) = Θ(1).
▶ Guess O(n3 ). (Prove O and Ω separately.)
▶ Assume that T (k) ≤ ck 3 for k < n.
▶ Prove T (n) ≤ cn3 by induction.
n
T (n) = 4T +n
2
n 3
≤ 4c +n
c 2
= n3 + n
2
c
= cn3 − n3 − n ←− desired − residual
2
≤ cn3 ←− desired
c
whenever n3 − n ≥ 0, for example, if c ≥ 2 and n ≥ 1.
2
residual
Idea:
▶ Strengthen the inductive hypothesis.
▶ Subtract a low-order term.
▶ Inductive hypothesis: T (k) ≤ c1 k 2 − c2 k for k < n.
n
T (n) = 4T +n
2 n
n 2
= 4 c1 − c2 +n
2 2
= c1 n2 − 2c2 n + n
= c1 n2 − c2 n − c2 n + n
= c1 n2 − c2 n − (c2 n − n)
≤ c1 n2 − c2 n if c2 ≥ 1.
n n
+ n2
Solve T (n) = T 4 +T 2
T(n)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.34
n2
T(n/4) T(n/2)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.35
n2
(n/4)2 (n/2)2
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.36
n2
(n/4)2 (n/2)2
Θ(1)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.37
n2 n2
(n/4)2 (n/2)2
Θ(1)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.38
n2 n2
5 n2
(n/4)2 (n/2)2
16
(n/16)2 (n/8)2 (n/8)2 (n/4)2
…
Θ(1)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.39
n2 n2
5 n2
(n/4)2 (n/2)2
16
25 n 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
256
…
…
Θ(1)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.40
n2 n2
5 n2
(n/4)2 (n/2)2
16
25 n 2
(n/16)2 (n/8)2 (n/8)2 (n/4)2
256
…
…
Θ(1) Total = n 2 1 + 16 (
5 + (5 ) + (5 ) +L
16 16
2 3
)
= Θ(n2) geometric series
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.41
n
Each of them is of size b.
The master method
n
Each of them is of size b.
n
Each of them is of size b.
Ex. n
T (n) = 4T +n
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n.
Ex. n
T (n) = 4T +n
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n.
Case 1:
Ex. n
T (n) = 4T +n
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n.
Case 1:f (n) = O(n2−ε ) for ε = 1.
Ex. n
T (n) = 4T +n
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n.
Case 1:f (n) = O(n2−ε ) for ε = 1.
∴ T (n) = Θ(n2 ).
Ex. n
T (n) = 4T + n2
2
Ex. n
T (n) = 4T + n2
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n2 .
Ex. n
T (n) = 4T + n2
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n2 .
Case 2:
Ex. n
T (n) = 4T + n2
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n2 .
Case 2:f (n) = Θ(n2 lg0 n), that is, k = 0.
Ex. n
T (n) = 4T + n2
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n2 .
Case 2:f (n) = Θ(n2 lg0 n), that is, k = 0.
∴ T (n) = Θ(n2 lg n).
Ex. n
T (n) = 4T + n3
2
Ex. n
T (n) = 4T + n3
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n3 .
Ex. n
T (n) = 4T + n3
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n3 .
Case 3:
Ex. n
T (n) = 4T + n3
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n3 .
Case 3:f (n) = Ω(n2+ε ) for ε = 1.
Ex. n
T (n) = 4T + n3
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n3 .
Case 3:f (n) = Ω(n2+ε ) for ε = 1.
n 3 1
and 4 ≤ cn3 (reg. cond.) for c = .
2 2
Ex. n
T (n) = 4T + n3
2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = n3 .
Case 3:f (n) = Ω(n2+ε ) for ε = 1.
n 3 1
and 4 ≤ cn3 (reg. cond.) for c = .
2 2
∴ T (n) = Θ(n3 ).
Ex.
n n2
T (n) = 4T +
2 lg n
Ex.
n n2
T (n) = 4T +
2 lg n
n2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = .
lg n
Ex.
n n2
T (n) = 4T +
2 lg n
n2
a = 4, b = 2 =⇒ nlogb a = n2 ; f (n) = .
lg n
2
▶ f (n) = lgn n . Have f (n) = o(n), so that f (n) grows more slowly
than n, it doesn’t grow polynomially slower.
▶ In terms of the master theorem, have f (n) = n2 lg−1 n, so that
k = −1.
▶ Master theorem holds only for k ≥ 0, so case 2 does not apply.
▶ Master method does not apply.
Τ (1)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.50
…
Τ (1)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.51
…
Τ (1)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.52
#leaves = ah
…
…
= alogbn
Τ (1) nlogbaΤ (1)
= nlogba
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.53
C ASE 1:
1: The
The weight
weight increases
…
CASE increases
geometrically
geometrically from the root
from the root to
to the
the
Τ (1) leaves. The leaves hold a constant
leaves. The leaves hold a constant nlogbaΤ (1)
fraction
fraction of
of the
the total
total weight.
weight.
Θ(nlogba)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.54
…
CCASEASE 2:
2: (k
(k == 0)
0) The
The weight
weight
Τ (1) isis approximately
approximately the the same
same on
on nlogbaΤ (1)
each of the log n levels.
each of the logbbn levels.
Θ(nlogbalg n)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.55
C ASE 3:
3: The
The weight
weight decreases
…
CASE decreases
geometrically
geometrically from the root
from the root to
to the
the
Τ (1) leaves. The root holds a constant
leaves. The root holds a constant nlogbaΤ (1)
fraction
fraction of
of the
the total
total weight.
weight.
Θ( f (n))
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.56
1 − xn+a
1 + x + x2 + · · · + xn = for x ̸= 1
1−x
1
1 + x + x2 + · · · = for |x| < 1
1−x
Content has been extracted from Introduction to Algorithms, Fourth Edition, by Cormen,
Leiserson, Rivest, and Stein. MIT Press. 2022.
Visit [Link]
Original slides from Introduction to Algorithms 6.046J/18.401J, Fall 2005 Class by Prof. Charles
Leiserson and Prof. Erik Demaine. MIT OpenCourseWare Initiative available at
[Link]