{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

12.AlgorithmAnalysis-07

# 12.AlgorithmAnalysis-07 - Verifying an Algorithmic Analysis...

This preview shows pages 1–4. Sign up to view the full content.

Verifying an Algorithmic Analysis through running actual code Let's assume that T(N) is the experimental running time of a piece of code and we'd like to see if T(N) O(F(N)). One way to do this is by computing T(N)/F(N) for a range of different values for 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. If these values stay relatively constant, then our guess for the running time is good. We have a close upper bound. If these values diverge to infinity, then our run-time is a function BIGGER than F(N). Otherwise, if these values converge to 0, then our run-time is more accurately described by a function smaller than F(N). 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 The calculated values converge to a positive constant (1.0757 × 10 -8 ) – so the estimate of O(n 3 ) is a good estimate. 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 -8 T(N)/F(N) = 0.03389/(1000 * 1000) = 3.389 × 10 -8 T(N)/F(N) = 10.6478/(10000 * 10000) = 1.064 × 10 -7 T(N)/F(N) = 2970.0177/(100000 * 100000) = 2.970 × 10 -7 T(N)/F(N) = 938521.971/(1000000 * 1000000) =9.385 × 10 - 7
The values diverge, so O(n 2 ) is an underestimate. Loop Tracing Example Here is an example from a previous foundation exam: Question: Find the value of x in terms of n after the following code segment below has executed. You may assume that n is a positive even integer. x = 0; for (i = 1; i <= n*(8*n+8); i++) { for (j = n/2; j <=n; j++) { x = x + (n – j); } } First notice that all we are doing is repeatedly adding numbers into x. Furthermore, since the inner loop is NOT dependant on the value of i, we are adding the same value into x for each iteration of the outer loop. Thus, we must first figure out how much is being added into to x each time the entire inner loop runs.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}