Search

Search papers and lessons

MinHash

Level 3 · 13 min read

MinHash compresses a set into a short signature whose matching entries estimate the Jaccard index of two sets, so near-duplicates can be found without comparing every pair of sets in full.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Algorithms
  • ReadReady to readNot yet

TL;DR

MinHash turns each set into a short list of numbers, called a signature, so that two sets can be compared by counting matching numbers instead of comparing their full contents. The chance that two signatures agree at one position equals the Jaccard Index of the two sets, so the fraction of agreeing positions is an estimate of their similarity. It is used to find near-duplicate documents in very large collections.

Builds on: Jaccard Index, Hash Function, Law of Large Numbers · Leads to: Locality-Sensitive Hashing

Where it's used

Removing duplicate web pages. A search index or a training corpus can contain many copies of the same page with small edits, such as a changed date or a different footer. Each page is turned into the set of its short word sequences, and MinHash signatures flag pages whose sets overlap heavily. Andrei Broder introduced the idea in 1997 for detecting near-duplicate web documents while working on web search at AltaVista.

Deduplicating training data. Large language model teams clean their corpora by removing near-copies so the model does not memorise a page that appears a thousand times. The same signature test runs over millions of documents, which is why storing a short signature per document matters more than storing the documents themselves.

Grouping similar items at scale. Online shops and music services use similarity between sets of purchased items or listened tracks to find related products. With signatures, the expensive part, comparing all pairs, is reduced: a signature is a few hundred numbers, and only pairs that agree often enough are examined in detail.

How it works

Take two small sets, each drawn from the numbers 1 to 4: A={1,2,3}A = \{1, 2, 3\} and B={2,3,4}B = \{2, 3, 4\}. Their Jaccard index is ∣A∩B∣÷∣A∪B∣=2÷4=0.5\lvert A \cap B \rvert \div \lvert A \cup B \rvert = 2 \div 4 = 0.5. MinHash should land near 0.5 without ever computing that quotient.

  1. Pick a hash function for each signature slot. Use two hash functions, h1h_1 and h2h_2, which map each number to a value. Here h1h_1 gives 1 → 5, 2 → 2, 3 → 7, 4 → 1, and h2h_2 gives 1 → 4, 2 → 6, 3 → 3, 4 → 8. Check: each number gets one value per function, and the values are all different within a function, so there are no ties.

  2. Take the minimum over each set, for each function. For AA with h1h_1, the values are 5, 2 and 7, so the minimum is 2. For AA with h2h_2, the values are 4, 6 and 3, so the minimum is 3. The signature of AA is (2,3)(2, 3). Check: the minimum always comes from an item in the set, and it is the item with the smallest hash value.

  3. Do the same for BB. For BB with h1h_1, the values are 2, 7 and 1, so the minimum is 1. For BB with h2h_2, the values are 6, 3 and 8, so the minimum is 3. The signature of BB is (1,3)(1, 3). Check: each signature has one entry per function, so two signatures always have the same length and can be compared position by position.

  4. Count the agreements. Position 1 gives 2≠12 \ne 1, a disagreement. Position 2 gives 3=33 = 3, an agreement. One of two positions agrees. Check: the estimate is 1÷2=0.51 \div 2 = 0.5, the same as the true Jaccard index in this tiny case.

  5. Trust the estimate only when there are enough positions. With two positions, one lucky match can move the estimate a lot. More functions give a steadier count, which the math section makes precise.

flowchart LR
  S[Set of items] --> H[k hash functions]
  H --> M[Minimum per function]
  M --> G[Signature of k numbers]
  G --> C[Count matching positions]
  C --> J[Estimate of Jaccard index]

The math

Definition: MinHash

For a set AA and a hash function hh, the MinHash value is min⁡x∈Ah(x)\min_{x \in A} h(x), the smallest hash value among the items of AA. A signature of length kk is the list (min⁡x∈Ah1(x),…,min⁡x∈Ahk(x))\big(\min_{x \in A} h_1(x), \dots, \min_{x \in A} h_k(x)\big) for kk independent hash functions.

Step 2 in symbols: what the minimum tells us

Model the hash as a random ordering of all items: each item gets an independent random rank, and the item with the smallest rank in a set is the one whose hash is the set's minimum. Consider the union A∪BA \cup B. Each of its items is equally likely to have the smallest rank in A∪BA \cup B. The minimum over AA and the minimum over BB are the same value exactly when the overall minimum of A∪BA \cup B belongs to the intersection A∩BA \cap B, because then it is the smallest in both sets. If the overall minimum lies in only one set, the two sets' minima differ.

So the probability of agreement is the chance that the overall minimum is one of the shared items:

Pr⁡[min⁡x∈Ah(x)=min⁡x∈Bh(x)]=∣A∩B∣∣A∪B∣=J(A,B).\Pr\big[\min_{x \in A} h(x) = \min_{x \in B} h(x)\big] = \frac{\lvert A \cap B \rvert}{\lvert A \cup B \rvert} = J(A, B).

Check with the walkthrough: the union {1,2,3,4}\{1, 2, 3, 4\} has four items and the intersection {2,3}\{2, 3\} has two, so the agreement chance is 2÷4=0.52 \div 4 = 0.5.

Step 3 in symbols: the estimator

For each of the kk hash functions, define the indicator Xi=1X_i = 1 if the two minima agree and Xi=0X_i = 0 otherwise. Each XiX_i equals 1 with probability JJ, so it is a Bernoulli variable. The estimate is their average:

J^=1k∑i=1kXi.\hat{J} = \frac{1}{k}\sum_{i=1}^{k} X_i .

The expected value is E[J^]=J\mathbb{E}[\hat{J}] = J, so the estimate is correct on average. Its variance is

Var⁡(J^)=J(1−J)k,\operatorname{Var}(\hat{J}) = \frac{J(1 - J)}{k},

because the XiX_i are treated as independent. The standard deviation shrinks like 1÷k1 \div \sqrt{k}, so halving the error needs four times as many hash functions.

Step 4 in symbols: a worked size

Let J=37J = \frac{3}{7} and k=8k = 8. Then J(1−J)=37⋅47=1249≈0.2449J(1 - J) = \frac{3}{7} \cdot \frac{4}{7} = \frac{12}{49} \approx 0.2449, and 0.24498≈0.0306\frac{0.2449}{8} \approx 0.0306. The standard deviation is 0.0306≈0.175\sqrt{0.0306} \approx 0.175, so an estimate from eight functions typically lies within about 0.180.18 of the true value.

Worked examples

Example
1. Two sets, two hash functions

Take A={1,2,3}A = \{1, 2, 3\} and B={2,3,4}B = \{2, 3, 4\}, with h1h_1: 1 → 5, 2 → 2, 3 → 7, 4 → 1 and h2h_2: 1 → 4, 2 → 6, 3 → 3, 4 → 8.

  1. Hash values of AA under h1h_1 are 55, 22, 77. The minimum is 22.
  2. Hash values of AA under h2h_2 are 44, 66, 33. The minimum is 33. So the signature of AA is (2,3)(2, 3).
  3. Hash values of BB under h1h_1 are 22, 77, 11. The minimum is 11.
  4. Hash values of BB under h2h_2 are 66, 33, 88. The minimum is 33. So the signature of BB is (1,3)(1, 3).
  5. Compare position by position: 2≠12 \ne 1 at position 1, and 3=33 = 3 at position 2.
  6. One of two positions agrees, so the estimate is 12=0.5\frac{1}{2} = 0.5, which equals the true value 24\frac{2}{4}.
Example
2. The standard case, eight hash functions

Two sets of 10 items each share 6 items. Their union has 10+10−6=1410 + 10 - 6 = 14 items, so the true Jaccard index is J=614=37≈0.4286J = \frac{6}{14} = \frac{3}{7} \approx 0.4286.

  1. Build a signature of k=8k = 8 minima for each set.
  2. Suppose 3 of the 8 positions agree.
  3. Divide: J^=38=0.375\hat{J} = \frac{3}{8} = 0.375.
  4. The gap from the truth is 0.4286−0.375=0.05360.4286 - 0.375 = 0.0536.
  5. Compare with the spread from Step 4 in the math: the standard deviation is about 0.1750.175, so a gap of 0.0540.054 is well inside one standard deviation. Eight positions are enough to say the sets are roughly as similar as 0.40.4, but not enough to tell 0.400.40 from 0.450.45.
  6. Quadrupling to k=32k = 32 halves the standard deviation to 0.2449÷32≈0.0875\sqrt{0.2449 \div 32} \approx 0.0875, which is the sense in which more functions help.
Example
3. Realistic: finding near-duplicate documents

A crawl holds 1,000,0001{,}000{,}000 documents. Each document is turned into a set of short word sequences, and each signature has k=128k = 128 numbers, stored as 4-byte integers.

  1. Comparing every pair needs n(n−1)2\frac{n(n-1)}{2} comparisons. With n=1,000,000n = 1{,}000{,}000, that is 1,000,000×999,9992=499,999,500,000\frac{1{,}000{,}000 \times 999{,}999}{2} = 499{,}999{,}500{,}000 pairs, about half a trillion.
  2. The signatures take 1,000,000×128=128,000,0001{,}000{,}000 \times 128 = 128{,}000{,}000 numbers, and 128,000,000×4=512,000,000128{,}000{,}000 \times 4 = 512{,}000{,}000 bytes, which is about 512 MB.
  3. The 512 MB of signatures fits in the memory of one machine, so the full set can be scanned in one pass.
  4. Pairs whose signatures agree on enough positions are then re-checked on their full sets. Finding those candidate pairs quickly is the job of Locality-Sensitive Hashing, which groups signatures into buckets so that only pairs sharing a bucket are compared.
  5. The data is shrunk from the size of the documents to about 512 MB of signatures, and the search space shrinks from half a trillion pairs to the few candidates that share a bucket.

Try it: the same method on 10,00010{,}000 documents. How many pairs would a full comparison examine? Answer: 10,000×9,9992=49,995,000\frac{10{,}000 \times 9{,}999}{2} = 49{,}995{,}000 pairs, about 50 million.

Common mistakes and tradeoffs

It is tempting to think MinHash computes the Jaccard index exactly. In fact it gives a random estimate whose error shrinks as the signature gets longer, with a standard deviation of about J(1−J)/k\sqrt{J(1-J)/k}. Two sets with a true index of 0.4 can produce an estimate of 0.3 or 0.5 with a short signature, so treat the result as a guide, not as a measurement.

It is tempting to think that one hash function is enough because the minimum is "random". In fact each signature position must be an independent hash function. Using one function repeatedly gives the same minimum every time, so the positions agree or disagree together and the estimate is worthless.

It is tempting to think that MinHash works for any data. In fact it compares sets. A repeated word or a weighted feature needs a different encoding, such as a set of word-and-count pairs, before MinHash can be applied. A hash function used here does not need to be cryptographic; it needs to spread values evenly and to be fast, which is a different goal from the one in Cryptographic Hash Function.

Use MinHash when sets are large or numerous, you care about similarity above a threshold, and an approximate answer is acceptable. Do not use it when sets are small enough to compare exactly, when you need the exact overlap for a count or a billing rule, or when the data is numeric and Cosine Similarity fits better.

History

Comparing documents by their shared word sequences was a practical problem in the 1990s, when search engines had to avoid showing near-identical pages and ranking them as separate results. Andrei Broder formalised resemblance between documents through the Jaccard index and showed that a small random sample of minima could estimate it. His 1997 paper made the method usable at web scale, and it became a standard tool for near-duplicate detection.

The idea caught on because the signature is small, the computation is one pass over each set, and the estimate has a clear error that is easy to control. Later work on locality-sensitive hashing (Indyk and Motwani, 1998) grouped signatures into buckets, so that likely matches are found without comparing all pairs. MinHash is its best-known example, which is why the two are taught together today.

Recap

  • A MinHash signature is a list of minima of a set under several independent hash functions.
  • The chance that two signatures agree at one position equals the Jaccard index of the sets, because the overall minimum of the union is equally likely to be any of its items.
  • The fraction of agreeing positions is an unbiased estimate of the Jaccard index, with variance J(1−J)/kJ(1-J)/k.
  • More hash functions shrink the error like 1÷k1 \div \sqrt{k}, which is the law of large numbers at work.
  • Signatures make near-duplicate search affordable; Locality-Sensitive Hashing makes the candidate search fast.

Read next: Locality-Sensitive Hashing, which turns signatures into buckets of likely matches.

Check yourself

QuestionWhy does the chance that two MinHash values agree equal the Jaccard index?Show answer

The smallest hash value in the union of the two sets is equally likely to belong to any item of the union. The two minima agree exactly when that smallest item is shared, which happens with probability ∣A∩B∣÷∣A∪B∣\lvert A \cap B \rvert \div \lvert A \cup B \rvert.

QuestionTwo sets have a Jaccard index of 0.250.25. A signature with k=4k = 4 positions is built. What is the expected number of agreeing positions?Show answer

Each position agrees with probability 0.250.25, so the expected number is 4×0.25=14 \times 0.25 = 1.

QuestionWhy must each signature position use a different hash function?Show answer

If every position used the same function, every position would give the same minimum, so all positions would agree or all would disagree together. The average would then be 0 or 1 regardless of the true similarity, and the estimate would carry no information about the sets.

QuestionA set has 6 items under hash function hh with values 99, 44, 1212, 77, 33 and 1010. What is its MinHash value under hh?Show answer

The minimum of 9,4,12,7,3,109, 4, 12, 7, 3, 10 is 33, so the MinHash value is 33.

QuestionA signature with k=100k = 100 is used to estimate a Jaccard index near 0.50.5. Roughly how far from the true value should a single estimate be expected to lie?Show answer

The standard deviation is J(1−J)/k=0.25÷100=0.0025=0.05\sqrt{J(1-J)/k} = \sqrt{0.25 \div 100} = \sqrt{0.0025} = 0.05. A typical estimate lies within about 0.050.05 of the true value, so it will often land between 0.450.45 and 0.550.55.

Sources