Adaptive Huffman coding

Adaptive Huffman coding (also called Dynamic Huffman coding) is an adaptive coding technique based on Huffman coding. It permits building the code as the symbols are being transmitted, having no initial knowledge of source distribution, that allows one-pass encoding and adaptation to changing conditions in data.

The benefit of one-pass procedure is that the source can be encoded in real time, though it becomes more sensitive to transmission errors, since just a single loss ruins the whole code.

Algorithms

There are a number of implementations of this method, the most notable are FGK (Faller-Gallager-Knuth) and Vitter algorithm.

FGK Algorithm

It is an online coding technique based on Huffman coding. Having no initial knowledge of occurrence frequencies, it permits dynamically adjusting the Huffman's tree as data are being transmitted. In a FGK Huffman tree, a special external node, called 0-node, is used to identify a newly-coming character. That is, whenever new data are encountered, output the path to the 0-node followed by the data. For a past-coming character, just output the path of the data in the current Huffman's tree. Most importantly, we have to adjust the FGK Huffman tree if necessary, and finally update the frequency of related nodes. As the frequency of a datum is increased, the sibling property of the Huffman's tree may be broken. The adjustment is triggered for this reason. It is accomplished by consecutive swappings of nodes, subtrees, or both. The data node is swapped with the highest-ordered node of the same frequency in the Huffman's tree, (or the subtree rooted at the highest-ordered node). All ancestor nodes of the node should also be processed in the same manner.

Since the FGK Algorithm has some drawbacks about the node-or-subtree swapping, Vitter proposed another algorithm to improve it.

Vitter algorithm

Some important terminologies & constraints :-

Blocks are interlinked by increasing order of their weights.

A leaf block always precedes internal block of same weight, thus maintaining the invariant.

'NYT(Not Yet Transferred) is special node and used to represents symbols which are 'not yet transferred.

Slide_And_Increment(leaf node) sliding starts. P is a leaf node.
Slide_And_Increment(leaf node) sliding step 2. As P is leaf node, it slides in front of next block nodes of equal weight.
Slide_And_Increment(leaf node) sliding step 3. Here we increase the current weight by 1.
Slide_And_Increment(leaf node) sliding step 4. Method comes to an end. P is the new parent.
Slide_And_Increment(internal node) sliding starts. P is an internal node.
Slide_And_Increment(internal node) sliding step 2. Node P slides in front of next block of nodes, with weight wt+1.
Slide_And_Increment(internal node) sliding step 3. Now we increase the weight by 9. Thus the invariant is maintained as the current node is an internal node and should occur in front of leaf nodes of equal weight as we have increased the weight.
Slide_And_Increment(internal node) sliding step 4. Now the 'P' points to the former parent ( as in the case of internal node according to algorithm)

Algorithm for adding a symbol

     leaf_to_increment = NULL
     p = pointer to the leaf node containing to the next symbol
     IF (p is NYT) THEN
           Extend P by adding two children
           Left child becomes new NYT and right child is the new symbol leaf node
           p = parent of new symbol leaf node
           leaf_to_increment = Right Child of p
     ELSE
           Swap p with leader of its block
           IF (new p is sibling to NYT) THEN
                 leaf_to_increment = p
                 p = parent of p
     WHILE (p != NULL)
           Slide_And_Increment(p)
     IF (leaf_to_increment != NULL)
           Slide_And_Increment(leaf_to_increment)

Function Slide_And_Increment(p)

     previous_p = parent of p
     IF (p is an internal node) THEN
             Slide p in the tree higher than the leaf nodes of weight wt + 1
             increase weight of p by 1
             p = previous_p
     ELSE
             Slide p in the tree higher than the internal nodes of weight wt
             increase weight of p by 1
             p = new parent of p.

Encoder and decoder start with only the root node, which has the maximum number. In the beginning it is our initial NYT node.

When we transmit an NYT symbol, we have to transmit code for the NYT node, then for its generic code.

For every symbol that is already in the tree, we only have to transmit code for its leaf node.

Example

Encoding "abb" gives 01100001 001100010 11.

Step 1:

Start with an empty tree.

For "a" transmit its binary code.

Step 2:

NYT spawns two child nodes: 254 and 255, both with weight 0. Increase weight for root and 255. Code for "a", associated with node 255, is 1.

For "b" transmit 0 (for NYT node) then its binary code.

Step 3:

NYT spawns two child nodes: 252 for NYT and 253 for leaf node, both with weight 0. Increase weights for 253, 254, and root. To maintain Vitter’s invariant that all leaves of weight w precede (in the implicit numbering) all internal nodes of weight w, the branch starting with node 254 should be swop (in terms of symbols and weights, but not number ordering) with node 255. Code for "b" is 11.

For the second "b" transmit 11.

It should be noted that for the convenience of explanation this step doesn't exactly follow Vitter's algorithm,[1] but the effects are equivalent.

Step 4:

Go to leaf node 253. Notice we have two blocks with weight 1. Node 253 and 254 is one block (consisting of leaves), node 255 is another block (consisting of internal nodes). For node 253, the biggest number in its block is 254, so swap the weights and symbols of nodes 253 and 254. Now node 254 and the branch starting from node 255 satisfy the SlideAndIncrement condition[1] and hence must be swop. At last increase node 255 and 256’s weight.

Future code for "b" is 1, and for "a" is now 01, which reflects their frequency.

References

  1. 1 2 "Adaptive Huffman Coding". Cs.duke.edu. Retrieved 2012-02-26.

External links

This article is issued from Wikipedia - version of the 8/13/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.