โ† Wiki Index / Computation & Algorithms / MinHash
๐Ÿ’ป Computation & Algorithms

MinHash

MinHash is a locality-sensitive randomized sketching scheme invented by Andrei Broder in 1997 in which the collision probability of minimum permuted hash values equals the Jaccard similarity between two sets.

MinHash (short for min-wise independent permutations locality-sensitive hashing) is a randomized dimensionality-reduction and set-sketching algorithm in computation and algorithms that compresses arbitrarily large sets into fixed-size integer signatures to estimate their Jaccard similarity coefficient J(A, B) = |A โˆฉ B| / |A โˆช B|. Invented by Andrei Z. Broder in 1997 at Digital Equipment Corporation's Systems Research Center to detect near-duplicate and mirror pages across millions of documents in the AltaVista web search engine, MinHash was also one of the foundational constructions in the theory of locality-sensitive hashing (LSH).

Given two sets A and B of arbitrary cardinality (such as the sets of 5-word w-shingles in two web documents or canonical 21-base-pair DNA k-mers in two sequenced genomes), computing |A โˆฉ B| / |A โˆช B| directly requires O(|A| + |B|) time and storage for every pair (A, B). MinHash reduces each set A to a compact signature of k minimum hash integers (hmin,1(A), ..., hmin,k(A)) in a single streaming pass so that the fraction of matching signature coordinates is an unbiased estimator of J(A, B) with standard error โ‰ค 1 / (2โˆšk)โ€”independent of the original set sizes |A| and |B|. Together with HyperLogLog, the Bloom filter, and the Countโ€“Min sketch, MinHash is a cornerstone of web-scale deduplication, LLM pretraining corpus filtering, collaborative filtering, and comparative genomics (Mash and Sourmash).

Jaccard Similarity and the Fundamental MinHash Probability Identity

Resemblance, w-Shingling, and the Jaccard Index

To compare two text documents or biological sequences as sets, Broder (1997) represented each document D as its set S(D, w) of contiguous w-grams (called w-shingles in information retrieval or k-mers in bioinformatics). For two non-empty subsets A, B โІ U drawn from a finite universe U = {0, 1, ..., N โˆ’ 1}, their Jaccard similarity coefficient (resemblance) J(A, B) and Jaccard distance dJ(A, B) are defined as:

J(A, B) = |A โˆฉ B| / |A โˆช B| โˆˆ [0, 1], dJ(A, B) = 1 โˆ’ J(A, B) = |A โ–ณ B| / |A โˆช B|

Notice that dJ(A, B) satisfies the triangle inequality on finite sets and forms a true mathematical metric space.

Proof of Broder's Min-Wise Collision Theorem

Let ฯ€ : U โ†’ U be a permutation chosen uniformly at random from the symmetric group SN of all N! permutations of U (or a hash function h : U โ†’ [0, 1] mapping elements to distinct random real numbers). Define the MinHash of set A under ฯ€ as the minimum permuted value across all members of A:

hฯ€(A) = minxโˆˆA ฯ€(x)

Theorem (Broder, 1997): Over a uniformly random permutation ฯ€ โˆˆ SN, the probability that A and B share the same minimum permuted element equals their Jaccard similarity:

Prฯ€[ hฯ€(A) = hฯ€(B) ] = |A โˆฉ B| / |A โˆช B| = J(A, B)

Proof: Consider the restricted set of elements A โˆช B. Because ฯ€ is a uniformly random permutation of U, every element z โˆˆ A โˆช B is equally likely to be mapped to the smallest value minxโˆˆAโˆชB ฯ€(x) among the |A โˆช B| elements of the union, each with probability 1 / |A โˆช B|. Moreover, since minxโˆˆAโˆชB ฯ€(x) = min( hฯ€(A), hฯ€(B) ), the two minimums hฯ€(A) and hฯ€(B) are equal if and only if that unique minimum element z* = arg minxโˆˆAโˆชB ฯ€(x) belongs to both A and Bโ€”that is, z* โˆˆ A โˆฉ B. Since there are |A โˆฉ B| such elements out of |A โˆช B| equally likely candidates, Pr[hฯ€(A) = hฯ€(B)] = |A โˆฉ B| / |A โˆช B| = J(A, B).

Multi-Hash, Bottom-k, and One-Permutation (OPH) Sketching Variants

Three Standard MinHash Signature Constructions

Because a single indicator variable I = 1[hฯ€(A) = hฯ€(B)] โˆˆ {0, 1} is a Bernoulli(J) random variable with variance J(1 โˆ’ J), estimating J(A, B) to within additive error ฮต requires combining k = O(1 / ฮต2) samples via one of three architectures:

  1. k-Hash Function MinHash: Choose k independent universal hash functions h1, h2, ..., hk and store the vector M(A) = [hmin,1(A), ..., hmin,k(A)]. The estimator ฤด(A, B) = (1 / k) โˆ‘i=1..k 1[hmin,i(A) = hmin,i(B)] has expected value J and standard deviation โˆš( J(1 โˆ’ J) / k ) โ‰ค 1 / (2โˆšk). For k = 400 hashes, standard error is โ‰ค 2.5%, at the cost of O(k |A|) hash evaluations per set.
  2. Bottom-k (Single-Hash k-Mins) Sketch: Apply a single hash function h to all elements of A and retain the k smallest hashed values mink(h(A)) in a bounded max-heap of size k (taking only O(|A| log k) time). To estimate J(A, B), merge the two sorted bottom-k lists of A and B, take the k smallest distinct hashes overall, and count what fraction belong to h(A) โˆฉ h(B).
  3. One-Permutation Hashing (OPH, Li, Owen, & Zhang, 2012) & Densification (Shrivastava, 2017): Partition the output space of a single hash function h into k equal bins (exactly like HyperLogLog bucketing) and record the minimum hash value within each bin in O(|A|) time, using optimal rotational densification to fill any empty bins when |A| < k log k.
MinHash VariantSketch Construction Time for Set ASignature Size (k Registers)Estimator Standard ErrorSupports LSH Banding Index?Primary Practical Application
k-Permutation MinHash (Broder, 1997)O(k ยท |A|)k ร— 32/64 bitsโˆš( J(1 โˆ’ J) / k )Yes (direct band hashing)Classic LSH tables, Spark MLlib MinHashLSH
b-Bit MinHash (Li & Kรถnig, 2010)O(k ยท |A|) or OPH O(|A|)k ร— b bits (b = 1 or 2 bits)Slightly scaled by 1/(1 โˆ’ 2โˆ’b)Yes (ultrafast SIMD XOR/POPCNT)Memory-compressed web-scale duplicate search
Bottom-k Sketch (Mash, Ondov et al., 2016)O(|A| log k) (1 hash/item)k ร— 64 bits (k = 1,000)โ‰ˆ โˆš( J(1 โˆ’ J) / k )Via sub-samplingComparative genomics (Mash, RefSeq clustering)
FracMinHash / Scaled MinHash (Irber, 2022)O(|A|) (keep hashes < 264/s)Variable (|A| / s)Bounded for containment C(A, B)Via inverted hash indexMetagenomic containment search (Sourmash)
Weighted MinHash / ICWS (Ioffe, 2010)O(k ยท nnz(v))k ร— (index, t) pairsโˆš( JW(1 โˆ’ JW) / k )YesWeighted bag-of-words & histogram similarity

LSH Banding Amplification and Genomic Distance Estimation (Mash)

Comparing all (N choose 2) = O(N2) MinHash signatures directly across N = 108 documents is still quadratic in N. To achieve sublinear query time via locality-sensitive hashing, the k-element MinHash signature vector is partitioned into b bands of r rows each (k = b ยท r):

  • For each band j โˆˆ {1, ..., b}, the r integers in band j are concatenated and hashed into a standard hash table bucket. Two documents become candidate duplicates if they collide in at least one of the b bands.
  • Because the probability that all r rows in a single band match is Jr, the probability that A and B collide in at least one of the b independent bands follows the sharp S-curve:
Pcandidate(J) = 1 โˆ’ ( 1 โˆ’ Jr )b

with inflection threshold ฯ„ โ‰ˆ (1 / b)1/r (for example, k = 100 split into b = 20 bands of r = 5 rows gives threshold ฯ„ โ‰ˆ (1/20)1/5 โ‰ˆ 0.549: pairs with J = 0.80 are captured with probability 99.96%, while pairs with J = 0.20 collide with probability only 0.64%).

In comparative genomics (Mash, Ondov et al., 2016), bottom-k MinHash sketches (k = 1,000 canonical 21-mers, occupying 8 KB per genome) transform the estimated Jaccard index j = ฤด(A, B) into the evolutionary Mash distance D (approximating 1 โˆ’ Average Nucleotide Identity, 1 โˆ’ ANI) under a Poisson mutation model of k-mer survival probability (1 โˆ’ D)k = 2j / (1 + j):

D = โˆ’(1 / k) ln( 2j / (1 + j) )

enabling all-pairs clustering of 54,118 NCBI RefSeq genomes in 33 CPU hours instead of years of alignment.

Frequently Asked Questions

Do we need true random permutations of the universe to implement MinHash?

No. Storing a true random permutation of a 64-bit universe is impossible, and Piotr Indyk (2001) proved that O(log(1/ฮต))-wise independent hash functions approximate min-wise independent permutations with negligible bias โ‰ค ฮต. In practice, simple universal affine hashes hi(x) = (aix + bi) mod p or 64-bit MurmurHash3 / xxHash with distinct seeds achieve the theoretical 1 / โˆšk standard error.

When should FracMinHash (Containment MinHash) be used instead of standard Bottom-k MinHash?

When comparing a small viral genome (A, 104 k-mers) against a massive host metagenome (B, 109 k-mers), their Jaccard similarity J(A, B) = |A โˆฉ B| / |A โˆช B| โ‰ค 10โˆ’5 is nearly zero even if 100% of A is contained inside B, so a fixed-size bottom-k sketch of B misses almost all hashes of A. FracMinHash (used in Sourmash) retains all hashes below a fixed threshold Hmax = 264 / s (e.g., scale factor s = 1,000), directly estimating the containment index C(A, B) = |A โˆฉ B| / |A| without dilution by |B|.

How does Weighted MinHash extend Jaccard similarity to real-valued frequency vectors?

For non-negative real vectors x, y โˆˆ โ„โ‰ฅ0d, the generalized (weighted) Jaccard similarity is JW(x, y) = โˆ‘i min(xi, yi) / โˆ‘i max(xi, yi). Sergey Ioffe's Consistent Weighted Sampling (ICWS, 2010) samples Gamma(2, 1) (sum of two exponential distribution draws) and uniform random variables per active coordinate i (xi > 0) so that the probability of selecting the same coordinate-value pair equals JW(x, y) in O(nnz(x)) time independent of the magnitude of xi.

References

  1. Broder, A. Z. (1997). "On the resemblance and containment of documents." Proceedings of the Compression and Complexity of Sequences 1997 (SEQUENCES '97), IEEE, 21โ€“29. https://doi.org/10.1109/SEQUEN.1997.666900
  2. Broder, A. Z., Charikar, M., Frieze, A. M., & Mitzenmacher, M. (2000). "Min-wise independent permutations." Journal of Computer and System Sciences, 60(3), 630โ€“659. https://doi.org/10.1006/jcss.1999.1690
  3. Ioffe, S. (2010). "Improved consistent sampling, weighted minhash and L1 sketching." 2010 IEEE International Conference on Data Mining (ICDM), 246โ€“255. https://doi.org/10.1109/ICDM.2010.80
  4. Li, P., Owen, A. B., & Zhang, C.-H. (2012). "One permutation hashing." Advances in Neural Information Processing Systems (NeurIPS 25), 3113โ€“3121. https://proceedings.neurips.cc/paper/2012/file/baa2be66c6f933fb9efbc4f8bf2a51bf-Paper.pdf
  5. Ondov, B. D., Treangen, T. J., Melsted, P., Mallonee, A. B., Bergman, N. H., Koren, S., & Phillippy, A. M. (2016). "Mash: fast genome and metagenome distance estimation using MinHash." Genome Biology, 17(1), 132. https://doi.org/10.1186/s13059-016-0997-x
๐Ÿงฌ Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
๐Ÿ’ก Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision