9 Heaps LempelZiv Heaps Heap A binary heap
9 תרגול Heaps Lempel-Ziv
Heaps Heap A binary heap can be considered as a complete binary tree, (the last level is full from the left to a certain point). Maximum. Heap Minimum. Heap For each node x, x ≥ left(x) and x ≥ right(x) The root in a Maximum-Heap is the maximal element in the heap Heap-Array For each node x, x ≤ left(x) and x ≤ right(x) The root in a Minimum-Heap is the minimal element in the heap
Heaps Actions on Heaps Insert ~ O(log(n)) Max ~ O(1) Extract-Max ~ O(log(n)) Build-Heap ~ O(n) Down-Heapify(H, i) – same as Max. Heapify seen in class but starting in the node in index i ~ O(log(n)) Up-Heapify(H, i) – Fixing the heap going from node in index i to the root as in Insert() ~ O(log(n))
Question 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 9 11 17 21 12 13 20 34 22 51 14 : הדגמה Extract(H, 2) • בצעו 7 11 9 21 34 13 12 22 51 14 20
Question 3 להוצאת אלגוריתם הציעו , מערך בעזרת שמיוצגת H • בערימה . ( הערימה מגודל קטן i כי )הניחו i- ה במיקום הערך ? האלגוריתם של הריצה זמן • מה Solution: Extract(H, i) • key H[i] • H[i] H[heap_size] /*Deleting the i'th element and replacing it with the last element*/ • heap_size − 1 /*Effectively shrinking the heap*/ • Down-Heapify(H, i) /*Subtree rooted in H[i] is legal heap*/ • Up-Heapify(H, i) /*Area above H[i] is legal heap*/ • return key . ( עומק באותו לפחות )או H[i] מ יותר גדול בעומק H[heap_size] ? up-heapify לבצע שנצטרך ייתכן מדוע , כך אם
Question 3 i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 9 11 17 21 12 13 Key = 11 20 34 22 51 14 Heap_size 7 – מאביו גדול מסיימים – הקטן מבנו גדול החלפה מבצעים 11 9 – הקטן מבנו קטן מסיימים 21 34 13 12 22 51 14 20 : הדגמה Extract(H, 2) • בצעו • • • key H[i] H[heap_size] heap_size -- Down-Heapify(H, i) Up-Heapify(H, i) return key
Question 1 • Given a maximum heap H, What is the time complexity of finding the 3 largest keys in H ? • What is the time complexity of finding the C largest keys in H ? (C is a constant)
Question 1 Solution: • It takes O(1) to find the 3 maximal keys in H. The 3 maximal keys in H are: – The root's maximal son y – The largest among y's maximal son and y's sibling • Finding the C largest keys in H can be done in a similar way: – The ith largest key is one of at most i keys, the sons of the i-1 largest key that have not been taken yet, thus can be found in O(i) time. – Finding the C largest keys takes O(C 2) = O(1) – (Another solution would be ordering all the keys in the first C levels of the heap)
Question 1 • Given a maximum heap H, What is the time complexity of finding the 3 largest keys in H ? • What is the time complexity of finding the C largest keys in H ? (C is a constant) • What if C is not constant?
Question 1 Solution: • If C is not constant we have to be more efficient. • We will use a new heap, where we will store nodes of the original heap so that for each node entered we will have it sons as put in this new heap but we also preserve access to its sons as in the original heap. • We first enter the root to the new heap. Then C times we do Extract-Max() on the new heap while at each time we Insert the original sons (the sons as in the original heap) to the new heap. • The logic is as before only this time since the new heap has at most C elements we get O(C*log(C)). • Note that we do not change the original heap at any point.
Question 2 Analyze the complexity of finding the i smallest elements in a set of size n using each of the following: – Sorting – Priority Queue Sorting Solution: – Sort the numbers using algorithm that takes O(nlogn) – Find the i smallest elements in O(i) – Total = O(i+nlogn)=O(nlogn) Priority Queue Solution: – Build a minimum heap in O(n) – Execute Extract-Min i times in O(ilogn) – Total = O(n+ilogn). (if we use Q 1: O(n+ilogi) ) Note: n+ilogn = Θ(max(n, ilogn)) and clearly n+ilogn = O(nlogn) (and faster if i ≠ Θ(n))
Question 4 • You are given two min-heaps H 1 and H 2 with n 1 and n 2 keys respectively. • Each element of H 1 is smaller than each element of H 2. • How can you merge H 1 and H 2 into one heap H (with n 1+n 2 keys) using only O(n 2) time? (Assume both heaps are represented in arrays of size > n 1+n 2)
Question 4 •
Question 6 • Suppose that you had to implement a priority queue that only needed to store items whose priorities were integer values between 0 and k. • Describe how a priority queue could be implemented so that insert() has complexity O(1) and Remove. Min() has complexity O(k). • (Hint: This is very similar to a hash table. )
Question 6 Solution: • Use an array A of size k+1 of linked lists. • The linked list in the ith slot stores entries with priority i. • To insert an item into the priority queue with priority i, append it to the linked list in the ith slot. E. g. , • insert(v) – A[v. priority]. add-to-tail(v)
Question 6 Solution: • To remove an item from the queue, scan the array of lists, starting at 0 until a non-empty list is found. • Remove the first element and return it—the forloop induces the O(k) worst case complexity. E. g. , • remove. Min() – for i 0 to k+1 • List L A[i] – if( L. empty() = false ) • v L. remove-head() /*Removes list's head and returns it dequeue*/ • return v – return NULL
Question 5 • Give an O(nlog k) time algorithm to merge k sorted arrays A 1. . Ak into one sorted array. n is the total number of elements (you may assume k≤n).
Question 5 • Give an O(nlog k) time algorithm to merge k sorted arrays A 1. . Ak into one sorted array. n is the total number of elements (you may assume k≤n). Solution: • • We will use a min-heap of k triples of the form (d, i, Ad[i]) for 1 ≤ d ≤ k, 1≤ i ≤ n. The heap will use an array B. First we build the heap with the first elements lists A 1. . . Ak in O(k) time. In each step extract the minimal element of the heap and add it at the end of the output Array M. If the array that the elementioned in step two came from is not empty, than remove its minimum and add it to our heap. for d 1 to k – B[d] (d, 1, Ad[1]) Build-Heap(B) /*By the order of Ad [1] */ for j=1 to n – (d, i, x) Extract-Min(B) – M[j] x – if i < Ad. length then Heap-Insert(B, (d, i+1, Ad[i+1])) Worst case time analysis: – – Build-Heap : O(k) – done 1 time Extract-Min : O(log k) – done n times Heap-Insert : O(log k) – done n times Total: O(nlogk)
Lempel-Ziv •
- Slides: 26