12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

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 Asymp<strong>to</strong>tic <strong>Analysis</strong> 67computer ten times faster yet, then the new computer (100 times faster than theoriginal computer) will only run a problem of size 19. If you had a second programwhose growth rate is 2 n <strong>and</strong> for which the original computer could run a problemof size 1000 in an hour, than a machine ten times faster can run a problem only ofsize 1003 in an hour! Thus, an exponential growth rate is radically different thanthe other growth rates shown in Figure 3.3. The significance of this difference isexplored in Chapter 17.Instead of buying a faster computer, consider what happens if you replace analgorithm whose running time is proportional <strong>to</strong> n 2 with a new algorithm whoserunning time is proportional <strong>to</strong> n log n. In the graph of Figure 3.1, a fixed amount oftime would appear as a horizontal line. If the line for the amount of time available<strong>to</strong> solve your problem is above the point at which the curves for the two growthrates in question meet, then the algorithm whose running time grows less quicklyis faster. An algorithm with running time T(n) = n 2 requires 1024 × 1024 =1, 048, 576 time steps for an input of size n = 1024. An algorithm with runningtime T(n) = n log n requires 1024 × 10 = 10, 240 time steps for an input ofsize n = 1024, which is an improvement of much more than a fac<strong>to</strong>r of ten whencompared <strong>to</strong> the algorithm with running time T(n) = n 2 . Because n 2 > 10n log nwhenever n > 58, if the typical problem size is larger than 58 for this example, thenyou would be much better off changing algorithms instead of buying a computerten times faster. Furthermore, when you do buy a faster computer, an algorithmwith a slower growth rate provides a greater benefit in terms of larger problem sizethat can run in a certain time on the new computer.3.4 Asymp<strong>to</strong>tic <strong>Analysis</strong>Despite the larger constant for the curve labeled 10n in Figure 3.1, the curve labeled2n 2 crosses it at the relatively small value of n = 5. What if we double the valueof the constant in front of the linear equation? As shown in the graph, the curvelabeled 20n is surpassed by the curve labeled 2n 2 once n = 10. The additionalfac<strong>to</strong>r of two for the linear growth rate does not much matter; it only doubles thex-coordinate for the intersection point. In general, changes <strong>to</strong> a constant fac<strong>to</strong>r ineither equation only shift where the two curves cross, not whether the two curvescross.When you buy a faster computer or a faster compiler, the new problem sizethat can be run in a given amount of time for a given growth rate is larger by thesame fac<strong>to</strong>r, regardless of the constant on the running-time equation. The timecurves for two algorithms with different growth rates still cross, regardless of theirrunning-time equation constants. For these reasons, we usually ignore the constants

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

Saved successfully!

Ooh no, something went wrong!