Algorithm Analysis
Algorithm Analysis
Analysis of Algorithms
Dr. Ying Lu
ylu@cse.unl.edu
August 28, 2012
http://www.cse.unl.edu/~ylu/raik283/
Purpose
Purpose
Purpose (Example)
Suppose a machine that performs a million floatingpoint operations per second (106 FLOPS), then how
long an algorithm will run for an input of size n=50?
1) If the algorithm requires n2 such operations:
Purpose (Example)
Suppose a machine that performs a million floatingpoint operations per second (106 FLOPS), then how
long an algorithm will run for an input of size n=50?
1) If the algorithm requires n2 such operations:
0.0025 second
Purpose (Example)
Suppose a machine that performs a million floatingpoint operations per second (106 FLOPS), then how
long an algorithm will run for an input of size n=50?
1) If the algorithm requires n2 such operations:
0.0025 second
2) If the algorithm requires 2n such operations:
Purpose (Example)
Suppose a machine that performs a million floatingpoint operations per second (106 FLOPS), then how
long an algorithm will run for an input of size n=50?
1) If the algorithm requires n2 such operations:
0.0025 second
2) If the algorithm requires 2n such operations:
over 35 years!
10
11
Definition of Time?
12
Definition of Time
(e.g.,
13
T(n) copC(n)
running time
execution time
for basic operation
Number of times
basic operation is
executed
14
Basic operation
Graph problem
Design and Analysis of Algorithms Chapter 2.1
15
Basic operation
Key comparison
Graph problem
Design and Analysis of Algorithms Chapter 2.1
16
Basic operation
Key comparison
Floating point
multiplication
Compute an
Graph problem
Design and Analysis of Algorithms Chapter 2.1
17
Basic operation
Floating point
multiplication
Compute an
Floating point
multiplication
Graph problem
Design and Analysis of Algorithms Chapter 2.1
18
Basic operation
Floating point
multiplication
Compute an
Floating point
multiplication
Graph problem
Visiting a vertex or
traversing an edge
19
20
21
22
Best case:
23
Best case
Average case
24
An example
25
Exact formula
e.g., C(n) = n(n-1)/2
26
Exact formula
e.g., C(n) = n(n-1)/2
Formula indicating order of growth with specific
multiplicative constant
e.g., C(n) 0.5 n2
Formula indicating order of growth with unknown
multiplicative constant
e.g., C(n) cn2
Most important: Order of growth within a constant
multiple as n
Design and Analysis of Algorithms Chapter 2.1
27
O(g(n)):
(g(n)):
(g(n)):
28
29
Table 2.1
30
Classifying Functions
32
Mammals
33
34
Dogs
35
Classifying Animals
Vertebrates
Fish
Reptiles
Mammals
Birds
Giraffe
Dogs
36
n1000
n2
2n
37
n1000
n2
2n
Polynomials
38
1000n2
3n2
2n3
39
1000n2
3n2
2n3
Quadratic
40
Classifying Functions?
Functions
41
Classifying Functions
Functions
Exponential
Factorial
(log n)5
Polynomial
5 log n
Poly Logarithmic
Logarithmic
Constant
5
n5
25n
n!
42
Classifying Functions?
Polynomial
43
Classifying Functions
Polynomial
Linear
Quadratic
Cubic
5n
5n2
5n3
5n4
44
Logarithmic
log10n = # digits to write n
log2n = # bits to write n
= 3.32 log10n
log(n1000) = 1000 log(n)
Differ only by a
multiplicative
constant
45
Poly Logarithmic
46
log1000 n
n0.001
47
48
10000 n
0.0001 n2
49
50
n1000
20.001 n
51
52
Ordering Functions
Functions
<<
25n
<<
<<
Factorial
5 << 5 log n
(log n)5
n5
<<
<<
<<
Exponential
<<
Polynomial
<<
Poly Logarithmic
Logarithmic
Constant
<<
n!
53
5
1,000,000,000,000
0.0000000000001
-5
0
8 + sin(n)
54
Yes 5
Yes 1,000,000,000,000
Yes 0.0000000000001
Yes -5
Yes 0
No 8 + sin(n)
55
56
5
1,000,000,000,000
9
0.0000000000001
7
-5
0
Lie in between
8 + sin(n)
Design and Analysis of Algorithms Chapter 2.1
57
58
n2
0.001 n2
Lie in between
1000 n2
5n2 + 3n + 2log n
59
60
Examples
f(n)
g(n)
1)
ln2n
2)
nk
n
cn
3)
4)
5)
6)
nsinn
2n
2n/2
nlgc
clgn
O(g(n))?
(g(n))? (g(n))?
lg(n!) lg(nn)
61