RAIK 283: Data Structures & Algorithms
Asymptotic Notations*
Dr. Ying Lu
[email protected]
August 30, 2012
http://www.cse.unl.edu/~ylu/raik283
*slides refrred to
http://www.aw-bc.com/info/levitin
Design and Analysis of Algorithms Chapter 2.2
Review: algorithm efficiency indicator
order of growth
of
an algorithms basic operation count
the algorithms time efficiency
Design and Analysis of Algorithms Chapter 2.2
Asymptotic growth rate
A way of comparing functions that ignores constant factors and
small input sizes
O(g(n)): class of functions t(n) that grow no faster than g(n)
(g(n)): class of functions t(n) that grow at same rate as g(n)
(g(n)): class of functions t(n) that grow at least as fast as g(n)
Design and Analysis of Algorithms Chapter 2.2
Big-oh
t(n) O(g(n))
c > 0, n0 0 , n n0, t(n) cg(n)
Design and Analysis of Algorithms Chapter 2.2
Small-oh
t(n) o(g(n))
c > 0, n0 0 , n n0, t(n) < cg(n)
Design and Analysis of Algorithms Chapter 2.2
Big-omega
t(n) (g(n))
Design and Analysis of Algorithms Chapter 2.2
Big-omega
t(n) (g(n))
c > 0, n0 0 , n n0, t(n) cg(n)
Design and Analysis of Algorithms Chapter 2.2
Small-omega
t(n) (g(n))
c > 0, n0 0 , n n0, t(n) > cg(n)
Design and Analysis of Algorithms Chapter 2.2
Big-theta
t(n) (g(n))
c1>c2>0, n00, n n0, c2g(n) t(n) c1g(n)
Design and Analysis of Algorithms Chapter 2.2
Big theta
t(n) (g(n))
The reverse statement of
c1>c2>0, n00, n n0, c2g(n) t(n) c1g(n)
Design and Analysis of Algorithms Chapter 2.2
10
Big theta
t(n) (g(n))
c1>c2>0, n00, n n0, t(n) < c2g(n) or t(n) > c1g(n)
Design and Analysis of Algorithms Chapter 2.2
11
Establishing rate of growth: Method 1 using definition
t(n) is O(g(n)) if order of growth of t(n) order of growth
of g(n) (within constant multiple)
There exist positive constant c and non-negative integer n0
such that
t(n) c g(n) for every n n0
Examples:
10n O(2n2)
5n+20 O(10n)
Design and Analysis of Algorithms Chapter 2.2
12
Establishing rate of growth: Method 1 using definition
A
Examples:
Yes
No
No
nk
n
cn
Yes
No
No
nsinn
No
No
No
2n
2n/2
No
Yes
No
5 nlgc
clgn
Yes
Yes
Yes
Yes
Yes
Yes
1 ln2n
2
6 lg(n!) lg(nn)
2n (2n/2)
2n (2n/2)
Design and Analysis of Algorithms Chapter 2.2
13
Establishing rate of growth: Method 1 using definition
A
Examples:
Yes
No
No
nk
n
cn
Yes
No
No
nsinn
No
No
No
2n
2n/2
No
Yes
No
5 nlgc
clgn
Yes
Yes
Yes
Yes
Yes
Yes
1 ln2n
2
6 lg(n!) lg(nn)
n O(nsinn)
Design and Analysis of Algorithms Chapter 2.2
n (nsinn)
14
Establishing rate of growth: Method 2 using limits
limn t(n)/g(n)
order of growth of t(n) < order of growth of g(n)
t(n) o(g(n)), t(n) O(g(n))
c>0
order of growth of t(n) = order of growth of g(n)
t(n) (g(n)), t(n) O(g(n)), t(n) (g(n))
order of growth of t(n) > order of growth of g(n)
t(n) (g(n)), t(n) (g(n))
Design and Analysis of Algorithms Chapter 2.2
15
Establishing rate of growth: Method 2 using limits
Examples:
logb n vs. logc n
logbn = logbc logcn
limn( logbn / logcn) = limn (logbc) = logbc
logbn (logcn)
Design and Analysis of Algorithms Chapter 2.2
16
Exercises: establishing rate of growth using limits
ln2n vs. lnn2
2n vs. 2n/2
2n-1 vs. 2n
log2n vs. n
Design and Analysis of Algorithms Chapter 2.2
17
LHpitals rule
If
limn t(n) = limn g(n) =
The derivatives f, g exist,
Then
lim
n
t(n)
g(n) =
lim t (n)
n g (n)
Example: log2n vs. n
Design and Analysis of Algorithms Chapter 2.2
18
Establishing rate of growth
A
Examples:
Yes
No
No
nk
n
cn
Yes
No
No
nsinn
No
No
No
2n
2n/2
No
Yes
No
5 nlgc
clgn
Yes
Yes
Yes
Yes
Yes
Yes
1 ln2n
2
6 lg(n!) lg(nn)
Design and Analysis of Algorithms Chapter 2.2
19
Stirlings formula
n n
n! 2n ( )
e
Design and Analysis of Algorithms Chapter 2.2
20
Examples using stirlings formula
n! v.s. nn
lg(n!) v.s. lg(nn)
Design and Analysis of Algorithms Chapter 2.2
21
Examples using stirlings formula
n! o(nn)
lg(n!) v.s. lg(nn)
??? lg(n!)
o(lg(nn))
Design and Analysis of Algorithms Chapter 2.2
22
Examples using stirlings formula
n! o(nn)
lg(n!) v.s. lg(nn)
lg(n!) (lg(nn))
Design and Analysis of Algorithms Chapter 2.2
23
Special attention
n! o(nn)
However,
lg(n!) o(lg(nn))
lg(n!) (lg(nn))
sinn (1/2) sinn O(1/2)
However,
n1/2 (nsinn) n1/2 O(nsinn)
Design and Analysis of Algorithms Chapter 2.2
sinn (1/2)
n1/2 (nsinn)
24
Asymptotic notation properties
Transitivity:
f(n) = (g(n)) && g(n) = (h(n)) f(n) = (h(n))
f(n) = O(g(n)) && g(n) = O(h(n)) f(n) = O(h(n))
f(n) = (g(n)) && g(n) = (h(n)) f(n) = (h(n))
If t1(n) O(g1(n)) and t2(n) O(g2(n)), then
t1(n) + t2(n)
Design and Analysis of Algorithms Chapter 2.2
25
Asymptotic notation properties
Transitivity:
f(n) = (g(n)) && g(n) = (h(n)) f(n) = (h(n))
f(n) = O(g(n)) && g(n) = O(h(n)) f(n) = O(h(n))
f(n) = (g(n)) && g(n) = (h(n)) f(n) = (h(n))
If t1(n) O(g1(n)) and t2(n) O(g2(n)), then
t1(n) + t2(n) O(max{g1(n), g2(n)})
Design and Analysis of Algorithms Chapter 2.2
26
In-Class Exercises
Exercises 2.2: Problem 1, 2, 3 & 12
Problem 1: Use the most appropriate notation among O, ,
and to indicate the time efficiency class of sequential
search:
a. in the worst case
b. in the best case
c. in the average case ( Hint: C(n) = p*(n+1)/2 + (1-p)*n )
Problem 2: Use the informal definitions of O, , and to
determine whether the following assertions are true or
false.
a. n(n+1)/2 O(n3)
c. n(n+1)/2 (n3)
b. n(n+1)/2 O(n2)
d. n(n+1)/2 (n)
Design and Analysis of Algorithms Chapter 2.2
27
Announcement
40-minute quiz next Tuesday
problems based on materials covered in Chapter 2.2 (Asymptotic
Notations and Basic Efficiency Classes)
Design and Analysis of Algorithms Chapter 2.2
28
In-Class Exercises
Establish the asymptotic rate of growth (O, , and ) of
the following pair of functions. Prove your assertions.
a. 2n vs. 3n
b. ln(n+1) vs. ln(n)
Problem 3: For each of the following functions, indicate the
class (g(n)) the function belongs to. (Use the simplest g(n)
possible in your answers.) Prove your assertions.
a. (n2 + 1)10
b.
10n 2 7 n 3
c. 2nlg(n+2)2 + (n+2)2lg(n/2)
d. 2n+1 + 3n-1
e. log2n
Design and Analysis of Algorithms Chapter 2.2
29