Wolfram Research

Function Repository Resource:

HuffmanCodeWords

Source Notebook

Find optimal Huffman code words given a list of probabilities

Contributed by: Stephen Wolfram

ResourceFunction["HuffmanCodeWords"][list]

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

Details and Options

Used in A New Kind of Science, page 1071.

Examples

Basic Examples

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

Resource History

See Also

License Information