Download Asymptotic Notation and Infinity in Computer Science and more Study notes Computer Science in PDF only on Docsity! Lecture 12: 5/12/2009 Announcements: Ps7 out today. Ps6, Midterm solutions out soon. Midterm info on Web page. Midterm back, 12 Asymptotics (review) O(g) = {f: \N -> \R: \exists C, N s.t. f(n) <= C g(n) for all n>= N} Θ(g) = {f: \N -> \R: \exists c,C N s.t. c g(n) <= f(n) <= C g(n) for all n>= N} Example: True/False: If f is Θ(n^2) then f is O(n^2) TRUE n! = O(2^n) NO n! = O(n^n) YES (Truth: n! = Θ((n/e)^n sqrt(n))) Claim: H_n = 1/1 + 1/2 + ... + 1/n = O(lg n) Draw picture. Upperbound by 1 + integral_1^n (1/x)dx = 1 + ln(n) = O(lg n) show Could I write O(log n) Sure; O(log n) = O(lg n) (following not done in class): Compute the asymptotic running time of the following algorithm: Given: a formula phi, decide if phi is satisfiable by trying all possible truth assignments. Suppose phi has n variables and takes m bits to write down. Answer: Need to try 2^n t.a. How long to try teach? O(m). All together? O(m 2^n). One reason asymptotic notation handy: O(n^2) + O(n^2) = O(n^2) O(n^2) + O(n^3) = O(n^3) O(n log n) + O(n) = O(n log n) etc. A reason we use asymptotic notation: model independence How long does the following code take to run, for searching for x in an array A: for i=1 to n do (If (A[i]=x) Return(i); Return “not found” Takes at most c1*n + c2 where c1 is the work to do one iteration of the for loop, and c2 is the max time for the final return. The total time is O(n). Faster algorithms can do this in O(logn) time or even O(1) if we can preprocess the array A. How long to sort by selection sort? (repeatedly find the smallest remaining element) O(n^2) Already enough to distinguish from more sophisticated algorithms. ------------------------------------------------ 2. How big is that infinity? (3.7 in book) ------------------------------------------------ Puzzle: Which are there more of: natural number or integers? Mathematicians answer: they are the same. Not because they are both infinite, but because there is a bijective function f: N -> \Z Recall e.g, f(x) = \lceil x/2 \rceil if x is odd -x/2 +1 if x is even Is there a bijective function from \Z to N