Big Oh from Measured Times How can you find the Big-Oh from measured running times? How can you check an Algorithm Analysis? What would you expect to find in the measured times for an algorithm that is O(n) (linear)? DEMO (demo1, measure the time for a linear algorithm) How large a problem can you solve with a linear algorithm? What would you expect to find in the measured times for an algorithm that is O(n^2) (quadratic)? DEMO (demo1, measure the time for a quadratic algorithm) How large a problem can you solve with a quadratic algorithm? What would you expect to find in the measured times for an algorithm that is O(n^3) (cubic)? DEMO (demo1, measure the time for a cubic algorithm) How large a problem can you solve with a cubic algorithm? What would you expect to find in the measured times for an algorithm that is O(2^n) (exponential)? DEMO (demo1, measure the time for an exponential algorithm) How large a problem can you solve with an exponential algorithm? What are some of the common Big-Oh functions? Which of the common Big-Oh's are practical for large data sizes? O(1) constant O(log(n)) logarithmic O(n) linear O(n*log(n)) nlogn O(n^2) quadratic O(n^3) cubic O(2^n) exponential Classwork You may work with a partner. The following times were measured when running some programs. What Big-Oh function best describes the running time of each program? size time 1000 259 2000 441 3000 754 4000 1010 size time 100 142 200 1244 400 9577 800 75385 size time 1000 4372 2000 17406 3000 39150 4000 69754 size time 100 46 200 47 300 44 400 47 size time 26 269 28 1077 30 4292 32 17041 Using the Ratio of Measured Time and Big-Oh Given the definition of Big-Oh, what should be true about T(n)/f(n) for large values of n? T(n) <= c*f(n) Consider an algorithm that is O(n^2). Suppose you measure the running time T(n) of the algorithm for large values of n. What should be true about T(n)/(n^2) for large values of n? it should converge to positive constant What should be true about T(n)/(n^3) for large values of n? it should converge to zero What should be true about T(n)/(n) for large values of n? it should diverge (grow larger as n grows larger) DEMO (demo2, convergence to a constant) Using Big-Oh to Estimate Running Time If an algorithm is O(n) and it takes 1 second to solve a problem of size 10, how long will it take to solve a problem of size 1000? If an algorithm is O(n^2) and it takes 1 second to solve a problem of size 10, how long will it take to solve a problem of size 1000? If an algorithm is O(n^3) and it takes 1 second to solve a problem of size 10, how long will it take to solve a problem of size 1000? If an algorithm is O(2^n) and it takes 1 second to solve a problem of size 10, how long will it take to solve a problem of size 1000? If an algorithm is O(1) and it takes 1 second to solve a problem of size 10, how long will it take to solve a problem of size 1000? Classwork You may work together with a partner. If an algorithm is O(n) and it takes 0.5 ms to solve a problem of size 100, how long will it take to solve a problem of size 500? If an algorithm is O(n^2) and it takes 0.5 ms to solve a problem of size 100, how long will it take to solve a problem of size 500? If an algorithm is O(n^3) and it takes 0.5 ms to solve a problem of size 100, how long will it take to solve a problem of size 500? If an algorithm is O(2^n) and it takes 0.5 ms to solve a problem of size 100, how long will it take to solve a problem of size 500? Using Big-Oh to Estimate Problem Size If an algorithm is O(n) and it takes 10 seconds to solve a problem of size 100, how large a problem can be solved in 1000 seconds? If an algorithm is O(n^2) and it takes 10 seconds to solve a problem of size 100, how large a problem can be solved in 1000 seconds? If an algorithm is O(n^3) and it takes 10 seconds to solve a problem of size 100, how large a problem can be solved in 1000 seconds? If an algorithm is O(10^n) and it takes 10 seconds to solve a problem of size 100, how large a problem can be solved in 1000 seconds? Classwork You may work together with a partner. If an algorithm is O(n) and it takes 2 seconds to solve a problem of size 2000, how large a problem can be solved in 16 seconds? If an algorithm is O(n^2) and it takes 2 seconds to solve a problem of size 2000, how large a problem can be solved in 16 seconds? If an algorithm is O(n^3) and it takes 2 seconds to solve a problem of size 2000, how large a problem can be solved in 16 seconds? If an algorithm is O(2^n) and it takes 2 seconds to solve a problem of size 2000, how large a problem can be solved in 16 seconds? Worst-Case vs Average-Case Is it possible for an algorithm to run faster for some inputs than for others? Suppose you sort lists A and B using the same sorting algorithm. Could the algorithm be faster for one of the lists? A = 0 1 2 3 4 9 5 6 7 8 B = 5 9 1 8 0 2 6 4 7 3 What's a Worst-case bound? longest time over all inputs of size N What's an Average-case bound? average time over all inputs of size N