Documente Academic
Documente Profesional
Documente Cultură
1)
w Most algorithms transform best case
Running Time
80
with the input size.
60
w Average case time is often
difficult to determine. 40
Analysis of Algorithms 2
algorithm 7000
algorithm, which may be difficult
w Run the program with 6000 w Results may not be indicative of the
Time (ms)
inputs of varying size and 5000 running time on other inputs not included
composition
w Use a method like
4000
in the experiment.
3000
System.currentTimeMillis() to 2000
w In order to compare two algorithms, the
get an accurate measure
of the actual running time 1000 same hardware and software
w Plot the results 0 environments must be used
0 50 100
Input Size
Counting Primitive
Primitive Operations Operations (§1.1)
w Basic computations w By inspecting the pseudocode, we can determine the
w Examples: maximum number of primitive operations executed by
performed by an algorithm
n Evaluating an an algorithm, as a function of the input size
w Identifiable in pseudocode expression
w Largely independent from the n Assigning a value Algorithm arrayMax(A, n) # operations
to a variable currentMax ← A[0]
programming language Indexing into an
2
n
for i ← 1 to n − 1 do 2 +n
w Exact definition not important array
if A[i] > currentMax then 2(n − 1)
(we will see why later) n Calling a method
currentMax ← A[i] 2(n − 1)
Returning from a
w Assumed to take a constant n
method
{ increment counter i } 2(n − 1)
amount of time in the RAM return currentMax 1
model Total 7n − 1
Analysis of Algorithms 9 Analysis of Algorithms 10
functions: 1E+26
Quadratic not affected by 1E+22 Quadratic
1E+24 1E+20 Linear
n Linear ≈ n 1E+22 Linear n constant factors or 1E+18 Linear
n Quadratic ≈ n2 1E+20
n lower-order terms 1E+16
1E+18
T ( n)
1E+14
Cubic ≈ n 3 w Examples
T (n )
n 1E+16
1E+12
1E+14
1E+12 n 102 n + 105 is a linear 1E+10
w In a log-log chart, 1E+10 function
1E+8
1E+6
the slope of the line 1E+8
1E+6 n 105 n2 + 108 n is a 1E+4
corresponds to the 1E+4 quadratic function 1E+2
1E+2
growth rate of the 1E+0
1E+0 1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
function 1E+0 1E+2 1E+4 1E+6 1E+8 1E+10 n
n
n 2n + 10 ≤ cn constant
1 10
n (c − 2) n ≥ 10
1 10 100 1,000
n n ≥ 10/(c − 2) n 1
n Pick c = 3 and n 0 = 10 1 10 100 1,000
n
little-omega f(n) is ω(g(n)) if, for any constant c > 0, there is an integer constant n0 ≥
n f(n) is ω(g(n)) if is asymptotically strictly greater than g(n)
0 such that f(n) ≥ c•g(n) for n ≥ n0
need 5n02 ≥ c•n0 → given c, the n0 that satifies this is n0 ≥ c/5 ≥ 0