Jim Royer
Digression: Priority Queues
CIS 675: Algorithms February 11, 2019
CIS 675: Algorithms ❖ Priority Queues 1
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
Jim Royer
CIS 675: Algorithms February 11, 2019
CIS 675: Algorithms ❖ Priority Queues 1
◮ Priorities = a set with a total ordering, (P, ≺)
◮ Each element has two fields: datum and priority ◮ Priority queue operations: newQueue(.. . ) makes a new priority queue max(pq) returns element with highest priority deleteMax(pq) e ← max(pq); remove e from pq; return e 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
Definition
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 example: For min-heaps: Change the ≤’s to ≥’s etc.
CIS 675: Algorithms ❖ Priority Queues 3
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
procedure sinkDown(pq, ptr)
q ← ptr; ℓ ← ptr.left; r ← ptr.right while (q is not a leaf) do if ℓ = null & q.key < ℓ.key then q ← ℓ if r = null & q.key < r.key then q ← r if q = ptr then return swap ptr.key and q.key ptr ← q
procedure deletemax(pq)
if the heap is empty then error e ← pq.root.data Pick a leaf node p pq.root.key ← p.key pq.root.data ← p.data delete p from the tree sinkDown(pq, pq.root) return e
CIS 675: Algorithms ❖ Priority Queues 5
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 + log2 n.
add: O(log2 n) time increaseKey: O(log2 n) time deletemax: O(log2 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
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
Option 2 (Bottom-up)
procedure buildHeap(int[] a) Put the elements of a into a complete binary tree (in any order) // Each leaf is a subtree with the max-heap property do ptr ← the lowest, rightmost node in the tree whose children are known to be heaps sinkDown(ptr) // Now ptr’s subtree is a max heap while ptr = root return the tree (which now has the max-heap property)
◮ The above has a Θ(n) run time!!! ◮ Key facts:
∞
k=1
1 2k = 1.
∞
k=1
k 2k = 2. ◮ Do a trace, draw the pictures. ◮ The array representation makes the above very easy. See: http://en.wikipedia.org/ wiki/Binary_heap
CIS 675: Algorithms ❖ Priority Queues 8
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(log2 n) time return a
CIS 675: Algorithms ❖ Priority Queues 9
◮ 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
◮ 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
CIS 675: Algorithms ❖ Priority Queues 11
◮ Two passes of merges
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
Operation Binary Fibonacci Pairing Rank-Pairing find-max Θ(1) Θ(1) Θ(1) Θ(1) delete-max Θ(log n) O(log n)a O(log n)a O(log n)a insert O(log n) Θ(1) Θ(1) Θ(1) increase-key O(log n) Θ(1)a
Θ(1)a merge Θ(n) Θ(1) Θ(1) Θ(1)
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