How to know if a code is Huffman or not without having the probability of each codeword? - Computer Science Stack Exchange most recent 30 from cs.stackexchange.com 2019-11-21T14:49:53Z https://cs.stackexchange.com/feeds/question/97333 https://creativecommons.org/licenses/by-sa/4.0/rdf https://cs.stackexchange.com/q/97333 2 How to know if a code is Huffman or not without having the probability of each codeword? Lod https://cs.stackexchange.com/users/93785 2018-09-14T20:09:02Z 2018-09-16T03:53:12Z <p>I am new to Huffman coding and I find myself facing a lot of confusion as to how to determine if a code is Huffman or not without having the probabilities associated to each codeword. I know one way is look at whether or not the code is prefix. If not, then it's not a Huffman code. If yes, then we need to check if it gives the shortest possible length, <em>but we don't know the probabilities</em>. How do we ensure the code guarantees the shortest possible length in such case? To illustrate my question, let us look at the following two examples: </p> <blockquote> <h3>Example 1: \$\{00,01,10,110\}\$</h3> <p>The code is a prefix code, but is the length minimal? I mean it's a bit unusual for me to see Huffman code for \$4\$ symbols that does not have one-bit codeword for one of its symbol (the symbol with highest probability), but them I thought maybe the first three symbols are equally probable. As such, I tried to find the Huffman code for \$\{0.33,0.33,0.33,0.01\}\$ and got \$\{00,01,10,11\}\$ with different orders (depending on the usage of \$0,1\$ or \$1,0\$ in the tree). This led me to thinking that the code under investigation might not be Huffman, yet I am still not sure.</p> <h3>Example 2: \$\{01, 10\}\$</h3> <p>Again another unusual prefix code as the obvious choice (and the one with minimal length) for two symbols would be \$\{0, 1\}\$. But could it be the case that \$\{01, 10\}\$ can possibly give a minimal length? I can <em>not</em> see any possible probabilities in which this is the case, thus I believe this is <em>not</em> a Huffman code.</p> </blockquote> <p>Am I on the right track? Does there exist an <em>actual</em> method to check whether or nor a code is Huffman without having the probabilities? </p> https://cs.stackexchange.com/questions/97333/-/97334#97334 1 Answer by RandomPerfectHashFunction for How to know if a code is Huffman or not without having the probability of each codeword? RandomPerfectHashFunction https://cs.stackexchange.com/users/92199 2018-09-14T20:41:56Z 2018-09-16T03:53:12Z <p>When a frequency tree is built for a message upon its characters, the leaf nodes of the tree are the characters composing the message with their frequency and internal nodes just have a frequency sum of all its descendents. <br> <strong>The characteristic property of frequency trees for Huffman encoding is that, all internal nodes have exactly two children.</strong> <br></p> <p>For your example 1, \$\{00,01,10,110\}\$, the frequency tree would be something like this (forgive me for how the tree looks like. Subtrees are denoted by + and the first subtree is the left subtree and the second is the right subtree): <br></p> <pre><code>(root) + (0) | + (0) (leaf) | + (1) (leaf) + (1) + (0) (leaf) + (1) + (0) (leaf) </code></pre> <p>The subtree \$\{root,1,1\}\$ has one child, not two. Hence, such a prefix code can not be a Huffman encoding for any message. Similarly for example two, subtrees \$\{root,0\}\$ and \$\{root,1\}\$ have exactly one child.<br></p> https://cs.stackexchange.com/questions/97333/-/97335#97335 3 Answer by gnasher729 for How to know if a code is Huffman or not without having the probability of each codeword? gnasher729 https://cs.stackexchange.com/users/17408 2018-09-14T20:42:29Z 2018-09-15T12:20:54Z <p>If you have a Huffman code, and the codes have lengths \$l_i\$, then the sum over \$2^{-l_i}\$ must be equal to 1. In your case, that sum is 1/4 + 1/4 + 1/4 + 1/8 = 7/8 &lt; 1, therefore not a Huffman code. You can replace the code 110 with 11. </p> <p>(I am quite sure you can prove that for any prefix code, the sum is ≤ 1. And I'm quite sure you can prove that if the sum is less than 1, then there is some bit that you can remove from some code with the code remaining a prefix code, and since there is a finite number of bits to remove, the sum must become 1 eventually).</p>