Professional Documents
Culture Documents
Lec 7
Lec 7
Lec 7
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
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
Returning from a
Assumed to take a constant
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
© 2004 Goodrich, Tamassia Analysis of Algorithms 10
Estimating Running Time
Algorithm arrayMax executes 8n 2 primitive
operations in the worst case. Define:
a = Time taken by the fastest primitive operation
b = Time taken by the slowest primitive operation
Let T(n) be worst-case time of arrayMax. Then
a (8n 2) T(n) b(8n 2)
Hence, the running time T(n) is bounded by two
linear functions
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)