rec18 - CS3110 Recitation 18 Review of Asymptotic...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS3110 Recitation 18 Review of Asymptotic Complexity Asymptotic Analysis Worst-Case and Average-Case Analysis Order of Growth and Big-O Notation Comparing Orders of Growth Binary Search Trees Exercises Asymptotic Analysis When analyzing the running time or space usage of programs, we usually try to estimate the time or space as function of the input size. For example, when analyzing the worst case running time of a function that sorts a list of numbers, we will be concerned with how long it takes as a function of the length of the input list. For example, we say the standard insertion sort takes time T ( n ) where T ( n ) = c*n 2 +k for some constants c and k . In contrast, merge sort takes time T ′( n ) = c *n*log 2 ( n ) + k . The asymptotic behavior of a function f(n) (such as f(n)=c*n or f(n)=c*n 2 , etc.) refers to the growth of f(n) as n gets large. We typically ignore small values of n , since we are usually interested in estimating how slow the program will be on large inputs. A good rule of thumb is: the slower the asymptotic growth rate, the better the algorithm (although this is often not the whole story). By this measure, a linear algorithm ( i.e., f(n)=d*n+k ) is always asymptotically better than a quadratic one ( e.g., f(n)=c*n 2 +q ). That is because for any given (positive) c,k,d , and q there is always some n at which the magnitude of c*n 2 +q overtakes d*n+k . For moderate values of n , the quadratic algorithm could very well take less time than the linear one, for example if c is significantly smaller than d and/or k is significantly smaller than q . However, the linear algorithm will always be better for sufficiently large inputs. Remember to THINK BIG when working with asymptotic rates of growth. Worst-Case and Average-Case Analysis When we say that an algorithm runs in time T(n) , we mean that T(n) is an upper bound on the running time that holds for all inputs of size n . This is called worst-case analysis . The algorithm may very well take less time on some inputs of size n , but it doesn't matter. If an algorithm takes T(n)=c*n 2 +k steps on only a single input of each size n and only n steps on the rest, we still say that it is a quadratic algorithm. A popular alternative to worst-case analysis is average-case analysis . Here we do not bound the worst case running time, but try to calculate the expected time spent on a randomly chosen input. This kind of analysis is generally harder, since it involves probabilistic arguments and often requires assumptions about the distribution of inputs that may be difficult to justify. On the other hand, it can be more useful because sometimes the worst-case behavior of an algorithm is misleadingly bad. A good example of this is the
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
popular quicksort algorithm, whose worst-case running time on an input sequence of length n is proportional to n 2 but whose expected running time is proportional to n log n. Order of Growth and Big-O Notation
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/25/2009 for the course PHYS 2214 at Cornell University (Engineering School).

Page1 / 6

rec18 - CS3110 Recitation 18 Review of Asymptotic...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online