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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

68 Chap. 3 <strong>Algorithm</strong> <strong>Analysis</strong>cost function, then any lower bound for g(n) is also a lower bound for your costfunction. Likewise for Θ not<strong>at</strong>ion.The significance of rule (2) is th<strong>at</strong> you can ignore any multiplic<strong>at</strong>ive constantsin your equ<strong>at</strong>ions when using big-Oh not<strong>at</strong>ion. This rule also holds true for Ω <strong>and</strong>Θ not<strong>at</strong>ions.Rule (3) says th<strong>at</strong> given two parts of a program run in sequence (whether twost<strong>at</strong>ements or two sections of code), you need consider only the more expensivepart. This rule applies to Ω <strong>and</strong> Θ not<strong>at</strong>ions as well: For both, you need consideronly the more expensive part.Rule (4) is used to analyze simple loops in programs. If some action is repe<strong>at</strong>edsome number of times, <strong>and</strong> each repetition has the same cost, then the total cost isthe cost of the action multiplied by the number of times th<strong>at</strong> the action takes place.This rule applies to Ω <strong>and</strong> Θ not<strong>at</strong>ions as well.Taking the first three rules collectively, you can ignore all constants <strong>and</strong> alllower-order terms to determine the asymptotic growth r<strong>at</strong>e for any cost function.The advantages <strong>and</strong> dangers of ignoring constants were discussed near the beginningof this section. Ignoring lower-order terms is reasonable when performing anasymptotic analysis. The higher-order terms soon swamp the lower-order terms intheir contribution to the total cost as n becomes larger. Thus, if T(n) = 3n 4 + 5n 2 ,then T(n) is in O(n 4 ). The n 2 term contributes rel<strong>at</strong>ively little to the total cost forlarge n.Throughout the rest of this book, these simplifying rules are used when discussingthe cost for a program or algorithm.3.4.5 Classifying FunctionsGiven functions f(n) <strong>and</strong> g(n) whose growth r<strong>at</strong>es are expressed as algebraic equ<strong>at</strong>ions,we might like to determine if one grows faster than the other. The best wayto do this is to take the limit of the two functions as n grows towards infinity,f(n)limn→∞ g(n) .If the limit goes to ∞, then f(n) is in Ω(g(n)) because f(n) grows faster. If thelimit goes to zero, then f(n) is in O(g(n)) because g(n) grows faster. If the limitgoes to some constant other than zero, then f(n) = Θ(g(n)) because both grow <strong>at</strong>the same r<strong>at</strong>e.Example 3.8 If f(n) = 2n log n <strong>and</strong> g(n) = n 2 , is f(n) in O(g(n)),Ω(g(n)), or Θ(g(n))? Becausen 22n log n =n2 log n ,

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

Saved successfully!

Ooh no, something went wrong!