- Alibek Jakupov

# Information Theory: Step 3 Huffman Algorithm

Updated: May 6, 2019

## Prefix codes

**Prefix codes** (sometimes called "**prefix-free codes**“) is a type of code system (typically a variable-length code) distinguished by its possession of the "prefix property", which requires that there is no codeword in the system that is a prefix (initial segment) of any other code word in the system.

A prefix code is a uniquely decodable code: a receiver can identify each word without requiring a special marker between words.

Examples of prefix codes:

Shannon-Fano code

Huffman code

Country calling codes

In this article we are going to implement our simple version of Huffman Encoding.

In 1951,David A. Huffmanand his MIT Information Theory classmates were given the choice of a term paper or a final exam. The professor,Robert M. Fano, assigned a term paper on the problem of finding the most efficient binary code. Huffman, unable to prove any codes were the most efficient, was about to give up and start studying for the final when he hit upon the idea of using a frequency-sorted binary tree and quickly proved this method the most efficient.

In doing so, the student outdid his professor, who had worked with Information Theory inventorClaude Shannonto develop a similar code.

AHuffman codeis an optimal code found using the algorithm developed by David A. Huffman while he was a Ph.D. student at MIT, and published in the 1952 paper "A Method for the Construction of Minimum-Redundancy Codes".

Huffman code is one of the optimal codes using for lossless data compression.

The algorithm's output can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file).

Huffman's algorithm derives this table based on the estimated probability or frequency of occurrence (

*weight*) for each possible value of the source symbol.As in other entropy encoding methods, more common symbols are generally represented using fewer bits than less common symbols.

## Step 1

Characters (or any message to be encoded) of the original alphabet are setted in descending order of probability.

## Step 2

Sum the two minimum probabilities.

Characters of the original alphabet are setted again in descending order of new probabilities.

## Step 3

Repeat the step 2 again and again until there is only 1 symbol with probability = 1.

## Step 4

Construct a binary tree

## Step 5

Set weights/bits

## Step 6

Construct codewords

And our implementation of this algorithm:

And on __git hub__

## References:

**Arndt C.**Information Measures: Information and its Description in Science and Engineering.**Thomas Cover.**Elements Of Information Theory.