The Huffman code length calculator gives the average Huffman code length, the entropy, the coding efficiency, the redundancy, the saving against a fixed 2-bit code and the size of a message for a source with four symbols.
How the calculation works
- Enter the frequency of each of the four symbols, as counts or percentages.
- Enter the length of the message in symbols.
- Read the average length, compare it with the entropy and see the size of the encoded message.
Example
Four symbols with frequencies 45, 30, 15 and 10 have an entropy of 1.7822 bits/symbol. The Huffman code has an average length of 1.8000 bits/symbol, an efficiency of 99.01 % and a redundancy of 0.0178 bits/symbol. It saves 10.0 % against a fixed 2-bit code, so a message of 10000 symbols takes 2,250 bytes.
Why it matters
The calculator helps computer science students, programmers and engineers who study data compression, check an exercise on Huffman trees, or want to know how close a prefix code gets to the entropy limit before they choose a coding scheme for a protocol or a file format.
Good to know
Use the real frequencies of the symbols in your data, as counts or percentages, all greater than zero. The result is the average length of an optimal code symbol by symbol, and it does not include the cost of storing the code table or the header. Real data often have dependencies between symbols that arithmetic coding or dictionary methods exploit better. For more than four symbols, the same merging procedure applies, but this calculator is limited to four.
Related tools
The Shannon entropy calculator gives the theoretical limit for any number of symbols, the data encoding overhead calculator shows the cost of an encoding, the parity bit calculator adds error detection and the combinations calculator counts possible messages.