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...

Create successful ePaper yourself

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

Sec. 15.4 Adversarial Lower Bounds Proofs 515how many assignments that largest must do. Function largest might do anassignment on any iteration of the for loop.Because this event does happen, or does not happen, if we are given no informationabout distribution we could guess that 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 iteration if <strong>and</strong> only if A[i] is the biggest of the thefirst i elements. Assuming all permutations 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 <strong>to</strong>log e n.How “reliable” is this average? That is, how much will a given run of theprogram deviate from the mean cost? According <strong>to</strong> Čebyšev’s Inequality, an observationwill fall within two st<strong>and</strong>ard deviations of the mean at least 75% of the time.For Largest, the variance isn∑i=11iH n − π26 = log e n − π26The st<strong>and</strong>ard deviation is thus about √ log e n. So, 75% of the observations arebetween log e n − 2 √ log e n <strong>and</strong> log e n + 2 √ log e n. Is this a narrow spread or awide spread? Compared <strong>to</strong> the mean value, this spread is pretty wide, meaning thatthe number of assignments varies widely from run <strong>to</strong> run of the program.15.4 Adversarial Lower Bounds ProofsOur next problem will be finding the second largest in a collection of objects. Considerwhat happens in a st<strong>and</strong>ard single-elimination <strong>to</strong>urnament. Even if we assumethat the “best” team wins in every game, is the second best the one who loses in thefinals? Not necessarily. We might expect that the second best must lose <strong>to</strong> the best,but they might meet at any time.Let us go through our st<strong>and</strong>ard “algorithm for finding algorithms” by firstproposing an algorithm, then a lower bound, <strong>and</strong> seeing if they match. Unlike ouranalysis for most problems, this time we are going <strong>to</strong> count the exact number ofcomparisons involved <strong>and</strong> attempt <strong>to</strong> minimize them. A simple algorithm for findingthe second largest is <strong>to</strong> first find the maximum (in n − 1 comparisons), discardit, <strong>and</strong> then find the maximum of the remaining elements (in n − 2 comparisons)

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

Saved successfully!

Ooh no, something went wrong!