CS 312: Algorithm Design & Analysis Lecture #2: Asymptotic Notation This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.Creative Commons Attribution-Share Alike 3.0 Unported License Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick
Pop Quiz 1.What is the reward for submitting project reports early? 2.How many penalty-free late days are in your budget for the semester? 3.How many penalty-free late days can you use on any one project? 4.What is the penalty for late submission of project reports (after you’ve used your penalty-free late days)? 5.What is the time of day for project deadlines? 6.T/F: You may submit late regular homework assignments. 7.T/F: You can do a whiteboard experience alone.
Announcements TA and instructor office hours are on the course wiki Project #1 Due dates on the schedule Instructions linked from the online schedule We’ll cover the mathematical ideas in class Help session with TA Purpose of help sessions HW #0 C# surprises?
Objectives Revisit orders of growth Formally introduce asymptotic notation: O, Classify functions in asymptotic orders of growth
Orders of Growth
nlog 2 nnnlog 2 nn2n2 n3n * * * * * *
Orders of Growth nlog 2 nnnlog 2 nn2n2 n3n3 2n2n n!n! * ~10 3 ~3.6* * ~1.3*10 30 ~9* * …… * * * Efficient
Kinds of Efficiency Need to decide which instance of a given size to use as the representative for that class: Algorithm Domain Instance size Instances Average Case (over all possible instances) Worst Case Best Case
Asymptotic Notation
Definitions: given an asymptotically non-negative fn. g(n),
Notational Convention
Asymptotic Notation f(n) c g(n) n0n0 f(n) c g(n) n0n0 n0n0 c 1 g(n) c 2 g(n) f(n)
Cases vs. Orders
Example Show that Must find positive constants c 1, c 2, n 0 such that Divide through by n 2 to get This proof is constructive
Another Example
Other proof types? Induction Optional example on HW #1 Deduction Others!
Duality
g(n) in O(f(n)) means all instances of size n are solvable within c 1 f(n) time. f(n) in Ω(g(n)) means at least one instance of size n is solvable in c 2 g(n) time. … for worst case performance.
Duality Suppose t(n) models worst case behavior of an algorithm. Then t(n) = O(n!) means every instance of size n is solvable in factorial time. Then t(n) = (n!) means at least one instance of size n requires factorial time. But infinitely many size-n instances may require n 2 time! Using our definition of , a bound on the worst case isn’t useful. 1. Change the definition. But then duality fails. 2. Live with it. That’s what we’ll do. Similarly, using duality we also conclude that a O bound on a best case isn’t useful!
The Limit Rule
Example
Useful Identity: L’Hopital’s Rule
Review: Log Identities
Review: More Logarithms
Assignment HW #1: 0.1 (a-e, g, m) in the textbook Problems like these will appear on the mid-term and final exams Justify your answers and show your work Remember: Don’t spend more than 2 focused hours on the homework exercises. If you reach 2 hours of concentrated effort, stop and make a note to that effect on your paper (unless you’re having fun and want to continue). The TAs will take that into consideration when grading.
The following slides are extra
Addition
Multiplication
Classic Multiplication 5001 x x American Style English Style O(n 2 ) for 2 n-digit numbers
Multiplication a la Francais / Russe
Mulitplication a la Francais / Russe function multiply(x,y) Input: Two n-bit integers x and y, where y 0 Output: Their product if y = 0: return 0 if y = 1: return x z = multiply(x, floor(y/2)) if y is even: return 2z else: return x+2z
Mulitplication a la Francais / Russe function multiply(x,y) Input: Two n-bit integers x and y, where y 0 Output: Their product if y = 0: return 0 if y = 1: return x z = multiply(x, floor(y/2)) if y is even: return 2z else: return x+2z
Mulitplication a la Francais / Russe function multiply(x,y) Input: Two n-bit integers x and y, where y>=0 Output: Their product if y = 0: return 0 if y = 1: return x z = multiply(x, floor(y/2)) if y is even: return 2z else: return x+2z
Multiplication a la russe Don’t need to know multiplication tables Just need to know how to double, halve, and add!
Multiplication a la russe x = = O(n 2 )
Arabic Multiplication
Division function divide(x,y) Input: Two n-bit integers x and y, where y 1 Output: The quotient and remainder of x divided by y if x=0: return (q, r) = (0,0) (q, r) = divide(floor(x/2),y) q=2*q, r=2*r if x is odd: r=r+1 if r y: r=r-y, q=q+1 return (q, r)
Division function divide(x,y) Input: Two n-bit integers x and y, where y 1 Output: The quotient and remainder of x divided by y if x=0: return (q, r) = (0,0) (q, r) = divide(floor(x/2),y) q=2*q, r=2*r if x is odd: r=r+1 if r y: r=r-y, q=q+1 return (q, r)