Messages, Symbols, and Codes
Define sources, alphabets, messages, encoders, decoders, and binary codewords; compare fixed and variable lengths; and use prefix-free trees and the Kraft inequality to guarantee unambiguous decoding.
A message is a sequence of symbols produced by a source. A code represents that sequence in another form so that it can be stored or transmitted. Before asking how short a representation can be, we must state which messages are possible, which distinctions the receiver must preserve, and how the receiver will know where one codeword ends and the next begins.
Sources Produce Symbols and Messages
An alphabet is the set of symbols that may appear. For a binary source the alphabet is . For a four-symbol source it might be . A source is the process or probability model that selects symbols from the alphabet. A message is a finite sequence of the selected symbols, such as .
The symbols need not be written letters. They may represent pixels, audio samples, DNA bases, measurement categories, class labels, or tokens in a language model. Information theory ignores many physical details so that the same reasoning can apply to each case. It still requires a precise alphabet and a clear statement of what counts as one message.
If an alphabet contains symbols, then there are possible messages of length when every position may contain any symbol. A three-symbol alphabet therefore produces
possible messages of length four. This count concerns possibilities, not their probabilities. A source may make some of the messages much more likely than others.
Exercise: Count possible messages
A sensor alphabet is . If a message contains exactly four readings and every reading may take any of the three values, how many different messages are possible?
Compute it first, then check your number.
HintChoose one symbol at each position
There are three choices at each of four positions, so use the multiplication rule.
SolutionFour positions produce eighty-one messages
Each position has three possible symbols. The number of length-four messages is therefore .
Encoding and Decoding Must Agree
A codeword is the representation assigned to a source symbol or source sequence. An encoder maps a source message to an encoded representation . A decoder maps that representation back to a reconstruction:
“X hat equals D of C of x.” The encoder first converts into a code. The decoder then converts that code into the reconstructed message .
For exact, or lossless, reconstruction, every valid message must satisfy
Different messages cannot then share the same complete encoded representation. If for two different messages, the decoder sees the same input in both cases and cannot know which original to return.
This condition also shows why sender and receiver must share the code. Suppose the sender uses and , while the receiver mistakenly uses and . The bits may arrive without error, yet the message will still be decoded incorrectly.
Exercise: Detect a mismatched codebook
The sender uses and , but the receiver uses
and . The sender encodes as 01. What message
will the receiver decode?
Select one choice, then check.
HintUse the receiver's mapping
The receiver interprets 0 as and 1 as .
SolutionDifferent codebooks produce a different message
Under the receiver's codebook, 0 decodes to and 1 decodes to .
The received message is therefore , not the sender's . Correctly
transmitted bits are insufficient when the two sides disagree about their
meaning.
Fixed-Length Binary Codes
With two bits, four source symbols can use the code:
| Source symbol | Codeword |
|---|---|
00 | |
01 | |
10 | |
11 |
A bit is a binary digit, either 0 or 1. Every source symbol in this code
costs two bits. A message of ten symbols therefore costs twenty bits,
regardless of which symbols occur.
More generally, bits provide different binary strings. A fixed-length binary code for possible source symbols needs at least
bits per symbol. The ceiling rounds up because an individual codeword has a whole number of bits. For , two bits provide only four codewords, while three bits provide eight. Three codewords remain unused, but three bits are still necessary.
“The codeword length is the ceiling of log base two of the alphabet size.” It is the smallest whole number of binary choices that can distinguish all source symbols.
Fixed-length codewords are easy to separate. If every codeword has length three, the decoder reads the bit stream in groups of three. No delimiter is needed as long as the decoder knows where the encoded stream begins.
Exercise: Find a fixed codeword length
A source has five possible symbols. What is the smallest fixed number of bits needed for each symbol?
Compute it first, then check your number.
HintCompare powers of two
Two bits provide four codewords; three bits provide eight.
SolutionFive symbols require three bits
Since but , the smallest fixed codeword length is bits. The three unused binary strings do not reduce that required length.
Probabilities Guide Variable-Length Codes
Fixed-length codes ignore how often symbols occur. If is common and is rare, a variable-length code can give a short codeword and a longer one:
| Source symbol | Probability | Codeword | Length |
|---|---|---|---|
0 | |||
10 | |||
110 | |||
111 |
The expected, or long-run average, codeword length is
where is the length of the codeword for . The corresponding fixed-length code used two bits per symbol, so this code saves an average of bit per source symbol. It does not make every encoded message shorter: a message containing many rare symbols may become longer. The saving is an average under the stated source probabilities.
“L equals the sum, over source symbols x, of the probability of x times the codeword length of x.” Frequent symbols contribute to the average more often, which is why assigning them shorter codewords can help.
Exercise: Calculate expected code length
A source emits with probability and with probability . Their codeword lengths are and bits. What is the expected length per source symbol?
Compute it first, then check your number.
HintWeight each length by its probability
Calculate .
SolutionAverage the lengths using source probabilities
The expected length is bits per source symbol. The more common symbol contributes its short length on three quarters of source outputs.
Prefix-Free Codes Mark Their Own Boundaries
The variable-length code above is prefix-free: no complete codeword is the
beginning, or prefix, of another complete codeword. A decoder can therefore
read from left to right and stop as soon as it reaches a complete codeword. The
stream 010111 splits uniquely as
0 | 10 | 111
and decodes to .
Without a suitable boundary rule, decoding can be ambiguous. Consider
The bit string 01 could represent the one-symbol message or the two-symbol
message . This code is not uniquely decodable. Prefix-free coding is a
convenient stronger condition that prevents such ambiguity immediately.
A prefix-free binary code can be drawn as a tree. Moving along a 0 or 1
edge leads to another node, and each source symbol occupies a leaf. A leaf
cannot be an ancestor of another leaf, which is the tree version of the prefix
condition.
Exercise: Decode a prefix-free message
Using , , , and , which
message is represented by 110010?
Select one choice, then check.
HintReturn to the root after each leaf
The first complete codeword is 110. Decode it, return to the tree root, and
continue with the remaining bits.
SolutionSplit at complete codewords
The unique split is 110 | 0 | 10, which maps to , , and . The
decoded message is therefore .
Codeword Lengths Cannot Be Chosen Freely
For binary prefix-free codeword lengths , the Kraft inequality requires
“The sum of two to the negative codeword length, over all codewords, is at most one.” Each term measures the fraction of leaves at that depth consumed by a codeword and all of the descendants that codeword prevents us from using.
For lengths , the sum is
The prefix tree is full. By contrast, lengths give , so no binary prefix-free code can have those lengths. The inequality checks whether a set of lengths can fit in a prefix tree; it does not by itself assign symbols to codewords.
Exercise: Check whether codeword lengths can fit
Can a binary prefix-free code have lengths ?
Select one choice, then check.
HintAdd the occupied tree fractions
Calculate and compare the result with .
SolutionThe lengths exactly fill a binary prefix tree
The Kraft sum is
. It satisfies the inequality, so such a
prefix-free code exists; for example, 0, 10, and 11.
From Code Length to Information
Coding turns the need to distinguish messages into a measurable length. Fixed-length codes depend only on the number of possible symbols. Variable-length codes can use source probabilities to reduce expected length, but they must remain decodable. Prefix-free codes provide clear boundaries, and the Kraft inequality states which collections of binary lengths can fit.
These ideas prepare two separate questions. First, what if the decoder must recover the source exactly, and what if a controlled approximation is enough? The next lesson answers that question through lossless and lossy compression. Afterward, self-information will connect an outcome's probability to its ideal code length, and entropy will average that quantity over the source.