Data Structures and Algorithms Binary Heaps (S&W 2.4)
Total Page:16
File Type:pdf, Size:1020Kb
Data structures and algorithms DAT038/TDA417, LP2 2019 Lecture 12, 2019-12-02 Binary heaps (S&W 2.4) Some slides by Sedgewick & Wayne Collections A collection is a data type that stores groups of items. stack Push, Pop linked list, resizing array queue Enqueue, Dequeue linked list, resizing array symbol table Put, Get, Delete BST, hash table, trie, TST set Add, ontains, Delete BST, hash table, trie, TST A priority queue is another kind of collection. “ 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 2 Priority queues Collections. Can add and remove items. Stack. Add item; remove the item most recently added. Queue. Add item; remove the item least recently added. Min priority queue. Add item; remove the smallest item. Max priority queue. Add item; remove the largest item. return contents contents operation argument value size (unordered) (ordered) insert P 1 P P insert Q 2 P Q P Q insert E 3 P Q E E P Q remove max Q 2 P E E P insert X 3 P E X E P X insert A 4 P E X A A E P X insert M 5 P E X A M A E M P 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 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 remove max P 6 E M A P L E A E E L M P A sequence of operations on a priority queue 3 Priority queue API Requirement. eneric items are Comparable. Basic A"#. Add item; find minimum; delete minimum. MinPQ() create an empty priority queue MinPQ(Key[] a) create a priority queue with given keys void insert(Key v) insert a key into the priority queue Key delMin() return and remove the smallest key boolean is mpty() is the priority queue empty? Key min() return the smallest key int size() number o# entries in the priority queue 4 Priority queue API Alternative. Add item; find maximum; delete maximum. (Exercise: use Min"Q to implement Max"Q.( Ma$PQ() create an empty priority queue Ma$PQ(Key[] a) create a priority queue with given keys void insert(Key v) insert a key into the priority queue Key delMa$() return and remove the largest key boolean is mpty() is the priority queue empty! Key ma$() return the largest key int size() number o# entries in the priority queue 5 Sorting with a priority queue )o sort a list, add its elements to a (min+(priority queue, then repeatedly find and remove the minimum element: public void sort(String[] a) { int N = a.length; MinPQ<String> pq = new MinPQ<String>(); for (int i = 0; i < N; i++) pq.insert(a[i]); for (int i = 0; i < N; i++) a[i] = pq.delMin(); } Q. ,hat is this algorithm-s performance? A. Depends on performance of priority queue' ● quadratic, if insert() or delMin() take linear time. ● N lg N, if insert%( and delMin() take lg N time. 6 Priority queue client example Challenge. 1ind the largest M items in a stream of N items. e.g. Fraud detection: isolate 22 transactions. 0 huge* M large Constraint. Not enough memory to store N items. % more tinyBatch.txt % java TopM 5 < tinyBatch.txt Turing 6/17/1990 644.08 Thompson 2/27/2000 4747.08 vonNeumann 3/26/2002 4121.85 vonNeumann 2/12/1994 4732.35 Dijkstra 8/22/2007 2678.40 vonNeumann 1/11/1999 4409.74 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 Turing 1/11/2002 66.10 key Thompson 2/27/2000 4747.08 Turing 2/11/1991 2156.86 ... 7 Priority queue client example Challenge. 1ind the largest M items in a stream of N items. e.g. Fraud detection: isolate 22 transactions. Constraint. Not enough memory to store N items. MinPQ<Transaction> pq = new MinPQ<Transaction>(); while (StdIn.hasNextLine()) { use a min+ )ransaction data String line = StdIn.readLine(); oriented pq type is Transaction item = new Transaction(line); Comparable pq.insert(item); (ordered by 22( if (pq.size() > M) pq.delMin(); pq } contains largest M items 8 Priority queue client example Challenge. Maintain a printer queue, but 4here 5o3s can have priorities e.g. the boss-s documents should be printed first Solution. 6se a max priority queue. 7rdering. Implement Compara3le<Document> as follo4s' ● 1irst compare who submitted the document; documents submitted 3y more senior people are greater ● #f seniority is equal, compare submission time; documents submitted earlier are greater 9 Priority queue applications Event-driven simulation.[ customers in a line, colliding particles ] Numerical computation. [ reducing roundoff error ] Data compression. [ Huffman codes ] Graph searching. [ Best-first search/Dijkstra's algorithm ] Number theory. [ sum of powers ] Artificial intelligence. [ A* search ] Statistics. [ online median in data stream ] Operating systems. [ load balancing, interrupt handling ] Computer networks. [ web cache ] Discrete optimization. [ bin packing, scheduling ] 10 Priority queue implementations Cost of insert() Cost of max() Cost of delMax() Unsorted array 1 N N Sorted array N 1 1 Unbalanced BST N N N Red-black BST lg N lg N lg N Binary heap lg N 1 lg N Balanced BS)s. ,ork fine* but 4e can do better. Binary heaps. Simpler and faster than a 3alanced BS). This lecture. :o4 to implement min-heaps to get a min priority queue. The book. Presents max-heaps (completely symmetric to min heaps). 11 Binary (min-)heaps Binary (min-)heaps – representation % binary heap implements a priority queue using a binary tree (not a B'()) ,- ,. ,/ 31 3, -. 44 0- - 12 3. %bove is just a binary tree. We will add an invariant to make it easy to #ind the minimum element. 13 Invariant #1 – the heap property % tree satisfies the hea! !ro!erty if the value of each node is less than (or equal to) the value o# its children5 8oot node is the - smallest 9 can #ind minimum 0- ,. in O(1) time ,/ ,- 3. 44 31 3, 12 -. 6nvariant 705 % binary heap must satisfy the heap property. 14 Binary heap % tree is complete if it is perfectly balanced e$cept the bottom level, which is filled from left to right5 Level 0 - Level 1 0- ,. Level 2 ,/ ,- 3. 44 Level 3 31 ,4 14 3, 12 <omplete trees have logarithmic height= Invariant 7,5 % binary heap must be complete) 15 Binary heap or not? - - 0- ,. 0- ,. ,/ ,- 44 ,/ ,- 3. - - - 1- ,- ,. ,/ .> -> ,/ 0- 3. 44 16 Binary heap or not? - - No: 0- No: ,. 0-not complete,. not complete ,/ ,- 44 ,/ ,- 3. - - No: - Aes 1- ,- ,. ,8 > 0- ,/ .> -> ,/ 0- 3. 44 17 Binary heap invariant (he binary heap invariant for min-heaps5 ● (he tree must be com!lete ● 6t must have the hea! !ro!erty (each node is less than or equal to its children) Why do we choose these invariants? ● <ompleteness: ensures balance ● Heap property: allows us to #ind the minimum in constant time (itCs the root!) Our task now: implement insert() and delMin() while !reserving the invariant 18 Adding an element to a binary heap 'tep 1: insert the element at the ne$t empty position in the tree - 0- ,. ,/ ,- 3. 44 31 ,4 14 3, 12 -. 12 (his might break the heap invariant! In this case, 12 is less than 66, its parent. 19 Adding an element to a binary heap 'tep 2: if the new element is less than its parent, swap it with its parent - 0- ,. ,/ ,- 3. 44 31 ,4 14 3, 12 -. 12 20 Adding an element to a binary heap 'tep 2: if the new element is less than its parent, swap it with its parent - 0- ,. ,/ ,- 3. 12 31 ,4 14 3, 12 -. 44 (he invariant is still broken, since 12 is less than ,9, its new parent 21 Adding an element to a binary heap 8epeat step 2 until the new element is greater than or equal to its parent. - 0- 12 ,/ ,- 3. ,. 31 ,4 14 3, 12 -. 44 ?ow 12 is in its right place; and the invariant is restored. (Think about why this algorithm restores the invariant.) 22 Why this works %t every step, the heap property almost holds except that the new element might be less than its parent %fter swapping the element and its parent, still only the new element can be in the wrong place (why!) - 0- ,. ,/ ,- 3. 12 31 ,4 14 3, 12 -. 44 23 Why this works Suppose that % is the new node and we swap it with its parent: ! z y z y ! On the left, we must have x ≤ y but x > z. On the right, this means z < x ≤ y) So only z can be in the wrong place afterwards! 24 Removing the minimum element To remove the minimum element, we are going to follow a similar scheme as #or insertion: ● First remove the minimum (root) element #rom the tree somehow; breaking the invariant in the process ● (hen repair the invariant &ecause of completeness, we can only really remove the last (bottom-right) element from the tree ● 'olution: #irst swa! the root element with the last element; then remove the last element 25 Removing the minimum element (he goal: remove the minimum element Eirst: swap the root and the last element (66); and then remove the last element 8 0- 0, ,/ ,- 3.