תרגול 9 Heaps Lempel-Ziv. Heaps Heap A binary heap can be considered as a complete binary tree,...

26
ללללל9 Heaps Lempel-Ziv

Transcript of תרגול 9 Heaps Lempel-Ziv. Heaps Heap A binary heap can be considered as a complete binary tree,...

Page 1: תרגול 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).

9תרגול

HeapsLempel-Ziv

Page 2: תרגול 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).

HeapsHeap 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

For each node x, x ≥ left(x) and x ≥ right(x)The root in a Maximum-Heap is the maximal element in the heap

Minimum-Heap

For each node x, x ≤ left(x) and x ≤ right(x)The root in a Minimum-Heap is the minimal element in the heap

Heap-Array

A heap can be represented using an array: 1.A[1] - the root of the heap (not A[0])2.length[A] - number of elements in the array 3.heap-size[A] - number of elements in the heap represented by the array A 4.For each index i :

Parent(i) = Left(i) = 2i Right(i) = 2i+1

Page 3: תרגול 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).

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 MaxHeapify

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

Page 4: תרגול 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).

Heaps

דוגמה:

את • 9הוסיפוהערך 1. את 9מוסיפים

כעלה Up-Heapifyמבצעים 2.

הערימה לתיקון

7

13

11

21

12

17

20

34

22

51

14

41 9

7 11 13 21 12 17 20 34 22 51 14 41 9

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Heap_size

מאביו – קטןמבצעים החלפה

Page 5: תרגול 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).

Heaps

7

13

11

21

12

9 20

34

22

51

14

41

17

7 11 13 21 12 9 20 34 22 51 14 41 17

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 דוגמה:

את • 9הוסיפוהערך 1. את 9מוסיפים

כעלה Up-Heapifyמבצעים 2.

הערימה לתיקון

Heap_size

מאביו – קטןמבצעים החלפה

מאביו – גדולמסיימים

Page 6: תרגול 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).

Heaps

דוגמה:

בצעו • כעתExtract-min

השורש 1. את מוציאים) ערכו) את ושומרים

את 2. לשורש מעביריםהאחרון העלה

Down-Heapifyמבצעים 3.לתיקון מהשורש

הערימה

7

911

21

12

13

20

34

22

51

14

41

17

7 11 9 21 12 13 20 34 22 51 14 41 17

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Heap_sizeKey = 7

17

Page 7: תרגול 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).

דוגמה:

בצעו • כעתExtract-min

השורש 1. את מוציאים) ערכו) את ושומרים

את 2. לשורש מעביריםהאחרון העלה

Down-Heapifyמבצעים 3.לתיקון מהשורש

הערימה

Heaps

17

911

21

12

13

20

34

22

51

14

41

17 11 9 21 12 13 20 34 22 51 14 41 17

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Heap_sizeKey = 7הקטן מבנו גדול

מבצעים– החלפה

Page 8: תרגול 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).

דוגמה:

בצעו • כעתExtract-min

השורש 1. את מוציאים) ערכו) את ושומרים

את 2. לשורש מעביריםהאחרון העלה

Down-Heapifyמבצעים 3.לתיקון מהשורש

הערימההערך 4. את מחזירים

שנשמר

Heaps

9

17

11

21

12

13

20

34

22

51

14

41

9 11 17 21 12 13 20 34 22 51 14 41 17

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Heap_sizeKey = 7

הקטן מבנו גדולמבצעים–

החלפה

הקטן מבנו קטןמסיימים–

Page 9: תרגול 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).

Question 3, Hבערימה • הציעו מערך בעזרת שמיוצגת

- ה במיקום הערך להוצאת הניחו ) iאלגוריתם(.iכי הערימה מגודל קטן

•? האלגוריתם של הריצה זמן מה

Page 10: תרגול 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).

1414

Question 3

הדגמה: Extract(H,2)בצעו •

7

911

21

12

13

20

34

22

51

9 11 17 21 12 13 20 34 22 51 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14

Page 11: תרגול 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).

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אם

Page 12: תרגול 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).

1414

Question 3

הדגמה: Extract(H,2)בצעו •

• key H[i]• H[i] H[heap_size] • heap_size -- • Down-Heapify(H, i)• Up-Heapify(H, i)• return key

7

911

21

12

13

20

34

22

51

9 11 17 21 12 13 20 34 22 51 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Heap_size

14

Key = 11

i

הקטן מבנו גדולמבצעים–

החלפה

הקטן מבנו קטןמסיימים–

מאביו – גדולמסיימים

Page 13: תרגול 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).

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)

Page 14: תרגול 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).

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 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(C2) = O(1)– (Another solution would be ordering all the keys in the first C levels

of the heap)

Page 15: תרגול 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).

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?

Page 16: תרגול 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).

Question 1Solution:• 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.

Page 17: תרגול 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).

Question 1את הדגמה: האלגוריתם 5מצאו בעזרת בערימה ביותר הקטנים המפתחות

7

13

11

21

12

17

20

34

22

51

14

41 9

לערימה 17. נכנסהחדשה

.27 , נכנסים 13ו 11יוצא

.311 , 21ו 12יוצא נכנסים

.412 , 51ו 14יוצא נכנסים

.513 , 20ו 17יוצא נכנסים

יוצא 614.

Page 18: תרגול 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).

Question 2Analyze 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))

Page 19: תרגול 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).

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)

Page 20: תרגול 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).

Question 4Solution :• 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 H2 have 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).

Page 21: תרגול 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).

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

Page 22: תרגול 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).

Question 6Solution:• 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)

Page 23: תרגול 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).

Question 6Solution:• 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

Page 24: תרגול 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).

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

Page 25: תרגול 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).

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 d1 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)

Page 26: תרגול 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).

Lempel-Ziv

•-Lempel Ziv