1 / 20

CPSC 335

CPSC 335. Compression and Huffman Coding Dr. Marina Gavrilova Computer Science University of Calgary Canada. Lecture Overview. Huffman Coding Non-determinism of the algorithm Implementations: Singly-linked List Doubly-linked list Recursive top-down Using heap Adaptive Huffman coding.

Download Presentation

CPSC 335

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC 335 Compression and Huffman Coding Dr. Marina Gavrilova Computer Science University of Calgary Canada

  2. Lecture Overview • Huffman Coding • Non-determinism of the algorithm • Implementations: • Singly-linked List • Doubly-linked list • Recursive top-down • Using heap • Adaptive Huffman coding

  3. Huffman Coding • Algorithm is used to assign a codework to each character in the text according to their frequencies. The codework is usually represented as a bitstring. • Algorithm starts with the set of individual trees, consisting of a single node, sorted in the order of increasing character probabilities. • Then two trees with the smallest probabilities are selected and processed so that they become the left and the right sub-tree of the parent node, combining their probabilities. • In the end, 0 are assigned to all left branches of the tree, 1 to all right branches, and the codework for all leaves (characters) of the tree is generated.

  4. Non-determinism of the Huffman Coding

  5. Non-determinism of the Huffman Coding

  6. Huffman Algorithm Implementation – Linked List • Implementation depends on the ways to represent the priority queue, which requires removing two smallest probabilities and inserting the new probability in the proper positions. • The first way to implement the priority queue is the singly linked list of references to trees, which resembles the algorithm presented in the previous slides. • The tree with the smallest probability is replaced by the newly created tree. • From the trees with the same probability, the first trees encountered are chosen.

  7. Doubly Linked List • All probability nodes are first ordered, the first two trees are always removed. • The new tree is inserted at the end of the list in the sorted order. • A doubly-linked list of references to trees with immediate access to the beginning and to the end of this list is used.

  8. Doubly Linked-List implementation

  9. Recursive Implementation • Top-down approach for building a tree starting from the highest probability. The root probability is known if lower probabilities, in the root’s children, have been determined, the latter are known if the lower probabilities have been computed etc. • Thus, the recursive algorithm can be used.

  10. Implementation using Heap • The min-heap of probabilities is built. • The highest probability is put in the root. • Next, the heap property is restored • The smallest probability is removed and the root probability is set to the sum of two smallest probabilities. • The processing is complete when there is only one node in the heap left.

  11. Huffman implementation with a heap

  12. Huffman Coding for pairs of characters

  13. Adaptive Huffman Coding • Devised by Robert Gallager and improved by Donald Knuth. • Algorithm is based on the sibling property: if each node has a sibling, and the breadth-first right-to-left tree traversal generates a list of nodes with non-increasing frequency counters, it is a Huffman tree. • In adaptive Huffman coding, the tree includes a counter for each symbol updated every time corresponding symbol is being coded. • Checking whether the sibling property holds ensures that the tree under construction is a Huffman tree. If the sibling property is violated, the tree is restored.

  14. Adaptive Huffman Coding

  15. Adaptive Huffman Coding

  16. Sources • Web links: • MP3 Converter: http://www.mp3-onverter.com/mp3codec/huffman_coding.htm • Practical Huffman Coding: http://www.compressconsult.com/huffman/ • Drozdek Textbook - Chapter 11

  17. Shannon-Fano • In the field of data compression, Shannon–Fano coding, named after Claude Shannon and Robert Fano, is a technique for constructing a prefix code based on a set of symbols and their probabilities (estimated or measured). • It is suboptimal in the sense that it does not achieve the lowest possible expected code word length like Huffman coding; however unlike Huffman coding, it does guarantee that all code word lengths are within one bit of their theoretical ideal – entropy.

  18. Shannon-Fano Coding • For a given list of symbols, develop a corresponding list of probabilities or frequency counts so that each symbol’s relative frequency of occurrence is known. • Sort the lists of symbols according to frequency, with the most frequently occurring symbols at the left and the least common at the right. • Divide the list into two parts, with the total frequency counts of the left part being as close to the total of the right as possible. • The left part of the list is assigned the binary digit 0, and the right part is assigned the digit 1. This means that the codes for the symbols in the first part will all start with 0, and the codes in the second part will all start with 1. • Recursively apply the steps 3 and 4 to each of the two halves, subdividing groups and adding bits to the codes until each symbol has become a corresponding code leaf on the tree.

  19. Shannon-Fano example

  20. Shannon-Fano • References • Shannon, C.E. (July 1948). "A Mathematical Theory of Communication". Bell System Technical Journal27: 379–423. http://cm.bell-labs.com/cm/ms/what/shannonday/shannon1948.pdf. • Fano, R.M. (1949). "The transmission of information". Technical Report No. 65 (Cambridge (Mass.), USA: Research Laboratory of Electronics at MIT).

More Related