Docsity

Analyzing Algorithmic Running Time: Big-Oh Notation and Growth Rates, Study notes of Computer Science

Toggle sidebar
bg1
Verifying an Algorithmic Analysis through running actual code
T(N) is the empirical (observed) running time of the code and the
claim is made that T(N) ∈ O(F(N)).
Technique is to compute a series of values T(N)/F(N) for a range of
N (commonly spaced out by a factors of two). Depending upon these
values of T(N)/F(N) we can determine how accurate our estimation
for F(N) is according to:
is a close answer(θ) if the values converge to a + const.
F(N) = is an overestimate if the values converge to zero.
is an underestimate if the values diverge .
Examples
Example 1
Consider the following table of data obtained from running an
instance of an algorithm assumed to be cubic. Decide if the
Big-Oh estimate, O(N3) is accurate.
Run N T(N)
1 100 0.017058 ms
2 1000 17.058 ms
3 5000 2132.2464 ms
4 10000 17057.971 ms
5 50000 2132246.375 ms
T(N)/F(N) = 0.017058/(100*100*100) = 1.0758 × 10-8
T(N)/F(N) = 17.058/(1000*1000*1000) = 1.0758 × 10-8
T(N)/F(N) = 2132.2464/(5000*5000*5000) = 1.0757 × 10-8
T(N)/F(N) = 17057.971/(10000*10000*10000) = 1.0757 × 10-8
T(N)/F(N) = 2132246.375/(50000*50000*50000) = 1.0757 × 10-8
pf3
pf4
pf5

Partial preview of the text

Download Analyzing Algorithmic Running Time: Big-Oh Notation and Growth Rates and more Study notes Computer Science in PDF only on Docsity!

Verifying an Algorithmic Analysis through running actual code

T(N) is the empirical (observed) running time of the code and the claim is made that T(N) ∈ O(F(N)).

Technique is to compute a series of values T(N)/F(N) for a range of N (commonly spaced out by a factors of two). Depending upon these values of T(N)/F(N) we can determine how accurate our estimation for F(N) is according to:

is a close answer(θ) if the values converge to a + const.

F(N) = is an overestimate if the values converge to zero.

is an underestimate if the values diverge.

Examples

Example 1 Consider the following table of data obtained from running an instance of an algorithm assumed to be cubic. Decide if the Big-Oh estimate, O(N^3 ) is accurate.

Run N T(N) 1 100 0.017058 ms 2 1000 17.058 ms 3 5000 2132.2464 ms 4 10000 17057.971 ms 5 50000 2132246.375 ms

T(N)/F(N) = 0.017058/(100100100) = 1.0758 × 10 - T(N)/F(N) = 17.058/(100010001000) = 1.0758 × 10 - T(N)/F(N) = 2132.2464/(500050005000) = 1.0757 × 10 - T(N)/F(N) = 17057.971/(100001000010000) = 1.0757 × 10 - T(N)/F(N) = 2132246.375/(500005000050000) = 1.0757 × 10 -

The calculated values converge to a positive constant (1.0757 × 10 -8^ ) – so the estimate of O(n^3 ) is a tight estimate. (Thus, in practice, this algorithm runs in θ(n^3 ) time.)

Example 2

Consider the following table of data obtained from running an instance of an algorithm assumed to be quadratic. Decide if the Big-Oh estimate, O(N^2 ) is accurate.

Run N T(N) 1 100 0.00012 ms 2 1000 0.03389 ms 3 10000 10.6478 ms 4 100000 2970.0177 ms 5 1000000 938521.971 ms

T(N)/F(N) = 0.00012/(100 * 100) = 1.6 × 10 - T(N)/F(N) = 0.03389/(1000 * 1000) = 3.389 × 10 - T(N)/F(N) = 10.6478/(10000 * 10000) = 1.064 × 10 - T(N)/F(N) = 2970.0177/(100000 * 100000) = 2.970 × 10 - T(N)/F(N) = 938521.971/(1000000 * 1000000) = 9.385 × 10 -

The values diverge, so the estimate of O(n^2 ) is an underestimate.

Limitations of Big-Oh Notation

  1. not useful for small sizes of input sets
  2. omission of the constants can be misleading – example 2NlogN and 1000N, even though its growth rate is larger the first function is probably better. Constants also reflect things like memory access and disk access.
  3. assumes an infinite amount of memory – not trivial when using large data sets
  4. accurate analysis relies on clever observations to optimize the algorithm.

1.09x10^12 ). Given our conditions this function will require about 18325 minutes (12.7 days) to compute. If n is increased to 50 the time required will increase to about 35.7 years. If n increases to 60 the time increases to 36558 years and if n increases to 100 a total of 4x10 16 years will be needed!

Suppose that an algorithm takes T(N) time to run for a problem of size N – the question becomes – how long will it take to solve a larger problem? As an example, assume that the algorithm is an O(N^3 ) algorithm. This implies:

T(N) = cN 3.

If we increase the size of the problem by a factor of 10 we have: T(10N) = c(10N)^3. This gives us: T(10N) = 1000cN 3 = 1000T(N) (since T(N) = cN^3 )

Therefore, the running time of a cubic algorithm will increase by a factor of 1000 if the size of the problem is increased by a factor of

  1. Similarly, increasing the problem size by another factor of 10 (increasing N to 100) will result in another 1000 fold increase in the running time of the algorithm (from 1000 to 1× 10 6 ).

T(100N) = c(100N)^3 = 1× 106 cN 3 = 1× 10 6 T(N)

A similar argument will hold for quadratic and linear algorithms, but a slightly different approach is required for logarithmic algorithms. These are shown below.

For a quadratic algorithm, we have T(N) = cN^2. This implies: T(10N) = c(10N)^2. Expanding produces the form: T(10N) = 100cN^2 = 100T(N). Therefore, when the input size increases by a factor of 10 the running time of the quadratic algorithm will increase by a factor of 100.

For a linear algorithm, we have T(N) = cN. This implies: T(10N) = c(10N). Expanding produces the form: T(10N) = 10cN = 10T(N). Therefore, when the input size increases by a factor of 10 the

running time of the linear algorithm will increase by the same factor of 10.

In general, an f -fold increase in input size will yield an f 3 -fold increase in the running time of a cubic algorithm, an f 2 -fold increase in the running time of a quadratic algorithm, and an f -fold increase in the running time of a linear algorithm.

The analysis for the linear, quadratic, cubic (and in general polynomial) algorithms does not work when in the presence of logarithmic terms. When an O(N logN) algorithm experiences a 10- fold increase in input size, the running time increases by a factor which is only slightly larger than 10. For example, increasing the input by a factor of 10 for an O(N logN) algorithm produces: T(10N) = c(10N) log(10N). Expanding this yields: T(10N) = 10cN log(10N) = 10cN log10 + 10cN logN = 10T(N) + c′N (where c′ = 10clog10). As N gets very large, the ratio T(10N)/T(N) gets closer to 10 (since c′N/T(N) ≈ (10 log10)/logN gets smaller and smaller as N increases.

The above analysis implies, for a logarithmic algorithm, if the algorithm is competitive with a linear algorithm for a sufficiently large value of N, it will remain so for slightly larger N.

Logarithms Revisited

Observation: Logarithms grow slowly. 210 = 1024 (log 2 1024 = 10).

2 20 = 1048576 ≈ 1x10 6 (log 2 1x10^6 ≈ 20). 2 30 = 1073741824 ≈ 1x10 9 (log 2 1x10^9 ≈ 30). This means that the performance of an O(N log N) algorithm is much closer to that of an O(N) algorithm than an O(N^2 ) algorithm, even for moderately large amounts of input.

Ceiling Function: N is the ceiling function and represents the smallest integer that is at least as large as N.

Floor Function: N is the floor function and represents the largest integer that is at least as small as N.