<p>Unfortunately, I also don't have an exact answer. After an initially wrong statement in my lecture notes, currently I do not provide a good bound on the Fano code (version 3.1 of lecture notes). I do, however, have a proof that shows that the Fano codes has an expected average codeword length of less than H(U)/log(2) + 1 - 2p_{min}. Unfortunately, I only have the proof for the case of a binary code, not a general D-ary code. This is why I haven't included this yet into the lecture notes. But I'm still working on it and hope that I will eventually be able to fix it in my lecture notes.</p>

<p>Note, however, that my result does NOT compare Shannon codes with Fano codes. It only gives a general upper bound on the Fano codes. I think it is very difficult to compare the two codes directly. In particular, I do not understand the proof given above and I have my doubts that it can be made rigorous, particularly not for D>2.</p>

CC BY-SA 3.0
2013-11-11T14:17:40.987Z
2013-11-11T14:17:40.987Z
Stefan Moser
<p>Nice counterexample.</p>

<p>A short "proof" that Shannon-Fano coding is always at least as good as Shannon coding over the long term, even though it may be worse for a few specific letters:</p>

<ol>
<li>Shannon coding always sets the length $ls_i$ of each codeword to a function of how many times $f_i$ it occurs in some text of length $LN$: $ls_i=\lceil -\log (f_i/LN)\rceil$.</li>
<li>two-symbol Shannon-Fano coding and Huffman coding: always sets the codeword for one symbol to 0, and the other codeword to 1, which is optimal -- therefore it is always better than Shannon coding in this case (or equal, in the case where both probabilities are 1/2).</li>
<li>multi-symbol Shannon-Fano coding and Huffman coding: case (a): sometimes there exists some letter $i$ assigned Fano codeword with a length $lf_i$ 1 bit longer (worse compression) than $ls_i$.</li>
<li>multi-symbol Shannon-Fano coding and Huffman coding: case (b): sometimes there exists some letter $j$ assigned Fano codeword with a length $lf_j$ shorter (better compression) than $ls_j$.</li>
<li>Whenever case (a) occurs, case (b) also occurs at least as many times.</li>
<li>Each letter (if any) $i$ that falls under case (a) can be paired up with some other letter $j$ that not only falls under case (b), but also letter $j$ is more frequent than letter $i$.</li>
<li>Therefore, whenever case (b) occurs, the total number of bits needed to store all the letters $i$ and all the letters $j$ with with Shannon-Fano coding is no worse than with Shannon coding: $f_i lf_i + f_j lf_j \leq f_i ls_i + f_j ls_j$.</li>
<li>Therefore Shannon-Fano coding is always at least as good as Shannon coding.</li>
</ol>

<p>Alas, there's a lot of hand-waving in this "proof".</p>

<p>I suspect there might be a better proof in
the book Yaglom and Yaglom: "<a href="http://books.google.com/books?id=vHwVgAFVKyIC" rel="nofollow">Probability and information</a>".</p>

<p>p.s.:
You might also be interested in yet another algorithm for generating prefix codes,
"<a href="http://en.wikipedia.org/wiki/User%3aC-processor/Polar_Tree" rel="nofollow">Polar coding</a>" developed by Andrew Polar.</p>

CC BY-SA 3.0
2011-09-28T06:28:26.533Z
Thanks for attempting an answer David. Unfortunately I don't really understand what you are saying here :( In points 3 and 4 are you saying that your claims apply to both Shannon-Fano and Huffman coding? Or are you somehow proving something about Shannon-Fano compared to Huffman? I just don't really see reasons for any of points 3-6.
Martin Leslie
CC BY-SA 3.0
2011-09-28T06:29:59.730Z
I had a look at the Yaglom and Yaglom book and I don't think it had anything to add on this topic: like most books it talks about Shannon-Fano coding but all its proofs are using Shannon coding.
Martin Leslie
CC BY-SA 3.0
2011-09-28T06:33:02.100Z
Thanks for the polar codes reference. Do you know whether this has anything to do with polar codes as used in channel coding? As far as I know those polar codes are so named because of channel polarisation, not after someone called Polar. The Korada and Urbanke paper linked from the link you gave seems to be about those kind of polar codes, applied to lossy compression.
Martin Leslie
CC BY-SA 3.0
2011-09-29T01:56:10.397Z
Yes, I'm saying 3 and 4 are true for Shannon Fano coding. (I'm also going on a tangent and mentioning that it is also true for Huffman coding as well, although that is irrelevant to the proof).
David Cary
CC BY-SA 3.0
2011-09-27T16:32:23.800Z
2011-09-27T16:40:56.657Z
2011-09-27T16:40:56.657Z
David Cary
<p>I am interested in a few algorithms for creating prefix codes:</p>

<ol>
<li><a href="http://en.wikipedia.org/wiki/Shannon%27s_noiseless_coding_theorem" rel="noreferrer">Shannon coding</a>: we take $l_i=\lceil -\log p_i\rceil$.</li>
<li><a href="http://en.wikipedia.org/wiki/Shannon%E2%80%93Fano_coding" rel="noreferrer">Shannon-Fano coding</a>: list probabilities in decreasing order and then split them in half at each step to keep the probability on each side balanced. Then codes/lengths come from resulting binary tree.</li>
</ol>

<p>My question is whether one of these algorithms always provides a better $L=\sum p_i l_i$? In a few examples I've done, Shannon-Fano seems better. Is this always true? Do you have any references or proofs? I realize these aren't the two best algorithms (Huffman coding is optimal). I'm just interested in comparing them.</p>

<p>Edit: Well, no interest so far? I guess this stuff isn't all that exciting, but I'd still like to know. Here's what I've come up with after some more google searching/playing around with numbers.</p>

<p>Practically, Shannon-Fano is often optimal for a small number of symbols with randomly generated probability distributions, or quite close to optimal for a larger number of symbols. I haven't found an example yet where Shannon-Fano is worse than Shannon coding.</p>

<p>In Shannon's <a href="http://cm.bell-labs.com/cm/ms/what/shannonday/shannon1948.pdf" rel="noreferrer">original 1948 paper</a> (p17) he gives a construction (equivalent to Shannon coding above) and claims that Fano's construction (Shannon-Fano above) is substantially equivalent, without any real proof. I haven't been able to find a copy of Fano's 1949 technical report to see whether it has any analysis.</p>

<p>Thomas and Cover's Elements of Information Theory says that Fano codes give $L \leq H +2$ and Shannon codes give $L \leq H+1$. However the <a href="http://www.sciencedirect.com/science/article/pii/S0019995877800119" rel="noreferrer">reference</a> they give for the Fano code analysis I think applies to alphabetical codes where you're not allowed to reorder the probabilities.</p>

<p>Stefan Moser's <a href="http://moser.cm.nctu.edu.tw/docs/it_script.pdf" rel="noreferrer">Information Theory Lecture Notes</a> (pp 50-59) agree with my historical analysis above and purport to prove that for Fano codes we have $l_i \leq \lceil -\log p_i \rceil$ which would be sufficient to prove they are better than Shannon codes. However I don't follow the proof and I think I have a counterexample:</p>

<p>Take probabilities $(0.4, 0.26, 0.02, 0.02, 0.02, \ldots, 0.02)$ (we have 17 0.02's so that probabilities add to 1). Then Shannon coding has lengths $(2,2,6,6,6,\ldots,6)$ while Fano coding splits between 0.4 and 0.26 and then for the 0.6 probability on the right it splits between the second and third 0.02. Continuing on we see that 0.26 is encoded with a length of 3, larger than Shannon length. However, the average length is still less for Fano than for Shannon (according to my program implementing Fano coding).</p>

<p>So, am I doing something wrong? Can you see how to construct a probability distribution to make Shannon code perform better than Fano, or a way to prove it's not possible?</p>

CC BY-SA 3.0
2011-09-23T12:37:24.517Z
Very nice counterexample. I think it's an interesting question. I agree that Cover and Thomas are probably talking about the case where you don't reorder the probabilities. I expect the theorem is true, and that nobody here knows any references to anybody looking at anything similar, and this is why you haven't gotten any answers.
Peter Shor 
CC BY-SA 3.0
2011-09-23T14:15:37.663Z
Thanks for the comment Peter. Glad to know somebody's reading. I'll post something if I ever figure out the answer.
Martin Leslie
<it.information-theory>
Comparing Shannon-Fano and Shannon coding
Martin Leslie
# Comparing Shannon-Fano and Shannon coding

(Asked by: username_0 on 2011-09-21)

I am interested in a few algorithms for creating prefix codes:

1. [Shannon coding](http://en.wikipedia.org/wiki/Shannon%27s_noiseless_coding_theorem): we take $l\_i=\lceil -\log p\_i\rceil$.
2. [Shannon-Fano coding](http://en.wikipedia.org/wiki/Shannon%E2%80%93Fano_coding): list probabilities in decreasing order and then split them in half at each step to keep the probability on each side balanced. Then codes/lengths come from resulting binary tree.

My question is whether one of these algorithms always provides a better $L=\sum p\_i l\_i$? In a few examples I've done, Shannon-Fano seems better. Is this always true? Do you have any references or proofs? I realize these aren't the two best algorithms (Huffman coding is optimal). I'm just interested in comparing them.

Edit: Well, no interest so far? I guess this stuff isn't all that exciting, but I'd still like to know. Here's what I've come up with after some more google searching/playing around with numbers.

Practically, Shannon-Fano is often optimal for a small number of symbols with randomly generated probability distributions, or quite close to optimal for a larger number of symbols. I haven't found an example yet where Shannon-Fano is worse than Shannon coding.

In Shannon's [original 1948 paper](http://cm.bell-labs.com/cm/ms/what/shannonday/shannon1948.pdf) (p17) he gives a construction (equivalent to Shannon coding above) and claims that Fano's construction (Shannon-Fano above) is substantially equivalent, without any real proof. I haven't been able to find a copy of Fano's 1949 technical report to see whether it has any analysis.

Thomas and Cover's Elements of Information Theory says that Fano codes give $L \leq H +2$ and Shannon codes give $L \leq H+1$. However the [reference](http://www.sciencedirect.com/science/article/pii/S0019995877800119) they give for the Fano code analysis I think applies to alphabetical codes where you're not allowed to reorder the probabilities.

Stefan Moser's [Information Theory Lecture Notes](http://moser.cm.nctu.edu.tw/docs/it_script.pdf) (pp 50-59) agree with my historical analysis above and purport to prove that for Fano codes we have $l\_i \leq \lceil -\log p\_i \rceil$ which would be sufficient to prove they are better than Shannon codes. However I don't follow the proof and I think I have a counterexample:

Take probabilities $(0.4, 0.26, 0.02, 0.02, 0.02, \ldots, 0.02)$ (we have 17 0.02's so that probabilities add to 1). Then Shannon coding has lengths $(2,2,6,6,6,\ldots,6)$ while Fano coding splits between 0.4 and 0.26 and then for the 0.6 probability on the right it splits between the second and third 0.02. Continuing on we see that 0.26 is encoded with a length of 3, larger than Shannon length. However, the average length is still less for Fano than for Shannon (according to my program implementing Fano coding).

So, am I doing something wrong? Can you see how to construct a probability distribution to make Shannon code perform better than Fano, or a way to prove it's not possible?

**Tags:** it.information-theory
> **Comments:**
> **username_1**: Very nice counterexample. I think it's an interesting question. I agree that Cover and Thomas are probably talking about the case where you don't reorder the probabilities. I expect the theorem is true, and that nobody here knows any references to anybody looking at anything similar, and this is why you haven't gotten any answers.  
> **username_0**: Thanks for the comment Peter. Glad to know somebody's reading. I'll post something if I ever figure out the answer.  

---

## 2 answers

### Answer by username_2 on 2013-11-11. Score: 3

Unfortunately, I also don't have an exact answer. After an initially wrong statement in my lecture notes, currently I do not provide a good bound on the Fano code (version 3.1 of lecture notes). I do, however, have a proof that shows that the Fano codes has an expected average codeword length of less than H(U)/log(2) + 1 - 2p\_{min}. Unfortunately, I only have the proof for the case of a binary code, not a general D-ary code. This is why I haven't included this yet into the lecture notes. But I'm still working on it and hope that I will eventually be able to fix it in my lecture notes.

Note, however, that my result does NOT compare Shannon codes with Fano codes. It only gives a general upper bound on the Fano codes. I think it is very difficult to compare the two codes directly. In particular, I do not understand the proof given above and I have my doubts that it can be made rigorous, particularly not for D>2.


---

### Answer by username_3 on 2011-09-27. Score: 1

Nice counterexample.

A short "proof" that Shannon-Fano coding is always at least as good as Shannon coding over the long term, even though it may be worse for a few specific letters:

1. Shannon coding always sets the length $ls\_i$ of each codeword to a function of how many times $f\_i$ it occurs in some text of length $LN$: $ls\_i=\lceil -\log (f\_i/LN)\rceil$.
2. two-symbol Shannon-Fano coding and Huffman coding: always sets the codeword for one symbol to 0, and the other codeword to 1, which is optimal -- therefore it is always better than Shannon coding in this case (or equal, in the case where both probabilities are 1/2).
3. multi-symbol Shannon-Fano coding and Huffman coding: case (a): sometimes there exists some letter $i$ assigned Fano codeword with a length $lf\_i$ 1 bit longer (worse compression) than $ls\_i$.
4. multi-symbol Shannon-Fano coding and Huffman coding: case (b): sometimes there exists some letter $j$ assigned Fano codeword with a length $lf\_j$ shorter (better compression) than $ls\_j$.
5. Whenever case (a) occurs, case (b) also occurs at least as many times.
6. Each letter (if any) $i$ that falls under case (a) can be paired up with some other letter $j$ that not only falls under case (b), but also letter $j$ is more frequent than letter $i$.
7. Therefore, whenever case (b) occurs, the total number of bits needed to store all the letters $i$ and all the letters $j$ with with Shannon-Fano coding is no worse than with Shannon coding: $f\_i lf\_i + f\_j lf\_j \leq f\_i ls\_i + f\_j ls\_j$.
8. Therefore Shannon-Fano coding is always at least as good as Shannon coding.

Alas, there's a lot of hand-waving in this "proof".

I suspect there might be a better proof in
the book Yaglom and Yaglom: "[Probability and information](http://books.google.com/books?id=vHwVgAFVKyIC)".

p.s.:
You might also be interested in yet another algorithm for generating prefix codes,
"[Polar coding](http://en.wikipedia.org/wiki/User%3aC-processor/Polar_Tree)" developed by Andrew Polar.

> **Comments:**
> **username_0**: Thanks for attempting an answer David. Unfortunately I don't really understand what you are saying here :( In points 3 and 4 are you saying that your claims apply to both Shannon-Fano and Huffman coding? Or are you somehow proving something about Shannon-Fano compared to Huffman? I just don't really see reasons for any of points 3-6.  
> **username_0**: I had a look at the Yaglom and Yaglom book and I don't think it had anything to add on this topic: like most books it talks about Shannon-Fano coding but all its proofs are using Shannon coding.  
> **username_0**: Thanks for the polar codes reference. Do you know whether this has anything to do with polar codes as used in channel coding? As far as I know those polar codes are so named because of channel polarisation, not after someone called Polar. The Korada and Urbanke paper linked from the link you gave seems to be about those kind of polar codes, applied to lossy compression.  
> **username_3**: Yes, I'm saying 3 and 4 are true for Shannon Fano coding. (I'm also going on a tangent and mentioning that it is also true for Huffman coding as well, although that is irrelevant to the proof).  

---

