# Week 12 - Wednesday. ï‚ What did we talk about last time? ï‚ Asymptotic notation

• View
232

0

Embed Size (px)

### Text of Week 12 - Wednesday. ï‚ What did we talk about last time? ï‚ Asymptotic...

CS121

CS322Week 12 - WednesdayLast timeWhat did we talk about last time?Asymptotic notation

Questions?Logical warmupA businesswoman has two cubes on her deskEvery day she arranges both cubes so that the front faces show the current day of the monthWhat numbers do you need on the faces of the cubes to allow this?Note: Both cubes must be used for every day3Asymptotic NotationsProving boundsProve a bound for g(x) = (1/4)(x 1)(x + 1) for x R

Prove that x2 is not O(x)Hint: Proof by contradictionPolynomialsLet f(x) be a polynomial with degree nf(x) = anxn + an-1xn-1 + an-2xn-2 + a1x + a0By extension from the previous results, if an is a positive real, thenf(x) is O(xs) for all integers s nf(x) is (xr) for all integers r nf(x) is (xn)Furthermore, let g(x) be a polynomial with degree mg(x) = bmxm + bm-1xm-1 + bm-2xm-2 + b1x + b0If an and bm are positive reals, thenf(x)/g(x) is O(xc) for real numbers c > n - mf(x)/g(x) is not O(xc) for real numbers c < n - mf(x)/g(x) is (xn - m)

Algorithm EfficiencyExtending notation to algorithmsWe can easily extend our -, O-, and - notations to analyzing the running time of algorithmsImagine that an algorithm A is composed of some number of elementary operations (usually arithmetic, storing variables, etc.)We can imagine that the running time is tied purely to the number of operationsThis is, of course, a lieNot all operations take the same amount of timeEven the same operation takes different amounts of time depending on caching, disk access, etc.

Running timeFirst, assume that the number of operations performed by A on input size n is dependent only on n, not the values of the dataIf f(n) is (g(n)), we say that A is (g(n)) or that A is of order g(n)If the number of operations depends not only on n but also on the values of the dataLet b(n) be the minimum number of operations where b(n) is (g(n)), then we say that in the best case, A is (g(n)) or that A has a best case order of g(n)Let w(n) be the maximum number of operations where w(n) is (g(n)), then we say that in the worst case, A is (g(n)) or that A has a worst case order of g(n)

TimeApproximate Time for f(n) Operations Assuming One Operation Per Nanosecondf(n)n = 10n = 1,000n = 100,000n = 10,000,000log2 n3.3 x 10-9 s10-8 s1.7 x 10-8 s2.3 x 10-8 sn10-8 s10-6 s0.0001 s0.01 sn log2 n3.3 x 10-8 s10-5 s0.0017 s0.23 sn210-7 s0.001 s10 s27.8 hoursn310-6 s1 s11.6 days31,668 years2n10-6 s3.4 x 10284 years3.1 x 1030086 years2.9 x 103010283 yearsComputing running timeWith a single for (or other) loop, we simply count the number of operations that must be performed:int p = 0;int x = 2;for( int i = 2; i 1, in which case logb is an increasing functionIf x1 < x2, logb(x1) < logb(x2), for b > 1 and positive x1 and x2Applying logHow many binary digits are needed to represent a number n?We can write n = 2k + ck-12k-1 + c222 + c12 + c0 where ci is either 0 or 1Thus, we need no more than k + 1 digits to represent nWe know that n < 2k + 1Since 2k n < 2k+1, k log2n < k+1The total number of digits we need k + 1 log2n + 1Recurrence relationsConsider the following recurrence relationa1 = 0ak = ak/2 + 1 for integers k 2What do you think its explicit formula is?It turns out that an = log nWe can prove this with strong inductionExponential and logarithmic ordersFor all real numbers b and r with b > 1 and r > 0logb x xr,for sufficiently large xxr bx,for sufficiently large xThese statements are equivalent to saying for all real numbers b and r with b > 1 and r > 0logb x is O(xr)xr O(bx)Important thingsWe don't have time to show these things fullyxk is O(xk logb x)xk logb x is O(xk+1)The most common case you will see of this is:x is O(x log x)x log x is O(x2)In other words, x log x is between linear and quadraticlogb x is (logc x) for all b > 1 and c > 1In other words, logs are equivalent, no matter what base, in asymptotic notation1/2 + 1/3 + + 1/n is (log2 n)

QuizUpcomingNext timeReview for Exam 3Relations, counting, graphs, and treesRemindersStudy for Exam 3Monday in class

Finish Assignment 9Due Friday by midnight

Talk on the Shadow programming languageTonight in E281 at 6pm

Documents
Engineering
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents