Content deleted Content added
Ira Leviton (talk | contribs) m Fixed a reference. Please see Category:CS1 errors: missing pipe. |
ReidMoffat (talk | contribs) m Link fix |
||
(29 intermediate revisions by 24 users not shown) | |||
Line 1:
{{distinguish|Hamming code}}
{{Short description|Technique to compress data
[[Image:Huffman tree 2.svg|thumb|Huffman tree generated from the exact frequencies of the text "this is an example of a huffman tree". Encoding the sentence with this code requires 135 (or 147) bits, as opposed to 288 (or 180) bits if 36 characters of 8 (or 5) bits were used. (This assumes that the code tree structure is known to the decoder and thus does not need to be counted as part of the transmitted information.) The frequencies and codes of each character are shown in the accompanying table.▼
{{Use dmy dates|date=May 2019|cs1-dates=y}}
▲[[Image:Huffman tree 2.svg|thumb|Huffman tree generated from the exact frequencies of the text "this is an example of a huffman tree". Encoding the sentence with this code requires 135 (or 147) bits, as opposed to 288 (or 180) bits if 36 characters of 8 (or 5) bits were used
{| class="wikitable sortable"
Line 36 ⟶ 38:
|-
|x ||1||10010
|}
]] In [[computer science]] and [[information theory]], a '''Huffman code''' is a particular type of optimal [[prefix code]] that is commonly used for [[Lossless compression|lossless data compression]]. The process of finding or using such a code is '''Huffman coding''', an algorithm developed by [[David A. Huffman]] while he was a [[Doctor of Science|Sc.D.]] student at [[Massachusetts Institute of Technology|MIT]], and published in the 1952 paper "A Method for the Construction of Minimum-Redundancy Codes".<ref name=":0">{{Cite journal | last1 = Huffman | first1 = D. |author-link1=David A. Huffman| title = A Method for the Construction of Minimum-Redundancy Codes | doi = 10.1109/JRPROC.1952.273898 | journal = [[Proceedings of the IRE]]| volume = 40 | issue = 9 | pages = 1098–1101 | year = 1952 | url = http://compression.ru/download/articles/huff/huffman_1952_minimum-redundancy-codes.pdf}}</ref>
The output from Huffman's algorithm can be viewed as a [[variable-length code]] table for encoding a source symbol (such as a character in a file). The algorithm derives this table from 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. Huffman's method can be efficiently implemented, finding a code in time [[linear time|linear]] to the number of input weights if these weights are sorted.<ref>{{cite journal | first = Jan | last = Van Leeuwen | author-link = Jan van Leeuwen | url = http://www.staff.science.uu.nl/~leeuw112/huffman.pdf | title = On the construction of Huffman trees | journal = ICALP | year =1976 | pages = 382–410 | access-date = 20 February 2014}}</ref> However, although optimal among methods encoding symbols separately, Huffman coding [[#Optimality|is not always optimal]] among all compression methods
== History ==
Line 53 ⟶ 57:
== Problem definition ==
{{More citations needed|date=December 2021}}
[[File:HuffmanCodeAlg.png|thumb|Constructing a Huffman
=== Informal description ===
;Given: A set of symbols <math>S</math> and for each symbol <math>x \in S</math>, the frequency <math>f_x</math> representing the fraction of symbols in the text that are equal to <math>x</math>.<ref name="kleinberg&Tardos">{{cite book |last1=Kleinberg |first1=Jon |last2=Tardos |first2=Eva |title=Algorithm Design |date=March 16, 2005 |publisher=[[Pearson Education]] |isbn=9780321295354 |page=165 |edition=1 |url=https://www.pearson.com/en-us/subject-catalog/p/algorithm-design/P200000003259/9780137546350 |access-date=26 January 2025}}</ref>
;Find: A [[Prefix code|prefix-free binary code]] (a set of codewords) with minimum [[Expected value|expected]] codeword length (equivalently, a tree with minimum [[weighted path length from the root]]).
Line 68 ⟶ 72:
<br />
'''Goal'''.<br />
Let <math display="inline">L
=== Example ===
Line 101 ⟶ 105:
|rowspan="2"|
|-
!style="background:#efefef;font-weight:normal"| Codeword length (in bits)<br />({{math|''
|align="center"| 3
|align="center"| 3
Line 108 ⟶ 112:
|align="center"| 2
|-
!style="background:#efefef;font-weight:normal"| Contribution to weighted path length<br />({{math|''
|align="center"| 0.30
|align="center"| 0.45
Line 117 ⟶ 121:
|-
!rowspan="3" style="background:#efefef"| Optimality
!style="background:#efefef;font-weight:normal"| Probability budget<br />({{math|2<sup>−''
| align="center" | 1/8
| align="center" | 1/8
Line 133 ⟶ 137:
|align="center"|
|-
! style="background: #efefef; font-weight: normal;" | Contribution to entropy<br />({{math|
|align="center"| 0.332
|align="center"| 0.411
Line 150 ⟶ 154:
The [[information entropy|entropy]] ''H'' (in bits) is the weighted sum, across all symbols {{math|''a''<sub>''i''</sub>}} with non-zero probability {{math|''w''<sub>''i''</sub>}}, of the information content of each symbol:
:<math display="block"> H(A) = \sum_{w_i > 0} w_i h(a_i) = \sum_{w_i > 0} w_i \log_2 {1 \over w_i} = - \sum_{w_i > 0} w_i \log_2
(Note: A symbol with zero probability has zero contribution to the entropy, since <math>\lim_{w \to 0^+} w \log_2 w = 0</math>. So for simplicity, symbols with zero probability can be left out of the formula above.)
Line 161 ⟶ 165:
===Compression===
[[File:Huffman_coding_visualisation.svg|thumb|
[[Image:Huffman coding example.svg|thumb|A source generates 4 different symbols <math>\{a_1 , a_2 , a_3 , a_4 \}</math> with probability <math>\{0.4 ; 0.35 ; 0.2 ; 0.05 \}</math>. A binary tree is generated from left to right taking the two least probable symbols and putting them together to form another equivalent symbol having a probability that equals the sum of the two symbols. The process is repeated until there is just one symbol. The tree can then be read backwards, from right to left, assigning different bits to different branches. The final Huffman code is:
Line 206 ⟶ 210:
#Start with current node set to the root.
#If node is not a leaf node, label the edge to the left child as
The final encoding of any symbol is then read by a concatenation of the labels on the edges along the path from the root node to the symbol.
Line 245 ⟶ 249:
=== ''n''-ary Huffman coding ===
The '''''n''-ary Huffman''' algorithm uses
Note that for ''n'' > 2, not all sets of source words can properly form a complete ''n''-ary tree for Huffman coding. In these cases, additional placeholder symbols with 0 probability may need to be added. This is because the structure of the tree needs to repeatedly join ''n'' branches into one - also known as an "''n'' to 1" combination. For binary coding, this is a "2 to 1" combination, which works with any number of symbols. For ''n''-ary coding, a complete tree is only possible when the total number of symbols (real + placeholders) leaves a remainder of 1 when divided by (n-1). <ref name=":0" />
=== Adaptive Huffman coding ===
A variation called '''[[adaptive Huffman coding]]''' involves calculating the probabilities dynamically based on recent actual frequencies in the sequence of source symbols, and changing the coding tree structure to match the updated probability estimates. It is used rarely in practice, since the cost of updating the tree makes it slower than optimized [[Arithmetic coding#Adaptive arithmetic coding|adaptive arithmetic coding]], which is more flexible and has better compression.{{citation needed|date=June 2025}}
=== Huffman template algorithm ===
Line 259 ⟶ 265:
In the standard Huffman coding problem, it is assumed that each symbol in the set that the code words are constructed from has an equal cost to transmit: a code word whose length is ''N'' digits will always have a cost of ''N'', no matter how many of those digits are 0s, how many are 1s, etc. When working under this assumption, minimizing the total cost of the message and minimizing the total number of digits are the same thing.
''Huffman coding with unequal letter costs'' is the generalization without this assumption: the letters of the encoding alphabet may have non-uniform lengths, due to characteristics of the transmission medium. An example is the encoding alphabet of [[Morse code]], where a 'dash' takes longer to send than a 'dot', and therefore the cost of a dash in transmission time is higher. The goal is still to minimize the weighted average codeword length, but it is no longer sufficient just to minimize the number of symbols used by the message. No algorithm is known to solve this in the same manner or with the same efficiency as conventional Huffman coding, though it has been solved by [
=== {{anchor|Hu-Tucker coding|Alphabetic Huffman coding|Alphabetic Huffman tree}}Optimal alphabetic binary trees (Hu–Tucker coding) ===
Line 290 ⟶ 296:
* [http://rosettacode.org/wiki/Huffman_coding Huffman coding in various languages on Rosetta Code]
* [https://gist.github.com/jasonrdsouza/1c9c895f43497d15eb2e Huffman codes (python implementation)]
* [https://github.com/gdavidbutler/canonicalHuffman Canonical Huffman codes (C implementation)]
* [https://
{{Compression Methods}}
Line 298 ⟶ 305:
[[Category:Lossless compression algorithms]]
[[Category:Binary trees]]
[[Category:Data compression]]
|