Kayavak / Information Theory / IT·02 Counting Possibilities: Bits, Logarithms and Questions / Deeper
Deeper · Information Theory
Counting Possibilities: Bits, Logarithms and Questions
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Counting arrangements is ancient. Indian scholars counted the possible metres of Sanskrit verse: Piṅgala's Chandaḥśāstra (around the last centuries BCE) enumerates patterns of short and long syllables, a binary system in all but name, and later commentators gave rules for the number of patterns of each length. Bhāskara II's Līlāvatī (1150) has rules for permutations and combinations, and in Europe Blaise Pascal's Traité du triangle arithmétique (written 1654) gathered the binomial coefficients into the triangle that bears his name. Abraham de Moivre and James Stirling (1730) found the approximation to that makes large counts usable.
The step from counting to coding came with machines. Morse's variable-length code suited human ears, but a machine reading pulses in step needs every character to take the same time. Émile Baudot's five-unit code (1870s), Donald Murray's revision (1901) and the international telegraph alphabet ITA2 (standardised in the 1920s and 1930s) fixed the pattern: count the characters, find the power of two that covers them. ASCII (1963) and the 8-bit byte followed. Meanwhile, the question-tree view of information gave algorithm designers their most famous lower bound: sorting by comparisons needs about of them, a result in the textbooks by the 1960s and a standard example of how information limits computation.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
Count the possibilities; of the count is the information when all are equally likely; a code spends whole binary digits, so it rounds up, and rounding once per block of symbols wastes less than rounding once per symbol.
| Question | Answer |
|---|---|
| What is it? | Counting (sequences, orders, choices) turned into bits, and the cost of fixed-length codes |
| Problem it solves | How many binary digits a message needs, and the least work any question strategy must do |
| Parts | , , ; ; blocks; binary trees |
| How it works | Logarithms turn products into sums; trees of depth have at most leaves |
| Assumptions | Every possibility allowed and equally likely; questions have two answers |
| When they change | Unequal probabilities (ch04) beat on average; many-answer questions beat comparisons |
| Where it fails | Real data is rarely uniform, so counting overstates its information |
| Related ideas | Logarithms (Mathematics Book 1, ch10), algorithms (Software Engineering, Volume 1, ch04), entropy (ch04) |
What if?
What if questions had three answers?
Reason it through, then open
A balance scale answers "left heavier", "right heavier" or "balanced", so each weighing can split the possibilities three ways and give up to bits. A tree with three branches at every node has leaves at depth , so possibilities need weighings: the twelve-coin puzzle of chapter 1 needs 3, since . Computers built on three-valued "trits" exist (the Soviet Setun, 1958), and base 3 is in a narrow sense the most economical base, but two-state hardware is far easier to build reliably, so binary won.
What if a shuffle used a 64-bit seed?
Reason it through, then open
It could produce at most different orders out of : about one in . Every game would still look random, but an attacker who sees a few cards could search the seeds, since is within reach of large computing efforts and the first few cards narrow it fast. The safe design draws the shuffle from a cryptographic generator whose state is far bigger than 226 bits, or draws fresh randomness for each card.
What if sorting were allowed to compare three items at once?
Reason it through, then open
A "which of these three is largest, middle and smallest?" question has 6 answers, worth up to bits. The bits still have to be gathered, so the number of such questions is at least : fewer questions, but each costs more. The total information is fixed by the problem; the hardware decides how much of it each step can collect. That's why sorting networks, GPUs and databases think about "how many bits per operation", not just "how many operations".
Where the boundary is
| Aspect | Counting possibilities () | Entropy (chapter 4) |
|---|---|---|
| Assumes | Every possibility equally likely | Possibilities have given probabilities |
| Measures | The most information a message from the set can carry | The average information it actually carries |
| For English letters | bits | About 4.19 bits (chapter 1's lab), less with context |
| Best code | Fixed-length, in blocks | Variable-length: short codes for likely symbols |
| Lower bound it gives | No code beats for every sequence | No code beats the entropy on average |
| When they agree | Uniform probabilities | Uniform probabilities |
Derive it
Why is about , without Stirling's formula.
- , because the logarithm of a product is the sum of the logarithms.
- The sum is close to the area under the curve from 1 to : each term is the area of a strip of width 1 and height , and the curve rises slowly.
- That area is .
- Since , this is .
- The strips overshoot the curve a little (by less than in total), which is the correction in Stirling's formula. For : , against the exact 118,458 in the chapter's lab.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| symbols from give sequences | Theorem | The multiplication principle |
| Any comparison sort needs comparisons in the worst case | Theorem | The decision-tree argument |
| The bound isn't always achievable: 12 items need 30, not 29 | Theorem | Mark Wells (1965), by exhaustive computer search; Knuth, volume 3 |
| Python's sort is within 2 % of the bound on shuffled data | Measured | The chapter's lab, up to 10,000 items |
| Blocks of symbols cost under per symbol | Theorem | Rounding up once per block |
| A byte is 8 bits | Convention | IBM System/360 (1964); earlier machines used 6- and 9-bit bytes |
| Baudot designed his code in the 1870s | Historical | His French patent of 1874 |
Still open
The exact minimum number of comparisons needed to sort items is known only for small : it has been settled for every into the low twenties by long computer searches, and beyond that only bounds are known. The information bound and the best known methods (Ford–Johnson merge insertion and its refinements) differ by a small amount that nobody has closed in general.
What would change your mind? The chapter says no comparison sort can beat in the worst case. A method that sorted every input of size in fewer comparisons would have to send two different orders to the same leaf of its tree, and so leave one of them unsorted; the claim can't fail for comparison sorts. It does fail as soon as the questions change: counting and radix sorts, or sorts that exploit known structure in the input.
Recall
How many sequences of n symbols from an alphabet of k?
k^n, carrying n log₂ k bits.
How many binary digits does a fixed-length code need for K symbols?
⌈log₂ K⌉.
How does coding in blocks help?
It rounds up once per block, so the cost per symbol falls below log₂ K + 1/m for blocks of m.
Why can no comparison sort beat log₂ n! comparisons in the worst case?
Its comparisons form a binary tree that needs n! leaves, so its depth is at least log₂ n!.
About how many bits is a shuffled 52-card deck?
About 225.6 (log₂ 52!).
What does Stirling's approximation give for log₂ n!?
About n log₂ n − 1.44 n.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.