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.

492 Chap. 15 Lower BoundsProof 1: The winner must compare against all other elements, so there must ben − 1 comparisons.✷This proof is clearly wrong, because the winner does not need to explicitly compareagainst all other elements to be recognized. For example, a st<strong>and</strong>ard singleelimin<strong>at</strong>ionplayoff sports tournament requires only n − 1 comparisons, <strong>and</strong> thewinner does not play every opponent. So let’s try again.Proof 2: Only the winner does not lose. There are n − 1 losers. A single comparisongener<strong>at</strong>es (<strong>at</strong> most) one (new) loser. Therefore, there must be n − 1 comparisons.✷This proof is sound. However, it will be useful l<strong>at</strong>er to abstract this by introducingthe concept of posets as we did in Section 15.2.1. We can view the maximumfindingproblem as starting with a poset where there are no known rel<strong>at</strong>ionships, soevery member of the collection is in its own separ<strong>at</strong>e DAG of one element.Proof 2a: To find the largest value, we start with a poset of n DAGs each witha single element, <strong>and</strong> we must build a poset having all elements in one DAG suchth<strong>at</strong> there is one maximum value (<strong>and</strong> by implic<strong>at</strong>ion, n − 1 losers). We wish toconnect the elements of the poset into a single DAG with the minimum number oflinks. This requires <strong>at</strong> least n − 1 links. A comparison provides <strong>at</strong> most one newlink. Thus, a minimum of n − 1 comparisons must be made.✷Wh<strong>at</strong> is the average cost of largest? Because it always does the same numberof comparisons, clearly it must cost n − 1 comparisons. We can also considerthe number of assignments th<strong>at</strong> largest must do. Function largest might doan assignment on any iter<strong>at</strong>ion of the for loop.Because this event does happen, or does not happen, if we are given no inform<strong>at</strong>ionabout distribution we could guess th<strong>at</strong> an assignment is made after each comparisonwith a probability of one half. But this is clearly wrong. In fact, largestdoes an assignment on the ith iter<strong>at</strong>ion if <strong>and</strong> only if A[i] is the biggest of the thefirst i elements. Assuming all permut<strong>at</strong>ions are equally likely, the probability ofthis being true is 1/i. Thus, the average number of assignments done is1 +n∑i=21i =which is the Harmonic Series H n . H n = Θ(log n). More exactly, H n is close tolog e n.How “reliable” is this average? Th<strong>at</strong> is, how much will a given run of theprogram devi<strong>at</strong>e from the mean cost? According to Čebyšev’s Inequality, an observ<strong>at</strong>ionwill fall within two st<strong>and</strong>ard devi<strong>at</strong>ions of the mean <strong>at</strong> least 75% of the time.For Largest, the variance isn∑i=11iH n − π26 = log e n − π26 .

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

Saved successfully!

Ooh no, something went wrong!