Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 201 Heaps 201 Goodrich ad Tamassia xkcd. http://xkcd.com/83/. Tree. Used with permissio uder Creative Commos 2. Licese. Heaps 1
Recall Priority Queue Operatios A priority ueue stores a collectio of etries Each etry is a pair (key, value) Mai methods of the Priority Queue ADT isert(k, v) iserts a etry with key k ad value v Additioal methods mi() returs, but does ot remove, a etry with smallest key size(), isempty() Applicatios: Stadby flyers Auctios removemi() removes ad returs the etry with smallest key Stock market eigies 201 Goodrich ad Tamassia Heaps 2
Recall PQ Sortig We use a priority ueue Isert the elemets with a series of isert operatios Remove the elemets i sorted order with a series of removemi operatios The ruig time depeds o the priority ueue implemetatio: Usorted seuece gives selectio-sort: O( 2 ) time Sorted seuece gives isertio-sort: O( 2 ) time Ca we do better? 201 Goodrich ad Tamassia Heaps 3
Heaps A heap is a biary tree storig keys at its odes ad satisfyig the followig properties: Heap-Order: for every iteral ode v other tha the root, key(v) key(paret(v)) The last ode of a heap is the rightmost ode of maximum depth 2 Complete Biary Tree: let h be the height of the heap for i = 0,, h - 1, there are 2 i odes of depth i at depth h - 1, the iteral odes are to the left of the exteral odes last ode 201 Goodrich ad Tamassia Heaps 4
Height of a Heap Theorem: A heap storig keys has height O(log ) Proof: (we apply the complete biary tree property) Let h be the height of a heap storig keys Sice there are 2 i keys at depth i = 0,, h - 1 ad at least oe key at depth h, we have 1 + 2 + 4 + + 2 h-1 + 1 Thus, 2 h, i.e., h log depth 0 1 h-1 h keys 1 2 2 h-1 1 201 Goodrich ad Tamassia Heaps
Heaps ad Priority Queues We ca use a heap to implemet a priority ueue We store a (key, elemet) item at each iteral ode We keep track of the positio of the last ode (2, Sue) (, Pat) (, Mark) (, Jeff) (, Aa) 201 Goodrich ad Tamassia Heaps
Array-based Heap Implemetatio We ca represet a heap with keys by meas of a array of legth For the ode at rak i the left child is at rak 2i the right child is at rak 2i + 1 2 Liks betwee odes are ot explicitly stored Operatio add correspods to isertig at rak + 1 Operatio remove_mi correspods to removig at rak Yields i-place heap-sort 2 1 2 3 4 201 Goodrich ad Tamassia Heaps
Isertio ito a Heap Method isertitem of the priority ueue ADT correspods to the isertio of a key k to the heap The isertio algorithm cosists of three steps Fid the isertio ode z (the ew last ode) Store k at z Restore the heap-order property (discussed ext) 2 2 1 z isertio ode z 201 Goodrich ad Tamassia Heaps 8
Upheap After the isertio of a ew key k, the heap-order property may be violated Algorithm upheap restores the heap-order property by swappig k alog a upward path from the isertio ode Upheap termiates whe the key k reaches the root or a ode whose paret has a key smaller tha or eual to k Sice a heap has height O(log ), upheap rus i O(log ) time 2 1 z 1 z 2 201 Goodrich ad Tamassia Heaps
Isertio Pseudo-Code Assumes a array-based heap implemetatio. 201 Goodrich ad Tamassia Heaps 10
Removal from a Heap Method removemi of the priority ueue ADT correspods to the removal of the root key from the heap The removal algorithm cosists of three steps Replace the root key with the key of the last ode w Remove w Restore the heap-order property (discussed ext) w w 2 ew last ode last ode 201 Goodrich ad Tamassia Heaps 11
Dowheap After replacig the root key with the key k of the last ode, the heap-order property may be violated Algorithm dowheap restores the heap-order property by swappig key k alog a dowward path from the root Upheap termiates whe key k reaches a leaf or a ode whose childre have keys greater tha or eual to k Sice a heap has height O(log ), dowheap rus i O(log ) time w w 201 Goodrich ad Tamassia Heaps 12
RemoveMi Pseudo-code Assumes heap is implemeted with a array. 201 Goodrich ad Tamassia Heaps 13
Performace of a Heap A heap has the followig performace for the priority ueue operatios. The above aalysis is based o the followig facts: The height of heap T is O(log ), sice T is complete. I the worst case, up-heap ad dow-heap bubblig take time proportioal to the height of T. Fidig the isertio positio i the executio of isert ad updatig the last ode positio i the executio of removemi takes costat time. The heap T has iteral odes, each storig a referece to a key ad a referece to a elemet. 201 Goodrich ad Tamassia Heaps 14
Heap-Sort Cosider a priority ueue with items implemeted by meas of a heap the space used is O() methods isert ad removemi take O(log ) time methods size, isempty, ad mi take time O(1) time Usig a heap-based priority ueue, we ca sort a seuece of elemets i O( log ) time The resultig algorithm is called heap-sort Heap-sort is much faster tha uadratic sortig algorithms, such as isertio-sort ad selectio-sort 201 Goodrich ad Tamassia Heaps 1
Mergig Two Heaps We are give two two heaps ad a key k 8 3 4 2 We create a ew heap with the root ode storig k ad with the two heaps as subtrees We perform dowheap to restore the heaporder property 8 3 2 4 2 3 4 8 201 Goodrich ad Tamassia Heaps 1
Bottom-up Heap Costructio We ca costruct a heap storig give keys i usig a bottom-up costructio with log phases I phase i, pairs of heaps with 2 i -1 keys are merged ito heaps with 2 i+1-1 keys 2 i -1 2 i -1 2 i+1-1 201 Goodrich ad Tamassia Heaps 1
Example 1 1 4 12 23 20 2 11 2 1 1 4 12 23 20 201 Goodrich ad Tamassia Heaps 18
Example (cotd.) 2 11 2 1 1 4 12 23 20 1 4 23 1 2 12 11 2 20 201 Goodrich ad Tamassia Heaps 1
Example (cotd.) 8 1 4 23 1 2 12 11 2 20 4 1 8 23 1 2 12 11 2 20 201 Goodrich ad Tamassia Heaps 20
Example (ed) 10 4 1 8 23 1 2 12 11 2 20 4 1 8 23 1 2 10 12 11 2 20 201 Goodrich ad Tamassia Heaps 21
Aalysis We visualize the worst-case time of a dowheap with a proxy path that goes first right ad the repeatedly goes left util the bottom of the heap (this path may differ from the actual dowheap path) Sice each ode is traversed by at most two proxy paths, the total umber of odes of the proxy paths is O() Thus, bottom-up heap costructio rus i O() time Bottom-up heap costructio is faster tha successive isertios ad speeds up the first phase of heap-sort, which takes O( log ) time i its secod phase. 201 Goodrich ad Tamassia Heaps 22