ArticleslgStudy

computer science

Tunstall coding

Tunstall coding is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Tunstall coding rather than just read about it. In short: In computer science and information theory, Tunstall coding is a form of entropy coding used for lossless data compression. History Tunstall coding was the subject of Brian Parker Tunstall's PhD thesis in 1967, while at Georgia Institute of Technology.

Tunstall coding — main illustration
Tunstall coding — illustration

Key takeaways

  • Tunstall coding belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Tunstall coding to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Tunstall coding from memory before moving on to harder problems.

Reference excerpt

In computer science and information theory, Tunstall coding is a form of entropy coding used for lossless data compression.

History Tunstall coding was the subject of Brian Parker Tunstall's PhD thesis in 1967, while at Georgia Institute of Technology. The subject of that thesis was "Synthesis of noiseless compression codes" Its design is a precursor to Lempel–Ziv.

Properties Unlike variable-length codes, which include Huffman and Lempel–Ziv coding, Tunstall coding is a code which maps source symbols to a fixed number of bits. Both Tunstall codes and Lempel–Ziv codes represent variable-length words by fixed-length codes. Unlike typical set encoding, Tunstall coding parses a stochastic source with codewords of variable length. It can be shown that, for a large enough dictionary, the number of bits per source letter can be arbitrarily close to H(U), the entropy of the source.

Algorithm The algorithm requires as input an input alphabet 𝒰, along with a distribution of probabilities for each word input. It also requires an arbitrary constant C, which is an upper bound to the size of the dictionary that it will compute. The dictionary in question, D, is constructed as a tree of probabilities, in which each edge is associated to a letter from the input alphabet. The algorithm goes like this:

D := tree of |𝒰| leaves, one for each letter in 𝒰. While |D|<C: Convert most probable leaf to tree with |𝒰| leaves.

Example

Let's imagine that we wish to encode the string "hello, world". Let's further assume (somewhat unrealistically) that the input alphabet 𝒰 contains only characters from the string "hello, world" — that is, 'h', 'e', 'l', ',', ' ', 'w', 'o', 'r', 'd'. We can therefore compute the probability of each character based on its statistical appearance in the input string. For instance, the letter L appears thrice in a string of 12 characters: its probability is 312. We initialize the tree, starting with a tree of |𝒰|=9 leaves. Each word is therefore directly associated to a letter of the alphabet. The 9 words that we thus obtain can be encoded into a fixed-sized output of ⌈log2(9)⌉=4 bits.

We then take the leaf of highest probability (here, w1), and convert it to yet another tree of |𝒰|=9 leaves, one for each character. We re-compute the probabilities of those leaves. For instance, the sequence of two letters L happens once. Given that there are three occurrences of letters followed by an L, the resulting probability is 13⋅312=112. We obtain 17 words, which can each be encoded into a fixed-sized output of ⌈log2(17)⌉=5 bits.

Note that we could iterate further, increasing the number of words by |𝒰|−1=8 every time.

Limitations Tunstall coding requires the algorithm to know, prior to the parsing operation, what the distribution of probabilities for each letter of the alphabet is. This issue is shared with Huffman coding. Its requiring a fixed-length block output makes it lesser than Lempel–Ziv, which has a similar dictionary-based design, but with a variable-sized block output.

Implied Read for base modification

This is an example of a Tunstall code being used to read (for transmit ) any data that is scrambled, e.g. by polynomial scrambling. This particular example helps to modify the base of the data from 2 to 3 in a stream therefore avoiding expensive base modification routines. With base modification we are particularly bound by 'efficiency' of reads, where ideally lognbits are used at an average to read the code. This ensures that upon use of the new base, which is duty bound to use at best lognbits per code, our reads do not result in lesser margin of efficiency of transmission for which we are employing the base modification in the first place. We can therefore then employ the read-to-modify-base mechanism for efficiently transmitting the data across channels that have a different base. eg. transmitting binary data across say MLT-3 channels with increased efficiency when compared to mapping codes (with large number of unused codes).

We are essentially reading perfectly scrambled binary data or 'implied data' for the purpose of transmitting it using base-3 channels. Please see leaf nodes in the Ternary Tunstall Tree. As we can see, the read will result in the first digit being 'B' - 25% of the time as it has an implied probability of 25%, being of length 2 trying to read from implied data. A 'B' such read does not read any further, but with 75% probability we read 'A' or 'C', requiring another code. Thus the efficiency of the read is 2.75 (average length of the size 7 Huffman code) / 1.75 (average length of the 1 or 2-digit base - 3 Tunstall code) = 1.57142857 which is as per requirement very close to log23=1.5849625 which calculates to an efficiency of 99.15%. We can then transmit the symbols using base-3 channels efficiently.

References

Illustrations

Tunstall coding illustration
Tunstall coding: Ternary Tunstall Tree
Ternary Tunstall Tree

Worked examples

Example 1 — a first encounter with Tunstall coding

Start with the simplest possible case. Write down what Tunstall coding claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Tunstall coding before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Tunstall coding ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Tunstall coding

In research
Tunstall coding appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Tunstall coding in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Tunstall coding is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data compression, Lossless compression algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Tunstall coding outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Tunstall coding” →

How to study Tunstall coding in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Tunstall coding means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Tunstall coding out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Tunstall coding in simple terms?

In computer science and information theory, Tunstall coding is a form of entropy coding used for lossless data compression. History Tunstall coding was the subject of Brian Parker Tunstall's PhD thesis in 1967, while at Georgia Institute of Technology.

Why does Tunstall coding matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Tunstall coding?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Tunstall coding.

Tags

  • Data compression
  • Lossless compression algorithms

Keep exploring