Priority Queues API elementary implementations binary heaps heapsort event-driven simulation References: Algorithms in Java, Chapter 9 http://www.cs.princeton.edu/introalgsds/34pq 1
API elementary implementations binary heaps heapsort event-driven simulation
2
Priority Queues Data. Items that can be compared. Basic operations. Insert. Remove largest.
• •
• Copy. • Create. • Destroy. • Test if empty.
defining ops
generic ops
3
Priority Queue Applications
• • • • • • • • • •
Event-driven simulation.
[customers in a line, colliding particles]
Numerical computation.
[reducing roundoff error]
Data compression.
[Huffman codes]
Graph searching.
[Dijkstra's algorithm, Prim's algorithm]
Computational number theory.
[sum of powers]
Artificial intelligence.
[A* search]
Statistics.
[maintain largest M values in a sequence]
Operating systems.
[load balancing, interrupt handling]
Discrete optimization.
[bin packing, scheduling]
Spam filtering.
[Bayesian spam filter]
Generalizes: stack, queue, randomized queue.
4
Priority queue client example Problem: Find the largest M of a stream of N elements. Fraud detection: isolate $$ transactions. File maintenance: find biggest files or directories.
• •
Constraint. Not enough memory to store N elements. Solution. Use a priority queue. Operation
time
space
sort
N lg N
N
elementary PQ
MN
M
binary heap
N lg M
M
best in theory
N
M
MinPQ pq = new MinPQ(); while(!StdIn.isEmpty()) { String s = StdIn.readLine(); t = new Transaction(s); pq.insert(t); if (pq.size() > M) pq.delMin(); } while (!pq.isEmpty()) System.out.println(pq.delMin()); 5
API elementary implementations binary heaps heapsort event-driven simulation
6
Priority queue: unordered array implementation
public class UnorderedPQ { private Item[] pq; // pq[i] = ith element on PQ private int N; // number of elements on PQ public UnorderedPQ(int maxN) { pq = (Item[]) new Comparable[maxN]; public boolean isEmpty() { return N == 0; }
}
no generic array creation
public void insert(Item x) { pq[N++] = x; } public Item delMax() { int max = 0; for (int i = 1; i < N; i++) if (less(max, i)) max = i; exch(max, N-1); return pq[--N]; } } 7
Priority queue elementary implementations
Implementation
Insert
Del Max
unordered array
1
N
ordered array
N
1
worst-case asymptotic costs for PQ with N items
Challenge. Implement both operations efficiently.
8
API elementary implementations binary heaps heapsort event-driven simulation
9
Binary Heap Heap: Array representation of a heap-ordered complete binary tree. Binary tree. Empty or Node with links to left and right trees.
• •
10
Binary Heap Heap: Array representation of a heap-ordered complete binary tree. Binary tree. Empty or Node with links to left and right trees.
• •
Heap-ordered binary tree. Keys in nodes. No smaller than children’s keys.
• •
11
Binary Heap Heap: Array representation of a heap-ordered complete binary tree. Binary tree. Empty or Node with links to left and right trees.
• •
Heap-ordered binary tree. Keys in nodes. No smaller than children’s keys.
• •
Array representation. Take nodes in level order. No explicit links needed since tree is complete.
• •
12
Binary Heap Properties Property A. Largest key is at root.
13
Binary Heap Properties Property A. Largest key is at root.
Property B. Can use array indices to move through tree. Note: indices start at 1. Parent of node at k is at k/2. Children of node at k are at 2k and 2k+1.
• • •
14
Binary Heap Properties Property A. Largest key is at root.
Property B. Can use array indices to move through tree. Note: indices start at 1. Parent of node at k is at k/2. Children of node at k are at 2k and 2k+1.
• • •
Property C. Height of N node heap is 1 + lg N. height increases only when N is a power of 2
N = 16 height = 5 15
Promotion In a Heap Scenario. Exactly one node has a larger key than its parent. To eliminate the violation: Exchange with its parent. Repeat until heap order restored.
• •
private void swim(int k) { while (k > 1 && less(k/2, k)) { exch(k, k/2); k = k/2; parent of node at k is at k/2 } }
Peter principle: node promoted to level of incompetence. 16
Insert Insert. Add node at end, then promote.
public void insert(Item x) { pq[++N] = x; swim(N); }
17
Demotion In a Heap Scenario. Exactly one node has a smaller key than does a child. To eliminate the violation: Exchange with larger child. Repeat until heap order restored.
• •
private void sink(int k) { children of node while (2*k