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> 69fast as T(n) (the running time of our algorithm) for the worst-case input, we wouldsay the algorithm is “in O(n 2 ) in the worst case.”The following is a precise definition for an upper bound. T(n) represents thetrue running time of the algorithm. f(n) is some expression for the upper bound.For T(n) a non-negatively valued function, T(n) is in set O(f(n))if there exist two positive constants c <strong>and</strong> n 0 such that T(n) ≤ cf(n)for all n > n 0 .Constant n 0 is the smallest value of n for which the claim of an upper bound holdstrue. Usually n 0 is small, such as 1, but does not need <strong>to</strong> be. You must also beable <strong>to</strong> pick some constant c, but it is irrelevant what the value for c actually is.In other words, the definition says that for all inputs of the type in question (suchas the worst case for all inputs of size n) that are large enough (i.e., n > n 0 ), thealgorithm always executes in less than cf(n) steps for some constant c.Example 3.4 Consider the sequential search algorithm for finding a specifiedvalue in an array of integers. If visiting <strong>and</strong> examining one value inthe array requires c s steps where c s is a positive number, <strong>and</strong> if the valuewe search for has equal probability of appearing in any position in the array,then in the average case T(n) = c s n/2. For all values of n > 1,c s n/2 ≤ c s n. Therefore, by the definition, T(n) is in O(n) for n 0 = 1 <strong>and</strong>c = c s .Example 3.5 For a particular algorithm, T(n) = c 1 n 2 + c 2 n in the averagecase where c 1 <strong>and</strong> c 2 are positive numbers. Then, c 1 n 2 + c 2 n ≤c 1 n 2 + c 2 n 2 ≤ (c 1 + c 2 )n 2 for all n > 1. So, T(n) ≤ cn 2 for c = c 1 + c 2 ,<strong>and</strong> n 0 = 1. Therefore, T(n) is in O(n 2 ) by the definition.Example 3.6 Assigning the value from the first position of an array <strong>to</strong>a variable takes constant time regardless of the size of the array. Thus,T(n) = c (for the best, worst, <strong>and</strong> average cases). We could say in thiscase that T(n) is in O(c). However, it is traditional <strong>to</strong> say that an algorithmwhose running time has a constant upper bound is in O(1).Just knowing that something is in O(f(n)) says only how bad things can get.Perhaps things are not nearly so bad. Because we know sequential search is inO(n) in the worst case, it is also true <strong>to</strong> say that sequential search is in O(n 2 ). But

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

Saved successfully!

Ooh no, something went wrong!