10 Huffman code Quicksort 1 10 152 PM

  • Slides: 61
Download presentation
10 תרגול Huffman code Quicksort 1 10 תרגול 152 נתונים מבני PM 4: 27:

10 תרגול Huffman code Quicksort 1 10 תרגול 152 נתונים מבני PM 4: 27: 48

Huffman code • Huffman coding - an encoding algorithm used for lossless data compression,

Huffman code • Huffman coding - an encoding algorithm used for lossless data compression, using a priority queue. • Algorithm description - Given data comprised of symbols from the set C (C can be the English alphabet, for example), Huffman code uses a priority queue (Minimum Heap based on symbol frequency) to assign encodings to the different symbols in the set. • The algorithm builds a binary tree (the Huffman tree) whose leafs are the elements of C. Every symbol in C is associated with a leaf in the Huffman tree. The binary encoding of a symbol is as long as the depth of the leaf associated with it, and contains a 0 bit for every left move and a 1 bit for every right move on the path from the root to that leaf. 2 10 תרגול 152 נתונים מבני PM 4: 27: 51

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) 3 10 תרגול 152 נתונים מבני PM 4: 27: 51

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 4 10 תרגול 152 נתונים מבני PM 4: 27: 51

 דוגמת הרצה MISSISSIPPI RIVER M I S P R V E _ 7

דוגמת הרצה MISSISSIPPI RIVER M I S P R V E _ 7 1 5 4 2 2 1 1 1 ISPRMVE_17 PRMVE_8 MVE_4 IS 9 I 5 S 4 PR 4 P 2 R 2 MV 2 E_2 M 1 V 1 E 1 10 תרגול 152 נתונים מבני _1 PM 4: 27: 53

 דוגמת הרצה MISSISSIPPI RIVER M I S P R V E _ 8

דוגמת הרצה MISSISSIPPI RIVER M I S P R V E _ 8 1 5 4 2 2 1 1 1 ISPRMVE_17 0 0 I 5 IS 9 1 S 4 1 PRMVE_8 1 0 MVE_4 PR 4 0 1 P 2 R 2 1 0 E_2 MV 2 0 1 M 1 V 1 E 1 _1 10 תרגול 152 נתונים מבני PM 4: 27: 53

 דוגמת הרצה MISSISSIPPI RIVER M I S P R V E _ 9

דוגמת הרצה MISSISSIPPI RIVER M I S P R V E _ 9 1 5 4 2 2 1 1 1 ISPRMVE_17 0 1 PRMVE_8 1 0 MVE_4 1 0 PR 4 E_2 MV 2 IS 9 0 1 1 0 1 I 5 S 4 P 2 R 2 M 1 V 1 E 1 _1 00 01 100 101 1100 1101 1110 1111 10 תרגול 152 נתונים מבני PM 4: 27: 54

 דוגמת הרצה MISSISSIPPI RIVER 1100 M I S P R V E _

דוגמת הרצה MISSISSIPPI RIVER 1100 M I S P R V E _ 10 1 5 4 2 2 1 1 1 ISPRMVE_17 0 1 PRMVE_8 1 0 MVE_4 1 0 PR 4 E_2 MV 2 IS 9 0 1 1 0 1 I 5 S 4 P 2 R 2 M 1 V 1 E 1 _1 00 01 100 101 1100 1101 1110 1111 10 תרגול 152 נתונים מבני PM 4: 27: 54

 דוגמת הרצה MISSISSIPPI RIVER 110000 M I S P R V E _

דוגמת הרצה MISSISSIPPI RIVER 110000 M I S P R V E _ 11 1 5 4 2 2 1 1 1 ISPRMVE_17 0 1 PRMVE_8 1 0 MVE_4 1 0 PR 4 E_2 MV 2 IS 9 0 1 1 0 1 I 5 S 4 P 2 R 2 M 1 V 1 E 1 _1 00 01 100 101 1100 1101 1110 1111 10 תרגול 152 נתונים מבני PM 4: 27: 55

 דוגמת הרצה MISSISSIPPI RIVER 11000001 M I S P R V E _

דוגמת הרצה MISSISSIPPI RIVER 11000001 M I S P R V E _ 12 1 5 4 2 2 1 1 1 ISPRMVE_17 0 1 PRMVE_8 1 0 MVE_4 1 0 PR 4 E_2 MV 2 IS 9 0 1 1 0 1 I 5 S 4 P 2 R 2 M 1 V 1 E 1 _1 00 01 100 101 1100 1101 1110 1111 10 תרגול 152 נתונים מבני PM 4: 27: 56

 דוגמת הרצה MISSISSIPPI RIVER 1100000101 M I S P R V E _

דוגמת הרצה MISSISSIPPI RIVER 1100000101 M I S P R V E _ 13 1 5 4 2 2 1 1 1 ISPRMVE_17 0 1 PRMVE_8 1 0 MVE_4 1 0 PR 4 E_2 MV 2 IS 9 0 1 1 0 1 I 5 S 4 P 2 R 2 M 1 V 1 E 1 _1 00 01 100 101 1100 1101 1110 1111 10 תרגול 152 נתונים מבני PM 4: 27: 57

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

דוגמת הרצה MISSISSIPPI RIVER 1100000101001001000011111010011011110101 = 46 bits M I S P R V E _ 14 1 5 4 2 2 1 1 1 ISPRMVE_17 0 1 PRMVE_8 1 0 MVE_4 1 0 PR 4 E_2 MV 2 IS 9 0 1 1 0 1 I 5 S 4 P 2 R 2 M 1 V 1 E 1 _1 00 01 100 101 1100 1101 1110 1111 10 תרגול 152 נתונים מבני PM 4: 27: 59

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

דוגמת הרצה MISSISSIPPI RIVER 1100000101001001000011111010011011110101 = 46 bits M I S P R V E _ 15 1 5 4 2 2 1 1 1 I 5 00 S 4 P 2 R 2 M 1 V 1 E 1 _1 01 100 101 1100 1101 1110 1111 10 תרגול 152 נתונים מבני PM 4: 28: 01

2 שאלה • tree diagram Explanation 21 10 תרגול 152 נתונים מבני PM 4:

2 שאלה • tree diagram Explanation 21 10 תרגול 152 נתונים מבני PM 4: 28: 09

 Quicksort PM 4: 28: 16 מבני נתונים 152 תרגול 10 25

Quicksort PM 4: 28: 16 מבני נתונים 152 תרגול 10 25

 האלגוריתם quick. Sort( A, low, high ) int partition( A, low, high )

האלגוריתם quick. Sort( A, low, high ) int partition( A, low, high ) if(high > low) pivot_value ← A[low] pivot ← partition(A, low, high) left ← low quick. Sort(A, low, pivot-1 ) pivot ← left quick. Sort(A, pivot+1, high) right ← high while ( left < right ) // Move left while item < pivot while( left < high && A[left] ≤ pivot_value) left++ // Move right while item > pivot quick. Sort(A, 0, length(A)-1) while( A[right] > pivot_value) right— // Make sure right has not passed left if( left < right ) SWAP(A, left, right) 27 // right is final position for the pivot A[low] ← A[right] ← pivot_item 10 תרגול 152 נתונים מבני return right PM 4: 28: 21

 דוגמה 4 1 7 3 8 6 2 Left Pivot 5 9 10

דוגמה 4 1 7 3 8 6 2 Left Pivot 5 9 10 Right // Move left while item < pivot while( left < high && A[left] ≤ pivot_value) left++ 28 10 תרגול 152 נתונים מבני PM 4: 28: 22

 דוגמה 4 1 7 3 8 6 2 Pivot Left 5 9 10

דוגמה 4 1 7 3 8 6 2 Pivot Left 5 9 10 Right // Move left while item < pivot while( left < high && A[left] ≤ pivot_value) left++ 29 10 תרגול 152 נתונים מבני PM 4: 28: 22

 דוגמה 4 Pivot 1 7 3 8 6 2 Left 5 9 10

דוגמה 4 Pivot 1 7 3 8 6 2 Left 5 9 10 Right // Move left while item < pivot while( left < high && A[left] ≤ pivot_value) left++ 30 10 תרגול 152 נתונים מבני PM 4: 28: 23

 דוגמה 4 Pivot 1 7 3 8 6 Left 2 5 9 10

דוגמה 4 Pivot 1 7 3 8 6 Left 2 5 9 10 Right // Move right while item > pivot while( A[right] > pivot_value) right-- 31 10 תרגול 152 נתונים מבני PM 4: 28: 24

 דוגמה 4 Pivot 1 7 3 8 6 Left 2 5 9 10

דוגמה 4 Pivot 1 7 3 8 6 Left 2 5 9 10 Right // Move right while item > pivot while( A[right] > pivot_value) right-- 32 10 תרגול 152 נתונים מבני PM 4: 28: 25

 דוגמה 4 Pivot 1 7 3 8 6 Left 2 5 9 10

דוגמה 4 Pivot 1 7 3 8 6 Left 2 5 9 10 Right // Move right while item > pivot while( A[right] > pivot_value) right-- 33 10 תרגול 152 נתונים מבני PM 4: 28: 26

 דוגמה 4 Pivot 1 7 3 8 6 Left 2 5 9 10

דוגמה 4 Pivot 1 7 3 8 6 Left 2 5 9 10 Right if( left < right ) \Make sure right has not passed left SWAP(A, left, right) 34 10 תרגול 152 נתונים מבני PM 4: 28: 26

 דוגמה 4 Pivot 1 2 3 8 6 Left 7 5 9 10

דוגמה 4 Pivot 1 2 3 8 6 Left 7 5 9 10 Right // Move left while item < pivot while( left < high && A[left] ≤ pivot_value) left++ 35 10 תרגול 152 נתונים מבני PM 4: 28: 27

 דוגמה 4 Pivot 1 2 3 8 6 Left 7 5 9 10

דוגמה 4 Pivot 1 2 3 8 6 Left 7 5 9 10 Right // Move left while item < pivot while( left < high && A[left] ≤ pivot_value) left++ 36 10 תרגול 152 נתונים מבני PM 4: 28: 27

 דוגמה 4 Pivot 1 2 3 8 6 7 5 9 10 Left

דוגמה 4 Pivot 1 2 3 8 6 7 5 9 10 Left Right // Move right while item > pivot while( A[right] > pivot_value) right-- 37 10 תרגול 152 נתונים מבני PM 4: 28: 30

 דוגמה 4 Pivot 1 2 3 8 6 7 5 9 10 Left

דוגמה 4 Pivot 1 2 3 8 6 7 5 9 10 Left Right // Move right while item > pivot while( A[right] > pivot_value) right-- 38 10 תרגול 152 נתונים מבני PM 4: 28: 31

 דוגמה 4 Pivot 1 2 3 8 6 7 5 9 10 Right

דוגמה 4 Pivot 1 2 3 8 6 7 5 9 10 Right Left // Move right while item > pivot while( A[right] > pivot_value) right-- 39 10 תרגול 152 נתונים מבני PM 4: 28: 32

 דוגמה 4 1 2 Pivot 3 8 6 7 5 9 10 Right

דוגמה 4 1 2 Pivot 3 8 6 7 5 9 10 Right Left // right is final position for the pivot A[low] ← A[right] ← pivot_value return right 40 10 תרגול 152 נתונים מבני PM 4: 28: 33

 דוגמה 9 10 5 7 6 8 2 9 10 8 7 6

דוגמה 9 10 5 7 6 8 2 9 10 8 7 6 5 . . . 3 2 9 10 8 7 6 5 3 2 PM 4: 28: 34 מבני נתונים 152 תרגול 10 4 4 2 1 3 1 1 41

Quicksort • stable sorting algorithms: maintain the relative order of records with equal keys

Quicksort • stable sorting algorithms: maintain the relative order of records with equal keys • Is the Quick. Sort version above stable? • No 43 10 תרגול 152 נתונים מבני PM 4: 28: 34

 פתרון - 3 שאלה Select(k, S) // returns k-th element in S. pick

פתרון - 3 שאלה Select(k, S) // returns k-th element in S. pick x in S partition S into: // Slightly different variant of partition() max(L) < x, E = {x}, x < min(G) if k ≤ length(L) // Searching for item ≤ x. return Select(k, L) else if k ≤ length(L) + length(E) // Found return x else // Searching for item ≥ x. return Select(k - length(L) - length(E), G) pivot , בגודלו k- ה האיבר – x : קבוצות 3 - ל S את מחלקים שלב בכל x- מ הגדולים האיברים כל -G x- מ הקטנים האיברים כל -L x- ל השווים האיברים כל -E G- ב או L- ב רקורסיבית אותו לחפש ממשיכים , x את מצאנו לא אם 45 10 תרגול 152 נתונים מבני PM 4: 28: 37