Skip to content

Solve set: trees, birthday bounds, Jaccard/MinHash/LSH S-curve, Bloom FP rate, recurrences

ModuleS-M06b · solve · none · Pass 3 · 4 to 5 h
You buildanswers in solve/S-M06b.toml (27 checked by SymPy) and 3 proofs in solve/S-M06b/qN.md (self-graded against their rubrics)
Contractnone: a pen and paper set
Testscourse/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
Needsreading: S-M06a (graphs, hashing, expected collisions), M06.2 (tries), and the Discrete Math 2 topic
Used byno 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)
MilestoneMS-P3 (the Pass 3 gate runs ol check on every solve part of the pass)
Optional depthLeskovec, 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)
  • A tree on nn nodes has n−1n - 1 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 m\sqrt{m} keys, not mm: 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 b×rb \times r MinHash rows turns that into an S-shaped candidate probability with its threshold near (1/b)1/r(1/b)^{1/r} (q14, q16 to q18).
  • A Bloom filter with m/nm/n bits per key is best with k=(m/n)ln⁡2k = (m/n) \ln 2 hashes, which sets half its bits and gives a false-positive rate of 2−k2^{-k} (q20 to q22).
  • Divide-and-conquer costs follow from their recurrences: 2T(n/2)+n2T(n/2) + n is nlog⁡2nn \log_2 n, and 8 half-size products cost n3n^3 (q23, q28, q29).
Terminal window
ol start S-M06b # writes solve/S-M06b.toml and one file per proof
ol 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 proof

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.

SymbolMeaningType / shape
nnthe number of nodes of a tree, of keys in a filter, or the size of a recurrence’s inputint
∣A∣\lvert A \rvertthe number of elements of a finite set AAint
mmthe number of buckets of a hash, or of bits of a Bloom filterint
kkthe number of keys hashed (birthday), or of hash functions per key (Bloom)int
J(A,B)=∣A∩B∣/∣A∪B∣J(A, B) = \lvert A \cap B \rvert / \lvert A \cup B \rvertthe Jaccard similarity of two non-empty setsfloat in [0,1][0, 1]
π\pia uniformly random permutation of the universe UUpermutation
bb, rrthe LSH bands and rows per band, brb r = signature lengthint
ssthe probability that one MinHash row agrees (the pair’s Jaccard similarity)float
T(n)T(n)the cost of a recursive algorithm on input size nnfunction
A(x)=∑n≥0anxnA(x) = \sum_{n \ge 0} a_n x^nthe ordinary generating function of a sequence a0,a1,…a_0, a_1, \dotsformal 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 n−1n - 1 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, 2⋅internal=n−12 \cdot \text{internal} = n - 1 with n=internal+leavesn = \text{internal} + \text{leaves}, gives internal=leaves−1\text{internal} = \text{leaves} - 1. A complete binary tree of nn nodes is the shape of a binary heap (node ii has children 2i2i and 2i+12i + 1), and its height is ⌊log⁡2n⌋\lfloor \log_2 n \rfloor. Cayley’s formula counts the trees on nn labelled nodes: nn−2n^{n-2}.

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:

∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣.\lvert A \cup B \cup C \rvert = \lvert A \rvert + \lvert B \rvert + \lvert C \rvert - \lvert A \cap B \rvert - \lvert A \cap C \rvert - \lvert B \cap C \rvert + \lvert A \cap B \cap C \rvert.

The same alternating sum over the events “element ii is fixed” counts derangements.

Birthday bounds. Hash kk keys uniformly into mm buckets. The keys are all in different buckets with probability ∏i=0k−1(1−i/m)\prod_{i=0}^{k-1}(1 - i/m): the (i+1)(i+1)-th key must avoid the ii occupied buckets. Using 1−x≤e−x1 - x \le e^{-x}, this is at most e−k(k−1)/(2m)≈e−k2/(2m)e^{-k(k-1)/(2m)} \approx e^{-k^2/(2m)}. Each of the (k2)\binom{k}{2} pairs collides with probability 1/m1/m, so by linearity of expectation the expected number of colliding pairs is (k2)/m\binom{k}{2}/m, 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 k≈mk \approx \sqrt{m}.

Jaccard and MinHash. Two documents are sets of shingles (overlapping substrings); their similarity is JJ. Apply a random permutation π\pi to the universe and keep the minimum of each set. Among A∪BA \cup B, the element with the smallest π\pi value is equally likely to be any of the ∣A∪B∣\lvert A \cup B \rvert elements; the two minima agree exactly when it lies in A∩BA \cap B. So P(min⁡π(A)=min⁡π(B))=J(A,B)P(\min \pi(A) = \min \pi(B)) = J(A, B) (q14). With NN independent permutations, the fraction that agree is an unbiased estimate of JJ with variance J(1−J)/NJ(1 - J)/N (a mean of Bernoulli variables). data.04 stands in for permutations with seeded hash functions.

LSH banding. Comparing every pair of 10610^6 documents is 5×10115 \times 10^{11} comparisons. Instead, split each signature into bb bands of rr rows and hash each band; two documents become candidates when they agree on every row of some band. With per-row agreement ss: one band agrees with probability srs^r, it fails with 1−sr1 - s^r, all bb fail with (1−sr)b(1 - s^r)^b, so

P(candidate)=1−(1−sr)b.P(\text{candidate}) = 1 - (1 - s^r)^b.

As a function of ss this is an S-curve: near 0 for dissimilar pairs, near 1 for similar ones, rising most steeply around t≈(1/b)1/rt \approx (1/b)^{1/r}. More rows per band push the threshold up and sharpen the curve; more bands pull it down.

Bloom filters. A Bloom filter is mm bits, all 0. Inserting a key sets the kk bits its kk hashes point to. A lookup reports “present” when all kk 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 nn insertions one bit is still 0 with probability (1−1/m)kn≈e−kn/m(1 - 1/m)^{kn} \approx e^{-kn/m}. Treating the kk probed bits as independent (an approximation, accurate for large mm), the false-positive rate is

f(k)=(1−e−kn/m)k.f(k) = \big(1 - e^{-kn/m}\big)^k.

Write p=e−kn/mp = e^{-kn/m} for the fraction of zero bits, so k=−(m/n)ln⁡pk = -(m/n) \ln p and ln⁡f=−(m/n)ln⁡p ln⁡(1−p)\ln f = -(m/n) \ln p \, \ln(1 - p). That is symmetric under p↔1−pp \leftrightarrow 1 - p and is smallest at p=1/2p = 1/2: the best filter has half its bits set, k=(m/n)ln⁡2k = (m/n) \ln 2, and f=2−kf = 2^{-k}. Solving 2−k=ptarget2^{-k} = p_{\text{target}} for the bits per key gives m/n=−ln⁡ptarget/(ln⁡2)2≈1.44log⁡2(1/ptarget)m/n = -\ln p_{\text{target}} / (\ln 2)^2 \approx 1.44 \log_2(1/p_{\text{target}}).

Recurrences. A recurrence defines a sequence by earlier terms. Three ways to solve one:

  1. Unroll and sum. T(n)=T(n−1)+nT(n) = T(n-1) + n unrolls to 1+2+⋯+n1 + 2 + \dots + n. For T(n)=2T(n/2)+nT(n) = 2T(n/2) + n each of the log⁡2n\log_2 n levels of the recursion costs nn in total.
  2. Characteristic equation. For an=c1an−1+c2an−2a_n = c_1 a_{n-1} + c_2 a_{n-2} try an=λna_n = \lambda^n: then λ2=c1λ+c2\lambda^2 = c_1 \lambda + c_2. With distinct roots λ1,λ2\lambda_1, \lambda_2 the general solution is αλ1n+βλ2n\alpha \lambda_1^n + \beta \lambda_2^n, and the two initial values fix α\alpha and β\beta.
  3. Generating functions. Multiply the recurrence by xnx^n and sum, and it becomes an equation for A(x)A(x). The basic pair is ∑nxn=1/(1−x)\sum_n x^n = 1/(1 - x), and differentiating it gives ∑n(n+1)xn=1/(1−x)2\sum_n (n + 1) x^n = 1/(1-x)^2.

For divide-and-conquer costs T(n)=a T(n/b)+ndT(n) = a\,T(n/b) + n^d, compare log⁡ba\log_b a with dd: the leaves dominate when log⁡ba>d\log_b a > d, giving T(n)=Θ(nlog⁡ba)T(n) = \Theta(n^{\log_b a}), which is why 8 half-size matrix products cost n3n^3 and Strassen’s 7 cost nlog⁡27n^{\log_2 7}.

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 k∗=(m/n)ln⁡2=8×0.6931=5.545k^* = (m/n) \ln 2 = 8 \times 0.6931 = 5.545. A filter needs an integer, so compare the neighbours with f(k)=(1−e−k/8)kf(k) = (1 - e^{-k/8})^k:

kke−k/8e^{-k/8}1−e−k/81 - e^{-k/8}f(k)f(k)
50.53530.46470.02168
60.47240.52760.02158

k=6k = 6 is (barely) better, giving about 2.16% false positives. At the real-valued optimum the rate would be 2−5.545=0.02142^{-5.545} = 0.0214. For 1% you would need m/n=ln⁡100/(ln⁡2)2=9.59m/n = \ln 100 / (\ln 2)^2 = 9.59 bits per key.

Read an LSH S-curve with 20 bands of 5 rows. The threshold is (1/20)1/5=0.549(1/20)^{1/5} = 0.549. At s=0.8s = 0.8: s5=0.32768s^5 = 0.32768, a band fails with 0.672320.67232, all 20 fail with 0.6723220=0.0003560.67232^{20} = 0.000356, so the pair becomes a candidate with probability 0.999640.99964. At s=0.3s = 0.3: s5=0.00243s^5 = 0.00243, (1−0.00243)20=0.9525(1 - 0.00243)^{20} = 0.9525, so only 0.04750.0475. Similar pairs are almost always compared and dissimilar ones rarely.

A model proof. Claim: a full binary tree with L≥1L \ge 1 leaves has L−1L - 1 internal nodes. Method: strong induction on LL. Base: L=1L = 1 is a single node, which is a leaf, with 0 internal nodes. Step: let L≥2L \ge 2 and assume the claim for every full binary tree with fewer than LL leaves. The root is internal (a leaf root would mean L=1L = 1), so it has two subtrees, each a full binary tree, with L1≥1L_1 \ge 1 and L2≥1L_2 \ge 1 leaves, L1+L2=LL_1 + L_2 = L, so each has fewer than LL. By the hypothesis they have L1−1L_1 - 1 and L2−1L_2 - 1 internal nodes. Adding the root: (L1−1)+(L2−1)+1=L−1(L_1 - 1) + (L_2 - 1) + 1 = L - 1. So every full binary tree with LL leaves has L−1L - 1 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.

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 {1,2,3,4,5}\{1, 2, 3, 4, 5\}? (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 n=100n = 100 nodes. What is its height, the number of edges on its longest root-to-leaf path? [number]

q6. Prove that every tree with n≥1n \ge 1 nodes has exactly n−1n - 1 edges. [proof]

q7. Three sets have ∣A∣=∣B∣=∣C∣=10\lvert A \rvert = \lvert B \rvert = \lvert C \rvert = 10, every pairwise intersection has 3 elements, and ∣A∩B∩C∣=1\lvert A \cap B \cap C \rvert = 1. Give ∣A∪B∪C∣\lvert A \cup B \cup C \rvert. [number]

q8. How many integers in {1,2,…,100}\{1, 2, \dots, 100\} are divisible by 2, 3, or 5? [number]

q9. A derangement of {1,2,3,4}\{1, 2, 3, 4\} is a permutation that moves every element. How many are there? [number]

q10. Three keys are hashed independently and uniformly into m=10m = 10 buckets. Give the exact probability that all three land in different buckets. [number] (exact)

q11. kk keys are hashed independently and uniformly into mm buckets. Give the expected number of unordered pairs of keys that share a bucket, as a formula in kk and mm. [expr] (variables k, m)

q12. Using P(no collision among k keys)≈e−k2/(2m)P(\text{no collision among } k \text{ keys}) \approx e^{-k^2/(2m)}, find the kk at which a collision becomes as likely as not, for a 32-bit hash (m=232m = 2^{32}). [number] (to 3 significant digits)

q13. Give the Jaccard similarity J(A,B)=∣A∩B∣/∣A∪B∣J(A, B) = \lvert A \cap B \rvert / \lvert A \cup B \rvert of the shingle sets A={ab,bc,cd,de}A = \{ab, bc, cd, de\} and B={bc,cd,de,ef,fg}B = \{bc, cd, de, ef, fg\}. [number] (exact)

q14. Let AA and BB be non-empty subsets of a finite universe UU and π\pi a uniformly random permutation of UU. Prove that P(min⁡π(A)=min⁡π(B))=J(A,B)P(\min \pi(A) = \min \pi(B)) = J(A, B). (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 JJ by the fraction of the 128 positions that agree. Each position agrees with probability JJ, independently. Give the variance of the estimate when J=1/2J = 1/2. [number] (exact)

q16. LSH splits a signature into bb bands of rr rows; two documents become a candidate pair when all rr rows of at least one band agree. Each row agrees with probability ss (their Jaccard similarity), independently. Give the probability that they become candidates for b=16b = 16, r=8r = 8 (the data.04 defaults), as a formula in ss. [expr] (variable s)

q17. The S-curve of q16 rises most steeply near the threshold t≈(1/b)1/rt \approx (1/b)^{1/r}. Give tt for b=16b = 16, r=8r = 8. [number] (to 3 significant digits)

q18. With b=16b = 16, r=8r = 8, give the probability that two documents with s=1/2s = 1/2 become candidates. [number] (to 6 significant digits)

A Bloom filter has mm bits, holds nn keys, and sets kk bits per key with independent uniform hashes. A lookup of a key that was never inserted is a false positive when all kk of its bits are set.

q19. Give the standard approximation of the false-positive rate, using (1−1/m)kn≈e−kn/m(1 - 1/m)^{kn} \approx e^{-kn/m}, as a formula in kk, nn, and mm. [expr] (variables k, n, m)

q20. The rate of q19 is smallest at k=(m/n)ln⁡2k = (m/n) \ln 2. Give this optimal kk for 10 bits per key (m/n=10m/n = 10), before rounding to an integer. [number] (to 6 significant digits)

q21. At the optimal kk of q20 every bit is set with probability 1/21/2. Give the false-positive rate at that kk. [number] (to 6 significant digits)

q22. With the optimal kk, the bits per key needed for a target false-positive rate pp are m/n=−ln⁡p/(ln⁡2)2m/n = -\ln p / (\ln 2)^2. Give m/nm/n for p=0.01p = 0.01. [number] (to 6 significant digits)

q23. Solve T(n)=2 T(n/2)+nT(n) = 2\,T(n/2) + n with T(1)=0T(1) = 0 for nn a power of 2. [expr] (variable n)

q24. Solve T(n)=T(n−1)+nT(n) = T(n - 1) + n with T(0)=0T(0) = 0. [expr] (variable n)

q25. Solve an=3an−1−2an−2a_n = 3a_{n-1} - 2a_{n-2} with a0=0a_0 = 0 and a1=1a_1 = 1. [expr] (variable n)

q26. Give the ordinary generating function A(x)=∑n≥0anxnA(x) = \sum_{n \ge 0} a_n x^n of the sequence an=n+1a_n = n + 1 (that is, 1,2,3,…1, 2, 3, \dots) 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 n×nn \times n matrix into four n/2×n/2n/2 \times n/2 blocks. (a) The plain method does 8 half-size products and Θ(n2)\Theta(n^2) additions: T(n)=8 T(n/2)+n2T(n) = 8\,T(n/2) + n^2. Give the exponent cc with T(n)=Θ(nc)T(n) = \Theta(n^c). (b) Strassen’s method does 7: T(n)=7 T(n/2)+n2T(n) = 7\,T(n/2) + n^2. Give cc. [number]

q29. Prove by induction that T(n)=2 T(n/2)+nT(n) = 2\,T(n/2) + n with T(1)=0T(1) = 0 gives T(n)=nlog⁡2nT(n) = n \log_2 n for every nn that is a power of 2. [proof]

PitfallSymptomCaught by
Counting a trie as one node per charactersizes a node arena for the worst case, ignoring shared prefixesq2 (canary 18)
Forgetting to add back the triple overlapa union undercounted by every element in all three setsq7 (canary 21), q8 (canary 71)
Reading the union bound as the probability“1 - 3/10” instead of the exact productq10 (canary 7/10), q18 (canary 16/256)
Expecting collisions only near a full tablea 32-bit-keyed cache that collides after 77 000 entries, not 4 billionq12 (canaries 2^16 and 2^31)
Dividing the overlap by one set’s sizea similarity that is not symmetric and overstates small documentsq13 (canaries 3/5 and 3/4)
Swapping bands and rows, or AND and ORthe S-curve’s threshold lands far from the intended similarityq16 (canaries)
One hash per Bloom key, or a single probea false-positive rate formula off by orders of magnitudeq19 (canaries)
Confusing kk with bits per keya filter sized at 6.6 bits per key that misses its 1% targetq22 (canary log(100)/log(2))
Natural log where log⁡2\log_2 is meantcosts off by a factor of ln⁡2\ln 2q23 (canary n*log(n))
DirectionModuleHow it uses this
BackS-M06aexpected colliding pairs (its q17) and modular hashing, extended here to birthday bounds
BackM06.2the trie whose node count q2 predicts
Forwardds.08Bloom::with_rate(n, p) computes mm and kk with q20 to q22
Forwarddata.04MinHash signatures of 128 values in 16 bands of 8 rows: q15 to q18
ForwardL9.1tiled matmul’s cost and the roofline use the recurrence of q28