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.

Sec. 3.4 Asymptotic <strong>Analysis</strong> 65If someone asked you out of the blue “Who is the best?” your n<strong>at</strong>ural reactionshould be to reply “Best <strong>at</strong> wh<strong>at</strong>?” In the same way, if you are asked “Wh<strong>at</strong> isthe growth r<strong>at</strong>e of this algorithm,” you would need to ask “When? Best case?Average case? Or worst case?” Some algorithms have the same behavior no m<strong>at</strong>terwhich input instance they receive. An example is finding the maximum in an arrayof integers. But for many algorithms, it makes a big difference, such as whensearching an unsorted array for a particular value. So any st<strong>at</strong>ement about theupper bound of an algorithm must be in the context of some class of inputs of sizen. We measure this upper bound nearly always on the best-case, average-case, orworst-case inputs. Thus, we cannot say, “this algorithm has an upper bound toits growth r<strong>at</strong>e of n 2 .” We must say something like, “this algorithm has an upperbound to its growth r<strong>at</strong>e of n 2 in the average case.”Knowing th<strong>at</strong> something is in O(f(n)) says only how bad things can be. Perhapsthings are not nearly so bad. Because sequential search is in O(n) in the worstcase, it is also true to say th<strong>at</strong> sequential search is in O(n 2 ). But sequential searchis practical for large n, in a way th<strong>at</strong> is not true for some other algorithms in O(n 2 ).We always seek to define the running time of an algorithm with the tightest (lowest)possible upper bound. Thus, we prefer to say th<strong>at</strong> sequential search is in O(n).This also explains why the phrase “is in O(f(n))” or the not<strong>at</strong>ion “∈ O(f(n))” isused instead of “is O(f(n))” or “= O(f(n)).” There is no strict equality to the useof big-Oh not<strong>at</strong>ion. O(n) is in O(n 2 ), but O(n 2 ) is not in O(n).3.4.2 Lower BoundsBig-Oh not<strong>at</strong>ion describes an upper bound. In other words, big-Oh not<strong>at</strong>ion st<strong>at</strong>esa claim about the gre<strong>at</strong>est amount of some resource (usually time) th<strong>at</strong> is requiredby an algorithm for some class of inputs of size n (typically the worst such input,the average of all possible inputs, or the best such input).Similar not<strong>at</strong>ion is used to describe the least amount of a resource th<strong>at</strong> an algorithmneeds for some class of input. Like big-Oh not<strong>at</strong>ion, this is a measure of thealgorithm’s growth r<strong>at</strong>e. Like big-Oh not<strong>at</strong>ion, it works for any resource, but wemost often measure the least amount of time required. And again, like big-Oh not<strong>at</strong>ion,we are measuring the resource required for some particular class of inputs:the worst-, average-, or best-case input of size n.The lower bound for an algorithm (or a problem, as explained l<strong>at</strong>er) is denotedby the symbol Ω, pronounced “big-Omega” or just “Omega.” The following definitionfor Ω is symmetric with the definition of big-Oh.For T(n) a non-neg<strong>at</strong>ively valued function, T(n) is in set Ω(g(n))if there exist two positive constants c <strong>and</strong> n 0 such th<strong>at</strong> T(n) ≥ cg(n)for all n > n 0 . 11 An altern<strong>at</strong>e (non-equivalent) definition for Ω is

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

Saved successfully!

Ooh no, something went wrong!