8 Heaps Huffman code 1 DS 202 ps

  • Slides: 41
Download presentation
8 תרגול Heaps Huffman code 1 DS 202 -ps 08 9/13/2021

8 תרגול Heaps Huffman code 1 DS 202 -ps 08 9/13/2021

Heaps Heap A binary heap can be considered as a complete binary tree, (the

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 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 3 DS 202 -ps 08 9/13/2021

Heaps Actions on Heaps 4 Insert O(log(n)) Max O(1) Extract-Max O(log(n)) Build-Heap O(n) Down-Heapify(H,

Heaps Actions on Heaps 4 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)) DS 202 -ps 08 9/13/2021

 Heaps 12 13 14 דוגמה : 9 11 41 10 14 9 8

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

 Heaps 12 13 14 דוגמה : 17 11 41 10 14 9 8

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

 שאלה 1 12 13 14 11 10 14 הדגמה : • בצעו )

שאלה 1 12 13 14 11 10 14 הדגמה : • בצעו ) Extract(H, 1 9 8 7 6 5 4 3 2 1 0 51 22 34 20 17 12 21 13 11 9 9 רעיונות? 13 20 11 17 12 14 9/13/2021 DS 202 -ps 08 21 51 34 22 11

1 שאלה הציעו אלגוריתם להוצאת הערך , שמיוצגת בעזרת מערך H • בערימה .

1 שאלה הציעו אלגוריתם להוצאת הערך , שמיוצגת בעזרת מערך H • בערימה . ( קטן מגודל הערימה i במערך )הניחו כי i- במיקום ה ? • מה זמן הריצה של האלגוריתם Solution: Extract(H, i) • key H[i] • H[i] H[heap_size-1] /*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-1] 12 DS 202 -ps 08 9/13/2021

 i שאלה 1 12 13 14 הדגמה : • בצעו ) Extract(H, 1

i שאלה 1 12 13 14 הדגמה : • בצעו ) Extract(H, 1 ] key H[i ] H[i] H[heap_size-1 heap_size- ) Down-Heapify(H, i ) Up-Heapify(H, i return key • • • 11 10 14 9 8 7 6 5 4 3 51 22 34 20 13 12 21 9 11 7 Key = 11 Heap_size 7 גדול מאביו – מסיימים גדול מבנו הקטן – מבצעים החלפה 9 11 קטן מבנו הקטן – מסיימים 20 13 אם כך , מדוע ייתכן שנצטרך לבצע ? up-heapify 9/13/2021 2 1 0 DS 202 -ps 08 12 14 21 51 34 22 13

1 שאלה i 0 1 2 3 4 5 6 7 8 9 7

1 שאלה i 0 1 2 3 4 5 6 7 8 9 7 11 15 21 12 17 20 34 22 51 Key = 17 10 11 14 Heap_size 7 11 15 גדול מבנו הקטן – מסיימים 21 : הדגמה Extract(H, 5) • בצעו • • • key H[i] H[heap_size-1] heap_size -Down-Heapify(H, i) Up-Heapify(H, i) 20 17 12 12 13 14 – קטן מאביו מחליפים 34 22 14 51 14 DS 202 -ps 08 9/13/2021

i 1 שאלה 0 1 2 3 4 5 7 11 15 21 12

i 1 שאלה 0 1 2 3 4 5 7 11 15 21 12 14 20 Key = 17 6 7 8 9 10 34 22 51 11 : הדגמה Extract(H, 5) • בצעו Heap_size 7 – גדול מאביו מסיימים 11 15 21 34 12 22 15 12 13 14 • • • key H[i] H[heap_size-1] heap_size-Down-Heapify(H, i) Up-Heapify(H, i) return key 20 14 51 DS 202 -ps 08 9/13/2021

2 שאלה • 17 DS 202 -ps 08 9/13/2021

2 שאלה • 17 DS 202 -ps 08 9/13/2021

2 שאלה • 19 DS 202 -ps 08 9/13/2021

2 שאלה • 19 DS 202 -ps 08 9/13/2021

2 שאלה • 21 DS 202 -ps 08 9/13/2021

2 שאלה • 21 DS 202 -ps 08 9/13/2021

3 שאלה • 23 DS 202 -ps 08 9/13/2021

3 שאלה • 23 DS 202 -ps 08 9/13/2021

4 שאלה • 24 DS 202 -ps 08 9/13/2021

4 שאלה • 24 DS 202 -ps 08 9/13/2021

4 שאלה • 25 DS 202 -ps 08 9/13/2021

4 שאלה • 25 DS 202 -ps 08 9/13/2021

5 שאלה • 26 DS 202 -ps 08 9/13/2021

5 שאלה • 26 DS 202 -ps 08 9/13/2021

5 שאלה • Worst case time analysis: for d 1 to k Build-Heap :

5 שאלה • Worst case time analysis: for d 1 to k Build-Heap : O(k) – done 1 time B[d] (d, 1, Ad[1]) Extract-Min : O(log k) – done n times Build-Heap(B) /*By the order of Ad [1] */ Heap-Insert : O(log k) – done n times for j=1 to n Total: O(nlogk) (d, i, x) Extract-Min(B) M[j] x if i < Ad. length then Heap-Insert(B, (d, i+1, Ad[i+1])) 27 DS 202 -ps 08 9/13/2021

 שאלה 5 דוגמה : ערימת מינימום 1, 1, 2 3, 1, 4 9/13/2021

שאלה 5 דוגמה : ערימת מינימום 1, 1, 2 3, 1, 4 9/13/2021 2, 1, 3 DS 202 -ps 08 A 3 A 2 A 1 4 3 2 7 8 5 11 9 6 14 12 10 16 18 15 27 30 22 32 35 23 28

Pseudo code • Huffman (C) n ← |C| Q ← { new priority queue

Pseudo code • Huffman (C) n ← |C| Q ← { new priority queue for the letters in C } for i ← 1 to n-1 z ← allocate new node x ← Extract_Min(Q) y ← Extract_Min(Q) z. left ← x z. right ← y frequency (z) ← frequency (x) + frequency (y) Insert(Q, z) 33 DS 202 -ps 08 9/13/2021

Example • Example Huffman tree with 4 symbols (C={e, s, x, y}) • Frequencies:

Example • Example Huffman tree with 4 symbols (C={e, s, x, y}) • Frequencies: x=1 y=1 s=7 e=20 • Encoding: e: 1 s: 01 x: 000 y: 001 34 DS 202 -ps 08 9/13/2021

 דוגמת הרצה • 9/13/2021 DS 202 -ps 08 36

דוגמת הרצה • 9/13/2021 DS 202 -ps 08 36

 דוגמת הרצה MISSISSIPPI RIVER 1100000101001001000011111010011011110101 = 46 bits M I S P R

דוגמת הרצה MISSISSIPPI RIVER 1100000101001001000011111010011011110101 = 46 bits M I S P R V E _ 37 1 5 4 2 2 1 17 0 0 9 1 1 5(I) 4(S) 8 0 0 2(P) 4 1 1 0 2(R) 2 0 1 4 1 2 0 1 1(M) 1(V) 1(E) 1(_) DS 202 -ps 08 9/13/2021

1 שאלה • 41 DS 202 -ps 08 9/13/2021

1 שאלה • 41 DS 202 -ps 08 9/13/2021

1 שאלה • 42 DS 202 -ps 08 9/13/2021

1 שאלה • 42 DS 202 -ps 08 9/13/2021

2 שאלה • tree diagram Explanation 43 DS 202 -ps 08 9/13/2021

2 שאלה • tree diagram Explanation 43 DS 202 -ps 08 9/13/2021