epiphany A Typst package for explanations

This page is a document written with epiphany. One Typst source builds it and its PDF; every box, storyboard and figure below is a primitive of the package, and the figure in section 2 runs live.

Binary search, explained

A gallery of epiphany: every primitive of the package, on one running example

Download PDF

Binary search finds a value in a sorted array by asking one question per step and throwing away half of what is left with each answer. This document explains it the way the package is meant to be used: a picture of one example first, then the idea in words, then in symbols.
Figures, Tables, Algorithms

1 Looking something up

How many questions does it take to find one value among a thousand, or a million?
Ten numbers, sorted, and one of them is 23. Checking them from the left finds it on the sixth try, and a value at the end would take ten. The order is wasted: a reader of a dictionary never starts at “a”.
Ask about the middle instead. The probe holds 16, which is less than 23, and because the array is sorted, so is everything to its left. One answer rules out half the array.
Table 1: The notation of this document, in words and in symbols.
In wordsIn symbols
the sorted array, and its length𝑎0≤𝑎1≤…≤𝑎𝑛−1
the value we look for𝑡
the part still worth searching𝑎𝑙,…,𝑎ℎ
the probe in its middle𝑚=⌊(𝑙+ℎ)/2⌋
Definition 1 (search range). The search range is the index interval [𝑙,ℎ] that still may hold 𝑡; it is empty when 𝑙>ℎ.
Invariant 1 (the target stays in range). If 𝑡 occurs in 𝑎, then it occurs in 𝑎𝑙,…,𝑎ℎ.

2 The algorithm

What, exactly, happens on each step?
Algorithm 1: Binary search for 𝑡 in a sorted array 𝑎.
Require: a sorted array 𝑎0≤𝑎1≤…≤𝑎𝑛−1 and a target 𝑡
Ensure: an index 𝑖 with 𝑎𝑖=𝑡, or none
1𝑙←0 and ℎ←𝑛−1
2while 𝑙≤ℎ do
3𝑚←⌊(𝑙+ℎ)/2⌋the middle of what is left
4if 𝑎𝑚=𝑡 then return 𝑚
5else if 𝑎𝑚<𝑡 then 𝑙←𝑚+1drop the left half
6else ℎ←𝑚−1drop the right half
7return none
1Start with everythingAlgorithm 1
Require: a sorted array 𝑎0≤𝑎1≤…≤𝑎𝑛−1 and a target 𝑡
Ensure: an index 𝑖 with 𝑎𝑖=𝑡, or none
1𝑙←0 and ℎ←𝑛−1
2while 𝑙≤ℎ do
3𝑚←⌊(𝑙+ℎ)/2⌋
4if 𝑎𝑚=𝑡 then return 𝑚
5else if 𝑎𝑚<𝑡 then 𝑙←𝑚+1
6else ℎ←𝑚−1
7return none
Nothing is ruled out yet, so the range is the whole array: 𝑙=0 and ℎ=9.
2Probe the middleAlgorithm 1
⋮
1𝑙←0 and ℎ←𝑛−1
2while 𝑙≤ℎ do
3𝑚←⌊(𝑙+ℎ)/2⌋
4if 𝑎𝑚=𝑡 then return 𝑚
⋮
The middle of [0,9] is 𝑚=4, and 𝑎4=16.
3Keep the half that can hold itAlgorithm 1
⋮
4if 𝑎𝑚=𝑡 then return 𝑚
5else if 𝑎𝑚<𝑡 then 𝑙←𝑚+1
6else ℎ←𝑚−1
⋮
16<23, so the target can only be to the right: 𝑙←5. Half the array is gone.
4Repeat until it is foundAlgorithm 1
⋮
2while 𝑙≤ℎ do
3𝑚←⌊(𝑙+ℎ)/2⌋
4if 𝑎𝑚=𝑡 then return 𝑚
5else if 𝑎𝑚<𝑡 then 𝑙←𝑚+1
⋮
𝑎7=56 is too large, so ℎ←6; then 𝑚=5 and 𝑎5=23. Found, on the third probe.
range [0,9], probe 𝑎4=16
range [5,9], probe 𝑎7=56
range [5,6], probe 𝑎5=23
Figure 1: Every probe of the search for 23: the range shrinks to the one cell that holds it.
Figure 2: Pick a target and step through the search; the lines of Algorithm 1 that run light up.

3 Why it works, and how fast

Why can the search never skip over the target, and how many probes can it need?
Lemma 1 (the invariant holds). Each pass of Algorithm 1 keeps Invariant 1.
Proof. A pass that does not return moves 𝑙 past 𝑚 only when 𝑎𝑚<𝑡, and then every 𝑎𝑖 with 𝑖≤𝑚 is at most 𝑎𝑚<𝑡 because 𝑎 is sorted; the case 𝑎𝑚>𝑡 is the mirror image. ∎
Theorem 1 (binary search is correct and fast). Algorithm 1 returns an index of 𝑡 if 𝑡 occurs in 𝑎, and none otherwise, after at most ⌊log2𝑛⌋+1 probes.
Proof. By Lemma 1 the target is never dropped, so the search returns none only once the range is empty. Each pass leaves at most half of the range, so after 𝑘 passes at most 𝑛/2𝑘 indices remain, which is less than one once 𝑘>log2𝑛. ∎
Proposition 1 (the example). For the array of Figure 1, every target takes at most ⌊log210⌋+1=4 probes; 23 takes 3.
scanning: 6 probes
binary search: 3 probes
Figure 3: Scanning from the left against binary search, for the same target.

4 Variants

What if the target is missing, or appears more than once?
Exact match
Return any index of 𝑡, or none: Algorithm 1 as written.
Lower bound
The first index with 𝑎𝑖≥𝑡: where 𝑡 would be inserted.
Upper bound
The first index with 𝑎𝑖>𝑡; with the lower bound, it counts the copies of 𝑡.

A Implementation notes

Colophon

This document is written in Typst, the programmable markup language for typesetting that Laurenz Mädje and Martin Haug began at TU Berlin [2], [3]. Typst builds both of its versions from the same source: the PDF, and the web page through its HTML export, where the live figure is a script plugged into the page. Every picture is drawn with Typst’s own shapes, from the data the live figure starts from. The primitives come from epiphany, a Typst package under the MIT No Attribution license.

References