AI Glossary / MLOps and deployment / Intermediate

Shannon - Fano coding

Definition

Shannon - Fano coding commonly refers to a variable-length prefix-code construction that sorts symbols by probability and repeatedly splits them into two groups with probability totals as close as possible. Each split assigns a binary branch, producing a codeword for every symbol when each final group contains one symbol.

What it means

Likely symbols should usually receive shorter descriptions than unlikely ones. The recursive splitting procedure tries to achieve this by balancing probability mass at each decision. A decoder follows the same tree of binary choices until it reaches a symbol.

Terminology varies: some texts use Shannon - Fano for a broader family that includes Shannon's cumulative-probability construction. This entry uses the recursive partitioning method associated with Fano. It should also be distinguished from Shannon - Fano - Elias coding, which uses cumulative interval midpoints and a different codeword-length rule.

How it works

Arrange symbols in nonincreasing probability order. Divide the ordered list into two nonempty contiguous groups whose total probabilities are as nearly equal as possible. Assign 0 to one group and 1 to the other. Repeat within each group, appending a bit at every split, until every group contains a single symbol.

Codewords correspond to leaves of a binary tree, making the code prefix-free. Equal-probability symbols and equally good splits require a consistent tie policy if implementations must recreate an identical table.

The procedure is greedy: it makes a locally balanced split without examining every possible final tree. Consequently, its expected length is not always minimal. Huffman coding uses a different, bottom-up construction with an optimality guarantee for binary prefix symbol codes under given probabilities.

Three practical examples

1. Learning compression: a probability tree gives a visual explanation of how frequent outcomes can receive shorter binary descriptions without introducing ambiguous boundaries.

2. Comparing coding methods: an engineer can apply recursive splitting and Huffman coding to the same event histogram, then compare expected lengths and complete codebook costs.

3. Small-format prototypes: a team can use a clearly specified code tree as an experimental representation of symbolic data. A production choice still needs compatibility rules, measurements and a plan for previously unseen symbols.

Think of it this way

Think of a guessing game in which each yes-or-no question tries to divide the remaining possibilities into equally likely groups. The answers along a path identify the final item.

The analogy stops at optimization: a locally balanced question is not guaranteed to lead to the best entire sequence of questions. The coding method also uses fixed symbol probabilities rather than learning new clues from meaning during each message.

Worked example

Use symbols A, B, C, D and E with probabilities 0.35, 0.17, 0.17, 0.16 and 0.15. The best first contiguous split is {A,B}, total 0.52, against {C,D,E}, total 0.48. Assign the first group prefix 0 and the second prefix 1.

Split {A,B} into its individual symbols, giving A→00 and B→01. For {C,D,E}, splitting after C gives totals 0.17 and 0.31, closer than the alternative 0.33 and 0.15. Therefore assign C→10 and split {D,E}, giving D→110 and E→111.

The expected length is 0.35×2 + 0.17×2 + 0.17×2 + 0.16×3 + 0.15×3 = 2.31 bits per symbol. The message AED becomes 00|111|110, which can be decoded by following the tree.

For comparison, a valid alternative assigns A a one-bit word and the other four symbols three-bit words, such as A→0, B→100, C→101, D→110 and E→111. Its expected length is 0.35×1 + 0.65×3 = 2.30 bits. This small difference is enough to show that recursive probability balancing does not always yield the shortest average symbol code. Both calculations exclude format overhead.

Illustrative case study

Fictional case: BranchByte creates a teaching-oriented telemetry prototype. Its developer chooses Shannon - Fano splitting because the resulting decision tree is easy to explain to colleagues.

When evaluating a fixed event distribution, another engineer finds a slightly shorter Huffman representation. The team documents both averages but also measures total message sizes: on tiny messages, codebook metadata outweighs the difference between the two payload averages.

The prototype remains useful for demonstrating prefix coding, while the deployment decision uses measured traffic and interoperability needs. This fictional case illustrates why a clear construction, an optimal expected payload and the smallest complete operational message are related but different goals.

Common misconceptions and mistakes

“Every split divides the number of symbols equally.” It aims to balance probability mass. The two groups can contain very different numbers of symbols.

“A frequent symbol always receives a one-bit code.” Its depth depends on the recursive partitions. A high probability alone does not specify its final length.

“Locally balanced means globally optimal.” The worked example shows otherwise. Early partitions can prevent a better overall tree.

“The decoder can infer the probabilities from the compressed message.” It needs the codebook or an agreed way to reconstruct it. Any transmitted model information must be included in storage and bandwidth accounting.

Business use

Shannon - Fano coding is most valuable as a transparent introduction to probability-based compression and as a comparative baseline. For a practical format, assess expected payload size together with table overhead, decoder simplicity and compatibility.

Use representative probability estimates and test changes in traffic. If the distribution changes, the same code remains decodable when both sides keep the table, but its average efficiency can change. Version any updated tables and test complete messages, including rare symbols and truncation. Prefix freedom provides intact-stream symbol boundaries; it does not replace error detection or framing rules.

People also ask

Is Shannon - Fano coding lossless?

Yes, when the codebook and complete encoded sequence are available and correctly interpreted. Each source symbol is recovered exactly.

Is there always one unique Shannon - Fano table?

No. Ties and the choice of branch labels can lead to different valid tables. A format must specify how the table is communicated or reproduced.

Why study it when Huffman can be better?

Its top-down structure makes probability balancing and prefix trees easy to inspect. Comparing the methods also illustrates the limits of a greedy design choice.

Check your understanding

A five-symbol code splits its first group into two symbols and its second into three. Has it violated Shannon - Fano's balancing rule?

Answer: Not necessarily. The rule balances total probabilities, not group sizes. Two common symbols may have almost the same combined probability as three less common ones.

Related terms

  • Shannon coding
  • Prefix code
  • Entropy
  • Shannon - Fano - Elias coding
  • Huffman coding
  • probability tree
  • expected codeword length

Learn next

Draw recursive probability partitions for several small alphabets, including tied probabilities. Compare their weighted codeword lengths with Huffman trees, then study how block coding changes the alphabet and the cost of representing its codebook.

More in MLOps and deployment

  • Serving - Serving is the process of making a trained machine-learning model available to produce predictions for an application or workflow.
  • Set partitioning in hierarchical trees - Set partitioning in hierarchical trees, usually abbreviated SPIHT, is an image-coding method that efficiently describes wavelet-transform coefficients by...
  • Set redundancy compression - Set redundancy compression refers to methods that exploit information shared across a collection of similar data items, especially images.
  • Shannon's source coding theorem - Shannon's source coding theorem establishes entropy as the fundamental rate limit for lossless compression under an appropriate source model.
  • Shard - A shard is a logical portion of a larger dataset, model or data structure that can be stored or processed separately.
  • Signaling compression - Signaling compression reduces the size of messages used to establish, manage or end communication sessions.

Sources

Primary historical description: Claude E. Shannon, “A Mathematical Theory of Communication,” Section 9 describes Fano's recursive division into nearly equal-probability groups: https://www.princeton.edu/~wbialek/rome/refs/shannon_48.pdf . The five-symbol example and fictional prototype are original illustrations; the naming convention used here is stated explicitly.

Previous: Shannon coding Next: Shannon - Fano - Elias coding

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer