Академический Документы
Профессиональный Документы
Культура Документы
1)
Most algorithms transform best case
average case
input objects into output
Analysis of Algorithms objects. 120
worst case
Running Time
80
with the input size.
60
Average case time is often
difficult to determine. 40
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
4000
in the experiment.
3000
System.currentTimeMillis() to 2000
In order to compare two algorithms, the
get an accurate measure same hardware and software
1000
of the actual running time
Plot the results
0 environments must be used
0 50 100
Input Size
© 2004 Goodrich, Tamassia Analysis of Algorithms 3 © 2004 Goodrich, Tamassia Analysis of Algorithms 4
Theoretical Analysis Pseudocode (§3.2)
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 Output maximum element of A
function of the input size, n. 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
© 2004 Goodrich, Tamassia Analysis of Algorithms 5 © 2004 Goodrich, Tamassia Analysis of Algorithms 6
T (n )
1E+16
Quadratic ≈ n2 1E+14 Indexing into an
Cubic ≈ n3 1E+12
1E+10
Exact definition not important array
Exponential ≈ 2n
1E+8 (we will see why later) Calling a method
1E+6
In a log
- log chart, the Returning from a
1E+4 Assumed to take a constant
method
slope of the line 1E+2
corresponds to the 1E+0 amount of time in the RAM
growth rate of the 1E+0 1E+2 1E+4
n
1E+6 1E+8 1E+10
model
function
© 2004 Goodrich, Tamassia Analysis of Algorithms 9 © 2004 Goodrich, Tamassia Analysis of Algorithms 10
Counting Primitive
Operations (§3.4) Estimating Running Time
By inspecting the pseudocode, we can determine the Algorithm arrayMax executes 8n − 2 primitive
maximum number of primitive operations executed by
an algorithm, as a function of the input size operations in the worst case. Define:
a = Time taken by the fastest primitive operation
Algorithm arrayMax(A, n) # operations b = Time taken by the slowest primitive operation
currentMax ← A[0] 2
for i ← 1 to n − 1 do 2n Let T(n) be worst-case time of arrayMax. Then
if A[i] > currentMax then 2(n − 1) a (8n − 2) ≤ T(n) ≤ b(8n − 2)
currentMax ← A[i] 2(n − 1) Hence, the running time T(n) is bounded by two
{ increment counter i } 2(n − 1)
return currentMax 1
linear functions
Total 8n − 2
© 2004 Goodrich, Tamassia Analysis of Algorithms 11 © 2004 Goodrich, Tamassia Analysis of Algorithms 12
Growth Rate of Running Time Constant Factors
1E+26
Changing the hardware/ software The growth rate is 1E+24 Quadratic
Quadratic
not affected by 1E+22
environment constant factors or
1E+20
1E+18
Linear
Linear
Affects T(n) by a constant factor, but lower-order terms 1E+16
1E+14
T (n )
Does not alter the growth rate of T(n) Examples 1E+12
1E+10
102n + 105 is a linear
The linear growth rate of the running function
1E+8
1E+6
time T(n) is an intrinsic property of 105n2 + 108n is a
quadratic function
1E+4
1E+2
algorithm arrayMax 1E+0
1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
© 2004 Goodrich, Tamassia Analysis of Algorithms 13 © 2004 Goodrich, Tamassia Analysis of Algorithms 14
2n + 10 ≤ cn constant
1 10
(c − 2) n ≥ 10
1 10 100 1,000
n ≥ 10/(c − 2) n 1
Pick c = 3 and n0 = 10 1 10 100 1,000
n
© 2004 Goodrich, Tamassia Analysis of Algorithms 15 © 2004 Goodrich, Tamassia Analysis of Algorithms 16
More Big-Oh Examples Big-Oh and Growth Rate
7n-2
7n-2 is O(n)
The big- Ohnotation gives an upper bound on the
need c > 0 and n0 ≥ 1 such that 7n-2 ≤ c•n for n ≥ n0 growth rate of a function
this is true for c = 7 and n0 = 1 The statement “f(n) is O(g(n))” means that the growth
rate of f(n) is no more than the growth rate of g(n)
3n3 + 20n2 + 5
We can use the big - Ohnotation to rank functions
3n3 + 20n2 + 5 is O(n3)
according to their growth rate
need c > 0 and n0 ≥ 1 such that 3n3 + 20n2 + 5 ≤ c•n3 for n ≥ n0
this is true for c = 4 and n0 = 21 f(n) is O(g(n)) g(n) is O(f(n))
3 log n + 5 g(n) grows more Yes No
3 log n + 5 is O(log n) f(n) grows more No Yes
need c > 0 and n0 ≥ 1 such that 3 log n + 5 ≤ c•log n for n ≥ n0
Same growth Yes Yes
this is true for c = 8 and n0 = 2
© 2004 Goodrich, Tamassia Analysis of Algorithms 17 © 2004 Goodrich, Tamassia Analysis of Algorithms 18
© 2004 Goodrich, Tamassia Analysis of Algorithms 19 © 2004 Goodrich, Tamassia Analysis of Algorithms 20
Computing Prefix Averages Prefix Averages (Quadratic)
We further illustrate The following algorithm computes prefix averages in
35
asymptotic analysis with X quadratic time by applying the definition
two algorithms for prefix 30 A Algorithm prefixAverages1(X, n)
averages 25 Input array X of n integers
The i- th prefix average of 20 Output array A of prefix averages of X #operations
an array X is average of the
first (i + 1) elements of X: 15 A ← new array of n integers n
for i ← 0 to n − 1 do n
A[i] = (X[0] + X[1] + … + X[i])/(i+1) 10
s ← X[0] n
Computing the array A of 5 for j ← 1 to i do 1 + 2 + …+ (n − 1)
prefix averages of another 0 s ← s + X[j] 1 + 2 + …+ (n − 1)
array X has applications to 1 2 3 4 5 6 7 A[i] ← s / (i + 1) n
financial analysis return A 1
© 2004 Goodrich, Tamassia Analysis of Algorithms 21 © 2004 Goodrich, Tamassia Analysis of Algorithms 22
© 2004 Goodrich, Tamassia Analysis of Algorithms 25 © 2004 Goodrich, Tamassia Analysis of Algorithms 26
© 2004 Goodrich, Tamassia Analysis of Algorithms 27 © 2004 Goodrich, Tamassia Analysis of Algorithms 28