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