Shannon–Fano–Elias coding


In information theory, Shannon–Fano–Elias coding is a precursor to arithmetic coding, in which probabilities are used to determine codewords.

Algorithm description

Given a discrete random variable X of ordered values to be encoded, let be the probability for any x in X. Define a function
Algorithm:

Example

Let X =, with probabilities p =.

Algorithm analysis

Prefix code

Shannon–Fano–Elias coding produces a binary prefix code, allowing for direct decoding.
Let bcode be the rational number formed by adding a decimal point before a binary code. For example, if code=1010 then bcode = 0.1010. For all x, if no y exists such that
then all the codes form a prefix code.
By comparing F to the CDF of X, this property may be demonstrated graphically for Shannon–Fano–Elias coding.
By definition of L it follows that
And because the bits after L are truncated from F to form code, it follows that
thus bcode must be no less than CDF.
So the above graph demonstrates that the, therefore the prefix property holds.

Code length

The average code length is
.

Thus for H, the Entropy of the random variable X,
Shannon Fano Elias codes from 1 to 2 extra bits per symbol from X than entropy, so the code is not used in practice.