TExcelTAD
Home › Information Theory › Minimum Distance and Code Bounds

Minimum Distance and Code Bounds

How good can a code be? For a given length nn and minimum distance dd, how many codewords can fit? Several classic bounds answer this, and they show that good codes must balance rate against distance. This lesson computes the Hamming, Singleton and Gilbert-Varshamov bounds and compares them with real codes.

Distance and error correction

The Hamming distance between two words is the number of positions in which they differ. A code with minimum distance dd corrects t=⌊d−12⌋t = \lfloor \frac{d - 1}{2} \rfloor errors, because spheres of radius tt around the codewords do not overlap.

The Hamming (sphere-packing) bound

Each sphere of radius tt contains V(n,t)=∑i=0t(ni)V(n, t) = \sum_{i=0}^t \binom{n}{i} words, and the MM spheres fit in 2n2^n words:

M⋅V(n,t)≤2nM \cdot V(n, t) \le 2^n

Codes meeting this with equality are perfect: the Hamming codes (t=1t = 1), the binary Golay code [23,12,7][23, 12, 7] (t=3t = 3) and the trivial repetition codes of odd length.

python · try it yourself
from math import comb, log2

def V(n, t):
    return sum(comb(n, i) for i in range(t + 1))

for name, n, k, d in [("Hamming [7,4,3]", 7, 4, 3), ("Hamming [15,11,3]", 15, 11, 3),
                      ("Golay [23,12,7]", 23, 12, 7), ("repetition [5,1,5]", 5, 1, 5),
                      ("ext. Hamming [8,4,4]", 8, 4, 4)]:
    t = (d - 1) // 2
    print(f"{name:21s}: 2^k V(n,t) = {2**k * V(n, t):7d}, 2^n = {2**n:7d}, perfect: {2**k * V(n, t) == 2**n}")
Hamming [7,4,3]      : 2^k V(n,t) =     128, 2^n =     128, perfect: True
Hamming [15,11,3]    : 2^k V(n,t) =   32768, 2^n =   32768, perfect: True
Golay [23,12,7]      : 2^k V(n,t) = 8388608, 2^n = 8388608, perfect: True
repetition [5,1,5]   : 2^k V(n,t) =      32, 2^n =      32, perfect: True
ext. Hamming [8,4,4] : 2^k V(n,t) =     144, 2^n =     256, perfect: False

The Singleton bound

Delete d−1d - 1 positions from every codeword. The shortened words are still distinct, so M≤2n−d+1M \le 2^{n - d + 1}, that is

k≤n−d+1k \le n - d + 1

Binary codes rarely come close, but Reed-Solomon codes over large alphabets meet it with equality (they are maximum distance separable).

The Gilbert-Varshamov bound

Good codes do exist: a greedy construction (keep adding any word at distance at least dd from all chosen ones) stops only when the spheres of radius d−1d - 1 cover everything, so

M≥2nV(n,d−1)M \ge \frac{2^n}{V(n, d - 1)}
python · try it yourself
import numpy as np
from math import comb

def V(n, r):
    return sum(comb(n, i) for i in range(r + 1))

def greedy_code(n, d):
    chosen = []
    for w in range(2 ** n):
        if all(bin(w ^ c).count("1") >= d for c in chosen):
            chosen.append(w)
    return len(chosen)

n = 10
print(" d | GV lower | greedy | Hamming upper")
for d in [3, 4, 5, 6]:
    t = (d - 1) // 2
    print(f"{d:2d} | {2**n / V(n, d - 1):8.2f} | {greedy_code(n, d):6d} | {2**n // V(n, t):6d}")
 d | GV lower | greedy | Hamming upper
 3 |    18.29 |     64 |     93
 4 |     5.82 |     32 |     93
 5 |     2.65 |      8 |     18
 6 |     1.61 |      4 |     18

Asymptotic rates

For long codes, write δ=dn\delta = \frac{d}{n}. Using log⁡2V(n,δn)≈nH2(δ)\log_2 V(n, \delta n) \approx nH_2(\delta), the bounds become rate limits:

Bound Rate
Gilbert-Varshamov (achievable) R≥1−H2(δ)R \ge 1 - H_2(\delta)
Hamming (upper) R≤1−H2(δ2)R \le 1 - H_2(\frac{\delta}{2})
python · try it yourself
import numpy as np

H2 = lambda p: 0.0 if p <= 0 else float(-p * np.log2(p) - (1 - p) * np.log2(1 - p))
for delta in [0.05, 0.1, 0.2, 0.3, 0.4]:
    print(f"delta = {delta:.2f}: GV rate {max(0.0, 1 - H2(delta)):.3f}, Hamming bound {1 - H2(delta / 2):.3f}")
delta = 0.05: GV rate 0.714, Hamming bound 0.831
delta = 0.10: GV rate 0.531, Hamming bound 0.714
delta = 0.20: GV rate 0.278, Hamming bound 0.531
delta = 0.30: GV rate 0.119, Hamming bound 0.390
delta = 0.40: GV rate 0.029, Hamming bound 0.278

Distance is not everything

The bounds talk about worst-case errors. Shannon's theorem is about typical errors: on a BSC with flip probability ff, a long block has about nfnf errors, and random-like codes correct almost all such patterns even when their minimum distance is less than 2nf2nf. LDPC and turbo codes have modest minimum distances yet approach capacity because they correct almost all typical error patterns (Low-Density Parity-Check Codes).

In software

  • Tables of the best known codes for each (n,k)(n, k) are maintained online (for example by Markus Grassl at codetables.de).
  • For small codes, compute the minimum distance by enumerating all 2k2^k codewords; for large codes this is infeasible and probabilistic estimates are used.

Exercises

Exercise 1: Correcting power

A code has minimum distance 7. How many errors can it correct, and how many can it detect?

Answer

Correct 3; detect 6.

Solution

⌊7−12⌋=3\lfloor \frac{7 - 1}{2} \rfloor = 3 and d−1=6d - 1 = 6.

Exercise 2: Singleton

Can a binary [10,6][10, 6] code have minimum distance 6?

Answer

No.

Solution

Singleton gives d≤n−k+1=5d \le n - k + 1 = 5.

Exercise 3: Hamming bound check (code)

Does the Hamming bound allow a binary [15,7][15, 7] code correcting 2 errors?

Answer

Yes: 27×121=15488≤327682^7 \times 121 = 15488 \le 32768. (The BCH code [15,7,5][15, 7, 5] exists.)

Solution
python · try it yourself
from math import comb

V = sum(comb(15, i) for i in range(3))
print(V, 2**7 * V, 2**15, 2**7 * V <= 2**15)
121 15488 32768 True

Test yourself

A perfect code is one where…

  1. the spheres of radius t around codewords fill the whole space
  2. the minimum distance equals n
  3. the rate is 1
  4. every error is detected

The Gilbert-Varshamov bound shows that…

  1. codes with rate at least 1−H2(δ)1 - H_2(\delta) exist
  2. no code beats rate 1−H2(δ)1 - H_2(\delta)
  3. all codes are perfect
  4. Reed-Solomon codes are optimal

The Singleton bound states…

  1. k≤n−d+1k \le n - d + 1
  2. k≥n−dk \ge n - d
  3. d≤3d \le 3
  4. M⋅V(n,t)≥2nM \cdot V(n, t) \ge 2^n