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.

516 Chap. 15 Lower Boundsfor a <strong>to</strong>tal cost of 2n − 3 comparisons. Is this optimal? That seems doubtful, butlet us now proceed <strong>to</strong> the step of attempting <strong>to</strong> prove a lower bound.Theorem 15.2 The lower bound for finding the second largest value is 2n − 3.Proof: Any element that loses <strong>to</strong> anything other than the maximum cannot besecond. So, the only c<strong>and</strong>idates for second place are those that lost <strong>to</strong> the maximum.Function largest might compare the maximum element <strong>to</strong> n − 1 others. Thus,we might need n − 2 additional comparisons <strong>to</strong> find the second largest. ✷This proof is wrong. It exhibits the necessity fallacy: “Our algorithm doessomething, therefore all algorithms solving the problem must do the same.”This leaves us with our best lower bounds argument at the moment being thatfinding the second largest must cost at least as much as finding the largest, or n−1.Let us take another try at finding a better algorithm by adopting a strategy of divide<strong>and</strong> conquer. What if we break the list in<strong>to</strong> halves, <strong>and</strong> run largest on eachhalf? We can then compare the two winners (we have now used a <strong>to</strong>tal of n − 1comparisons), <strong>and</strong> remove the winner from its half. Another call <strong>to</strong> largest onthe winner’s half yields its second best. A final comparison against the winner ofthe other half gives us the true second place winner. The <strong>to</strong>tal cost is ⌈3n/2⌉ − 2.Is this optimal? What if we break the list in<strong>to</strong> four pieces? The best would be⌈5n/4⌉ − 1. What if we break the list in<strong>to</strong> eight pieces? Then the cost would be⌈9n/8⌉.Pushing this idea <strong>to</strong> its extreme, the only c<strong>and</strong>idates for second place are losers<strong>to</strong> the eventual winner, <strong>and</strong> our goal is <strong>to</strong> have as few of these as possible. So weneed <strong>to</strong> keep track of the set of elements that have lost in direct comparison <strong>to</strong> the(eventual) winner. We also observe that we learn the most from a comparison whenboth competi<strong>to</strong>rs are known <strong>to</strong> be larger than the same number of other values. Sowe would like <strong>to</strong> arrange our comparisons <strong>to</strong> be against “equally strong” competi<strong>to</strong>rs.We can do all of this with a binomial tree. A binomial tree of height m has2 m nodes. Either it is a single node (if m = 0), or else it is two height m − 1 binomialtrees with one tree’s root becoming a child of the other. Figure 15.2 illustrateshow a binomial tree with eight nodes would be constructed.The resulting algorithm is simple in principle: Build the binomial tree for all nelements, <strong>and</strong> then compare the ⌈log n⌉ children of the root <strong>to</strong> find second place.We could s<strong>to</strong>re the binomial tree as an explicit tree structure, <strong>and</strong> easily build it intime linear on the number of comparisons as each comparison requires one link beadded. Because the shape of a binomial tree is heavily constrained, we can alsos<strong>to</strong>re the binomial tree implicitly in an array, much as we do for a heap. Assumethat two trees, each with 2 k nodes, are in the array. The first tree is in positions 1

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

Saved successfully!

Ooh no, something went wrong!