Академический Документы
Профессиональный Документы
Культура Документы
\
|
+ + + + =
+ + +
1
1
1 0 1 0
lim lim
\
|
+ + =
n n n n n
n T
n n
For most common complexity functions, it's this easy to determine the big-O and/or
big-O complexity using the given theorems.
Asymptotics
Data Structures & Algorithms
16
CS@VT 2000-2009 McQuain
Complexity of Linear Storage
For a contiguous list of N elements, assuming each is equally likely to be the target of a
search:
- average search cost is O(N) if list is randomly ordered
- average search cost is O(log N) is list is sorted
- average random insertion cost is O(N)
- insertion at tail is O(1)
For a linked list of N elements, assuming each is equally likely to be the target of a
search:
- average search cost is O(N), regardless of list ordering
- average random insertion cost is O(1), excluding search time
Asymptotics
Data Structures & Algorithms
17
CS@VT 2000-2009 McQuain
Most Common Complexity Classes
Theorem 5 lists a collection of representatives of distinct big- equivalence classes:
K [constant]
log
b
(n) [always log base 2 if no base is shown]
n
n log
b
(n)
n
2
n to higher powers
2
n
3
n
larger constants to the n-th power
n! [n factorial]
n
n
Most common algorithms fall into one of these classes.
Knowing this list provides some knowledge of how to compare and choose the right algorithm.
The following charts provide some visual indication of how significant the differences are
Asymptotics
Data Structures & Algorithms
18
CS@VT 2000-2009 McQuain
Graphical Comparison
Common Growth Curves
0
200
400
600
800
1000
1200
1 2 3 4 5 6 7 8 9 10 11
n (input size)
log n
n
n log n
n^2
n^3
2^n
10^n
Asymptotics
Data Structures & Algorithms
19
CS@VT 2000-2009 McQuain
Lower-order Classes
Low-order Curves
0
20
40
60
80
100
120
1 3 5 7 9
1
1
1
3
1
5
1
7
1
9
2
1
2
3
n (input size)
log n
n
n log n
n
2
For significantly large
values of n, only these
classes are truly
practical, and whether
n
2
is practical is
debated.
Asymptotics
Data Structures & Algorithms
20
CS@VT 2000-2009 McQuain
Proof of Theorem 8
Theorem 8: f(n) is O(g(n)) if
< < =
c c
n g
n f
n
0 where
) (
) (
lim
Suppose that f and g are non-negative functions of n, and that
< < =
c c
n g
n f
n
0 where
) (
) (
lim
Then, from the definition of the limit, for every > 0 there exists an N > 0 such that
whenever n > N:
( )
from which we have ( ) ( ) ( ) ( ) ( )
( )
f n
c c g n f n c g n
g n
c c c < s s +
Let = c/2, then we have that:
3
( ) ( ) whence is ( )
2
c
f n g n f g s O
( ) ( ) whence is ( )
2
c
g n f n f g s O
Therefore, by definition, f is (g).