MinHash / LSH

Estimates the Jaccard similarity of two sets using only k small "signature" numbers per set, never the full sets themselves. Locality-sensitive hashing (LSH) builds on this to find similar items in huge collections without comparing every pair.

How it works

For each of k independent hash functions, a MinHash signature keeps the minimum hash value seen across every element added to the set. A classic result (Broder, 1997) shows that, under a random permutation of all possible elements, the minimum element of A ∪ B falls in A ∩ B with probability exactly |A∩B| / |A∪B|: the Jaccard similarity. So P(minhash_i(A) == minhash_i(B)) = Jaccard(A, B) for each hash function, and averaging agreement across k independent hashes gives an unbiased estimate with standard error √(J(1−J)/k).

This is what makes MinHash useful at scale: comparing two 32-number signatures is far cheaper than comparing two full sets, and you never need to bring both original sets into memory at once. LSH takes this further by splitting each signature into bands and bucketing items so that only likely-similar pairs are ever compared directly, turning an O(n²) all-pairs comparison into something close to linear.

Interactive demo

|A|, |B|5, 5
Exact Jaccard0.429
MinHash estimate0.563
Abs. error0.134

Signatures (first 24 of 32 shown)

053771511580233199599224605238219757241002582936504027211045932801315999
053355511580233199599224686238219876241002134936184395211421443338315897

Highlighted cells are positions where A and B's signatures agree: the fraction of agreement across all k positions is the similarity estimate above.

No operations yet. Try adding an item above.

API surface

OperationDescriptionComplexity
new MinHash(k)Allocate a length-k signature, initialized to +Infinity.O(k) space
add(item): voidUpdate each of the k signature slots with min(current, hash_i(item)).O(k) time
similarity(other): numberFraction of signature positions that match: the Jaccard estimate.O(k) time
exactJaccard(a, b): numberGround-truth Jaccard similarity for comparison.O(|a|+|b|) time