Lecture 25

advertisement
Data Structures
CSCI 132, Spring 2014
Lecture 25
Asymptotic Analysis
1
Comparing Running Times
If f(n) describes the running time of some algorithm with
respect to the input size, n, we would like to be able to
compare f(n) with some known function g(n):
g(n) = 1
g(n) = log n
g(n) = n
g(n) = n2
g(n) = n3
g(n) = 2n
Constant
Logarithmic
Linear
Quadratic
Cubic
Exponential
2
Comparing f(n) with g(n)
To compare f(n) with g(n) as n gets large, we take the
limit of f(n)/g(n) as n approaches infinity:
limn f(n)/g(n) = 0
f(n) = o(g(n))
limn f(n)/g(n) is finite
f(n) = O(g(n))
limn f(n)/g(n) = finite, non-zero
f(n) = Q(g(n))
limn f(n)/g(n) = non-zero
f(n) = W(g(n))
limn f(n)/g(n) = 
f(n) = w(g(n))
3
Assumptions
1. f(n) > 0 and g(n) > 0 for all n > c0 for some c0 > 0.
2. limn f(n)/g(n) exists
Also note that:
limn f(n)/g(n) =  implies that
limn g(n)/f(n) = 0
4
Examples
We will work these out in class
1. Polynomials:
f(n) = 4n2 + n - 10
Compare to: g(n) = n, g(n) = n2, g(n) = n3
2. Logarithms:
f(n) = log(n)
Compare to: g(n) = lg(n), g(n) = n2
(note: we need L’Hopital’s rule: limn f(n)/g(n) = limn f'(n)/g'(n))
3. Exponentials:
f(n) = 2n
Compare to: g(n) = n2, g(n) = 3n
5
General Rules
• If f(n) is a polynomial in n of degree r, then f(n) has the same
order of magnitude as nr.
• If r<s, then nr has order strictly less than ns.
• The base of a logarithm makes no difference to the order of
magnitude.
• f(n) = log(n) has strictly smaller order of magnitude than nr for
r > 0.
6
More General Rules
• f(n) = an (for a > 1) has strictly greater magnitude than nr, where r
is a positive integer.
• If 0 <= a < b then an has order of magnitude strictly less than bn.
•If h(n) > 0 for n > c0, then
limn f(n)/g(n) = limn (f(n)h(n))/(g(n)h(n))
so f(n)h(n) relates to g(n)h(n) in the same way as f(n) relates to g(n).
7
Graphical Comparisons
8
Numerical Comparisons
n
1
10
100
1000
lgn
0
3.3
6.6
9.97
n
1
10
100
1000
n2
2
100
10,000
1,000,000
2n
2
1024
1.3 x 1030
1.07 x 10301
9
Big-Oh Notation
f(n) is
Say:
Meaning
limn f(n)/g(n)
o(g(n))
little-oh of g(n)
<
0
O(g(n))
Big-oh of g(n)
<=
finite
Q(g(n))
Big-theta of g(n)
=
non-zero, finite
W(g(n))
Big-omega of g(n)
>=
non-zero
w(g(n))
little-omega of g(n)
>
infinite
10
Rules using O notation
•If f(n) is a polynomial of degree r, then f(n) = Q(nr)
•If r < s, then nr is o(ns)
•If a > 1, b> 1 then loga(n) is Q(logb(n))
•log(n) is o(nr) for r> 0
•nr is o(an) for a >1, r positive integer
•If 0 <= a < b then an is o(bn)
Examples:
Sequential search is Q(n)
Binary search is Q(lgn)
Retrieval from contiguous list is O(1)
Retrieval from linked list is O(n)
Tower of Hanoi is W(2n)
11
Keeping the dominant term
Sometimes we want to know the specifics of the dominant
term, but we don't care about the lower order terms. We can
then write:
f(n) = g(n) + O(h(n))
Example:
f(n) = 3n2 + 4n - 5
= 3n2 + O(n)
Binary Search 2 has running time f(n) = 2lg n + O(1)
Sequential Search has running time f(n) = (1/2) n + O(1)
12
Download