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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/22/2010 for the course EECS COP 3502 taught by Professor Guha during the Spring '09 term at University of Central Florida.

Page1 / 11

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online