第 19 章
线性码与 Hamming 码:从维恩图到综合解码
随机码本不可实现,需要结构化的码。从 4 位消息加 3 个校验位的维恩图构造出发,抽象出线性码、生成矩阵与校验矩阵;用最小距离刻画纠错能力,最后用综合(syndrome)完成单比特纠错,并说明 Hamming 码为何是完美码。
Linear Codes and Hamming Codes: From Venn Diagrams to Syndrome Decoding
Random codebooks are unimplementable, so we need structure. Starting from a four-message-bit, three-parity-bit Venn construction, we abstract linear codes, generator and parity-check matrices; minimum distance measures correction capability, and syndrome decoding corrects a single bit. Hamming codes are perfect codes.
篇内目录
- From Shannon's random codes to implementable ones
- Timeline
- The (7,4) code from three circles
- Encoding example
- Correcting an error
- Linear codes
- Generator matrix
- The (7,4) generator matrix
- Parity-check matrix
- Weight and distance
- Minimum distance determines correction
- The general Hamming family
- Syndrome decoding
- Encoding
- Syndrome
- From syndrome to error
- Decoding example
- Perfect codes
- What this lecture establishes
- Comprehension check
- Relation to neighbouring lectures
从 Shannon 的随机码到可实现的码
Shannon 证明了:只要速率低于容量,就存在把错误率压到零的码。但证明里的码本是随机抽出来的,有 条码字,逐条存储是指数级;解码要计算收到的向量与每条码字的距离,同样是指数级。更糟的是,最近码字解码对一般线性码是 NP 难的,所以不存在大幅优于遍历的通用算法。
于是任务变成:找结构特殊、解码高效的码。
历史上有两条线索:
- 1948 年 Shannon 证明存在性;
- 1950 年 Hamming 给出第一个可实现的纠错码。
本讲先讲最简单的 Hamming(7,4) 码——4 位消息编成 7 位、纠正 1 位错误——再抽象成线性码与 Hamming 码族,最后给出综合解码。
注意朴素的「4 位一组」方案有局限:若要传一百万位、错误率百分之一,总会有某个 4 位块里出现 2 个错误,超出纠错能力。我们需要能一次纠正更多错误的码,那是下一讲 Reed–Solomon 码的内容。
时间轴
| 时间 | 内容 |
|---|---|
| 0:00 | 回顾 Shannon 定理;随机码在 大时不可实现 |
| 6:23 | Hamming 1950 年给出第一类纠错码 |
| 8:29 | 用维恩图构造 (7,4) 码:4 个消息位 + 3 个校验位 |
| 12:23 | 单比特翻转后如何定位错误 |
| 15:48 | 线性码的定义:对加法与数乘封闭 |
| 18:10 | 生成矩阵 ;编码 |
| 22:32 | Hamming 重量与距离 |
| 26:51 | 定理:最小距离 的码纠正 个错误 |
| 34:13 | 线性码的最小距离 = 非零码字的最小重量 |
| 37:38 | 一般 Hamming 码族:, |
| 49:31 | 生成矩阵与校验矩阵 ; |
| 1:02:15 | 综合(syndrome):只依赖错误,不依赖消息 |
| 1:06:59 | 用综合解码 (7,4) 码的例子 |
| 1:10:33 | 完美码与 Golay 码 |
维恩图构造:(7,4) 码
取 4 个消息位 ,加 3 个校验位 。画三个两两相交的圆,让每个消息位落在特定区域:
- 圆 1:;
- 圆 2:;
- 圆 3:。
校验位的取值规则:每个圆内 1 的个数为偶数(异或和为 0)。于是
码字写成 。
编码例子
消息 :
- ;
- ;
- 。
得到码字 。三个圆的校验都是偶数,成立。
纠错例子
把第 2 位()翻转,收到 。重新算三个圆的奇偶性:
- 圆 1():,奇数,出错;
- 圆 2():,奇数,出错;
- 圆 3():,偶数,正常。
出错的位必须同时属于圆 1 和圆 2、且不属于圆 3。两个错误圆的交集是 ,去掉圆 3 中含的 ,只剩 。所以翻转的是第 2 位,改回来即可。
这就是「校验和定位」的几何版本:每个错误位在各圆中的归属模式不同,三个圆的结果(三个比特)唯一地指出错误位置。
线性码
把上面的构造抽象成代数。设 是一个域(本讲主要是 )。线性码 是满足以下条件的向量集合:
- 若 ,则 ;
- 若 、,则 。
也就是说,任何线性组合仍是码字。取 立刻得到:每个线性码都包含全零向量。
维恩图构造满足线性性:若两个向量各自使三个圆的和为 0,则它们的和也使每个圆的和为 。
生成矩阵
由线性代数,任何线性码都有一组基。把这组基按行排成矩阵 (),码就是 所有行的线性组合:
称为生成矩阵, 是消息(长度 的行向量), 是码字(长度 )。编码因此是一次矩阵乘法:消息中为 1 的那些位置对应的行相加。
(7,4) 码的生成矩阵
按上面的异或公式,四个单位消息对应的码字是
每一行的后三位正是该消息位出现在哪些校验位里。验证第一行: 出现在 和 中,所以后三位是 。
消息 的码字是前三行之和:
校验矩阵
校验矩阵 满足
它的每一行给出一个校验方程:码字与每一行的内积为 0,等价于「对应的圆内 1 的个数为偶数」。
把 写成 (前 列是单位矩阵, 是 的校验部分),则
满足 。验算如下:。在 中 ,符号可以省略;在其他域上则不能。
对本讲的 (7,4) 码:
第一列 对应位置 1( 参与 与 ),第二列 对应位置 2( 参与 与 ),依此类推。
注意 的列恰好是 的全部非零向量,顺序不同而已。这个事实是后面「完美码」的关键。
重量与距离
对 :
- Hamming 重量 :非零坐标的个数;
- Hamming 距离 :两个向量不同坐标的个数。
距离就是差的重量:。若两个坐标相同,差为 0;不同则差非零。
Hamming 距离满足三角不等式,因此是一个度量。
码的最小距离定义为不同码字间距离的最小值:
对线性码,它可以简化成最小非零重量:
理由:,而线性码对减法封闭, 跑遍所有非零码字。
纠错能力由最小距离决定
定理。 最小距离为 的码可以纠正
个错误。
证明。 反设不能纠正 个错误。那么存在一个收到的向量 ,以及两个不同码字 ,使得
由三角不等式,
这违反「 是不同码字间的最小距离」。
直观上,以每个码字为中心、半径 的「球」互不相交。收到的向量若在某个球内,就能唯一地判定原本的码字;若落在两个球的交界附近,就无法判定。
是能保证的唯一纠错个数: 时 , 时 。
一般的 Hamming 码族
(7,4) 码不是孤例。固定整数 ,取
构造 : 的每一列取遍所有长度为 、至少含两个 1 的非零向量。
为什么恰好有 列?长度为 的二进制串共 个,去掉全零(1 个)和恰含一个 1 的( 个),剩下 个。而 正是「消息位 + 校验位」的总数。
- :,,就是 (7,4) 码;
- :,,即 (15,11) 码;
- :,。
最小距离为 3。 证明分三种情况:
- 单行:单位矩阵部分贡献 1 个 1, 的对应列至少含两个 1,所以重量至少 3。
- 两行之和:单位矩阵部分恰有 2 个 1; 部分是两列不同的向量之和,因为两列不同,异或结果非零,至少 1 个 1。总重量至少 3。
- 三行及以上之和:单位矩阵部分已经有至少 3 个 1,重量至少 3。
所以 ,纠正 个错误。
这也说明了为什么 的列必须互不相同:若两列相同,它们之和为 0,两行之和的重量会降到 2,最小距离变成 2,只能检错、不能纠错。
综合解码
有了 与 ,编码与解码都很简洁。
编码
综合
发送 ,信道引入错误向量 ,收到
计算综合(syndrome)
第一步用了 ,所以 综合只依赖错误 ,与消息 无关。这就是「综合」这个名字的来源:它像症状一样,只反映出了什么错。
从综合到错误
一般线性码没有高效的通用算法从 反推 ——这正是前面说的 NP 难问题的另一面。但对 Hamming 码非常简单。
Hamming 码只纠 1 位错误,所以只需考虑重量为 1 的 。若 在第 位为 1,则
也就是说:综合直接等于 的第 列。而 的列是全部非零 位向量,互不相同,所以查表即可定位错误位置,再把那一位翻转。
解码例子
用 (7,4) 码发送 ,编码得 。
假设第 6 位翻转,收到 。计算综合:
在 中找 :它是第 6 列。所以错误在第 6 位,翻转后恢复 ,前 4 位就是消息 。
整个过程只需一次矩阵乘法与一次查表,耗时 ,与码长成多项式关系——与随机码本的指数级遍历形成鲜明对比。
完美码
定义。 一个纠正 位错误的码称为完美码,若每个综合都对应某个重量不超过 的错误向量。
等价的说法:以码字为中心、半径 的 Hamming 球恰好铺满整个空间,既不重叠也不留空隙。
对二进制 Hamming 码:
- 综合有 种(长度 的比特串);
- 零综合对应无错误,共 1 种;
- 其余 种各对应一个位置的单比特错误。
数目恰好吻合,所以 Hamming 码是完美码。
一般地,一个 纠错的 元完美码必须满足球填充条件
左边是半径 的球的大小(按重量 分层计数),右边是空间除以码字数的商。等式成立意味着球恰好铺满。
除了 Hamming 码,二进制情形下另一个解是 、:
对应 的 Golay 码,它与有限单群等深刻结构有关。三元域上还有 的三元 Golay 码。完美码非常稀少,这也说明不能指望靠「完美」来解决所有纠错需求。
本节结论
- 随机码本不可实现,最近码字解码一般是指数级甚至 NP 难,需要结构化的线性码。
- 线性码对加法与数乘封闭,含全零向量,可用生成矩阵 描述,编码为 。
- 校验矩阵 满足 ;综合 只依赖错误。
- 最小距离 决定纠错能力:可纠 个错误;线性码的 等于最小非零重量。
- Hamming 码:、、,纠 1 位错误;其校验矩阵的列取遍所有非零 位向量。
- Hamming 码解码就是「综合等于哪一列」,代价多项式级。
- Hamming 码是完美码: 个综合恰好对应「无错误 + 个单比特错误」;二进制完美码只有 Hamming 码与 Golay 码。
理解检验
1. 用维恩图构造说明:为什么 (7,4) 码能定位 1 位错误,而不能纠正 2 位错误?
参考答案:三个校验圆给出 3 个校验比特,每个错误位置对应一种「落在哪些圆里」的归属模式。7 个位置(4 消息 + 3 校验)的归属各不相同,所以 3 个校验比特足以唯一确定单个错误的位置——这相当于校验矩阵的 7 列两两不同。
不能纠正 2 位错误的原因是:两个不同的双位错误可能产生相同的校验结果。例如翻转位置 与 ,其校验结果是两列之和;若存在另一对 使两列之和相同,就无法区分。更根本地,该码最小距离为 3,按定理只能纠 位。
2. 线性码为什么一定包含全零向量?由此说明最小距离为什么等于最小非零重量。
参考答案:线性码对数乘封闭。取任意 与标量 ,则 。
于是对任意两个不同码字 ,差 也在 中且非零(线性码对加法逆元封闭)。而 ,所以「不同码字间的距离」与「非零码字的重量」取的是同一组数值,两者的最小值相等。
3. 证明:最小距离为 的码可以纠正 个错误。
参考答案:设 ,则 。反设存在收到的向量 与两个不同码字 ,使 且 。
由三角不等式,
与「 是不同码字间的最小距离」矛盾。所以 至多落在一个码字的半径- 球内,解码唯一。
4. 对 码,写出生成矩阵的第四行,并说明它的每一位从何而来。
参考答案:第四行对应消息位 ,为 。
前四位是单位矩阵部分,只有第 4 位是 1。后三位是 参与的校验:从维恩图看, 位于三个圆的公共区域,出现在 、、 三个式子中,所以后三位全是 1。
这也解释了为什么 对应的行重量是 4:它是唯一落在中心区域的比特。
5. 为什么 Hamming 码族要求校验矩阵的列两两不同?如果两列相同会发生什么?
参考答案:两列相同意味着存在两个位置,它们参与完全相同的校验组合。单比特错误发生在这两个位置之一时,综合相同,无法判断是哪一个,纠错失败。
从距离看:若 的第 、 列相同,则单位消息 的码字满足 ,说明它落在码中,重量为 2(因为生成矩阵的对应两行之和的重量可能降到 2)。于是最小距离至多是 2,只能检测 1 位错误,不能纠正。
Hamming 码的构造正是取遍所有非零列,既保证两两不同,又用满了 位向量,从而成为完美码。
6. 综合 为什么与发送的消息无关?这一点对解码有什么意义?
参考答案:
而 ,故 。所以 ,只依赖错误向量。
意义在于:解码器不需要先猜测消息,也不需要搜索所有码字。它只根据「错误长什么样」来定位并纠正错误,纠正后再除以 (或直接取消息位)得到原始消息。这把一个看似需要遍历码本的问题,化成了一个查表问题。
7. 对 码,若收到 ,求综合并完成解码。
参考答案:校验矩阵为
计算 :第一行内积 ;第二行 ;第三行 。综合为 。
在 中查找该列:第 6 列为 。所以错误在第 6 位。翻转后得到 ,前 4 位即消息 。
8. 什么是完美码?用球填充条件说明 Hamming 码为什么完美,并解释为什么完美码很少。
参考答案:完美码指每个综合都对应某个重量不超过 的错误,等价于以码字为中心、半径 的球恰好铺满整个空间,不重叠也不留空。
球填充条件是
左边是球的体积,右边是空间大小除以码字数。对二进制 Hamming 码,,左边为 ,右边为 ,恰好相等。
完美码稀少,是因为这是一个非常紧的丢番图方程:、、 都要恰好配合。二进制情形只有 Hamming 码与 Golay 码 ,三元情形多一个 的 Golay 码。大多数参数下球必然有重叠或空隙。
与前后课的关系
上一讲用随机码本证明了容量可达,但码本指数大、解码 NP 难;本讲给出第一类可实现的结构化码:线性码用矩阵描述码本,Hamming 码用综合实现多项式时间的单比特纠错。
Hamming 码的局限是最小距离只有 3,只能纠 1 位错误;码长越长,单块里出现多个错误的概率越高。下一讲 Reed–Solomon 码用有限域上的多项式求值构造码字,把最小距离提高到 ,并用错误定位多项式实现多错误纠正。
更后面,课程会转向密码学与秘密共享,那里线性代数的语言会再次出现。
From Shannon's random codes to implementable ones
Shannon proved that below capacity there exists a code driving the error to zero. But his codebook is drawn at random: codewords, exponential storage, and decoding compares the received vector against every codeword, also exponential. Worse, nearest-codeword decoding for a general linear code is NP-hard, so no generic algorithm is substantially better than scanning.
The task therefore becomes: find structured codes with efficient decoding.
Two historical markers:
- 1948: Shannon proves existence;
- 1950: Hamming gives the first implementable error-correcting code.
This lecture starts with the simplest, Hamming(7,4) — four message bits encoded into seven, correcting one bit — then abstracts to linear codes and the Hamming family, and finishes with syndrome decoding.
The naive "blocks of four" scheme has a limit: to send a million bits at a one-percent error rate, some block of four will contain two errors and fail. Codes correcting many errors at once are needed; that is the Reed–Solomon lecture.
Timeline
| Time | Topic |
|---|---|
| 0:00 | Recap of Shannon's theorem; random codes are impractical for large |
| 6:23 | Hamming's 1950 construction of the first error-correcting codes |
| 8:29 | Venn-diagram construction of the (7,4) code: four message bits plus three parity bits |
| 12:23 | Locating a single flipped bit |
| 15:48 | Definition of a linear code: closed under addition and scalar multiplication |
| 18:10 | Generator matrix ; encoding |
| 22:32 | Hamming weight and distance |
| 26:51 | Theorem: a code of minimum distance corrects errors |
| 34:13 | For a linear code, minimum distance equals minimum nonzero weight |
| 37:38 | The general Hamming family: , |
| 49:31 | Generator and parity-check matrices; |
| 1:02:15 | The syndrome depends only on the error |
| 1:06:59 | Syndrome decoding on the (7,4) code |
| 1:10:33 | Perfect codes and the Golay code |
The (7,4) code from three circles
Take four message bits and add three parity bits . Draw three mutually overlapping circles and place the message bits in specific regions:
- circle 1: ;
- circle 2: ;
- circle 3: .
The rule is: each circle contains an even number of 1s (its XOR is zero). Hence
A codeword is .
Encoding example
For the message :
- ;
- ;
- .
The codeword is , and all three parity checks are even.
Correcting an error
Flip the second bit : receive . Recompute the parity of each circle:
- circle 1 (): , odd, fails;
- circle 2 (): , odd, fails;
- circle 3 (): , even, passes.
The error must lie in both failing circles and outside the passing one. Circles 1 and 2 intersect in ; removing (which lies in circle 3) leaves . So the second bit is wrong and is flipped back.
This is parity checking in geometric form: every position has a distinct pattern of circle membership, so the three parity results identify the error position uniquely.
Linear codes
Abstract the construction. Let be a field (here mostly ). A linear code satisfies:
- if , then ;
- if and , then .
Every linear combination of codewords is again a codeword. Taking shows every linear code contains the all-zero vector.
The Venn construction is linear: if two vectors make all three circle sums zero, their sum does too, since .
Generator matrix
By linear algebra, any linear code has a basis. Put the basis vectors as rows of a matrix ; the code is the set of all linear combinations of rows:
is a generator matrix, is the message (a row vector of length ), and is the codeword (length ). Encoding is one matrix multiplication: add the rows where the message has a 1.
The (7,4) generator matrix
From the XOR formulas, the codewords of the four unit messages are
Each row's last three entries record which parity bits that message bit enters. Row one: appears in and , so the tail is .
The message encodes to the sum of the first three rows:
Parity-check matrix
A parity-check matrix satisfies
Each row of is one parity equation: the codeword's inner product with that row is zero, i.e. the corresponding circle has an even number of 1s.
Writing (identity block then parity block), the matrix
works, since . Over , , so signs can be dropped; over other fields they cannot.
For the (7,4) code,
Column one is for position 1 ( enters and ); column two is for position 2 ( enters and ), and so on.
The columns of are exactly the seven nonzero vectors of , merely reordered. That fact is what makes Hamming codes perfect.
Weight and distance
For :
- the Hamming weight is the number of nonzero coordinates;
- the Hamming distance is the number of coordinates where they differ.
Distance is the weight of the difference: , since equal coordinates contribute 0 and unequal ones contribute a nonzero difference.
Hamming distance satisfies the triangle inequality, hence is a metric.
The minimum distance of a code is
For a linear code it reduces to the minimum nonzero weight:
Indeed , and since the code is closed under subtraction, ranges over all nonzero codewords.
Minimum distance determines correction
Theorem. A code of minimum distance corrects
errors.
Proof. Suppose it cannot. Then some received vector lies within distance of two distinct codewords :
The triangle inequality gives
contradicting the definition of .
Geometrically, the balls of radius around distinct codewords are disjoint. A received vector inside one ball has a unique owner; near an overlap it would not. The value is exactly what can be guaranteed: gives , gives .
The general Hamming family
(7,4) is one member. Fix and set
Build where the columns of run through all length- binary vectors with at least two 1s.
Why exactly columns? There are binary strings of length ; remove the all-zero string (one) and the unit vectors (s), leaving . And is the number of message plus parity positions.
- : , — the (7,4) code;
- : , — the (15,11) code;
- : , .
The minimum distance is 3. Three cases:
- A single row: the identity block contributes one 1, and the corresponding column of has at least two 1s, so the weight is at least 3.
- A sum of two rows: the identity block has exactly two 1s; the parity part is the XOR of two distinct columns, which is nonzero, so at least one more 1. Weight at least 3.
- A sum of three or more rows: the identity block alone has at least three 1s.
So and error is corrected.
This also explains why the columns of must be distinct: two equal columns would XOR to zero, dropping the weight of that two-row sum to 2 and the minimum distance to 2, which detects but cannot correct.
Syndrome decoding
With and , encoding and decoding are clean.
Encoding
Syndrome
Send , let the channel add an error vector , and receive
The syndrome is
The first term vanishes because , so the syndrome depends only on , not on the message. That is why it is called a syndrome: like a symptom, it reports only what went wrong.
From syndrome to error
For a general linear code there is no efficient universal way to invert — the other face of the NP-hardness mentioned earlier. For Hamming codes it is trivial.
A Hamming code corrects one bit, so only weight-one errors matter. If has its 1 in position , then
So the syndrome is literally column of . Since the columns are all nonzero vectors and therefore distinct, a lookup locates the error, which is then flipped.
Decoding example
Send , encoded as .
Suppose position 6 flips, so . Compute
In , the vector is column 6. The error is in position 6; flipping it recovers , whose first four bits are the message .
The whole procedure is one matrix multiplication and one lookup, costing — polynomial in the block length, in sharp contrast with scanning an exponential codebook.
Perfect codes
Definition. A -error-correcting code is perfect if every syndrome corresponds to some error of weight at most .
Equivalently, the Hamming balls of radius around the codewords tile the whole space: no overlaps, no gaps.
For a binary Hamming code:
- there are syndromes (binary strings of length );
- the zero syndrome corresponds to no error;
- the remaining syndromes correspond to single-bit errors at the positions.
The counts match exactly, so Hamming codes are perfect.
In general, a -correcting -ary perfect code must satisfy the sphere-packing condition
The left side is the volume of a radius- ball (counted layer by layer), the right side the space size divided by the number of codewords. Equality means the balls tile perfectly.
Beyond Hamming codes, the binary solution with , is
the Golay code, connected to finite simple groups and other deep structures. Over there is the ternary Golay code. Perfect codes are extremely rare, so one cannot rely on perfection for all correction needs.
What this lecture establishes
- Random codebooks are unimplementable and nearest-codeword decoding is generally exponential or NP-hard; structured linear codes are needed.
- A linear code is closed under addition and scalar multiplication, contains zero, and is described by a generator matrix with .
- A parity-check matrix satisfies , and the syndrome depends only on the error.
- Minimum distance gives correction of errors; for linear codes is the minimum nonzero weight.
- Hamming codes have , , , and correct one error; their parity-check columns are all nonzero -bit vectors.
- Hamming decoding is "which column equals the syndrome", costing polynomial time.
- Hamming codes are perfect: syndromes match "no error plus single-bit errors"; the only binary perfect codes are Hamming and Golay.
Comprehension check
1. Using the Venn construction, why can the (7,4) code locate one error but not correct two?
Reference answer: The three parity circles produce three check bits, and each position has a distinct pattern of circle membership. The seven positions (four message, three parity) have seven distinct patterns, so three check bits identify a single flipped position — equivalently, the seven columns of the parity-check matrix are distinct.
Two errors cannot be corrected because two different pairs may yield the same check result: flipping positions gives the XOR of two columns, and another pair could give the same XOR, making them indistinguishable. More fundamentally, the minimum distance is 3, so the theorem guarantees only .
2. Why must a linear code contain the all-zero vector? Use this to show that minimum distance equals minimum nonzero weight.
Reference answer: A linear code is closed under scalar multiplication. For any and scalar , .
For distinct codewords , the difference is a nonzero codeword (closure under additive inverses). Since , the set of pairwise distances equals the set of weights of nonzero codewords, so their minima agree.
3. Prove that a code of minimum distance corrects errors.
Reference answer: Let , so . Suppose some received vector is within distance of two distinct codewords . The triangle inequality gives
contradicting the minimality of . Hence lies in at most one radius- ball, and decoding is unique.
4. For the (7,4) code, write the fourth row of the generator matrix and explain each entry.
Reference answer: The row for message bit is .
The first four entries are the identity block, with only the fourth equal to 1. The last three record which parity bits enters: in the Venn diagram lies in the central region and appears in , , and , so all three are 1.
This is also why that row has weight 4: is the only bit in the centre.
5. Why must the columns of a Hamming parity-check matrix be distinct? What goes wrong otherwise?
Reference answer: Equal columns mean two positions participate in exactly the same set of checks. A single-bit error at either position produces the same syndrome, so the decoder cannot tell which one occurred, and correction fails.
In distance terms: if columns and of coincide, then , so lies in the code with weight 2 (the code is the kernel of ). The minimum distance is then at most 2, which detects one error but cannot correct it.
The Hamming construction takes all nonzero columns: distinct by construction, exactly exhausting the -bit vectors, which is what makes the code perfect.
6. Why is the syndrome independent of the message? What does that buy the decoder?
Reference answer:
and , so . Hence , depending only on the error.
The decoder therefore does not need to guess the message or search codewords. It locates and corrects the error from the error's own signature, then reads off the message. A problem that seemed to require scanning the codebook becomes a lookup.
7. Decode the received word for the (7,4) code.
Reference answer: With
compute : row one gives ; row two gives ; row three gives . The syndrome is .
Column 6 of is , so the error is at position 6. Flipping it yields , whose first four bits are the message .
8. What is a perfect code? Use the sphere-packing condition to explain why Hamming codes are perfect, and why perfect codes are rare.
Reference answer: A perfect code has every syndrome correspond to an error of weight at most , equivalently the radius- Hamming balls around codewords tile the space with no overlap or gap.
The sphere-packing condition is
the ball volume on the left, the space size divided by the code size on the right. For binary Hamming codes with , the left side is , matching .
Perfect codes are rare because this is a very tight Diophantine equation in , , and . Over the binary alphabet only Hamming codes and the Golay code qualify; over there is additionally the ternary Golay code. For most parameters the balls must overlap or leave gaps.
Relation to neighbouring lectures
The previous lecture used random codebooks to prove capacity is achievable, but the codebook is exponential and decoding NP-hard; this lecture gives the first implementable structured family: linear codes describe the codebook with a matrix, and Hamming codes decode single-bit errors in polynomial time via the syndrome.
The limitation is minimum distance 3 — only one error. The next lecture constructs Reed–Solomon codes from polynomial evaluations over finite fields, raising the minimum distance to and correcting many errors with an error-locator polynomial.
Later the course turns to cryptography and secret sharing, where linear algebra returns in a different guise.