algorithms
play

Algorithms R OBERT S EDGEWICK | K EVIN W AYNE 2.4 P RIORITY Q UEUES - PowerPoint PPT Presentation

Algorithms R OBERT S EDGEWICK | K EVIN W AYNE 2.4 P RIORITY Q UEUES API and elementary implementations binary heaps heapsort Algorithms event-driven simulation F O U R T H E D I T I O N R OBERT S EDGEWICK | K EVIN W AYNE


  1. Algorithms R OBERT S EDGEWICK | K EVIN W AYNE 2.4 P RIORITY Q UEUES ‣ API and elementary implementations ‣ binary heaps ‣ heapsort Algorithms ‣ event-driven simulation F O U R T H E D I T I O N R OBERT S EDGEWICK | K EVIN W AYNE http://algs4.cs.princeton.edu Last updated on 3/29/17 9:01 AM

  2. 2.4 P RIORITY Q UEUES ‣ API and elementary implementations ‣ binary heaps ‣ heapsort Algorithms ‣ event-driven simulation R OBERT S EDGEWICK | K EVIN W AYNE http://algs4.cs.princeton.edu

  3. Collections A collection is a data type that stores a group of items. data type core operations data structure stack P USH , P OP linked list, resizing array queue E NQUEUE , D EQUEUE linked list, resizing array priority queue INSERT , D ELETE -M AX binary heap symbol table P UT , G ET , D ELETE binary search tree, hash table set A DD , C ONTAINS , D ELETE binary search tree, hash table “ Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won't usually need your code; it'll be obvious.” — Fred Brooks 3

  4. 
 
 Priority queue Collections. Insert and delete items. Which item to delete? Stack. Remove the item most recently added. Queue. Remove the item least recently added. Randomized queue. Remove a random item. Priority queue. Remove the largest (or smallest) item. Generalizes: stack, queue, randomized queue. return operation argument value insert P 1 insert Q 2 P insert E 3 P Q remove max Q 2 P E E P insert X 3 P E insert A 4 P E X insert M 5 P E X A remove max X 4 P E M A A E M P insert P 5 P E M A insert L 6 P E M A P insert E 7 P E M A P L remove max P 6 E M A P L E A E E L M P 4

  5. 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Priority queue API Requirement. Items are generic; they must also be Comparable . Key must be Comparable (bounded type parameter) public class MaxPQ<Key extends Comparable<Key>> create an empty priority queue MaxPQ() create a priority queue with given keys MaxPQ(Key[] a) insert a key into the priority queue insert(Key v) void return and remove a largest key delMax() Key is the priority queue empty? isEmpty() boolean return a largest key max() Key number of entries in the priority queue size() int Note. Duplicate keys allowed; delMax() picks any maximum key. 5

  6. Priority queue: applications ・ Event-driven simulation. [ customers in a line, colliding particles ] ・ Numerical computation. [ reducing roundoff error ] ・ Discrete optimization. [ bin packing, scheduling ] ・ Artificial intelligence. [ A* search ] ・ Computer networks. [ web cache ] ・ Operating systems. [ load balancing, interrupt handling ] ・ Data compression. [ Huffman codes ] ・ Graph searching. [ Dijkstra's algorithm, Prim's algorithm ] ・ Number theory. [ sum of powers ] ・ Spam filtering. [ Bayesian spam filter ] ・ Statistics. [ online median in data stream ] 6

  7. 
 Priority queue: client example Challenge. Find the largest m items in a stream of n items. ・ Fraud detection: isolate $$ transactions. ・ NSA monitoring: flag most suspicious documents. n huge, m large Constraint. Not enough memory to store n items. Transaction data 
 type is Comparable (ordered by $$) MinPQ<Transaction> pq = new MinPQ<Transaction>(); while (StdIn.hasNextLine()) use a min-oriented pq { String line = StdIn.readLine(); Transaction transaction = new Transaction(line); pq.insert(transaction); if (pq.size() > m) pq now contains 
 pq.delMin(); largest m items } 7

  8. Priority queue: client example Challenge. Find the largest m items in a stream of n items. implementation time space sort n log n n elementary PQ m n m binary heap n log m m best in theory n m order of growth of finding the largest m in a stream of n items 8

  9. Priority queue: unordered and ordered array implementation return contents contents operation argument size value (unordered) (ordered) insert P 1 P P P P insert Q 2 P Q Q P Q insert E 3 P Q E E E E P Q remove max Q 2 P E E P 2 P E E P insert X 3 P E X X E P X insert A 4 P E X A A A A E P X insert M 5 P E X A M M A E M P X remove max X 4 P E M A A E M P 4 P E M A A E M P insert P 5 P E M A P P A E M P P insert L 6 P E M A P L L A E L M P P insert E 7 P E M A P L E E A E E L M P P remove max P 6 E M A P L E A E E L M P 6 E M A P L E A E E L M P A sequence of operations on a priority queue 9

  10. 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Priority queue: implementations cost summary Challenge. Implement all operations efficiently. implementation insert del max max unordered array 1 n n ordered array n 1 1 goal log n log n log n order of growth of running time for priority queue with n items Solution. Partially-ordered array. 10

  11. 2.4 P RIORITY Q UEUES ‣ API and elementary implementations ‣ binary heaps ‣ heapsort Algorithms ‣ event-driven simulation R OBERT S EDGEWICK | K EVIN W AYNE http://algs4.cs.princeton.edu

  12. 
 
 
 
 
 
 
 
 
 
 Complete binary tree Binary tree. Empty or node with links to left and right binary trees. Complete tree. Perfectly balanced, except for bottom level. complete binary tree with n = 16 nodes (height = 4) Property. Height of complete binary tree with n nodes is ⎣ lg n ⎦ . Pf. Height increases only when n is a power of 2. 12

  13. A complete binary tree in nature 13

  14. 
 
 Binary heap: representation Binary heap. Array representation of a heap-ordered complete binary tree. Heap-ordered binary tree. ・ Keys in nodes. ・ Parent's key no smaller than 
 children's keys. i 0 1 2 3 4 5 6 7 8 9 10 11 a[i] - T S R P N O A E I H G Array representation. T S R ・ Indices start at 1. P N O A ・ Take nodes in level order. ・ No explicit links needed! E I H G E I H G 1 1 T 3 2 S R 6 7 P N O A 4 5 10 11 8 9 E I H G Heap representations 14

  15. 
 Binary heap: properties Proposition. Largest key is a[1] , which is root of binary tree. Proposition. Can use array indices to move through tree. ・ Parent of node at k is at k/2 . ・ Children of node at k are at 2k and 2k+1 . i 0 1 2 3 4 5 6 7 8 9 10 11 a[i] - T S R P N O A E I H G T S R P N O A E I H G 1 T 3 2 S R 6 7 P N O A 4 5 10 11 8 9 E I H G Heap representations 15

  16. Binary heap demo Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. heap ordered T P R N H O A E I G T P R N H O A E I G 16

  17. Binary heap demo Insert. Add node at end, then swim it up. Remove the maximum. Exchange root with node at end, then sink it down. heap ordered S R O N G A P E I H S R O N P G A E I H 17

  18. 
 
 
 
 
 
 
 
 
 
 
 Binary heap: promotion Scenario. A key becomes larger than its parent's key. To eliminate the violation: ・ Exchange key in child with key in parent. ・ Repeat until heap order restored. S P R private void swim(int k) { 5 N T O A while (k > 1 && less(k/2, k)) violates heap order E I H G (larger key than parent) { exch(k, k/2); 1 T k = k/2; 2 S R } parent of node at k is at k/2 5 } N P O A E I H G Peter principle. Node promoted to level of incompetence. 18

  19. Binary heap: insertion Insert. Add node at end, then swim it up. Cost. At most 1 + lg n compares. insert T P R N H O A key to insert E I G S public void insert(Key x) { T pq[++n] = x; P R swim(n); } N H O A add key to heap E I G S violates heap order T swim up S R N P O A E I G H 19

  20. 
 
 
 
 
 
 
 
 
 
 
 Binary heap: demotion Scenario. A key becomes smaller than one (or both) of its children's. why not smaller child? To eliminate the violation: ・ Exchange key in parent with key in larger child. ・ Repeat until heap order restored. private void sink(int k) violates heap order (smaller than a child) T { 2 children of node at k H R while (2*k <= n) are 2*k and 2*k+1 5 P S O A { int j = 2*k; E I N G if (j < n && less(j, j+1)) j++; T if (!less(k, j)) break; 2 S R exch(k, j); 5 P N O A k = j; 10 E I H G } } Top-down reheapify (sink) Power struggle. Better subordinate promoted. 20

Recommend


More recommend