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.

490 Chap. 15 Lower BoundsABCGDEFFigure 15.1 Illustr<strong>at</strong>ion of using a poset to model our current knowledge of therel<strong>at</strong>ionships among a collection of objects. A directed acyclic graph (DAG) isused to draw the poset (assume all edges are directed downward). In this example,our knowledge is such th<strong>at</strong> we don’t know how A or B rel<strong>at</strong>e to any of the otherobjects. However, we know th<strong>at</strong> both C <strong>and</strong> G are gre<strong>at</strong>er than E <strong>and</strong> F. Further,we know th<strong>at</strong> C is gre<strong>at</strong>er than D, <strong>and</strong> th<strong>at</strong> E is gre<strong>at</strong>er than F.comparing elements in L we have <strong>at</strong> least n − m partial orders. Every such partialorder needs <strong>at</strong> least one comparison against K to make sure th<strong>at</strong> K is not somewherein th<strong>at</strong> partial order. Thus, any algorithm must make <strong>at</strong> least n comparisons in theworst case.✷15.2.2 Searching in Sorted ListsWe will now assume th<strong>at</strong> list L is sorted. In this case, is linear search still optimal?Clearly no, but why not? Because we have additional inform<strong>at</strong>ion to work with th<strong>at</strong>we do not have when the list is unsorted. We know th<strong>at</strong> the st<strong>and</strong>ard binary searchalgorithm has a worst case cost of O(log n). Can we do better than this? We canprove th<strong>at</strong> this is the best possible in the worst case with a proof similar to th<strong>at</strong> usedto show the lower bound on sorting.Again we use the decision tree to model our algorithm. Unlike when searchingan unsorted list, comparisons between elements of L tell us nothing new about theirrel<strong>at</strong>ive order, so we consider only comparisons between K <strong>and</strong> an element in L. Atthe root of the decision tree, our knowledge rules out no positions in L, so all arepotential c<strong>and</strong>id<strong>at</strong>es. As we take branches in the decision tree based on the resultof comparing K to an element in L, we gradually rule out potential c<strong>and</strong>id<strong>at</strong>es.Eventually we reach a leaf node in the tree representing the single position in Lth<strong>at</strong> can contain K. There must be <strong>at</strong> least n + 1 nodes in the tree because we haven + 1 distinct positions th<strong>at</strong> K can be in (any position in L, plus not in L <strong>at</strong> all).Some p<strong>at</strong>h in the tree must be <strong>at</strong> least log n levels deep, <strong>and</strong> the deepest node in thetree represents the worst case for th<strong>at</strong> algorithm. Thus, any algorithm on a sortedarray requires <strong>at</strong> least Ω(log n) comparisons in the worst case.We can modify this proof to find the average cost lower bound. Again, wemodel algorithms using decision trees. Except now we are interested not in thedepth of the deepest node (the worst case) <strong>and</strong> therefore the tree with the leastdeepestnode. Instead, we are interested in knowing wh<strong>at</strong> the minimum possible is

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

Saved successfully!

Ooh no, something went wrong!