Beruflich Dokumente
Kultur Dokumente
Asymptotic notations
Algorithm Analysis
The amount of resources used by the algorithm
Space
Computational time
Running time:
The number of primitive operations (steps) executed before
termination
Order of growth
The leading term of a formula
Expresses the behavior of a function toward infinity
Asymptotic Notations
Logarithms
In algorithm analysis we often use the notation log n without
specifying the base
Binary logarithm
lg n = log2 n
log x y = y log x
Natural logarithm
ln n = loge n
lg n = (lg n )
lg lg n = lg(lg n )
log
x
= log x log y
y
loga x = loga b logb x
a logb x =
x logb a
Asymptotic notations
Big O Notation
Examples
2n2 = O(n3): 2n2 cn3 2 cn c = 1 and n0= 2
n2 = O(n2): n2 cn2 c 1 c = 1 and n0= 1
1000n2+1000n = O(n2):
1000n2+1000n 1000n2+1000n2=2000n2 c=2000 and n0 = 1
n = O(n2): n cn2 cn 1 c = 1 and n0= 1
Examples
5n2 = (n)
c, n0 such that: 0 cn 5n2 cn 5n2 c = 1 and n0 = 1
100n + 5 (n2)
c, n0 such that: 0 cn2 100n + 5
100n + 5 100n + 5n ( n 1) = 105n
cn2 105n n(cn 105) 0
Since n is positive cn 105 0 n 105/c
contradiction: n cannot be smaller than a constant
n = (2n), n3 = (n2), n = (logn)
8
Examples
n2/2 n/2 = (n2)
n2 - n n2 n 0
c2=
n2 - n n2 - n * n ( n 2 ) = n2 c1=
Comparisons of Functions
Theorem:
f(n) = (g(n)) f = O(g(n)) and f = (g(n))
Transitivity:
f(n) = (g(n)) and g(n) = (h(n)) f(n) = (h(n))
Same for O and
Reflexivity:
f(n) = (f(n))
Same for O and
Symmetry:
f(n) = (g(n)) if and only if g(n) = (f(n))
Transpose symmetry:
f(n) = O(g(n)) if and only if g(n) = (f(n))
12
Practical Complexity
250
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
14
Practical Complexity
500
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
15
Practical Complexity
1000
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1
11
13
15
17
19
16
Practical Complexity
5000
4000
f(n) = n
f(n) = log(n)
3000
f(n) = n log(n)
f(n) = n^2
2000
f(n) = n^3
f(n) = 2^n
1000
0
1
11
13
15
17
19
17
f(n) = (g(n))
f(n) = O(g(n))
f(n) = (g(n))
f(n) = (g(n))
f(n) = (g(n))
f(n) = (g(n))
n
f(n) = 2n; g(n)
18 = 3
f(n) = O(g(n))
lim (f(n)/g(n)) = 0
n
19
lim (f(n)/g(n)) =
n
20
Intuitions
Intuitively,
() is like >
() is like
() is like =
21
(logN) (logarithmic)
(N) (linear)
(N logN)
22
(N2 ) (quadratic)
Typical for algorithms that process all pairs of data items (double nested
loops)
(N3 ) (cubic)
(NK ) (polynomial)
(2N ) (exponential)
23
Arithmetic series:
k = 1 + 2 + ... + n =
Geometric series:
k =1
n
n( n + 1)
2
= 1 + x + x 2 + ... + x n =
k =0
x
k =0
n
x n +1 1
(x 1)
x 1
1
1 x
Harmonic series:
k = 1 + 2 + ... + n
lg k = lg(k!)
ln n
k =1
n
n lg n
k =1
n
k
k =1
= 1p + 2 p + ... + n p
1
n p +1
p +1
24