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: and . Their Jaccard index is . MinHash should land near 0.5 without ever computing that quotient.
-
Pick a hash function for each signature slot. Use two hash functions, and , which map each number to a value. Here gives 1 → 5, 2 → 2, 3 → 7, 4 → 1, and 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.
-
Take the minimum over each set, for each function. For with , the values are 5, 2 and 7, so the minimum is 2. For with , the values are 4, 6 and 3, so the minimum is 3. The signature of is . Check: the minimum always comes from an item in the set, and it is the item with the smallest hash value.
-
Do the same for . For with , the values are 2, 7 and 1, so the minimum is 1. For with , the values are 6, 3 and 8, so the minimum is 3. The signature of is . Check: each signature has one entry per function, so two signatures always have the same length and can be compared position by position.
-
Count the agreements. Position 1 gives , a disagreement. Position 2 gives , an agreement. One of two positions agrees. Check: the estimate is , the same as the true Jaccard index in this tiny case.
-
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
For a set and a hash function , the MinHash value is , the smallest hash value among the items of . A signature of length is the list for 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 . Each of its items is equally likely to have the smallest rank in . The minimum over and the minimum over are the same value exactly when the overall minimum of belongs to the intersection , 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:
Check with the walkthrough: the union has four items and the intersection has two, so the agreement chance is .
Step 3 in symbols: the estimator
For each of the hash functions, define the indicator if the two minima agree and otherwise. Each equals 1 with probability , so it is a Bernoulli variable. The estimate is their average:
The expected value is , so the estimate is correct on average. Its variance is
because the are treated as independent. The standard deviation shrinks like , so halving the error needs four times as many hash functions.
Step 4 in symbols: a worked size
Let and . Then , and . The standard deviation is , so an estimate from eight functions typically lies within about of the true value.
Worked examples
Take and , with : 1 → 5, 2 → 2, 3 → 7, 4 → 1 and : 1 → 4, 2 → 6, 3 → 3, 4 → 8.
- Hash values of under are , , . The minimum is .
- Hash values of under are , , . The minimum is . So the signature of is .
- Hash values of under are , , . The minimum is .
- Hash values of under are , , . The minimum is . So the signature of is .
- Compare position by position: at position 1, and at position 2.
- One of two positions agrees, so the estimate is , which equals the true value .
Two sets of 10 items each share 6 items. Their union has items, so the true Jaccard index is .
- Build a signature of minima for each set.
- Suppose 3 of the 8 positions agree.
- Divide: .
- The gap from the truth is .
- Compare with the spread from Step 4 in the math: the standard deviation is about , so a gap of is well inside one standard deviation. Eight positions are enough to say the sets are roughly as similar as , but not enough to tell from .
- Quadrupling to halves the standard deviation to , which is the sense in which more functions help.
A crawl holds documents. Each document is turned into a set of short word sequences, and each signature has numbers, stored as 4-byte integers.
- Comparing every pair needs comparisons. With , that is pairs, about half a trillion.
- The signatures take numbers, and bytes, which is about 512 MB.
- The 512 MB of signatures fits in the memory of one machine, so the full set can be scanned in one pass.
- 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.
- 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 documents. How many pairs would a full comparison examine? Answer: 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 . 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 .
- More hash functions shrink the error like , 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 .
QuestionTwo sets have a Jaccard index of . A signature with positions is built. What is the expected number of agreeing positions?Show answer
Each position agrees with probability , so the expected number is .
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 with values , , , , and . What is its MinHash value under ?Show answer
The minimum of is , so the MinHash value is .
QuestionA signature with is used to estimate a Jaccard index near . Roughly how far from the true value should a single estimate be expected to lie?Show answer
The standard deviation is . A typical estimate lies within about of the true value, so it will often land between and .
Sources
- MinHash, Wikipedia: https://en.wikipedia.org/wiki/MinHash
- Leskovec, Rajaraman and Ullman, Mining of Massive Datasets, Chapter 3 (similarity and minhashing): http://www.mmds.org