#!/usr/local/bin/python DEBUG = False string = """Huffman coding In computer science and information theory, a Huffman code is an optimal prefix code found using the algorithm developed by David A. Huffman while he was a Ph.D. student at MIT, and published in the 1952 paper "A Method for the Construction of Minimum-Redundancy Codes". The process of finding and/or using such a code is called Huffman coding and is a common technique in entropy encoding, including in lossless data compression. The algorithm's output can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file). Huffman's algorithm derives this table based on 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 linear time to the number of input weights if these weights are sorted. However, although optimal among methods encoding symbols separately, Huffman coding is not always optimal among all compression methods. History In 1951, David A. Huffman and his MIT information theory classmates were given the choice of a term paper or a final exam. The professor, Robert M. Fano, assigned a term paper on the problem of finding the most efficient binary code. Huffman, unable to prove any codes were the most efficient, was about to give up and start studying for the final when he hit upon the idea of using a frequency-sorted binary tree and quickly proved this method the most efficient. In doing so, the student outdid his professor, who had worked with information theory inventor Claude Shannon to develop a similar code. By building the tree from the bottom up instead of the top down, Huffman avoided the major flaw of the suboptimal Shannon-Fano coding. Terminology Huffman coding uses a specific method for choosing the representation for each symbol, resulting in a prefix code (sometimes called "prefix-free codes", that is, the bit string representing some particular symbol is never a prefix of the bit string representing any other symbol). Huffman coding is such a widespread method for creating prefix codes that the term "Huffman code" is widely used as a synonym for "prefix code" even when such a code is not produced by Huffman's algorithm. """ class NodeTree(object): def __init__(self, left=None, right=None): self.left = left self.right = right def children(self): return (self.left, self.right) def nodes(self): return (self.left, self.right) def __str__(self): return "%s_%s" % (self.left, self.right) ## Tansverse the NodeTress in every possible way to get codings def huffmanCodeTree(node, left=True, binString=""): if type(node) is str: return {node: binString} (l, r) = node.children() d = dict() d.update(huffmanCodeTree(l, True, binString + "0")) d.update(huffmanCodeTree(r, False, binString + "1")) return d if DEBUG: print "Input file: " + sys.argv[1] freq = {} for c in string: if c in freq: freq[c] += 1 else: freq[c] = 1 #Sort the frequency table based on occurrence this will also convert the #dict to a list of tuples freq = sorted(freq.items(), key=lambda x: x[1], reverse=True) if DEBUG: print " Char | Freq " for key, c in freq: print " %4r | %d" % (key, c) nodes = freq while len(nodes) > 1: key1, c1 = nodes[-1] key2, c2 = nodes[-2] nodes = nodes[:-2] node = NodeTree(key1, key2) nodes.append((node, c1 + c2)) # Re-sort the list nodes = sorted(nodes, key=lambda x: x[1], reverse=True) if DEBUG: print "left: %s" % nodes[0][0].nodes()[0] print "right: %s" % nodes[0][0].nodes()[1] huffmanCode = huffmanCodeTree(nodes[0][0]) print " Char | Freq | Huffman code " print "-----------------------------" for char, frequency in freq: print " %-4r | %5d | %12s" % (char, frequency, huffmanCode[char])
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question