1 / 26

תרגול 9

תרגול 9. Heaps Lempel-Ziv. Heaps. Heaps. Heaps. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15. דוגמה: הוסיפו את 9 מוסיפים את הערך 9 כעלה מבצעים Up- Heapify לתיקון הערימה. 7. 11. 13. 21. 12. 17. 20. 34. 22. 51. 14. 41.

joann
Download Presentation

תרגול 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. תרגול 9 Heaps Lempel-Ziv

  2. Heaps

  3. Heaps

  4. Heaps 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 דוגמה: • הוסיפו את 9 • מוסיפים את הערך 9 כעלה • מבצעים Up-Heapify לתיקון הערימה 7 11 13 21 12 17 20 34 22 51 14 41 9 7 Heap_size 11 13 קטן מאביו – מבצעים החלפה 20 17 21 12 14 41 9 51 34 22

  5. Heaps 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 דוגמה: • הוסיפו את 9 • מוסיפים את הערך 9 כעלה • מבצעים Up-Heapify לתיקון הערימה 7 11 13 21 12 9 20 34 22 51 14 41 17 7 Heap_size 11 13 גדול מאביו – מסיימים קטן מאביו – מבצעים החלפה 20 9 21 12 14 41 17 51 34 22

  6. Heaps 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 דוגמה: • כעת בצעו Extract-min • מוציאים את השורש (ושומרים את ערכו) • מעבירים לשורש את העלה האחרון • מבצעים Down-Heapify מהשורש לתיקון הערימה 7 11 9 21 12 13 20 34 22 51 14 41 17 17 7 Key = 7 Heap_size 11 9 20 13 21 12 14 41 17 51 34 22

  7. Heaps 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 דוגמה: • כעת בצעו Extract-min • מוציאים את השורש (ושומרים את ערכו) • מעבירים לשורש את העלה האחרון • מבצעים Down-Heapify מהשורש לתיקון הערימה 17 11 9 21 12 13 20 34 22 51 14 41 17 17 Key = 7 Heap_size 11 9 גדול מבנו הקטן – מבצעים החלפה 20 13 21 12 14 41 51 34 22

  8. Heaps 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 דוגמה: • כעת בצעו Extract-min • מוציאים את השורש (ושומרים את ערכו) • מעבירים לשורש את העלה האחרון • מבצעים Down-Heapify מהשורש לתיקון הערימה • מחזירים את הערך שנשמר 9 11 17 21 12 13 20 34 22 51 14 41 17 9 Key = 7 Heap_size 11 17 גדול מבנו הקטן – מבצעים החלפה קטן מבנו הקטן – מסיימים 20 13 21 12 14 41 51 34 22

  9. Question 3 • בערימה H שמיוצגת בעזרת מערך, הציעו אלגוריתם להוצאת הערך במיקום ה-i (הניחו כי i קטן מגודל הערימה). • מה זמן הריצה של האלגוריתם?

  10. 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 14 הדגמה: • בצעו Extract(H,2) 7 11 9 20 13 21 12 14 14 51 34 22

  11. 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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[heap_size] בעומק גדול יותר מH[i] (או לפחות באותו עומק). אם כך, מדוע ייתכן שנצטרך לבצע up-heapify?

  12. 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 14 הדגמה: • בצעו Extract(H,2) • key  H[i] • H[i] H[heap_size] • heap_size -- • Down-Heapify(H, i) • Up-Heapify(H, i) • return key Key = 11 i 7 Heap_size 11 9 גדול מבנו הקטן – מבצעים החלפה קטן מבנו הקטן – מסיימים גדול מאביו – מסיימים 20 13 21 12 14 14 51 34 22

  13. 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)

  14. Question 1 Solution: • It takes O(1) to find the 3 maximal keys in H.The 3 maximal keys in H are: • The root • 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 ithlargest 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(C2) = O(1) • (Another solution would be ordering all the keys in the first C levels of the heap)

  15. 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?

  16. 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.

  17. Question 1 הדגמה: מצאו את 5 המפתחות הקטנים ביותר בערימה בעזרת האלגוריתם 7 נכנס לערימה החדשה 7 יוצא, 11 ו13 נכנסים 11 יוצא, 12 ו21 נכנסים 12 יוצא, 14 ו51 נכנסים 13 יוצא, 17 ו20 נכנסים 14 יוצא 7 11 13 20 17 21 12 14 41 9 51 34 22

  18. 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 Q1: O(n+ilogi) ) Note:n+ilogn = Θ(max(n,ilogn)) and clearly n+ilogn = O(nlogn) (and faster if i ≠ Θ(n))

  19. Question 4 • You are given two min-heaps H1 and H2 with n1 and n2 keys respectively. • Each element of H1 is smaller than each element of H2. • How can you merge H1 and H2 into one heap H (with n1+n2 keys) using only O(n2) time? (Assume both heaps are represented in arrays of size > n1+n2)

  20. Question 4 Solution: • If n1≥ n2:Add the keys of H2 to the array of H1 from index n1+1 to index n1+n2. • Claim: All keys from H2have been added as leaves. • Proof: for , the father of A[i] is , and. So the father of every key from H2 is a node from H1. • As all keys in H1 are smaller than any key in H2, the heap property still holds in the resulting array. • If n1 < n2: Build a new heap with all the elements of H1 and H2 in O(n1 + n2) = O(2n2)=O(n2).

  21. 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 RemoveMin() has complexity O(k). • (Hint: This is very similar to a hash table.)

  22. 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)

  23. 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 for-loop induces the O(k) worst case complexity. E.g., • removeMin() • 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

  24. Question 5 • Give an O(nlog k) time algorithm to merge k sorted arrays A1 .. Ak into one sorted array. n is the total number of elements (you may assume k≤n).

  25. Question 5 • Give an O(nlog k) time algorithm to merge k sorted arrays A1 .. 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 A1...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 element mentioned 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)

  26. Lempel-Ziv • Lempel-Ziv

More Related