Ch3 Algorithm Analysis
Ch3 Algorithm Analysis
Running Time
80
with the input size.
Average case time is 60
often difficult to 40
determine. 20
We focus on the worst 0
case running time. 1000 2000 3000 4000
I nput Size
Easier to analyze
Crucial to applications such
Time (ms)
5000
and composition, noting
4000
the time needed:
3000
2000
1000
0
0 50 100
Plot the results I nput Size
© 2013 Goodrich, Tamassia,
Goldwasser Analysis of Algorithms 3
Limitations of Experiments
It is necessary to implement the
algorithm, which may be difficult
Results may not be indicative of the
running time on other inputs not
included in the experiment.
In order to compare two algorithms,
the same hardware and software
environments must be used
An potentially
unbounded bank of 2
1
memory cells, each of 0
which can hold an
arbitrary number or
character
Memory cells are numbered and
accessing any cell in memory takes
unit time. Analysis of Algorithms
© 2013 Goodrich, Tamassia,
Goldwasser 8
Seven Important Functions
Seven functions that
often appear in 1E+30
1E+28 Cubic
algorithm analysis: 1E+26
Constant 1 1E+24 Quadratic
Logarithmic log n 1E+22 Linear
1E+20
Linear n 1E+18
N-Log-N n log n 1E+16
T (n )
1E+14
Quadratic n2 1E+12
Cubic n3 1E+10
Exponential 2n 1E+8
1E+6
1E+4
In a log-log chart, the 1E+2
slope of the line 1E+0
corresponds to the 1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
growth rate
© 2013 Goodrich, Tamassia,
Goldwasser Analysis of Algorithms 9
Slide by Matt
Functions Graphed Stallmann included
with permission.
Using “Normal” Scale
g(n) = n lg n
g(n) = 1 g(n) = 2n
g(n) = n2
g(n) = lg n
g(n) = n
g(n) = n3
c lg n c lg (n + 1) c (lg n + 1) c(lg n + 2)
cn c (n + 1) 2c n 4c n
~ c n lg n 2c n lg n + 4c n lg n + runtime
c n lg n quadruple
+ cn 2cn 4cn
s
c n2 ~ c n2 + 2c n 4c n2 16c n2 when
problem
c n3 ~ c n3 + 3c n2 8c n3 64c n3 size
doubles
c 2n c 2 n+1 c 2 2n c 2 4n
© 2013 Goodrich, Tamassia,
Goldwasser Analysis of Algorithms 15
Slide by Matt
Stallmann included
with permission.
Comparison of Two Algorithms
insertion sort is
n2 / 4
merge sort is
2 n lg n
sort a million items?
insertion sort takes
roughly 70 hours
while
merge sort takes
roughly 40 seconds
3 log n + 5 is O(log n)
need c > 0 and n0 1 such that 3 log n + 5 c•log n for n
n0
© 2013 Goodrich, Tamassia,
this is true for c =
Goldwasser 8 andAnalysis
n0 = of 2 Algorithms 20
Big-Oh and Growth Rate
The big-Oh notation gives an upper bound on the
growth rate of a function
The statement “f(n) is O(g(n))” means that the
growth rate of f(n) is no more than the growth rate
of g(n)
We can use the big-Oh notation to rank functions
according to their growth rate
f(n) is O(g(n)) g(n) is O(f(n))
g(n) grows Yes No
more
f(n) grows more No Yes
Same growth
© 2013 Goodrich, Tamassia, Yes Yes
Goldwasser Analysis of Algorithms 21
Big-Oh Rules
an array X is average of 20
the first (i 1) elements of 15
X:
10
A[i] X[0] X[1] … X[i])/(i+1)
5
Computing the array A of
prefix averages of another 0
array X has applications to 1 2 3 4 5 6 7
financial analysis
© 2013 Goodrich, Tamassia,
Goldwasser Analysis of Algorithms 24
Prefix Averages
(Quadratic)
The following algorithm computes prefix
averages in quadratic time by applying the
definition
big-Theta
f(n) is (g(n)) if there are constants c’ > 0