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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 5.5 Heaps <strong>and</strong> Priority Queues 177takes for the node being sifted to reach the bottom of the tree. In any complete tree,approxim<strong>at</strong>ely half of the nodes are leaves <strong>and</strong> so cannot be moved downward <strong>at</strong>all. One quarter of the nodes are one level above the leaves, <strong>and</strong> so their elementscan move down <strong>at</strong> most one level. At each step up the tree we get half the number ofnodes as were <strong>at</strong> the previous level, <strong>and</strong> an additional height of one. The maximumsum of total distances th<strong>at</strong> elements can go is thereforelog n∑(i − 1) n 2 i = n 2i=1log n∑i=1i − 12 i−1 .From Equ<strong>at</strong>ion 2.9 we know th<strong>at</strong> this summ<strong>at</strong>ion has a closed-form solution ofapproxim<strong>at</strong>ely 2, so this algorithm takes Θ(n) time in the worst case. This is farbetter than building the heap one element <strong>at</strong> a time, which would cost Θ(n log n)in the worst case. It is also faster than the Θ(n log n) average-case time <strong>and</strong> Θ(n 2 )worst-case time required to build the BST.Removing the maximum (root) value from a heap containing n elements requiresth<strong>at</strong> we maintain the complete binary tree shape, <strong>and</strong> th<strong>at</strong> the remainingn − 1 node values conform to the heap property. We can maintain the proper shapeby moving the element in the last position in the heap (the current last element inthe array) to the root position. We now consider the heap to be one element smaller.Unfortun<strong>at</strong>ely, the new root value is probably not the maximum value in the newheap. This problem is easily solved by using siftdown to reorder the heap. Becausethe heap is log n levels deep, the cost of deleting the maximum element isΘ(log n) in the average <strong>and</strong> worst cases.The heap is a n<strong>at</strong>ural implement<strong>at</strong>ion for the priority queue discussed <strong>at</strong> thebeginning of this section. Jobs can be added to the heap (using their priority valueas the ordering key) when needed. Method removemax can be called whenever anew job is to be executed.Some applic<strong>at</strong>ions of priority queues require the ability to change the priority ofan object already stored in the queue. This might require th<strong>at</strong> the object’s positionin the heap represent<strong>at</strong>ion be upd<strong>at</strong>ed. Unfortun<strong>at</strong>ely, a max-heap is not efficientwhen searching for an arbitrary value; it is only good for finding the maximumvalue. However, if we already know the index for an object within the heap, it isa simple m<strong>at</strong>ter to upd<strong>at</strong>e its priority (including changing its position to maintainthe heap property) or remove it. The remove method takes as input the positionof the node to be removed from the heap. A typical implement<strong>at</strong>ion for priorityqueues requiring upd<strong>at</strong>ing of priorities will need to use an auxiliary d<strong>at</strong>a structureth<strong>at</strong> supports efficient search for objects (such as a BST). Records in the auxiliaryd<strong>at</strong>a structure will store the object’s heap index, so th<strong>at</strong> the object can be deletedfrom the heap <strong>and</strong> reinserted with its new priority (see Project 5.5). Sections 11.4.1<strong>and</strong> 11.5.1 present applic<strong>at</strong>ions for a priority queue with priority upd<strong>at</strong>ing.

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

Saved successfully!

Ooh no, something went wrong!