Skip to content
GATEiit

GATE EC · Communications

Source coding

Applying Shannon's noiseless source coding theorem for lossless compression bounds, testing prefix-free codewords via the Kraft-McMillan inequality, constructing minimum-variance D-ary Huffman codes, and evaluating coding efficiency.

Before you start

Concept notes

1. Shannon's Source Coding Theorem, Average Length, and Efficiency

A memoryless discrete source emits symbols x1,…,xM with probabilities pi and entropy H(X)=−∑i=1Mpilog⁡2pi (bits/symbol). Assigning binary codewords of lengths li yields expected codeword length, efficiency, and redundancy: Lˉ=∑i=1Mpili,η=H(X)Lˉ≤1,Redundancy γ=1−η. Shannon's Noiseless Source Coding Theorem: For any uniquely decodable D-ary code (here D=2 bits), average length is fundamentally bounded by entropy: HD(X)≤Lˉ<HD(X)+1. Note: efficiency reaches 100% (Lˉ=H(X)) iff every symbol probability is an exact dyadic power (pi=2−li, e.g. 0.5,0.25,0.125,0.125)! When probabilities are non-dyadic, grouping n symbols together into an extended source Xn drops per-symbol overhead below 1/n: Lˉnn→H(X) as n→∞!

2. Prefix-Free (Instantaneous) Codes and Kraft–McMillan Inequality

Code Class Definition / Decoder Rule Example (s1,s2,s3,s4)
Nonsingular Distinct symbols map to distinct codewords (blocks ambiguous!) 0, 01, 10, 1 (010 ambiguous!)
Uniquely Decodable Any concatenated bit stream decodes unambiguously 0, 01, 011, 0111 (needs lookahead 0)
Prefix-Free (Instantaneous) No codeword is a prefix of any other (decoded bit-by-bit live!) 0, 10, 110, 111 (tree leaves only!)

Kraft–McMillan Theorem: A D-ary prefix-free (or uniquely decodable) code with integer lengths l1,…,lM exists if and only if: ∑i=1MD−li≤1(binary D=2:  ∑i=1M2−li≤1). Beware the direction! Satisfying Kraft guarantees SOME prefix code of those lengths exists, NOT that every code with those lengths is prefix-free (e.g., 0, 01, 011, 0111 fails prefix test even though lengths work!). And equality (=1) indicates a complete tree (no wasted leaves).

Prefix-Free Code Tree: Codewords Strictly at Leaves Satisfy Kraft Inequality ∑ 2^(−l_i) ≤ 1 Root 0 s₁: 0 (l=1) 1 0 s₂: 10 (l=2) 1 0 s₃: 110 1 s₄: 111 Kraft–McMillan Test Lengths: {1, 2, 3, 3} ∑ 2^(−l_i) = ½ + ¼ + ⅛ + ⅛ = 1 Complete tree (no wasted leaves!) • Prefix-free ⇔ no leaf extends • Instantaneous decode (no lookahead)
Prefix-free codes place every assigned codeword strictly on leaf nodes of the D-ary tree, keeping Kraft sum ∑ 2^(−l_i) ≤ 1.

3. Huffman Coding Algorithm & Minimum-Variance Tie-Breaking

David Huffman's bottom-up greedy algorithm constructs a guaranteed optimal prefix-free code minimizing Lˉ:

  1. Sort symbols in descending probability; merge the two smallest probabilities, assigning 0 and 1 (repeating until one root of probability 1 remains).
  2. Minimum-Variance Tie-Breaker: Whenever a merged probability ties with an existing symbol probability, place the merged node as HIGH as possible in the sorted list! This preserves exact identical optimal Lˉ while shrinking codeword length variance σL2=∑pi(li−Lˉ)2 (reducing buffer jitter!).
  3. D-ary Huffman Padding Rule (D≥3): Each D-ary step reduces the node count by D−1 (M -> M - (D - 1)), leaving 1 root cleanly iff total leaf count satisfies: M≡1(modD−1)  ⟺  M−1D−1∈Z! If not, append k dummy zero-probability (p=0) symbols first (at the very bottom of the very first merge only!) until (M+k−1) is divisible by (D−1)!
Huffman Merge Construction for p = [0.40, 0.25, 0.20, 0.10, 0.05]: Avg Length L̄ = 2.10 bits A: 0.40 B: 0.25 C: 0.20 D: 0.10 E: 0.05 0 1 0.15 0 1 0.35 0 1 0.60 0 1 Root 1.00 A=0 (1), B=10 (2) C=110 (3) D=1110, E=1111 (4) L̄ = 2.10 bits/sym
Huffman construction merges lowest probabilities bottom-up, assigning shorter codes to frequent symbols cleanly.

4. Shannon–Fano vs Huffman and Universal Lempel–Ziv (LZ78)

  • Shannon–Fano splits sorted probabilities roughly into equal 50/50 sums top-down (lengths li≈⌈−log⁡2pi⌉); easy conceptually, yet occasionally produces strictly higher Lˉ than optimal Huffman (always use Huffman unless Shannon–Fano is specifically asked!).
  • Lempel–Ziv (LZ77/LZ78) is a universal dictionary encoder that builds dynamic phrase pointers on the fly—achieving asymptotic entropy H(X) without needing source symbol probabilities pi beforehand (powering gzip/PNG/ZIP).

Formulas to remember

Average length, coding efficiency & redundancy
Lˉ=∑i=1Mpili,η=H(X)Lˉ,Redundancy=1−η
Shannon noiseless coding bounds (n-extension)
HD(X)≤Lˉnn<HD(X)+1n
Kraft–McMillan prefix inequality
∑i=1MD−li≤1(binary: ∑i=1M2−li≤1)
D-ary Huffman zero-dummy requirementAlways combine any zero-probability dummy leaves exclusively inside the FIRST merge!
Meff=M+kdummy≡1(modD−1),0≤kdummy<D−1
Codeword length variance
σL2=∑i=1Mpi(li−Lˉ)2

Common mistakes

  • Thinking Kraft inequality guarantees ANY code with valid lengths is prefix-free: Kraft ∑2−li≤1 guarantees existence of a prefix code, yet {0, 01, 011, 0111} satisfies Kraft (1/2+1/4+1/8+1/16<1) while failing the prefix property!
  • Skipping dummy zero-probability padding in D-ary Huffman (D≥3): Always ensure Meff≡1(modD−1) by adding 0-probability leaves into the first bottom-up merge only.
  • Adding σL2 tie-breaking penalties by moving combined sums LOW: To minimize variance (and keep balanced trees), place newly combined sums as high as ties allow!
  • Assuming binary block efficiency can't exceed H(X) single-symbol 1-bit limit: Extending to n-symbol blocks compresses strongly skewed sources smoothly toward H(X)!
  • Confusing uniquely decodable with instantaneous: Uniquely decodable codes like {0, 01} may require unbounded future bit lookahead to resolve boundaries, whereas prefix codes decode immediately on every leaf!

Worked examples

Try each step yourself, then reveal the working.

Example 1: Binary Huffman Coding Efficiency and Variance Comparison

numerical

A discrete memoryless source emits 5 symbols with probabilities P=[0.40,0.25,0.20,0.10,0.05]. Design a binary Huffman code, compute average length Lˉ, source entropy H(X), and coding efficiency η.

  1. Step 1: Merge bottom-up two smallest at each step

    Try it on paper first.

Example 2: Minimum-Variance Tie Breaking in Huffman Trees

numerical

A 4-symbol source has P=[0.4,0.2,0.2,0.2]. Compare lengths and variances σL2 obtained by placing the first merged node (0.2+0.2=0.4) at the top vs the bottom.

  1. Step 1: Bottom placement (standard greedy stay-low)

    Try it on paper first.

Example 3: Ternary (D = 3) Huffman Code With Zero-Probability Dummy Padding

numerical

Encode 6 source symbols with P=[0.35,0.25,0.15,0.12,0.08,0.05] into an optimal ternary (D=3) prefix code and compute average ternary digits Lˉ.

  1. Step 1: Apply D-ary leaf modulo condition M + k ≡ 1 mod (D−1)

    Try it on paper first.

Example 4: Applying Kraft–McMillan to Test Codeword Feasibility

conceptual

Check whether a binary prefix-free code exists with lengths (a) {1,2,2,3} and (b) {2,2,2,3,3,4}.

  1. Step 1: Compute Kraft sum for (a)

    Try it on paper first.

Previous year questions

No official GATE EC question is tagged with this topic yet. The topic test below uses practice questions.

Topic test

8 questions: original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE EC syllabus; being checked by a reviewer. Updated 2026-11-29. Spotted a mistake? Use Report an issue on any question, or contact us.