In a nutshell:
Is the Shannon-Fano coding as described in Fano's paper The Transmission of Information (1952) really ambiguous?
In Detail:
3 papers
Claude E. Shannon published his famous paper A Mathematical Theory of Communication in July 1948. In this paper he invented the term bit as we know it today and he also defined what we call Shannon entropy today. And he also proposed an entropy based data compression algorithm in this paper. But Shannon's algorithm was so weak, that under certain circumstances the "compressed" messages could be even longer than in fix length coding. A few month later (March 1949) Robert M. Fano published an improved version of Shannons algorithm in the paper The Transmission of Information. 3 years after Fano (in September 1952) his student David A. Huffman published an even better version in his paper A Method for the Construction of Minimum-Redundancy Codes. Hoffman Coding is more efficient than its two predecessors and it is still used today. But my question is about the algorithm published by Fano which usually is called Shannon-Fano-Coding.
The algorithm
This description is based on the description from Wikipedia. Sorry, I did not fully read Fano's paper. I only browsed through it. It is 37 pages long and I really tried hard to find a passage where he talks about the topic of my question, but I could not find it. So, here is how Shannon-Fano encoding works:
- Count how often each character appears in the message.
- Sort all characters by frequency, characters with highest frequency on top of the list
- Divide the list into two parts, such that the sums of frequencies in both parts are as equal as possible. Add the bit
0to one part and the bit1to the other part. - Repeat step 3 on each part that contains 2 or more characters until all parts consist of only 1 character.
- Concatenate all bits from all rounds. This is the Shannon-Fano-code of that character.
An example
Let's execute this on a really tiny example (I think it's the smallest message where the problem appears). Here is the message to encode:
aaabcde
Steps 1 and 2 produce the first 2 columns of both tables shown below. But if Wikipedia's explanation of Fanos's algorithm is correct, then step 3 is ambiguous. If you apply this step on my example, you have two possibilities to split the list in 2 parts (see below). These possibilities produce different codes, which by itself would not be worth to be mentioned. But the point is: The two possibilities produce codes of different lengths.
possibility 1
If there are 2 ways to split the list such that both parts are as equal to each other as possible, then put that character, that stands at the splitting point (this is character b in my example) to the part containing the low frequent characters
+------+-------+-----+-----+-----+-----+-----+-----+------+
| | | round1 | round2 | round3 | |
| char | frequ | sum | bit | sum | bit | sum | bit | code |
+------+-------+-----+-----+-----+-----+-----+-----+------+
| a | 3 | 3 | 0 | | 0 |
| | +-----+-----+-----+-----+-----+-----+------+
| b | 1 | | | | | 1 | 0 | 100 |
| | | | | 2 | 0 +-----+-----+------+
| c | 1 | | | | | 1 | 1 | 101 |
| | | 4 | 1 +-----+-----+-----+-----+------+
| d | 1 | | | | | 1 | 0 | 110 |
| | | | | 2 | 1 +-----+-----+------+
| e | 1 | | | | | 1 | 1 | 111 |
+------+-------+-----+-----+-----+-----+-----+-----+------+
The encoded message is
000100101110111 length = 15 bit
aaab c d e
possibility 2
If there are 2 ways to split the list such that both parts are as equal to each other as possible, then put that character, that stands at the splitting point to the part containing the high frequent characters
+------+-------+-----+-----+-----+-----+-----+-----+------+
| | | round1 | round2 | round3 | |
| char | frequ | sum | bit | sum | bit | sum | bit | code |
+------+-------+-----+-----+-----+-----+-----+-----+------+
| a | 3 | | | 3 | 0 | | 00 |
| | | 4 | 0 +-----+-----+ +------+
| b | 1 | | | 1 | 1 | | 01 |
| | +-----+-----+-----+-----+-----+-----+------+
| c | 1 | | | | | 1 | 0 | 100 |
| | | | | 2 | 0 |-----+-----+------+
| d | 1 | 3 | 1 | | | 1 | 1 | 101 |
| | | | +-----+-----+-----+-----+------+
| e | 1 | | | 1 | 1 | | 11 |
+------+-------+-----+-----+-----+-----+-----+-----+------+
The encoded message is
0000000110010111 length = 16 bit
a a a b c d e
So, it is one bit longer.
So, here are my questions:
- Is Wikipedia's description of Shannon-Fano Coding really correct and complete? If this is the case, than Shannon-Fano Coding is ambiguous.
- Or did Fano in his paper add another step that is missing in Wikipedia's description? If this is the case: How did Fano solve the problem described here? Which of both versions is compatible with Fano's original description?