Function Repository Resource:

HuffmanEncode

Source Notebook

Find a Huffman encoding from a string

Contributed by: Wolfram Staff (original content by Stephen Wolfram)

ResourceFunction["HuffmanEncode"]["string"]

gives an optimal binary Huffman encoding for a string.

Details

Huffman encoding is featured in A New Kind of Science, page 1071.
ResourceFunction["HuffmanEncode"] returns an association with the following components:
"Symbols"unique symbols in the input string
"CodeWords"binary code words corresponding to each symbol
"Encoding"the Huffman-encoded version of the string

Examples

Basic Examples (2) 

Find the Huffman encoding for a string:

In[1]:=
ResourceFunction[
 "HuffmanEncode"]["compression is based on redundancy"]
Out[1]=

Use the resource function HuffmanDecode to decode the Huffman encoding:

In[2]:=
ResourceFunction["HuffmanDecode"][%]
Out[2]=

Properties and Relations (1) 

The resource function HuffmanDecode is the inverse of HuffmanEncode:

In[3]:=
encoded = ResourceFunction["HuffmanEncode"]["the cat in the hat"]
Out[3]=
In[4]:=
ResourceFunction["HuffmanDecode"][encoded]
Out[4]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 2.0.0 – 20 March 2023
  • 1.0.0 – 31 December 2018

Source Metadata

Related Resources

License Information