Let a memoryless source have probabilities , and let a uniquely decodable -ary code have word lengths and mean length . The Shannon source coding theorem states
and that some prefix code satisfies
For the lower bound, the Kraft inequality gives . Set . The Gibbs inequality, or nonnegativity of relative entropy, gives
Therefore .
For achievability choose . Then , so the converse part of the Kraft theorem supplies a prefix code with these lengths, and
Hence the entropy is the optimal mean code length up to less than one code symbol.
Every member of the typical set has probability at least . Summing these probabilities gives
so
The upper bound itself holds at every block length. For sufficiently large , the high-probability property also gives the companion lower bound
hence . Together these are the typical-set cardinality bounds underlying Shannon source coding theorem.
Let indicate whether the outcome differs from . Then . The chain rule for information entropy gives
since is determined when . The remaining conditional distribution has at most outcomes, so the maximum entropy on a finite alphabet is . Therefore the Shannon source coding theorem limit obeys
Here is the binary entropy function; equality holds when the rare outcomes are equiprobable. A convenient explicit small- bound is
because . For a fixed alphabet, this upper bound tends to zero as .
The binary entropy is
The Shannon source coding theorem requires
for the standard Shannon construction, while every prefix code obeys the lower bound. Numerically,
Thus both codes satisfy the theorem, with the Huffman code closer to the entropy lower bound.