compression and huffman coding - university of...

16
Compression and Huffman Coding

Upload: trinhphuc

Post on 29-Jun-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

Compression and Huffman Coding

Huffman Coding Non-determinism of the algorithm Implementations: ◦ Singly-linked List ◦ Doubly-linked list ◦ Recursive top-down ◦ Using heap

Adaptive 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 the 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, o are assigned to all left branches of the tree, 1 to all right branches, and the codework for all leaves 9(characters) of the tree is generated.

Non-determinism of the Huffman Coding

Non-determinism of the Huffman Coding

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.

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.

Doubly Linked-List 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.

Recursive top-down algorithm

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.

Huffman implementation with a heap

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.

Adaptive Huffman Coding

What is Huffman Coding? Is algorithm non-deterministic and why? List all possible implementations (i.e. lsit,

recursive, heap). What is the main idea of adaptive Huffman

coding?

Web links: ◦ MP3 Converter http://www.mp3-

converter.com/mp3codec/huffman_coding.htm ◦ Practical Huffman Coding:

http://www.compressconsult.com/huffman/

Drozdek Textbook - Chapter 11