Solve set: trees, birthday bounds, Jaccard/MinHash/LSH S-curve, Bloom FP rate, recurrences
Overview
Section titled “Overview”| Module | S-M06b · solve · none · Pass 3 · 4 to 5 h |
| You build | answers in solve/S-M06b.toml (27 checked by SymPy) and 3 proofs in solve/S-M06b/qN.md (self-graded against their rubrics) |
| Contract | none: a pen and paper set |
| Tests | course/solve/S-M06b/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M06b/problems.md and in section 4 |
| Needs | reading: S-M06a (graphs, hashing, expected collisions), M06.2 (tries), and the Discrete Math 2 topic |
| Used by | no call site (a solve set). Read before ds.08 (sizing a Bloom filter: q19 to q22), data.04 (MinHash and LSH bands: q13 to q18), and L9.1 (the cost of tiled matmul: q28) |
| Milestone | MS-P3 (the Pass 3 gate runs ol check on every solve part of the pass) |
| Optional depth | Leskovec, Rajaraman, and Ullman, Mining of Massive Datasets, ch. 3 (MinHash, LSH); Broder and Mitzenmacher, “Network Applications of Bloom Filters: A Survey” (2004); Graham, Knuth, and Patashnik, Concrete Mathematics, ch. 7 (generating functions) |
Key Takeaways
Section titled “Key Takeaways”- A tree on nodes has edges, and a trie stores each shared prefix once, so its size is one more than the number of distinct prefixes (q1, q2, q6).
- Collisions arrive at about keys, not : a 32-bit hash is likely to collide after about 77 000 keys (q11, q12).
- One random permutation’s minimum agrees on two sets with probability exactly their Jaccard similarity, and banding MinHash rows turns that into an S-shaped candidate probability with its threshold near (q14, q16 to q18).
- A Bloom filter with bits per key is best with hashes, which sets half its bits and gives a false-positive rate of (q20 to q22).
- Divide-and-conquer costs follow from their recurrences: is , and 8 half-size products cost (q23, q28, q29).
How to work this chapter
Section titled “How to work this chapter”ol start S-M06b # writes solve/S-M06b.toml and one file per proofol check S-M06b # SymPy checks the answers, then asks each proof rubric (y/n)ol check S-M06b --regrade # ask the rubrics again after you change a proof1. Why now
Section titled “1. Why now”Pass 3 builds the data structures your corpus pipeline and tokenizers stand on, and every one of them has a number you must choose before it runs. The trie (M06.2) is a tree whose size you should be able to predict. The Bloom filter (ds.08) that data.03 uses to drop exact duplicates needs a bit budget and a hash count; pick them by feel and the filter is ten times leakier than it needs to be, silently. The near-duplicate stage (data.04) splits 128 MinHash values into 16 bands of 8 rows, which decides at what similarity two documents get compared at all; the wrong split either compares everything or misses the near-copies. And the hash tables keyed by 32- or 64-bit hashes need to know when collisions start. None of these mistakes raises an error. This set gives you the formulas, derived, so the parameters in your code are calculations.
2. Principles
Section titled “2. Principles”| Symbol | Meaning | Type / shape |
|---|---|---|
| the number of nodes of a tree, of keys in a filter, or the size of a recurrence’s input | int | |
| the number of elements of a finite set | int | |
| the number of buckets of a hash, or of bits of a Bloom filter | int | |
| the number of keys hashed (birthday), or of hash functions per key (Bloom) | int | |
| the Jaccard similarity of two non-empty sets | float in | |
| a uniformly random permutation of the universe | permutation | |
| , | the LSH bands and rows per band, = signature length | int |
| the probability that one MinHash row agrees (the pair’s Jaccard similarity) | float | |
| the cost of a recursive algorithm on input size | function | |
| the ordinary generating function of a sequence | formal power series |
Trees. A tree is a connected graph with no cycle. Between two of its nodes there is exactly one path; it has exactly edges (q6 asks for the proof); a tree with at least two nodes has at least two leaves. Rooting it makes every non-root node the child of exactly one parent. In a full binary tree every internal node has two children, so counting edges two ways, with , gives . A complete binary tree of nodes is the shape of a binary heap (node has children and ), and its height is . Cayley’s formula counts the trees on labelled nodes: .
Inclusion-exclusion. To count a union, add the sizes, subtract every pairwise overlap (counted twice), add back every triple overlap (subtracted once too often), and so on:
The same alternating sum over the events “element is fixed” counts derangements.
Birthday bounds. Hash keys uniformly into buckets. The keys are all in different buckets with probability : the -th key must avoid the occupied buckets. Using , this is at most . Each of the pairs collides with probability , so by linearity of expectation the expected number of colliding pairs is , and by the union bound that is also an upper bound on the probability of any collision. All three say the same thing: collisions start at .
Jaccard and MinHash. Two documents are sets of shingles (overlapping substrings); their similarity is . Apply a random permutation to the universe and keep the minimum of each set. Among , the element with the smallest value is equally likely to be any of the elements; the two minima agree exactly when it lies in . So (q14). With independent permutations, the fraction that agree is an unbiased estimate of with variance (a mean of Bernoulli variables). data.04 stands in for permutations with seeded hash functions.
LSH banding. Comparing every pair of documents is comparisons. Instead, split each signature into bands of rows and hash each band; two documents become candidates when they agree on every row of some band. With per-row agreement : one band agrees with probability , it fails with , all fail with , so
As a function of this is an S-curve: near 0 for dissimilar pairs, near 1 for similar ones, rising most steeply around . More rows per band push the threshold up and sharpen the curve; more bands pull it down.
Bloom filters. A Bloom filter is bits, all 0. Inserting a key sets the bits its hashes point to. A lookup reports “present” when all of its bits are set, so it never misses an inserted key, and it reports a false positive when an absent key finds all its bits set by others. After insertions one bit is still 0 with probability . Treating the probed bits as independent (an approximation, accurate for large ), the false-positive rate is
Write for the fraction of zero bits, so and . That is symmetric under and is smallest at : the best filter has half its bits set, , and . Solving for the bits per key gives .
Recurrences. A recurrence defines a sequence by earlier terms. Three ways to solve one:
- Unroll and sum. unrolls to . For each of the levels of the recursion costs in total.
- Characteristic equation. For try : then . With distinct roots the general solution is , and the two initial values fix and .
- Generating functions. Multiply the recurrence by and sum, and it becomes an equation for . The basic pair is , and differentiating it gives .
For divide-and-conquer costs , compare with : the leaves dominate when , giving , which is why 8 half-size matrix products cost and Strassen’s 7 cost .
3. Worked example by hand
Section titled “3. Worked example by hand”These are siblings of the graded problems, not answers to them.
Size a Bloom filter at 8 bits per key. The optimal number of hashes is . A filter needs an integer, so compare the neighbours with :
| 5 | 0.5353 | 0.4647 | 0.02168 |
| 6 | 0.4724 | 0.5276 | 0.02158 |
is (barely) better, giving about 2.16% false positives. At the real-valued optimum the rate would be . For 1% you would need bits per key.
Read an LSH S-curve with 20 bands of 5 rows. The threshold is . At : , a band fails with , all 20 fail with , so the pair becomes a candidate with probability . At : , , so only . Similar pairs are almost always compared and dissimilar ones rarely.
A model proof. Claim: a full binary tree with leaves has internal nodes. Method: strong induction on . Base: is a single node, which is a leaf, with 0 internal nodes. Step: let and assume the claim for every full binary tree with fewer than leaves. The root is internal (a leaf root would mean ), so it has two subtrees, each a full binary tree, with and leaves, , so each has fewer than . By the hypothesis they have and internal nodes. Adding the root: . So every full binary tree with leaves has internal nodes. Every symbol is defined, the hypothesis is used for the subtrees and not for the tree itself, and the last line restates the claim: that is what the rubrics of q6 and q29 ask for.
4. The problem set
Section titled “4. The problem set”Write each answer in solve/S-M06b.toml; lettered parts are their own tables:
[q11]answer = "k*(k-1)/(2*m)"[q28.a]answer = "3"q1. A tree has 12 nodes. How many edges does it have? [number]
q2. Build the trie (M06.2) of the five keys car, cart, care, cat, dog. How many nodes does it have, counting the root? [number]
q3. A full binary tree is a rooted tree in which every node has either 0 or 2 children. One has 10 leaves. How many internal (non-leaf) nodes does it have? [number]
q4. How many different trees are there on the 5 labelled nodes ? (Two trees differ when some pair of nodes is joined in one and not in the other.) [number]
q5. A complete binary tree fills every level except possibly the last, and fills the last level from the left. One has nodes. What is its height, the number of edges on its longest root-to-leaf path? [number]
q6. Prove that every tree with nodes has exactly edges. [proof]
Inclusion-exclusion and birthday bounds
Section titled “Inclusion-exclusion and birthday bounds”q7. Three sets have , every pairwise intersection has 3 elements, and . Give . [number]
q8. How many integers in are divisible by 2, 3, or 5? [number]
q9. A derangement of is a permutation that moves every element. How many are there? [number]
q10. Three keys are hashed independently and uniformly into buckets. Give the exact probability that all three land in different buckets. [number] (exact)
q11. keys are hashed independently and uniformly into buckets. Give the expected number of unordered pairs of keys that share a bucket, as a formula in and . [expr] (variables k, m)
q12. Using , find the at which a collision becomes as likely as not, for a 32-bit hash (). [number] (to 3 significant digits)
Jaccard, MinHash, and LSH
Section titled “Jaccard, MinHash, and LSH”q13. Give the Jaccard similarity of the shingle sets and . [number] (exact)
q14. Let and be non-empty subsets of a finite universe and a uniformly random permutation of . Prove that . (This is why one MinHash value per permutation is an unbiased estimate of Jaccard similarity.) [proof]
q15. A MinHash signature with 128 independent permutations estimates by the fraction of the 128 positions that agree. Each position agrees with probability , independently. Give the variance of the estimate when . [number] (exact)
q16. LSH splits a signature into bands of rows; two documents become a candidate pair when all rows of at least one band agree. Each row agrees with probability (their Jaccard similarity), independently. Give the probability that they become candidates for , (the data.04 defaults), as a formula in . [expr] (variable s)
q17. The S-curve of q16 rises most steeply near the threshold . Give for , . [number] (to 3 significant digits)
q18. With , , give the probability that two documents with become candidates. [number] (to 6 significant digits)
Bloom filters
Section titled “Bloom filters”A Bloom filter has bits, holds keys, and sets bits per key with independent uniform hashes. A lookup of a key that was never inserted is a false positive when all of its bits are set.
q19. Give the standard approximation of the false-positive rate, using , as a formula in , , and . [expr] (variables k, n, m)
q20. The rate of q19 is smallest at . Give this optimal for 10 bits per key (), before rounding to an integer. [number] (to 6 significant digits)
q21. At the optimal of q20 every bit is set with probability . Give the false-positive rate at that . [number] (to 6 significant digits)
q22. With the optimal , the bits per key needed for a target false-positive rate are . Give for . [number] (to 6 significant digits)
Recurrences and generating functions
Section titled “Recurrences and generating functions”q23. Solve with for a power of 2. [expr] (variable n)
q24. Solve with . [expr] (variable n)
q25. Solve with and . [expr] (variable n)
q26. Give the ordinary generating function of the sequence (that is, ) in closed form. [expr] (variable x)
q27. How many binary strings of length 10 contain no two consecutive 1s? [number]
q28. Recursive matrix multiplication splits each matrix into four blocks. (a) The plain method does 8 half-size products and additions: . Give the exponent with . (b) Strassen’s method does 7: . Give . [number]
q29. Prove by induction that with gives for every that is a power of 2. [proof]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Counting a trie as one node per character | sizes a node arena for the worst case, ignoring shared prefixes | q2 (canary 18) |
| Forgetting to add back the triple overlap | a union undercounted by every element in all three sets | q7 (canary 21), q8 (canary 71) |
| Reading the union bound as the probability | “1 - 3/10” instead of the exact product | q10 (canary 7/10), q18 (canary 16/256) |
| Expecting collisions only near a full table | a 32-bit-keyed cache that collides after 77 000 entries, not 4 billion | q12 (canaries 2^16 and 2^31) |
| Dividing the overlap by one set’s size | a similarity that is not symmetric and overstates small documents | q13 (canaries 3/5 and 3/4) |
| Swapping bands and rows, or AND and OR | the S-curve’s threshold lands far from the intended similarity | q16 (canaries) |
| One hash per Bloom key, or a single probe | a false-positive rate formula off by orders of magnitude | q19 (canaries) |
| Confusing with bits per key | a filter sized at 6.6 bits per key that misses its 1% target | q22 (canary log(100)/log(2)) |
| Natural log where is meant | costs off by a factor of | q23 (canary n*log(n)) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Back | S-M06a | expected colliding pairs (its q17) and modular hashing, extended here to birthday bounds |
| Back | M06.2 | the trie whose node count q2 predicts |
| Forward | ds.08 | Bloom::with_rate(n, p) computes and with q20 to q22 |
| Forward | data.04 | MinHash signatures of 128 values in 16 bands of 8 rows: q15 to q18 |
| Forward | L9.1 | tiled matmul’s cost and the roofline use the recurrence of q28 |