Apps Ch5
Apps Ch5
Apps Ch5
Coding Theory
Prerequisites: The prerequisites for this chapter are the basics of logic, set
theory, number theory, matrices, and probability. See Sections 1.1, 2.1, 2.2,
3.4–3.7, and 6.1 of Discrete Mathematics and Its Applications.
Introduction
The usual way to represent, manipulate, and transmit information is to use
bit strings, that is, sequences of zeros and ones. It is extremely difficult, and
often impossible, to prevent errors when data are stored, retrieved, operated
on, or transmitted. Errors may occur from noisy communication channels,
electrical interference, human error, or equipment error. Similarly, errors are
introduced into data stored over a long period of time on magnetic tape as the
tape deteriorates.
It is particularly important to ensure reliable transmission when large com-
puter files are rapidly transmitted or when data are sent over long distances,
such as data transmitted from space probes billions of miles away. Similarly, it
is often important to recover data that have degraded while stored on a tape.
To guarantee reliable transmission or to recover degraded data, techniques from
coding theory are used. Messages, in the form of bit strings, are encoded by
translating them into longer bit strings, called codewords. A set of codewords
73
74 Applications of Discrete Mathematics
is called a code. As we will see, we can detect errors when we use certain
codes. That is, as long as not too many errors have been made, we can de-
termine whether one or more errors have been introduced when a bit string
was transmitted. Furthermore, when codes with more redundancy are used, we
can correct errors. That is, as long as not too many errors were introduced in
transmission, we can recover the codeword from the bit string received when
this codeword was sent.
Coding theory, the study of codes, including error detecting and error
correcting codes, has been studied extensively for the past forty years. It has
become increasingly important with the development of new technologies for
data communications and data storage. In this chapter we will study both
error detecting and error correcting codes. We will introduce an important
family of error correcting codes. To go beyond the coverage in this chapter and
to learn about the latest applications of coding theory and the latest technical
developments, the reader is referred to the references listed at the end of the
chapter.
Example 1 Suppose that a parity check bit is added to a bit string before
it is transmitted. What can you conclude if you receive the bit strings 1110011
and 10111101 as messages?
Solution: Since the string 1110011 contains an odd number of 1s, it cannot
be a valid codeword (and must, therefore, contain an odd number of errors).
Chapter 5 Coding Theory 75
On the other hand, the string 10111101 contains an even number of 1s. Hence
it is either a valid codeword or contains an even number of errors.
Example 2 Encode the bit string 011001 by repeating each bit twice.
Solution: Repeating each bit twice produces the codeword 001111000011.
What errors can be detected when we repeat each bit of a codeword twice?
Since the codewords are those bit strings that contain pairs of matching bits,
that is, where the first two bits agree, the third and fourth bits agree, and so
on, we can detect errors that change no more than one bit of each pair of these
matching bits. For example, we can detect errors in the second bit, the third
bit, and the eighth bit of when codewords have eight bits (such as detecting
that 01101110, received when the codeword 00001111 was sent, has errors). On
the other hand, we cannot detect an error when the third and fourth bit are
both changed (such as detecting that 00111111, received when the codeword
00001111 was sent, has errors).
So far we have discussed codes that can be used to detect errors. When
errors are detected, all we can do to obtain the correct codeword is to ask for
retransmission and hope that no errors will occur when this is done. However,
there are more powerful codes that can not only detect but can also correct
errors. We now turn our attention to these codes, called error correcting codes.
We decode a bit string, which may contain errors, using the simple majority
rule. For example, to determine x1 , we look at x1 , x4 , and x7 . If two or three
of these bits are 1, we conclude that x1 = 1. Otherwise, if two or three of
these bits are 0, we conclude that x1 = 0. In general, we look at the three bits
corresponding to each bit in the original message. We decide that a bit in the
message is 1 if a majority of bits in the string received in positions corresponding
to this bit are 1s and we decide this bit is a 0 otherwise. Using this procedure,
we correctly decode the message as long as at most one error has been made in
the bits corresponding to each bit of the original message.
For example, when a triple repetition code is used, if we receive 011111010,
we conclude that the message sent was 011. (For instance, we decided that the
first bit of the message was 0 since the first bit is 0, the fourth bit is 1, and the
seventh bit is 0, leading us to conclude that the fourth bit is wrong.)
To make the ideas introduced by the triple repetition code more precise
we need to introduce some ideas about the distance between codewords and
the probability of errors. We will develop several important concepts before
returning to error correcting codes.
Hamming Distance
There is a simple way to measure the distance between two bit strings. We look
at the number of positions in which these bit strings differ. This approach was
used by Richard Hamming* in his fundamental work in coding theory.
Note that the Hamming distance between two bit strings equals the number
of changes in individual bits needed to change one of the strings into the other.
* Richard Hamming (1915–1998) was one of the founders of modern coding theory.
He was born in Chicago and received his B.S. from the University of Chicago, his M.A.
from the University of Nebraska, and his Ph.D. from the University of Illinois in 1942.
He was employed by the University of Illinois from 1942 to 1944 and the University
of Louisville from 1944 to 1945. From 1945 until 1946 he was on the staff of the
Manhattan Project in Los Alamos. He joined Bell Telephone Laboratories in 1946,
where he worked until 1976. His research included work in the areas of coding theory,
numerical methods, statistics, and digital filtering. Hamming joined the faculty of the
Naval Postgraduate School in 1976. Among the awards he won are the Turing Prize
from the ACM and the IEEE Hamming Medal (named after him).
Chapter 5 Coding Theory 77
Example 4 Find the Hamming distance between the bit strings 01110 and
11011 and the Hamming distance between the bit strings 00000 and 11111.
Solution: Since 01110 and 11011 differ in their first, third, and fifth bits,
d(01110, 11011) = 3. Since 00000 and 11111 differ in all five bits, we conclude
that d(00000, 11111) = 5.
Theorem 1 Let d(x, y) represent the Hamming distance between the bit
strings x and y of length n. Then
(i) d(x, y) ≥ 0 for all x, y
(ii) d(x, y) = 0 if and only if x = y
(iii) d(x, y) = d(y, x) for all x, y
(iv) d(x, y) ≤ d(x, z) + d(z, y) for all x, y, z.
Proof: Properties (i), (ii), and (iii) follow immediately from the definition of
the Hamming distance. To prove (iv), we use the fact that d(x, y) is the number
of changes of bits required to change x into y. Note that for every string z of
length n the number of changes needed to change x into y does not exceed the
number of changes required to change x into z and to then change z into y.
was sent from the code C = {0000, 1110, 1011} if 0110 is received.
Solution: We first find the distance between 0110 and each of the codewords.
We find that
d(0000, 0110) = 2,
d(1110, 0110) = 1,
d(1011, 0110) = 3.
Since the closest codeword to 0110 is 1110, we conclude that 1110 was the
codeword sent.
Will nearest neighbor decoding produce the most likely codeword that was
sent from a binary string that was received? It is not hard to see that it will
if each bit sent has the same probability p of being received incorrectly and
p < 1/2. We call a transmission channel with this property a binary symmetric
channel. Such a channel is displayed in Figure 1.
Example 6 Suppose that when a bit is sent over a binary symmetric channel
the probability it is received incorrectly is 0.01. What is the probability that
the bit string 100010 is received when the bit string 000000 is sent?
Solution: Since the probability a bit is received incorrectly is 0.01, the prob-
ability that a bit is received correctly is 1 − 0.01 = 0.99. For 100010 to be
received, when 000000 is sent, it is necessary for the first and fifth bits to be
received incorrectly and the other four bits to be received correctly. The prob-
ability that this occurs is
We will now show that nearest neighbor decoding gives us the most likely
codeword sent, so that it is also maximum likelihood decoding.
Chapter 5 Coding Theory 79
Solution: To compute the minimum distance of this code we will find the
distance between each pair of codewords and then find the smallest such dis-
tance. We have d(00000, 01110) = 3, d(00000, 10011) = 3, d(00000, 11111) = 5,
d(01110, 10011) = 4, d(01110, 11111) = 2, and d(10011, 11111) = 2. We see
that the minimum distance of C is 2.
C = {000000, 111111}.
80 Applications of Discrete Mathematics
Solution: Since there are only two codewords and d(000000, 111111) = 6, the
minimum distance of this code is 6.
The minimum distance of a code tells us how many errors it can detect
and how many errors it can correct, as the following two theorems show.
Perfect Codes
To allow error correction we want to make the minimum distance between
codewords large. But doing so limits how many codewords are available. Here
we will develop a bound on the number of codewords in a binary code with a
given minimum distance.
bit strings y of length n such that d(x, y) ≤ k (where d is the Hamming dis-
tance).
Proof: Let i be a nonnegative integer. The number of bit strings y with
d(x, y) = i equals the number of ways to select the i locations where x and y
differ. This can be done in C(n, i) ways. It follows that there are
We can now give a useful bound on how many codewords can be in a code
consisting of n-tuples that can correct a specified number of errors.
2n
C(n, 0) + C(n, 1) + · · · + C(n, k).
Proof: There are 2n bit strings of length n. By Lemma 1 the sphere of radius k
centered at a codeword x contains
bit strings. Since no bit string can be in two such spheres (by Lemma 2),
it follows that the number of bit strings of length n is at least as large as
the number of codewords times the number of bit strings in each such sphere.
Hence,
2n ≥ |C|[C(n, 0) + C(n, 1) + · · · + C(n, k)].
We obtain the inequality we want by dividing by the second factor on the right-
hand side of this inequality (and writing the inequality with the smaller term
first).
The sphere packing bound gives us an upper bound for the number of
codewords in a binary code with a given minimum distance where codewords
are bit strings of length n. The codes that actually achieve this upper bound,
Chapter 5 Coding Theory 83
that is, that have the most codewords possible, are of special interest because
they are the most efficient error correcting codes. Such codes are known as
perfect codes.
Example 11 Show that the code consisting of just two codewords 00000
and 11111 is a perfect binary code.
Solution: The minimum distance between codewords in this code is 5. The
sphere packing bound states that there are at most
25 /[C(5, 0) + C(5, 1) + C(5, 2)] = 32/16 = 2
codewords in a code consisting of 5-tuples with minimum distance 5. Since
there are 2 codewords in this code, it is a perfect binary code.
The code in Example 11 is called a trivial perfect code since it only consists
of the two codewords, one containing only 0s and the other containing only 1s.
As Exercise 8 demonstrates, when n is an odd positive integer there are trivial
perfect codes consisting of the two codewords which are bit strings of length n
consisting of all 0s and of all 1s. Finding perfect binary codes different from
the trivial codes has been one of the most important problems in coding theory.
In the next section we will introduce a class of perfect binary codes known as
Hamming codes.
Generator Matrices
Before describing Hamming codes, we need to generalize the concept of a parity
check bit. When we use a parity check bit, we encode a message x1 x2 . . . xk as
x1 x2 . . . xk xk+1 where xk+1 = (x1 + x2 + · · · + xk ) mod 2. To generalize this
notion, we add more than one check bit. More precisely, we encode a message
x1 x2 . . . xk as x1 x2 . . . xk xk+1 . . . xn , where the last n − k bits xk+1 ,...,xn , are
parity check bits, obtained from the k bits in the message. We will describe
how these parity check bits are specified.
Consider a k-bit message x1 x2 · · · xk as a 1 × k matrix x. Let G be a k × n
matrix that begins with the k × k identity matrix Ik . That is, G = (Ik |A),
where A is a k × (n − k) matrix, known as a generator matrix. We encode x
as E(x) = xG, where we do arithmetic modulo 2. Coding using a parity check
bit and using the triple repetition code are special cases of this technique, as
illustrated in Examples 12 and 13.
Example 13 We can represent encoding using the triple repetition code for
three-bit messages as E(x) = xG, where
⎛ ⎞
1 0 0 1 0 0 1 0 0
G = ⎝0 1 0 0 1 0 0 1 0⎠.
0 0 1 0 0 1 0 0 1
Note that G is formed by repeating the identity matrix of order three, I3 , three
times, that is,
What are the codewords in the code generated by this generator matrix?
Chapter 5 Coding Theory 85
It is easy to see that we can find the codewords in a binary code generated
by a generator matrix G by taking all possible linear combinations of the rows
of G (since arithmetic is modulo 2, this means all sums of subsets of the set
of rows of G). The reader should verify this for codewords in the code in
Example 14.
It is easy to see that the binary codes formed using generator matrices have
the property that the sum of any two codewords is again a codeword. That
is, they are linear codes. To see this, suppose that y1 and y2 are codewords
generated by the generator matrix G. Then there are bit strings x1 and x2 such
that E(x1 ) = y1 and E(x2 ) = y2 , where E(x) = xG. It follows that y1 + y2
is also a codeword since E(x1 + x2 ) = y1 + y2 . (Here we add bit strings by
adding their components in the same positions using arithmetic modulo 2.)
We will see that there is an easy way to find the minimum distance of a
linear code. Before we see this, we need to make the following definition.
Example 15 Find the weights of the codewords 00000, 10111, and 11111.
Solution: Counting the number of 1s in each of these codewords we find that
w(00000) = 0, w(10111) = 4, and w(11111) = 5.
We also will need the fact that 0, the bit string with all 0s, belongs to a
linear code.
86 Applications of Discrete Mathematics
Example 16 Use the parity check matrix to determine which codeword from
the code in Example 14 was sent if 001111 was received. Assume that at most
one error was made.
88 Applications of Discrete Mathematics
This is the fifth column of H. If follows that the fifth bit of 001111 is
incorrect. Hence the code word sent was 001101.
Hamming Codes
We can now define the Hamming codes. We define them using parity check
matrices.
The 16 codewords in this code C can be found by taking all possible sums of
the rows of G. We leave this as an exercise at the end of the chapter.
To show that the Hamming codes are perfect codes we first need to establish
two lemmas.
Summary
In this chapter we have studied how codes can be used for error detection and
error correction. We have introduced an important class of codes known as the
Hamming codes. However, we have only touched the surface of a fascinating and
important subject that has become extremely important for modern computing
and communications. The interested reader can consult the references listed at
the end of this chapter to learn about many other classes of codes that have
practical applications.
Chapter 5 Coding Theory 91
For example, pictures of the planets taken by space probes have been en-
coded using powerful codes, such as a code known as the Reed-Muller code
(see [5] for details). This code has been used to encode the bit string of length 6
representing the brightness of each pixel of an image by a bit string of length 32.
This Reed-Muller code consists of 64 codewords, each a bit string of length 32,
with minimum distance 16. Another interesting example is the use of a family of
codes known as Reed-Solomon codes used in digital audio recording (see [5] for
details). Finally, many concepts and techniques from both linear algebra and
abstract algebra are used in coding theory. Studying coding theory may con-
vince the skeptical reader about the applicability of some of the more abstract
areas of mathematics.
Suggested Readings
Exercises
1. Could the following bit strings have been received correctly if the last bit
is a parity bit?
a) 1000011
b) 111111000
c) 10101010101
d) 110111011100
2. Find the Hamming distance between each of the following pairs of bit
strings.
92 Applications of Discrete Mathematics
a) 00000,11111
b) 1010101,0011100
c) 000000001,111000000
d) 1111111111,0100100011
3. Suppose the bit string 01011 is sent using a binary symmetric channel where
the probability a bit is received incorrectly is 0.01. What is the probability
that
a) 01011, the bit string sent, is received?
b) 11011 is received?
c) 01101 is received?
d) 10111 is received?
e) no more than one error is present in the bit string received?
4. How many errors can each of the following binary codes detect and how
many can it correct?
a) {0000000, 1111111}
b) {00000, 00111, 10101, 11011}
c) {00000000, 11111000, 01100111, 100101101}
5. Suppose that the probability of a bit error in transmission over a binary
symmetric channel is 0.001. What is the probability that when a codeword
with eight bits is sent from a code with minimum distance five, the bit
string received is decoded as the codeword sent (when nearest neighbor
decoding is used)?
6. Show that if the minimum distance between codewords is four it is possible
to correct an error in a single bit and to detect two bit errors without
correction.
7. Use the sphere packing bound to give an upper bound on the number of
codewords in a binary code where codewords are bit strings of length nine
and the minimum distance between codewords is five.
8. Show that whenever n is an odd positive integer, the binary code consisting
of the two bit strings of length n containing all 0s or all 1s is a perfect code.
9. Suppose that x and y are bit strings of length n and m is the number of
positions where both x and y have 1s. Show that w(x+y) = w(x)+w(y)−
2m.
10. Find the parity check matrix associated with the code formed by adding a
parity check bit to a bit string of length 4.
11. Find the parity check matrix associated with the triple repetition code for
bit strings of length 3.
Chapter 5 Coding Theory 93
Computer Projects
1. Given a binary code, determine the number of errors that it can detect and
the number of errors that it can correct.
2. Given a binary code with minimum distance k, where k is a positive integer,
write a program that will detect errors in codewords in as many as k − 1
positions and correct errors in as many as (k − 1)/2 positions.