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.

58 Chap. 3 <strong>Algorithm</strong> <strong>Analysis</strong>n log log n log n n n log n n 2 n 3 2 n16 2 4 2 4 2 · 2 4 = 2 5 2 8 2 12 2 16256 3 8 2 8 8 · 2 8 = 2 11 2 16 2 24 2 2561024 ≈ 3.3 10 2 10 10 · 2 10 ≈ 2 13 2 20 2 30 2 102464K 4 16 2 16 16 · 2 16 = 2 20 2 32 2 48 2 64K1M ≈ 4.3 20 2 20 20 · 2 20 ≈ 2 24 2 40 2 60 2 1M1G ≈ 4.9 30 2 30 30 · 2 30 ≈ 2 35 2 60 2 90 2 1GFigure 3.2 Costs for growth r<strong>at</strong>es represent<strong>at</strong>ive of most computer algorithms.The growth r<strong>at</strong>e for an algorithm is the r<strong>at</strong>e <strong>at</strong> which the cost of the algorithmgrows as the size of its input grows. Figure 3.1 shows a graph for six equ<strong>at</strong>ions, eachmeant to describe the running time for a particular program or algorithm. A varietyof growth r<strong>at</strong>es represent<strong>at</strong>ive of typical algorithms are shown. The two equ<strong>at</strong>ionslabeled 10n <strong>and</strong> 20n are graphed by straight lines. A growth r<strong>at</strong>e of cn (for c anypositive constant) is often referred to as a linear growth r<strong>at</strong>e or running time. Thismeans th<strong>at</strong> as the value of n grows, the running time of the algorithm grows in thesame proportion. Doubling the value of n roughly doubles the running time. Analgorithm whose running-time equ<strong>at</strong>ion has a highest-order term containing a factorof n 2 is said to have a quadr<strong>at</strong>ic growth r<strong>at</strong>e. In Figure 3.1, the line labeled 2n 2represents a quadr<strong>at</strong>ic growth r<strong>at</strong>e. The line labeled 2 n represents an exponentialgrowth r<strong>at</strong>e. This name comes from the fact th<strong>at</strong> n appears in the exponent. Theline labeled n! is also growing exponentially.As you can see from Figure 3.1, the difference between an algorithm whoserunning time has cost T(n) = 10n <strong>and</strong> another with cost T(n) = 2n 2 becomestremendous as n grows. For n > 5, the algorithm with running time T(n) = 2n 2 isalready much slower. This is despite the fact th<strong>at</strong> 10n has a gre<strong>at</strong>er constant factorthan 2n 2 . Comparing the two curves marked 20n <strong>and</strong> 2n 2 shows th<strong>at</strong> changing theconstant factor for one of the equ<strong>at</strong>ions only shifts the point <strong>at</strong> which the two curvescross. For n > 10, the algorithm with cost T(n) = 2n 2 is slower than the algorithmwith cost T(n) = 20n. This graph also shows th<strong>at</strong> the equ<strong>at</strong>ion T(n) = 5n log ngrows somewh<strong>at</strong> more quickly than both T(n) = 10n <strong>and</strong> T(n) = 20n, but notnearly so quickly as the equ<strong>at</strong>ion T(n) = 2n 2 . For constants a, b > 1, n a growsfaster than either log b n or log n b . Finally, algorithms with cost T(n) = 2 n orT(n) = n! are prohibitively expensive for even modest values of n. Note th<strong>at</strong> forconstants a, b ≥ 1, a n grows faster than n b .We can get some further insight into rel<strong>at</strong>ive growth r<strong>at</strong>es for various algorithmsfrom Figure 3.2. Most of the growth r<strong>at</strong>es th<strong>at</strong> appear in typical algorithms areshown, along with some represent<strong>at</strong>ive input sizes. Once again, we see th<strong>at</strong> thegrowth r<strong>at</strong>e has a tremendous effect on the resources consumed by an algorithm.

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

Saved successfully!

Ooh no, something went wrong!