digression priority queues
play

Digression: Priority Queues CIS 675: Algorithms February 11, 2019 - PowerPoint PPT Presentation

Jim Royer Digression: Priority Queues CIS 675: Algorithms February 11, 2019 CIS 675: Algorithms Priority Queues 1 Priority Queues Priorities = a set with a total ordering, ( P , ) E.g., ( P , ) = ( N , < ) the


  1. Jim Royer Digression: Priority Queues CIS 675: Algorithms February 11, 2019 CIS 675: Algorithms ❖ Priority Queues 1

  2. Priority Queues ◮ Priorities = a set with a total ordering, ( P , ≺ ) ⋆ • E.g., ( P , ≺ ) = ( N , < ) — the bigger the number, the more important • E.g., ( P , ≺ ) = ( N , > ) — the smaller the number, the more important ◮ Each element has two fields: datum and priority ◮ Priority queue operations: newQueue(.. . ) makes a new priority queue max(pq) returns element with highest priority e ← max ( pq ) ; remove e from pq ; return e deleteMax(pq) insert(pq, d, p) add a datum d with priority p increaseKey(pq, d, p) change the priority of datum d to p CIS 675: Algorithms ❖ Priority Queues 2

  3. Priority Queues via Binary Max-Heaps Definition For example: A binary tree has the max-heap property when each node has the max-value in the subtree below it. v values <= v values <= v For min-heaps: Change the ≤ ’s to ≥ ’s etc. CIS 675: Algorithms ❖ Priority Queues 3

  4. Binary Max-Heap: Adding an Element & IncreaseKey procedure bubbleUp ( pq , ptr ) while ( ptr � = pq . root ) & ( ptr ’s key > ptr ’s parent’s key) do swap ptr with its parent ptr ← ptr ’s parent procedure add ( pq , ky ) ptr ← a new leaf node in the tree ptr . key ← ky bubbleUp ( pq , ptr ) procedure increaseKey ( pq , ptr , nk ) ptr . key ← nk bubbleUp ( pq , ptr ) CIS 675: Algorithms ❖ Priority Queues 4

  5. Binary Max-Heap: Deletemax procedure sinkDown ( pq , ptr ) procedure deletemax ( pq ) q ← ptr ; ℓ ← ptr . left ; r ← ptr . right if the heap is empty then error while ( q is not a leaf) do e ← pq . root . data if ℓ � = null & q . key < ℓ . key then q ← ℓ Pick a leaf node p if r � = null & q . key < r . key then q ← r pq . root . key ← p . key if q = ptr then return pq . root . data ← p . data swap ptr . key and q . key delete p from the tree ptr ← q sinkDown ( pq , pq . root ) return e CIS 675: Algorithms ❖ Priority Queues 5

  6. Binary Max-Heap: Complete Binary Trees Definition A complete binary tree is one where all levels are complete — except perhaps for the lowest level which is filled in left to right. Fact: A complete binary tree of n elements is of height ≤ 1 + log 2 n . ∴ If the heap tree is complete, then add: O ( log 2 n ) time increaseKey: O ( log 2 n ) time deletemax: O ( log 2 n ) time In fact: There is a very simple array representation of a max-heap. See the text, or the blackboard, or http://en.wikipedia.org/wiki/Binary_heap CIS 675: Algorithms ❖ Priority Queues 6

  7. Binary Max-Heap: How to build one, 1 Option 1 (Top-down) procedure slowBuildHeap ( int [] a ) pq ← an empty priority queue for i ← 0 to a . length − 1 do add ( pq , a [ i ]) return pq ◮ What is a O ( · ) upper-bound on the runtime of this? ◮ Do a trace. ◮ How can we do better? CIS 675: Algorithms ❖ Priority Queues 7

  8. Binary Max-Heap: How to build one, 2 ◮ The above has a Θ ( n ) run Option 2 (Bottom-up) time!!! procedure buildHeap ( int [] a ) ◮ Key facts: Put the elements of a into a complete binary tree (in any order) ∞ ∞ 1 k ∑ ∑ // Each leaf is a subtree with the max-heap property 2 k = 1. 2 k = 2. do k = 1 k = 1 � the lowest, rightmost node in the ptr ← tree whose children are known to be ◮ Do a trace, draw the pictures. heaps ◮ The array representation sinkDown ( ptr ) makes the above very easy. // Now ptr ’s subtree is a max heap See: while ptr � = root return the tree (which now has the max-heap property) http://en.wikipedia.org/ wiki/Binary_heap CIS 675: Algorithms ❖ Priority Queues 8

  9. Heapsort procedure heapsort(int[] a ) n ← a . length pq ← a max-heap build from the elements of a . // Θ ( n ) time for i ← n − 1 to 0 do // n iterations a [ i ] ← deletemax ( pq ) ; // O ( log 2 n ) time return a CIS 675: Algorithms ❖ Priority Queues 9

  10. Pairing Heaps ◮ Pairing heaps (see: https://en.wikipedia.org/wiki/Pairing_heap ) ◮ Introduced by Michael Fredman, Robert Sedgewick, Daniel Sleator, and Robert Tarjan in 1986. ◮ Pairing heaps are multiway trees with the max-heap (or min-heap) property. 7 6 5 4 6 5 4 3 5 3 2 3 1 2 CIS 675: Algorithms ❖ Priority Queues 10

  11. Pairing Heaps: Operations ◮ find-max return the top element of the heap ◮ merge compare the two root elements, (draw the picture) add the smaller’s heap as a a subtree just below the larger’s root ◮ insert make the new element into a one-element heap (draw the picture) merge the main pairing heap with the new heap ◮ increase-key (optional) deletes & merges (we are skipping this) ◮ delete-max remove the root and somehow ⋆ merge its subtrees ⋆ This is the interesting part. CIS 675: Algorithms ❖ Priority Queues 11

  12. Pairing Heaps: Delete-max, the standard strategy ◮ Two passes of merges • From left-to-right: merge successive pairs to trees • From right-to-left: merge all the trees into one tree pairingMerge :: (list of heaps) → heap pairingMerge [] = an-empty-heap pairingMerge [h] = h pairingMerge (h1:h2:hs) = merge(merge(h1,h2),pairingMerge(hs)) ◮ draw some pictures ◮ Use the visualizations on https://people.ksp.sk/~kuko/gnarley-trees/ CIS 675: Algorithms ❖ Priority Queues 12

  13. Pairing Heaps: Runtimes Operation Binary Fibonacci Pairing Rank-Pairing Θ ( 1 ) Θ ( 1 ) Θ ( 1 ) Θ ( 1 ) find-max O ( log n ) a O ( log n ) a O ( log n ) a Θ ( log n ) delete-max O ( log n ) Θ ( 1 ) Θ ( 1 ) Θ ( 1 ) insert Θ ( 1 ) a o ( log n ) a Θ ( 1 ) a O ( log n ) increase-key Θ ( n ) Θ ( 1 ) Θ ( 1 ) Θ ( 1 ) merge a amortized time Experimental results indicate that in practice: ◮ Often faster than array-based binary heaps and d-ary heaps, ◮ Almost always faster than other pointer-based heaps, including Fibonacci heaps CIS 675: Algorithms ❖ Priority Queues 13

Recommend


More recommend