Analysis
Analysis
Running Time
80
with the input size.
60
Average case time is often
Input Algorithm Output difficult to determine. 40
running time. 0
1000 2000 3000 4000
Easier to analyze Input Size
Crucial to applications such as
games, finance and robotics
© 2010 Goodrich, Tamassia Analysis of Algorithms 1 © 2010 Goodrich, Tamassia Analysis of Algorithms 2
algorithm 7000
algorithm, which may be difficult
Run the program with 6000 Results may not be indicative of the
Time (ms)
inputs of varying size and 5000 running time on other inputs not included
composition
Use a method like clock()
4000
in the experiment.
3000
to get an accurate 2000
In order to compare two algorithms, the
measure of the actual same hardware and software
1000
running time
Plot the results
0 environments must be used
0 50 100
Input Size
© 2010 Goodrich, Tamassia Analysis of Algorithms 3 © 2010 Goodrich, Tamassia Analysis of Algorithms 4
Theoretical Analysis Pseudocode
High-level description Example: find max
Uses a high-level description of the
of an algorithm element of an array
algorithm instead of an implementation More structured than Algorithm arrayMax(A, n)
Characterizes running time as a English prose Input array A of n integers
Less detailed than a
function of the input size, n.
Output maximum element of A
program
currentMax ← A[0]
Takes into account all possible inputs Preferred notation for
for i ← 1 to n − 1 do
describing algorithms
Allows us to evaluate the speed of an Hides program design if A[i] > currentMax then
currentMax ← A[i]
algorithm independent of the issues
return currentMax
hardware/software environment
© 2010 Goodrich, Tamassia Analysis of Algorithms 5 © 2010 Goodrich, Tamassia Analysis of Algorithms 6
© 2010 Goodrich, Tamassia Analysis of Algorithms 11 © 2010 Goodrich, Tamassia Analysis of Algorithms 12
Estimating Running Time Growth Rate of Running Time
Algorithm arrayMax executes 8n − 2 primitive
Changing the hardware/ software
operations in the worst case. Define:
a = Time taken by the fastest primitive operation
environment
b = Time taken by the slowest primitive operation Affects T(n) by a constant factor, but
Let T(n) be worst-case time of arrayMax. Then Does not alter the growth rate of T(n)
a (8n − 2) ≤ T(n) ≤ b(8n − 2) The linear growth rate of the running
Hence, the running time T(n) is bounded by two time T(n) is an intrinsic property of
linear functions algorithm arrayMax
© 2010 Goodrich, Tamassia Analysis of Algorithms 13 © 2010 Goodrich, Tamassia Analysis of Algorithms 14
n
constant factors or 1E+18 Linear positive constants
lower-order terms 1E+16
c and n0 such that 100
T (n )
1E+14
Examples 1E+12
f(n) ≤ cg(n) for n ≥ n0
1E+10
102n + 105 is a linear 10
function
1E+8 Example: 2n + 10 is O(n)
1E+6
105n2 + 108n is a 1E+4 2n + 10 ≤ cn
1
quadratic function 1E+2 (c − 2) n ≥ 10
1E+0 1 10 100 1,000
n ≥ 10/(c − 2) n
1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n Pick c = 3 and n0 = 10
© 2010 Goodrich, Tamassia Analysis of Algorithms 17 © 2010 Goodrich, Tamassia Analysis of Algorithms 18
© 2010 Goodrich, Tamassia Analysis of Algorithms 23 © 2010 Goodrich, Tamassia Analysis of Algorithms 24
Prefix Averages (Quadratic) Arithmetic Progression
The following algorithm computes prefix averages in 7
quadratic time by applying the definition The running time of
6
prefixAverages1 is
Algorithm prefixAverages1(X, n)
O(1 + 2 + …+ n) 5
Input array X of n integers
Output array A of prefix averages of X #operations The sum of the first n 4
A ← new array of n integers n integers is n(n + 1) / 2
3
for i ← 0 to n − 1 do n There is a simple visual
proof of this fact 2
s ← X[0] n
for j ← 1 to i do 1 + 2 + …+ (n − 1) Thus, algorithm 1
s ← s + X[j] 1 + 2 + …+ (n − 1) prefixAverages1 runs in 0
A[i] ← s / (i + 1) n O(n2) time
1 2 3 4 5 6
return A 1
© 2010 Goodrich, Tamassia Analysis of Algorithms 25 © 2010 Goodrich, Tamassia Analysis of Algorithms 26
© 2010 Goodrich, Tamassia Analysis of Algorithms 29 © 2010 Goodrich, Tamassia Analysis of Algorithms 30