Professional Documents
Culture Documents
Analysis
Analysis
Analysis
Running Time
80
with the input size.
60
Average case time is often
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 2
Experimental Studies
Write a program 9000
algorithm 7000
Run the program with 6000
Time (ms)
inputs of varying size and 5000
composition 4000
Use a method like clock() 3000
to get an accurate 2000
measure of the actual
1000
running time
0
Plot the results 0 50 100
Input Size
g(n) = n2
g(n) = lg n
g(n) = n
g(n) = n3
Algorithm arrayMax(A, n)
currentMax A[0] 2
for i 1 to n 1 do 2n
if A[i] currentMax then 2(n 1)
currentMax A[i] 2(n 1)
{ increment counter i } 2(n 1)
return currentMax 1
Total 8n 2
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 quadruples
+ cn 2cn 4cn
when
c n2 ~ c n2 + 2c n 4c n2 16c n2 problem
size doubles
c n3 ~ c n3 + 3c n2 8c n3 64c n3
c 2n c 2 n+1 c 2 2n c 2 4n
7n-2 is O(n)
3n3 + 20n2 + 5
3n3 + 20n2 + 5 is O(n3)
3 log n + 5
3 log n + 5 is O(log n)