Huffman Codes ASCII is a fixed length 7 bit code that uses the same number of bits to define each character regardless of how frequently it occurs. Huffman codes can represent data more efficiently by assigning shorter codes to characters that occur frequently and longer codes to characters that occur less frequently If the bit patterns are chosen carefully, ambiguity can be avoided This can be done by making use of the properties of a binary tree, called a Huffman Tree 2/11/10 05-Huffman
Example 1 AGAIN AND AGAIN AND AGAIN Derive a Huffman Code to encode the phrase: AGAIN AND AGAIN AND AGAIN This was part of a question in 2000, worth just 4 marks (i.e. it should be done in about 7 minutes) It is rather hard for an exam question. 2/11/10 05-Huffman
Step 1- Count the frequency of each symbol 8 G 3 I N 5 D 2 _ 4 Total 25 2/11/10 05-Huffman
Step 2 – Write out symbols and in order of their frequency 8 5 4 3 2 A N _ G I D If two symbols have the same frequency then they can be ordered arbitrarily. The decision will alter the coding of each symbol, but not the number of bits used to represent each symbol 2/11/10 05-Huffman
Step 3 – Combine the two lowest frequency symbols from the right into a tree and add their frequencies together. 2/11/10 05-Huffman
Step 4 – Reorder the tree using the new combined value 2/11/10 05-Huffman
Keep repeating Steps 3 and 4 until there is a single tree Combine 2/11/10 05-Huffman
Re-order 2/11/10 05-Huffman
Combine 2/11/10 05-Huffman
Re-order 2/11/10 05-Huffman
Combine 2/11/10 05-Huffman
Re-order 2/11/10 05-Huffman
The Huffman Tree is now complete Combine The Huffman Tree is now complete 2/11/10 05-Huffman
To derive a code for a symbol, start at the root and follow the path to the symbol. Write a 0 every time you go left and a 1 every time you go right 1 A 00 N 11 _ 010 G 011 I 100 D 101 2/11/10 05-Huffman
Encode the String “GAINING” 011 A 00 I 100 N 11 1 2/11/10 05-Huffman
Decode the Huffman Code 011001001110011011 G A I N I N G Traverse the tree by moving left for 0 and right for 1 until you reach a leaf, write down the symbol. Start again for the next symbol 1 2/11/10 05-Huffman
Question from the 2002 exam Given an alphabet code below that is derived from a Huffman coding, decode the entire message below: 010000111000000000000010001011111001010111100 A 000 H 100 S 11101 B 001 N 101 T 11110 C 010000 P 110 U 11111 D 010001 Q 111000 E 010110 G 011 R 111001 F 010111 2/11/10 05-Huffman
Huffman Tree for the 2002 exam question 2/11/10 05-Huffman
Answer from the 2002 exam CQAAADGRFH 2/11/10 05-Huffman
Exercise Using the following Huffman code mappings, send a short (one word) Huffman-coded message to your neighbour for him/her to decode using a Huffman tree 2/11/10 05-Huffman
Huffman Code Table for the Alphabet 2/11/10 05-Huffman
Huffman Tree for the Alphabet I S O N R H L W V Y D M G P U B T C F K X J Q Z Root 2/11/10 05-Huffman