Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms Huffman compression: An Application of Binary Trees and Priority Queues.

Similar presentations


Presentation on theme: "Data Structures and Algorithms Huffman compression: An Application of Binary Trees and Priority Queues."— Presentation transcript:

1 Data Structures and Algorithms Huffman compression: An Application of Binary Trees and Priority Queues

2 CS 102 Encoding and Compression  Fax Machines  ASCII  Variations on ASCII  min number of bits needed  cost of savings  patterns  modifications

3 CS 102 Purpose of Huffman Coding  Proposed by Dr. David A. Huffman in 1952  “ A Method for the Construction of Minimum Redundancy Codes ”  Applicable to many forms of data transmission  Our example: text files

4 CS 102 The Basic Algorithm  Huffman coding is a form of statistical coding  Not all characters occur with the same frequency!  Yet all characters are allocated the same amount of space  1 char = 1 byte, be it e or x

5 CS 102 The Basic Algorithm  Any savings in tailoring codes to frequency of character?  Code word lengths are no longer fixed like ASCII.  Code word lengths vary and will be shorter for the more frequently used characters.

6 CS 102 The (Real) Basic Algorithm 1.Scan text to be compressed and tally occurrence of all characters. 2.Sort or prioritize characters based on number of occurrences in text. 3.Build Huffman code tree based on prioritized list. 4.Perform a traversal of tree to determine all code words. 5.Scan text again and create new file using the Huffman codes.

7 CS 102 Building a Tree  Consider the following short text:  Eerie eyes seen near lake.  Count up the occurrences of all characters in the text

8 CS 102 Building a Tree Eerie eyes seen near lake.  What characters are present? E e r i space y s n a r l k.

9 CS 102 Building a Tree Prioritize characters  Create binary tree nodes with character and frequency of each character  Place nodes in a priority queue  The lower the occurrence, the higher the priority in the queue

10 CS 102 Building a Tree Prioritize characters  Uses binary tree nodes public class HuffNode { public char myChar; public int myFrequency; public HuffNode myLeft, myRight; } priorityQueue myQueue;

11 CS 102 Building a Tree  The queue after inserting all nodes  Null Pointers are not shown E1E1 i1i1 y1y1 l1l1 k1k1.1.1 r2r2 s2s2 n2n2 a2a2 sp 4 e8e8

12 CS 102 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

13 CS 102 Building a Tree E1E1 i1i1 y1y1 l1l1 k1k1.1.1 r2r2 s2s2 n2n2 a2a2 sp 4 e8e8

14 CS 102 Building a Tree E1E1 i1i1 y1y1 l1l1 k1k1.1.1 r2r2 s2s2 n2n2 a2a2 sp 4 e8e8 2

15 CS 102 Building a Tree E1E1 i1i1 y1y1 l1l1 k1k1.1.1 r2r2 s2s2 n2n2 a2a2 sp 4 e8e8 2

16 CS 102 Building a Tree E1E1 i1i1 k1k1.1.1 r2r2 s2s2 n2n2 a2a2 sp 4 e8e8 2 y1y1 l1l1 2

17 CS 102 Building a Tree E1E1 i1i1 k1k1.1.1 r2r2 s2s2 n2n2 a2a2 sp 4 e8e8 2 y1y1 l1l1 2

18 CS 102 Building a Tree E1E1 i1i1 r2r2 s2s2 n2n2 a2a2 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2

19 CS 102 Building a Tree E1E1 i1i1 r2r2 s2s2 n2n2 a2a2 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2

20 CS 102 Building a Tree E1E1 i1i1 n2n2 a2a2 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4

21 CS 102 Building a Tree E1E1 i1i1 n2n2 a2a2 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4

22 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4

23 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4

24 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4

25 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4

26 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6

27 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 What is happening to the characters with a low number of occurrences?

28 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8

29 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8

30 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10

31 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10

32 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10 16

33 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10 16

34 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10 16 26

35 CS 102 Building a Tree E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10 16 26 After enqueueing this node there is only one node left in priority queue.

36 CS 102 Building a Tree Dequeue the single node left in the queue. This tree contains the new code words for each character. Frequency of root node should equal number of characters in text. E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10 16 26 Eerie eyes seen near lake.  26 characters

37 CS 102 Encoding the File Traverse Tree for Codes  Perform a traversal of the tree to obtain new code words  Going left is a 0 going right is a 1  code word is only completed when a leaf node is reached E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10 16 26

38 CS 102 Encoding the File Traverse Tree for Codes CharCode E0000 i0001 y0010 l0011 k0100.0101 space011 e10 r1100 s1101 n1110 a1111 E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10 16 26

39 CS 102 Encoding the File  Rescan text and encode file using new code words Eerie eyes seen near lake. CharCode E0000 i0001 y0010 l0011 k0100.0101 space011 e10 r1100 s1101 n1110 a1111 000010110000011001110001010110110100 111110101111110001100111111010010010 1  Why is there no need for a separator character?.

40 CS 102 Encoding the File Results  Have we made things any better?  73 bits to encode the text  ASCII would take 8 * 26 = 208 bits 000010110000011001110001010110110100 111110101111110001100111111010010010 1  If modified code used 4 bits per character are needed. Total bits 4 * 26 = 104. Savings not as great.

41 CS 102 Decoding the File  How does receiver know what the codes are?  Tree constructed for each text file.  Considers frequency for each file  Big hit on compression, especially for smaller files  Tree predetermined  based on statistical analysis of text files or file types  Data transmission is bit based versus byte based

42 CS 102 Decoding the File  Once receiver has tree it scans incoming bit stream  0  go left  1  go right E1E1 i1i1 sp 4 e8e8 2 y1y1 l1l1 2 k1k1.1.1 2 r2r2 s2s2 4 n2n2 a2a2 4 4 6 8 10 16 26 101000110111101111011 11110000110101

43 CS 102 Summary  Huffman coding is a technique used to compress files for transmission  Uses statistical coding  more frequently used symbols have shorter code words  Works well for text and fax transmissions  An application that uses several data structures

44 44 Huffman Compression 1)Is the code correct?  Based on the way the tree is formed, it is clear that the codewords are valid  Prefix Property is assured, since each codeword ends at a leaf  all original nodes corresponding to the characters end up as leaves 2)Does it give good compression?  For a block code of N different characters,  log 2 N  bits are needed per character  Thus a file containing M ASCII characters, 8M bits are needed

45 45 Huffman Compression  Given Huffman codes {C 0,C 1,…C N-1 } for the N characters in the alphabet, each of length |C i |  Given frequencies {F 0,F 1,…F N-1 } in the file  Where sum of all frequencies = M  The total bits required for the file is:  Sum from 0 to N-1 of (|C i | * F i )  Overall total bits depends on differences in frequencies  The more extreme the differences, the better the compression  If frequencies are all the same, no compression  See example from board

46 46 Huffman Shortcomings  What is Huffman missing?  Although OPTIMAL for single character (word) compression, Huffman does not take into account patterns / repeated sequences in a file  Ex: A file with 1000 As followed by 1000 Bs, etc. for every ASCII character will not compress AT ALL with Huffman  Yet it seems like this file should be compressable  We can use run-length encoding in this case (see text)  However run-length encoding is very specific and not generally effective for most files (since they do not typically have long runs of each character)


Download ppt "Data Structures and Algorithms Huffman compression: An Application of Binary Trees and Priority Queues."

Similar presentations


Ads by Google