11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 3.4 Asymptotic <strong>Analysis</strong> 633.4 Asymptotic <strong>Analysis</strong>Despite the larger constant for the curve labeled 10n in Figure 3.1, 2n 2 crossesit <strong>at</strong> the rel<strong>at</strong>ively small value of n = 5. Wh<strong>at</strong> if we double the value of theconstant in front of the linear equ<strong>at</strong>ion? As shown in the graph, 20n is surpassedby 2n 2 once n = 10. The additional factor of two for the linear growth r<strong>at</strong>e doesnot much m<strong>at</strong>ter. It only doubles the x-coordin<strong>at</strong>e for the intersection point. Ingeneral, changes to a constant factor in either equ<strong>at</strong>ion only shift where the twocurves cross, not whether the two curves cross.When you buy a faster computer or a faster compiler, the new problem sizeth<strong>at</strong> can be run in a given amount of time for a given growth r<strong>at</strong>e is larger by thesame factor, regardless of the constant on the running-time equ<strong>at</strong>ion. The timecurves for two algorithms with different growth r<strong>at</strong>es still cross, regardless of theirrunning-time equ<strong>at</strong>ion constants. For these reasons, we usually ignore the constantswhen we want an estim<strong>at</strong>e of the growth r<strong>at</strong>e for the running time or otherresource requirements of an algorithm. This simplifies the analysis <strong>and</strong> keeps usthinking about the most important aspect: the growth r<strong>at</strong>e. This is called asymptoticalgorithm analysis. To be precise, asymptotic analysis refers to the study ofan algorithm as the input size “gets big” or reaches a limit (in the calculus sense).However, it has proved to be so useful to ignore all constant factors th<strong>at</strong> asymptoticanalysis is used for most algorithm comparisons.It is not always reasonable to ignore the constants. When comparing algorithmsmeant to run on small values of n, the constant can have a large effect. For example,if the problem is to sort a collection of exactly five records, then an algorithmdesigned for sorting thous<strong>and</strong>s of records is probably not appropri<strong>at</strong>e, even if itsasymptotic analysis indic<strong>at</strong>es good performance. There are rare cases where theconstants for two algorithms under comparison can differ by a factor of 1000 ormore, making the one with lower growth r<strong>at</strong>e impractical for most purposes due toits large constant. Asymptotic analysis is a form of “back of the envelope” estim<strong>at</strong>ionfor algorithm resource consumption. It provides a simplified model of therunning time or other resource needs of an algorithm. This simplific<strong>at</strong>ion usuallyhelps you underst<strong>and</strong> the behavior of your algorithms. Just be aware of the limit<strong>at</strong>ionsto asymptotic analysis in the rare situ<strong>at</strong>ion where the constant is important.3.4.1 Upper BoundsSeveral terms are used to describe the running-time equ<strong>at</strong>ion for an algorithm.These terms — <strong>and</strong> their associ<strong>at</strong>ed symbols — indic<strong>at</strong>e precisely wh<strong>at</strong> aspect ofthe algorithm’s behavior is being described. One is the upper bound for the growthof the algorithm’s running time. It indic<strong>at</strong>es the upper or highest growth r<strong>at</strong>e th<strong>at</strong>the algorithm can have.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!