Analysis of Algorithms: An Algorithm Is A Step-By-Step Procedure For Solving A Problem in A Finite Amount of Time
Analysis of Algorithms: An Algorithm Is A Step-By-Step Procedure For Solving A Problem in A Finite Amount of Time
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
algorithm 7000
Time (ms)
inputs of varying size and 5000
composition 4000
Use a method like 3000
System.currentTimeMillis() to 2000
get an accurate measure
1000
of the actual running time
0
Plot the results 0 50 100
Input Size
An potentially unbounded
bank of memory cells, 2
1
each of which can hold an 0
arbitrary number or
character
Memory cells are numbered and accessing
any cell in memory takes unit time.
© 2004 Goodrich, Tamassia Analysis of Algorithms 8
Seven Important Functions (§3.3)
Seven functions that
often appear in 1E+30
algorithm analysis: 1E+28
1E+26
Cubic
Constant 1 1E+24 Quadratic
Logarithmic log n 1E+22
Linear
Linear n 1E+20
1E+18
N-Log-N n log n
T (n )
1E+16
Quadratic n2 1E+14
Cubic n3 1E+12
1E+10
Exponential 2n 1E+8
1E+6
In a log-log chart, the 1E+4
slope of the line 1E+2
1E+0
corresponds to the 1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
growth rate of the n
function
© 2004 Goodrich, Tamassia Analysis of Algorithms 9
Primitive Operations
Basic computations
Examples:
performed by an algorithm Evaluating an
Identifiable in pseudocode expression
Largely independent from the Assigning a value
to a variable
programming language Indexing into an
Exact definition not important array
(we will see why later) Calling a method
Assumed to take a constant Returning from a
method
amount of time in the RAM
model
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
1
1 10 100 1,000
n
big-Theta
f(n) is (g(n)) if there are constants c’ > 0 and c’’
equal to g(n)