Slide Lập trình C nâng cao - Fit Lec 11 (HUST) GV.AnhTT

LI
lienhejb
0 次下载
22
0

资料内容

第 1 页

goupee.vn goupee.com Data compression anhtt-fit@mail.hut.edu.vn dungct@it-hut.edu.vn fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 2 页

goupee.vn goupee.com Data Compression  Data in memory have used fixed length for representation  For data transfer (in particular), this method is inefficient.  For speed and storage efficiencies, data symbols should use the minimum number of bits possible for representation.  Methods Used For Compression:  Encode high probability symbols with fewer bits  Shannon-Fano, Huffman, UNIX compact  Encode sequences of symbols with location of sequence in a dictionary  PKZIP, ARC, GIF, UNIX compress, V.42bis  Lossy compression  JPEG and MPEG fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 3 页

goupee.vn goupee.com Variable Length Bit Codings  Suppose ‘A’ appears 50 times in text, but ‘B’ appears only 10 times  ASCII coding assigns 8 bits per character, so total bits for ‘A’ and ‘B’ is 60 * 8 = 480  If ‘A’ gets a 4-bit code and ‘B’ gets a 12-bit code, total is 50 * 4 + 10 * 12 = 320 Compression rules:  Use minimum number of bits  No code is the prefix of another code  Enables left-to-right, unambiguous decoding fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 4 页

goupee.vn goupee.com Variable Length Bit Codings  No code is a prefix of another  For example, can’t have ‘A’ map to 10 and ‘B’ map to 100, because 10 is a prefix (the start of) 100.  Enables left-to-right, unambiguous decoding  That is, if you see 10, you know it’s ‘A’, not the start of another character. fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 5 页

goupee.vn goupee.com Variable-length encoding  Use different number of bits to encode different characters.  Ex. Morse code.  Issue: ambiguity.  • • • - - - • • •  SOS ?  IAMIE ?  EEWNI ?  V7O ? fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 6 页

goupee.vn goupee.com Huffman code  Constructed by using a code tree, but starting at the leaves  A compact code constructed using the binary Huffman code construction method Huffman code Algorithm ① Make a leaf node for each code symbol Add the generation probability of each symbol to the leaf node ② Take the two leaf nodes with the smallest probability and connect them into a new node Add 1 or 0 to each of the two branches The probability of the new node is the sum of the probabilities of the two connecting nodes ③ If there is only one node left, the code construction is completed. If not, go back to (2) fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 7 页

goupee.vn goupee.com Demo  65demo-huffman.ppt fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 8 页

goupee.vn goupee.com Compress a text  Consider the following short text: Eerie eyes seen near lake.  Count up the occurrences of all characters in the text Char Freq. Char Freq. Char Freq. E 1 y 1 k 1 e 8 s 2 . 1 r 2 n 2 i 1 a 2 space 4 l 1 fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 9 页

goupee.vn goupee.com Building a Tree  The queue after inserting all nodes E i y l k . r s n a s e p 1 1 1 1 1 1 2 2 2 2 8 4  Null Pointers are not shown fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 10 页

goupee.vn goupee.com Building a Tree  While priority queue contains two or more nodes  Create new node  Dequeue node and make it left subtree  Dequeue next node and make it right subtree  Frequency of new node equals sum of frequency of left and right children  Enqueue new node back into queue fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 11 页

goupee.vn goupee.com Building a Tree E i y l k . r s n a s e p 1 1 1 1 1 1 2 2 2 2 8 4 fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 12 页

goupee.vn goupee.com Building a Tree y l k . r s n a sp e 1 1 1 1 2 2 2 2 4 8 2 E i 1 1 fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 13 页

goupee.vn goupee.com Building a Tree y l k . r s n a sp e 2 1 1 1 1 2 2 2 2 4 8 E i 1 1 fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 14 页

goupee.vn goupee.com Building a Tree k . r s n a sp e 2 1 1 2 2 2 2 4 8 E i 1 1 2 y l 1 1 fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 15 页

goupee.vn goupee.com Building a Tree k . r s n a 2 2 sp e 1 1 2 2 2 2 4 8 y l E i 1 1 1 1 fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 16 页

goupee.vn goupee.com Building a Tree r s n a 2 2 sp e 2 2 2 2 4 8 y l E i 1 1 1 1 2 k . 1 1 fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 17 页

goupee.vn goupee.com Building a Tree r s n a 2 2 sp e 2 2 2 2 2 4 8 E i y l k . 1 1 1 1 1 1  To continue … fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 18 页

goupee.vn goupee.com At the end After enqueueing 26 this node there is only 16 one node left 10 4 in priority e 8 6 8 queue. 2 2 2 sp 4 4 4 E i y l k . 1 1 1 1 1 1 r s n a 2 2 2 2 fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 19 页

goupee.vn goupee.com How to implement ?  Reuse JRB to represent the tree  Each new node is created as a JRB node  The edges are directional from the parents to the children.  Two edges are created and marked using label 0 or 1 when a parent node is created.  Reuse Dllist or JRB to represent the priority queue  A queue node contains a key as the frequency of the related node in the tree  The queue node’s value is a pointer referencing to the node in the tree fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

第 20 页

goupee.vn goupee.com Quiz 1  Reuse the graph API defined in previous class to write a function that builds a Huffman tree from a string as the following typedef struct { Graph graph; JRB root; } HuffmanTree; HuffmanTree makeHuffman (char * buffer, int size); fb.com/goupee.vn CuuDuongThanCong.com https://fb.com/tailieudientucntt

显示前 20 页。下载资料查看完整内容。

评论 (0)

登录 以加入讨论

暂无评论。快来抢沙发吧!