MinHashing compresses a large set into a short list of integers, called a signature, from which we can estimate how similar two sets are. It is widely used to find near-duplicate documents.
For text, we first break each document into shingles (short overlapping pieces, such as pairs of words). A document then becomes a set of shingles, and we compare sets with the Jaccard similarity:
\[ J(A, B) = \frac{|A \cap B|}{|A \cup B|} \]
Instead of storing thousands of shingles per document, MinHash stores one small column of numbers.
Why it works
Put the sets in a table of 0s and 1s: one row per shingle, one column per document. Now shuffle the rows at random, and for each column note the first row that holds a 1. This is the MinHash value of the column.
For two columns, only rows with at least one 1 matter. Each of them is equally likely to come first, and the two MinHash values match only when that first row has a 1 in both columns. Therefore:
\[ P(\text{values match}) = J(A, B) \]
Rows holding 0 in both columns play no part at all.
Hash functions instead of shuffling
Shuffling a huge table is too slow. So we run the row number \(r\) through a hash function, which gives the row a new position:
\[ h(r) = (a\,r + b) \bmod p \]
Here \(p\) is a prime at least as large as the number of rows, and \(a\), \(b\) are constants. Each choice of \(a\) and \(b\) acts as one more (approximate) shuffle. For every column, we keep the smallest hash value among the rows that hold a 1.
Example 1: three documents
We have three documents and five shingles, in rows 0 to 4.
Step 1: write the table.
| Shingle (row) | D₁ | D₂ | D₃ |
|---|---|---|---|
| 0 (the cat) | 1 | 0 | 1 |
| 1 (cat sat) | 0 | 1 | 0 |
| 2 (sat on) | 1 | 0 | 1 |
| 3 (on the) | 0 | 1 | 1 |
| 4 (the mat) | 1 | 0 | 0 |
Step 2: find the true similarity.
\[ \begin{aligned} J(D_1, D_2) &= 0/5 = 0 \\ J(D_1, D_3) &= 2/4 = 0.50 \\ J(D_2, D_3) &= 1/4 = 0.25 \end{aligned} \]
For instance, D₁ and D₃ share rows {0, 2} out of rows {0, 2, 3, 4}, ignoring row 1, where both have a 0.
Step 3: choose the hash functions.
\[ \begin{aligned} h_1(r) &= (r + 1) \bmod 5 \\ h_2(r) &= (3r + 1) \bmod 5 \end{aligned} \]
Step 4: hash every row number.
| Row \(r\) | D₁ | D₂ | D₃ | \(h_1(r)\) | \(h_2(r)\) |
|---|---|---|---|---|---|
| 0 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 | 2 | 4 |
| 2 | 1 | 0 | 1 | 3 | 2 |
| 3 | 0 | 1 | 1 | 4 | 0 |
| 4 | 1 | 0 | 0 | 0 | 3 |
Step 5: look where each document has a 1, and take the minimum.
\[ \begin{aligned} D_1 &\to \{0, 2, 4\} \\ h_1 &\to \{1, 3, 0\} \to \min = 0 \\ h_2 &\to \{1, 2, 3\} \to \min = 1 \end{aligned} \]
\[ \begin{aligned} D_2 &\to \{1, 3\} \\ h_1 &\to \{2, 4\} \to \min = 2 \\ h_2 &\to \{4, 0\} \to \min = 0 \end{aligned} \]
\[ \begin{aligned} D_3 &\to \{0, 2, 3\} \\ h_1 &\to \{1, 3, 4\} \to \min = 1 \\ h_2 &\to \{1, 2, 0\} \to \min = 0 \end{aligned} \]
Step 6: collect the minimums in the signature matrix.
| Hash | D₁ | D₂ | D₃ |
|---|---|---|---|
| \(h_1\) | 0 | 2 | 1 |
| \(h_2\) | 1 | 0 | 0 |
Step 7: estimate the similarity. It is the fraction of signature rows in which two columns agree.
| Pair | Matching rows | Estimate | True \(J\) |
|---|---|---|---|
| D₁, D₂ | 0 of 2 | 0.00 | 0.00 |
| D₁, D₃ | 0 of 2 | 0.00 | 0.50 |
| D₂, D₃ | 1 of 2 | 0.50 | 0.25 |
Two hash functions give a very rough estimate. With a hundred or more good ones, the estimates usually come much closer to the true values.
Example 1 in short
We skip the 0s: write down only the rows with a 1, hash them, and keep the smallest values (in bold).
| Doc | Rows with 1 | \(h_1\) | \(h_2\) |
|---|---|---|---|
| D₁ | 0, 2, 4 | 1, 3, 0 → 0 | 1, 2, 3 → 1 |
| D₂ | 1, 3 | 2, 4 → 2 | 4, 0 → 0 |
| D₃ | 0, 2, 3 | 1, 3, 4 → 1 | 1, 2, 0 → 0 |
The bold values are the signature matrix.
Example 2: two documents
Now we have two documents and five shingles, in rows 1 to 5.
Step 1: write the table.
| Shingle (row) | C1 | C2 |
|---|---|---|
| 1 | 1 | 0 |
| 2 | 0 | 1 |
| 3 | 1 | 1 |
| 4 | 1 | 0 |
| 5 | 0 | 1 |
Step 2: find the true similarity. Only row 3 has a 1 in both columns:
\[ J(C_1, C_2) = 1/5 = 0.2 \]
Step 3: choose the hash functions.
\[ \begin{aligned} h(x) &= x \bmod 5 \\ g(x) &= (2x + 1) \bmod 5 \end{aligned} \]
Step 4: hash every row number.
| Row \(x\) | C1 | C2 | \(h(x)\) | \(g(x)\) |
|---|---|---|---|---|
| 1 | 1 | 0 | 1 | 3 |
| 2 | 0 | 1 | 2 | 0 |
| 3 | 1 | 1 | 3 | 2 |
| 4 | 1 | 0 | 4 | 4 |
| 5 | 0 | 1 | 0 | 1 |
Step 5: look where each document has a 1, and take the minimum.
\[ \begin{aligned} C_1 &\to \{1, 3, 4\} \\ h &\to \{1, 3, 4\} \to \min = 1 \\ g &\to \{3, 2, 4\} \to \min = 2 \end{aligned} \]
\[ \begin{aligned} C_2 &\to \{2, 3, 5\} \\ h &\to \{2, 3, 0\} \to \min = 0 \\ g &\to \{0, 2, 1\} \to \min = 0 \end{aligned} \]
Step 6: collect the minimums in the signature matrix.
| Hash | C1 | C2 |
|---|---|---|
| \(h(x)\) | 1 | 0 |
| \(g(x)\) | 2 | 0 |
Step 7: estimate the similarity. The columns agree in neither row:
\[ \text{sim}(C_1, C_2) = 0/2 = 0 \]
Example 2 in short
| Doc | Rows with 1 | \(h\) | \(g\) |
|---|---|---|---|
| C1 | 1, 3, 4 | 1, 3, 4 → 1 | 3, 2, 4 → 2 |
| C2 | 2, 3, 5 | 2, 3, 0 → 0 | 0, 2, 1 → 0 |
Why we can skip the 0s
A 0 never changes a signature, because we take the minimum only over the rows that hold a 1. So the full table in Step 4 is not needed.
Compute the hash values of a row only once, and reuse them for every column that has a 1 there. A row with 0 in every column can be ignored completely.
Real document tables are almost entirely 0s, because one document contains only a tiny share of all shingles. Storing just the row numbers of the 1s therefore saves a great deal of time and memory.
The same in Python
Each column is written as the list of its 1 rows:
sets = {"C1": [1, 3, 4], "C2": [2, 3, 5]} # rows that hold a 1
hashes = [lambda x: x % 5, lambda x: (2 * x + 1) % 5]
for name, rows in sets.items():
print(name, [min(h(x) for x in rows) for h in hashes])C1 [1, 2]
C2 [0, 0]The two signatures do not match anywhere, so the estimated similarity is 0. The true value is 0.2, since only row 3 out of five has a 1 in both columns.
With five rows, we can try all 120 shuffles and see the probability rule at work:
from itertools import permutations
C1, C2 = {1, 3, 4}, {2, 3, 5}
orders = list(permutations(range(1, 6))) # every shuffle of 5 rows
matches = sum(
min(p[r - 1] for r in C1) == min(p[r - 1] for r in C2)
for p in orders
)
print(matches, "of", len(orders), "=", matches / len(orders))24 of 120 = 0.2The share of matching shuffles equals the Jaccard similarity exactly.
Key takeaways
- Compression: with 100 hash functions, a document with 100,000 shingles shrinks to 100 integers.
- Accuracy and cost: more hash functions give a closer estimate, but need more time and storage.
- Only the 1s matter: 0 rows never change a signature, so we can skip them.
- Next step, LSH: comparing every pair of signatures is still a brute-force job. Locality-Sensitive Hashing splits the signature matrix into bands of rows and compares only the columns that land in the same bucket.