Professional Documents
Culture Documents
Asymptotic Notation Demo
Asymptotic Notation Demo
LECTURE 3
• Asymptotic Notation
• Basic Data Structures
Adam Smith
8/29/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Asymptotic notation
O-notation (upper bounds):
We write f(n) = O(g(n)) if there
exist constants c > 0, n0 > 0 such
that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0.
• n½ + log(n) = O(n½)
• n = O(n2)
8/29/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Ω-notation (lower bounds)
O-notation is an upper-bound notation. It
makes no sense to say f(n) is at least O(n2).
EXAMPLE: (c = 1, n0 = 16)
9/15/08
S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson
Ω-notation (lower bounds)
• Be careful: “Any comparison-based sorting algorithm
requires at least O(n log n) comparisons.”
– Meaningless!
– Use Ω for lower bounds.
8/29/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Θ-notation (tight bounds)
EXAMPLE:
8/29/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Conventions for formulas
Convention: A set in a formula represents
an anonymous function in the set.
EXAMPLE: f(n) = n3 + O(n2)
(right-hand side) means
f(n) = n3 + h(n)
for some h(n) ∈ O(n2) .
9/15/08
S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson
Convention for formulas
Convention: A set in a formula represents
an anonymous function in the set.
EXAMPLE: n2 + O(n) = O(n2)
(left-hand side) means
for any f(n) ∈ O(n):
n2 + f(n) = h(n)
for some h(n) ∈ O(n2) .
9/15/08
S. Raskhodnikova and A. Smith. Based on notes by E. Demaine and C. Leiserson
Properties
• Transitivity.
– If f = O(g) and g = O(h) then f = O(h).
– If f = Ω(g) and g = Ω(h) then f = Ω(h).
– If f = Θ(g) and g = Θ(h) then f = Θ(h).
• Additivity.
– If f = O(h) and g = O(h) then f + g = O(h).
– If f = Ω(h) and g = Ω(h) then f + g = Ω(h).
– If f = Θ(h) and g = O(h) then f + g = Θ(h).
8/29/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Exercise: Show that log(n!) = Θ(n log n)
√ ! n "n
• Stirling’s formula: n! = ( 2πn) (1 + o(1))
e
√
log(n!) = log( 2πn) + n log(n) − n log(e) + log(1 + o(1))
! "# $
=log(1)+o(1)
since log is continuous
% log(2πn) &
= n log(n) − log(e) + + o(1)
! "# n $
constant+o(1)
8/29/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne
Overview
Notation … means … Think… E.g. Lim f(n)/g(n)
f(n)=O(n) 9 c>0, n0>0, 8 n > n0 : Upper 100n2 If it exists, it
0 · f(n) < cg(n) bound = O(n3) is < 1
8/29/2008
A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne