Function Repository Resource:

HuffmanCodeWords

Source Notebook

Find optimal Huffman code words given a list of probabilities

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

ResourceFunction["HuffmanCodeWords"][list]

gives optimal binary Huffman code words corresponding to probabilities in list.

Details

Used in A New Kind of Science, page 1071.

Examples

Basic Examples (2) 

Generate a list of code words:

In[1]:=
ResourceFunction["HuffmanCodeWords"][{1/2, 1/4, 1/8, 1/8}]
Out[1]=

Get code words from a Wikipedia article for letter frequency of the whole alphabet:

In[2]:=
KeyTake[LetterCounts[WikipediaData["computer"]], Alphabet[]]
Out[2]=
In[3]:=
ResourceFunction["HuffmanCodeWords"][Values[%]]
Out[3]=
In[4]:=
AssociationThread[Alphabet[] -> %]
Out[4]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.1 – 13 September 2021
  • 1.0.0 – 10 December 2018

Source Metadata

Related Resources

License Information