binary heap operations binary heap operations
play

Binary heap operations Binary heap operations Insert. Add node at - PowerPoint PPT Presentation

BBM 202 - ALGORITHMS TODAY Heapsort API Elementary implementations D EPT . OF C OMPUTER E NGINEERING Binary heaps Heapsort Event-driven simulation P RIORITY Q UEUES AND H EAPSORT Feb. 25, 2016 Acknowledgement: The


  1. 
 BBM 202 - ALGORITHMS TODAY ‣ Heapsort ‣ API ‣ Elementary implementations D EPT . OF C OMPUTER E NGINEERING ‣ Binary heaps ‣ Heapsort ‣ Event-driven simulation P RIORITY Q UEUES AND H EAPSORT Feb. 25, 2016 Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick 
 and K. Wayne of Princeton University. Priority queue Priority queue API Collections. Insert and delete items. Which item to delete? Requirement. Generic items are Comparable . Stack. Remove the item most recently added. Key must be Comparable (bounded type parameter) Queue. Remove the item least recently added. Randomized queue. Remove a random item. public class MaxPQ<Key extends Comparable<Key>> Priority queue. Remove the largest (or smallest) item. MaxPQ() create an empty priority queue MaxPQ(Key[] a) create a priority queue with given keys return operation argument value void insert(Key v) insert a key into the priority queue insert P 1 insert Q 2 P Key delMax() return and remove the largest key insert E 3 P Q remove max Q 2 P E E P boolean isEmpty() is the priority queue empty? insert X 3 P E insert A 4 P E X Key max() return the largest key insert M 5 P E X A remove max X 4 P E M A A E M P int size() number of entries in the priority queue 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 3 4

  2. Priority queue applications Priority queue client example • Event-driven simulation. [customers in a line, colliding particles] Challenge. Find the largest M items in a stream of N items ( N huge, M • Numerical computation. [reducing roundoff error] large). • Data compression. [Huffman codes] • Fraud detection: isolate $$ transactions. • Graph searching. [Dijkstra's algorithm, Prim's algorithm] • File maintenance: find biggest files or directories. • Computational number theory. [sum of powers] Constraint. Not enough memory to store N items. • Artificial intelligence. [A* search] • Statistics. [maintain largest M values in a sequence] • Operating systems. % more tinyBatch.txt % java TopM 5 < tinyBatch.txt [load balancing, interrupt handling] Turing 6/17/1990 644.08 Thompson 2/27/2000 4747.08 • Discrete optimization. [bin packing, scheduling] vonNeumann 3/26/2002 4121.85 vonNeumann 2/12/1994 4732.35 Dijkstra 8/22/2007 2678.40 vonNeumann 1/11/1999 4409.74 • Spam filtering. [Bayesian spam filter] vonNeumann 1/11/1999 4409.74 Hoare 8/18/1992 4381.21 Dijkstra 11/18/1995 837.42 vonNeumann 3/26/2002 4121.85 Hoare 5/10/1993 3229.27 vonNeumann 2/12/1994 4732.35 Hoare 8/18/1992 4381.21 sort key Turing 1/11/2002 66.10 Thompson 2/27/2000 4747.08 Generalizes: stack, queue, randomized queue. Turing 2/11/1991 2156.86 Hoare 8/12/2003 1025.70 vonNeumann 10/13/1993 2520.97 Dijkstra 9/10/2000 708.95 Turing 10/12/1993 3532.36 Hoare 2/10/2005 4050.20 5 6 Priority queue client example P RIORITY Q UEUES AND H EAPSORT Challenge. Find the largest M items in a stream of N items ( N huge, M large). ‣ Heapsort ‣ API MinPQ<Transaction> pq = new MinPQ<Transaction>(); ‣ Elementary implementations while (StdIn.hasNextLine()) ‣ Binary heaps Transaction data 
 { use a min-oriented pq type is Comparable ‣ Heapsort String line = StdIn.readLine(); Transaction item = new Transaction(line); (ordered by $$) ‣ Event-driven simulation pq.insert(item); if (pq.size() > M) pq contains 
 pq.delMin(); largest M items } order of growth of finding the largest M in a stream of N items implementatio time space n sort N log N N elementary PQ M N M binary heap N log M M best in theory N M 7

  3. Priority queue: unordered array implementation Priority queue: unordered and ordered array implementation public class UnorderedMaxPQ<Key extends Comparable<Key>> { private Key[] pq; // pq[i] = ith element on pq private int N; // number of elements on pq return contents contents operation argument size value (unordered) (ordered) public UnorderedMaxPQ(int capacity) no generic 
 insert P 1 P P { pq = (Key[]) new Comparable[capacity]; } array creation insert Q 2 P Q P Q insert E 3 P Q E E P Q public boolean isEmpty() remove max Q 2 P E E P { return N == 0; } insert X 3 P E X E P X insert A 4 P E X A A E P X public void insert(Key x) insert M 5 P E X A M A E M P X { pq[N++] = x; } remove max X 4 P E M A A E M P insert P 5 P E M A P A E M P P public Key delMax() insert L 6 P E M A P L A E L M P P { insert E 7 P E M A P L E A E E L M P P int max = 0; remove max P 6 E M A P L E A E E L M P for (int i = 1; i < N; i++) less() and exch() 
 if (less(max, i)) max = i; similar to sorting methods A sequence of operations on a priority queue exch(max, N-1); return pq[--N]; } } null out entry to prevent loitering 9 10 Priority queue elementary implementations P RIORITY Q UEUES AND H EAPSORT Challenge. Implement all operations efficiently. ‣ Heapsort ‣ API ‣ Elementary implementations ‣ Binary heaps ‣ Heapsort ‣ Event-driven simulation order-of-growth of running time for priority queue with N items implementation insert del max max unordered array 1 N N ordered array N 1 1 goal log N log N log N 11

  4. 
 
 
 
 
 
 
 
 Binary tree A complete binary tree in nature Binary tree. Empty or node with links to left and right binary trees. 
 Complete tree. Perfectly balanced, except for bottom level. 
 complete tree with N = 16 nodes (height = 4) Property. Height of complete tree with N nodes is ⎣ lg N ⎦ . Pf. Height only increases when N is a power of 2. 13 14 Binary heap representations Binary heap properties Binary heap. Array representation of a heap-ordered complete binary Proposition. Largest key is a[1] , which is root of binary tree. 
 tree. 
 Proposition. Can use array indices to move through tree. • Parent of node at k is at k/2 . Heap-ordered binary tree. • Children of node at k are at 2k and 2k+1 . • Keys in nodes. • Parent's key no smaller than 
 i 0 1 2 3 4 5 6 7 8 9 10 11 i 0 1 2 3 4 5 6 7 8 9 10 11 children's keys. 
 a[i] - T S R P N O A E I H G a[i] - T S R P N O A E I H G T T Array representation. S R S R • Indices start at 1. P N O A P N O A • Take nodes in level order. • No explicit links needed! E I H G E I H G 1 1 T T 2 3 2 3 S R S R 6 7 6 7 4 P 5 N O A 4 P 5 N O A 8 E 9 I 10 H 11 G 8 E 9 I 10 H 11 G Heap representations Heap representations 15 16

  5. Promotion in a heap Insertion in a heap Scenario. Child's key becomes larger key than its parent's key. Insert. Add node at end, then swim it up. Cost. At most 1 + lg N compares. To eliminate the violation: insert T • Exchange key in child with key in parent. P R • Repeat until heap order restored. N H O A S E I G S key to insert public void insert(Key x) private void swim(int k) P R { { 5 T N T O A pq[++N] = x; while (k > 1 && less(k/2, k)) violates heap order swim(N); P R E I H G (larger key than parent) { } N H O A exch(k, k/2); 1 T add key to heap k = k/2; E I G S violates heap order 2 } S R parent of node at k is at k/2 } T 5 N P O A swim up S R E I H G N P O A E I G H Peter principle. Node promoted to level of incompetence. 17 18 Demotion in a heap Delete the maximum in a heap Scenario. Parent's key becomes smaller than one (or both) of its children's Delete max. Exchange root with node at end, then sink it down. keys. Cost. At most 2 lg N compares. To eliminate the violation: why not smaller child? remove the maximum • Exchange key in parent with key in larger child. key to remove T • Repeat until heap order restored. S R N P O A public Key delMax() exchange key violates heap order private void sink(int k) { E I G H with root (smaller than a child) T { Key max = pq[1]; children of node 
 2 violates H R while (2*k <= N) H exch(1, N--); heap order at k are 2k and 2k+1 { 5 P S O A sink(1); S R int j = 2*k; pq[N+1] = null; E I N G prevent loitering N P O A if (j < N && less(j, j+1)) j++; return max; T remove node if (!less(k, j)) break; } E I G T from heap 2 S R exch(k, j); 5 P N O A S k = j; 10 } E I H G P R sink down } Top-down reheapify (sink) N H O A E I G Power struggle. Better subordinate promoted. 19 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend