CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Linda Shapiro Winter 2015.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Lecture: Algorithmic complexity
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
HST 952 Computing for Biomedical Scientists Lecture 10.
the fourth iteration of this loop is shown here
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Dan Grossman Spring 2010.
Chapter 10 Algorithm Efficiency
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Dan Grossman Spring 2012.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Asymptotic Notations Iterative Algorithms and their analysis
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Catie Baker Spring 2015.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
CSC – 332 Data Structures Generics Analysis of Algorithms Dr. Curry Guinn.
Introduction to Analysis of Algorithms CS342 S2004.
Time Complexity of Algorithms (Asymptotic Notations)
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 7 Analysis of Algorithms © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Algorithm Complexity L. Grewe 1. Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them?
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
CSE 326: Data Structures Asymptotic Analysis Hannah Tang and Brian Tjaden Summer Quarter 2002.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Tyler Robison Summer
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
AP National Conference, AP CS A and AB: New/Experienced A Tall Order? Mark Stehlik
Analysis of Algorithms
Introduction to Algorithms
CSE373: Data Structures and Algorithms Lecture 3: Asymptotic Analysis
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
Algorithm Analysis (not included in any exams!)
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis
CSE373: Data Structures and Algorithms Lecture 3: Asymptotic Analysis
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Linda Shapiro Winter 2015

Efficiency What does it mean for an algorithm to be efficient? –We primarily care about time (and sometimes space) Is the following a good definition? –“An algorithm is efficient if, when implemented, it runs quickly on real input instances” –What does “quickly” mean? –What constitutes “real input?” –How does the algorithm scale as input size changes? Winter 20152CSE373: Data Structure & Algorithms

Gauging efficiency (performance) Uh, why not just run the program and time it? –Too much variability, not reliable or portable: Hardware: processor(s), memory, etc. OS, Java version, libraries, drivers Other programs running Implementation dependent –Choice of input Testing (inexhaustive) may miss worst-case input Timing does not explain relative timing among inputs (what happens when n doubles in size) Often want to evaluate an algorithm, not an implementation –Even before creating the implementation (“coding it up”) Winter 20153CSE373: Data Structure & Algorithms

Comparing algorithms When is one algorithm (not implementation) better than another? –Various possible answers (clarity, security, …) –But a big one is performance: for sufficiently large inputs, runs in less time (our focus) or less space We will focus on large inputs because probably any algorithm is “plenty good” for small inputs (if n is 10, probably anything is fast) –Time difference really shows up as n grows Answer will be independent of CPU speed, programming language, coding tricks, etc. Answer is general and rigorous, complementary to “coding it up and timing it on some test cases” - Can do analysis before coding! Winter 20154CSE373: Data Structure & Algorithms

We usually care about worst-case running times Has proven reasonable in practice –Provides some guarantees Difficult to find a satisfactory alternative –What about average case? –Difficult to express full range of input –Could we use randomly-generated input? –May learn more about generator than algorithm Winter 20155CSE373: Data Structure & Algorithms

Example Find an integer in a sorted array Winter 20156CSE373: Data Structure & Algorithms // requires array is sorted // returns whether k is in array boolean find(int[]arr, int k){ ??? }

Linear search Find an integer in a sorted array Winter 20157CSE373: Data Structure & Algorithms // requires array is sorted // returns whether k is in array boolean find(int[]arr, int k){ for(int i=0; i < arr.length; ++i) if(arr[i] == k) return true; return false; } Best case? k is in arr[0] c1 steps = O(1) Worst case? k is not in arr c2*(arr.length) = O(arr.length)

Binary search Find an integer in a sorted array –Can also be done non-recursively but “doesn’t matter” here Winter 20158CSE373: Data Structure & Algorithms // requires array is sorted // returns whether k is in array boolean find(int[]arr, int k){ return help(arr,k,0,arr.length); } boolean help(int[]arr, int k, int lo, int hi) { int mid = (hi+lo)/2; // i.e., lo+(hi-lo)/2 if(lo==hi) return false; if(arr[mid]==k) return true; if(arr[mid]< k) return help(arr,k,mid+1,hi); else return help(arr,k,lo,mid); }

Binary search Winter 20159CSE373: Data Structure & Algorithms // requires array is sorted // returns whether k is in array boolean find(int[]arr, int k){ return help(arr,k,0,arr.length); } boolean help(int[]arr, int k, int lo, int hi) { int mid = (hi+lo)/2; if(lo==hi) return false; if(arr[mid]==k) return true; if(arr[mid]< k) return help(arr,k,mid+1,hi); else return help(arr,k,lo,mid); } Best case: c1 steps = O(1) Worst case: T(n) = c2 steps + T(n/2) where n is hi-lo O( log n) where n is array.length Solve recurrence equation to know that…

Solving Recurrence Relations 1.Determine the recurrence relation. What is the base case? –T(n) = c2 + T(n/2)T(1) = c1first eqn. 2.“Expand” the original relation to find an equivalent general expression in terms of the number of expansions. –T(n) = c2 + c2 + T(n/4)2 nd eqn. = c2 + c2 + c2 + T(n/8)3 rd eqn. = … = c2(k) + T(n/(2 k ))kth eqn. 3.Find a closed-form expression by setting the number of expansions to a value (e.g. 1) which reduces the problem to a base case –n/(2 k ) = 1 means n = 2 k means k = log 2 n –So T(n) = c2 log 2 n + T(1) –So T(n) = c2 log 2 n + c1 (get to base case and do it) –So T(n) is O( log n) Winter CSE373: Data Structure & Algorithms

Ignoring constant factors So binary search is O( log n) and linear search is O(n) –But which is faster? Could depend on constant factors –How many assignments, additions, etc. for each n E.g. T(n) = 5,000,000n vs. T(n) = 5n 2 –And could depend on overhead unrelated to n E.g. T(n) = 5,000,000 + log n vs. T(n) = 10 + n But there exists some n 0 such that for all n > n 0 binary search wins Let’s play with a couple plots to get some intuition… Winter CSE373: Data Structure & Algorithms

Example Let’s try to “help” linear search –Run it on a computer 100x as fast (say 2015 model vs. 1994) –Use a new compiler/language that is 3x as fast –Be a clever programmer to eliminate half the work –So doing each iteration is 600x as fast as in binary search Winter CSE373: Data Structure & Algorithms not enough iterations to show it enough iterations to show it

Big-Oh relates functions We use O on a function f(n) (for example n 2 ) to mean the set of functions with asymptotic behavior less than or equal to f(n) So (3n 2 +17) is in O(n 2 ) –3n and n 2 have the same asymptotic behavior Confusingly, we also say/write: –(3n 2 +17) is O(n 2 ) –(3n 2 +17) = O(n 2 ) But we would never say O(n 2 ) = (3n 2 +17) Winter CSE373: Data Structure & Algorithms

Big-O, formally Definition: g(n) is in O( f(n) ) if there exist positive constants c and n 0 such that g(n)  c f(n) for all n  n 0 To show g(n) is in O( f(n) ), pick a c large enough to “cover the constant factors” and n 0 large enough to “cover the lower-order terms” –Example: Let g(n) = 3n and f(n) = n 2 c=5 and n 0 =10 is more than good enough (3*10 2 )+17  5*10 2 so 3n is O(n 2 ) This is “less than or equal to” –So 3n is also O(n 5 ) and O(2 n ) etc. But usually we’re interested in the tightest upper bound. Winter CSE373: Data Structure & Algorithms

Example 1, using formal definition Let g(n) = 1000n and f(n) = n –To prove g(n) is in O(f(n)), find a valid c and n 0 –We can just let c = –That works for any n 0, such as n 0 = 1. –g(n) = 1000n  c f(n) = 1000n for all n  1. Winter CSE373: Data Structure & Algorithms Definition: g(n) is in O( f(n) ) if there exist positive constants c and n 0 such that g(n)  c f(n) for all n  n 0

Example 1’, using formal definition Let g(n) = 1000n and f(n) = n 2 –To prove g(n) is in O(f(n)), find a valid c and n 0 –The “cross-over point” is n=1000 g(n) = 1000*1000 and f(n) = –So we can choose n 0 =1000 and c=1 –Then g(n) = 1000n  c f(n) = 1n 2 for all n  1000 Winter CSE373: Data Structure & Algorithms Definition: g(n) is in O( f(n) ) if there exist positive constants c and n 0 such that g(n)  c f(n) for all n  n 0

Examples 1 and 1’ Which is it? Is g(n) = 1000n called f(n) or f(n 2 )? By definition, it can be either one. We prefer to use the smallest one. Winter CSE373: Data Structure & Algorithms

Example 2, using formal definition Let g(n) = n 4 and f(n) = 2 n –To prove g(n) is in O(f(n)), find a valid c and n 0 –We can choose n 0 =20 and c=1 g(n) = 20 4 vs. f(n) = 1*2 20 –g(n) = n 4  c f(n) = 1*2 n for all n  20 –If I were doing a complexity analysis, would I pick O(2 n )? Winter CSE373: Data Structure & Algorithms Definition: g(n) is in O( f(n) ) if there exist positive constants c and n 0 such that g(n)  c f(n) for all n  n 0

Comparison n n 4 2 n 10 10,000 1, ,0001,048, ,000 1,073,741, ,560, x10 12 Winter CSE373: Data Structure & Algorithms

What’s with the c The constant multiplier c is what allows functions that differ only in their largest coefficient to have the same asymptotic complexity Consider: g(n) = 7n+5 f(n) = n –These have the same asymptotic behavior (linear) So g(n) is in O(f(n)) even through g(n) is always larger The c allows us to provide a coefficient so that g(n)  c f(n) –In this example: To prove g(n) is in O(f(n)), have c = 12, n 0 = 1 (7*1)+5  12*1 Winter CSE373: Data Structure & Algorithms

What you can drop Eliminate coefficients because we don’t have units anyway –3n 2 versus 5n 2 doesn’t mean anything when we have not specified the cost of constant-time operations –Both are O(n 2 ) Eliminate low-order terms because they have vanishingly small impact as n grows –5n n n n n + 1 is ? Do NOT ignore constants that are not multipliers –n 3 is not O(n 2 ) –3 n is not O(2 n ) Winter CSE373: Data Structure & Algorithms

Upper and Lower Bounds Winter CSE373: Data Structure & Algorithms g(x) f1(x) f2(x) x g(x) f1(x) is an upper bound for g(x); f2(x) is a lower bound. g(x)  f1(x) and g(x)  f2(x).

More Asymptotic * Notation Upper bound: O( f(n) ) is the set of all functions asymptotically less than or equal to f(n) –g(n) is in O( f(n) ) if there exist constants c and n 0 such that g(n)  c f(n) for all n  n 0 Lower bound:  ( f(n) ) is the set of all functions asymptotically greater than or equal to f(n) –g(n) is in  ( f(n) ) if there exist constants c and n 0 such that g(n)  c f(n) for all n  n 0 Tight bound:  ( f(n) ) is the set of all functions asymptotically equal to f(n) –g(n) is in  ( f(n) ) if both g(n) is in O( f(n) ) and g(n) is in  ( f(n) ) Winter CSE373: Data Structure & Algorithms *approaching arbitrarily closely

Correct terms, in theory A common error is to say O( f(n) ) when you mean  ( f(n) ) –Since a linear algorithm is also O(n 5 ), it’s tempting to say “this algorithm is exactly O(n)” –That doesn’t mean anything, say it is  (n) –That means that it is not, for example O( log n) Less common notation: –“little-oh”: intersection of “big-Oh” and not “big-Theta” For all c, there exists an n 0 such that…  Example: array sum is O(n) and o(n 2 ) but not o(n) –“little-omega”: intersection of “big-Omega” and not “big-Theta” For all c, there exists an n 0 such that…  Example: array sum is O(n) and  ( log n) but not  (n) Winter CSE373: Data Structure & Algorithms

Explanation array sum is O(n) and o(n 2 ) but not o(n) –n times = O(n) –Also <= cn 2 so O(n 2 ) –Now o(n 2 ) says it is O(n 2 ) and not  (n 2 ), which means it is not  (n 2 ), ie. not >= n 2, so it IS o(n 2 ) –If it were o(n) it would be O(n) which is OK and also not  (n). –But it IS  (n). So it is not o(n) array sum is O(n) and  ( log n) but not  (n) Similar Winter CSE373: Data Structure & Algorithms

What we are analyzing: Complexity The most common thing to do is give an O upper bound to the worst-case running time of an algorithm Example: binary-search algorithm –Common: O( log n) running-time in the worst-case –Less common:  (1) in the best-case (item is in the middle) –Less common (but very good to know): the find-in-sorted- array problem is  ( log n) in the worst-case (lower bound) No algorithm can do better A problem cannot be O(f(n)) since you can always make a slower algorithm Winter CSE373: Data Structure & Algorithms

Other things to analyze Space instead of time –Remember we can often use space to gain time Average case –Sometimes only if you assume something about the probability distribution of inputs –Sometimes uses randomization in the algorithm Will see an example with sorting –Sometimes an amortized guarantee Average time over any sequence of operations Winter CSE373: Data Structure & Algorithms

Summary Analysis can be about: The problem or the algorithm (usually algorithm) Time or space (usually time) –Or power or dollars or … Best-, worst-, or average-case (usually worst) Upper-, lower-, or tight-bound (usually upper or tight) Winter CSE373: Data Structure & Algorithms

Addendum: Timing vs. Big-Oh Summary Big-oh is an essential part of computer science’s mathematical foundation –Examine the algorithm itself, not the implementation –Reason about (even prove) performance as a function of n Timing also has its place –Compare implementations –Focus on data sets you care about (versus worst case) –Determine what the constant factors “really are” Winter CSE373: Data Structure & Algorithms