Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Algorithms · Probabilistic

Similarity in a Signature.

Watch the explainer

Comparing two huge sets element by element is slow, and comparing millions of pairs is hopeless. MinHash shrinks each set to a tiny signature of k numbers, then estimates similarity by comparing signatures. The magic property: for a single hash function, the probability that the two sets share the same minimum hash value is exactly their Jaccard similarity — the fraction of elements they have in common. Average over k hashes and you get a fast, accurate estimate. Add hashes and watch the estimate home in on the true overlap.

signature of k numbers · P(min matches) = Jaccard similarity · near-duplicate detection at scale
Jaccard similarity

|A ∩ B| / |A ∪ B| — the fraction of the combined elements that both sets share.

min-hash

For one hash function, the minimum hash value over all of a set’s elements.

signature

A set’s k min-hash values — a tiny fixed-size fingerprint, regardless of set size.

estimate

The fraction of the k signature positions where two sets’ min-hashes agree.

minhash.js — k minima, one similarity
Ready
Two sets with some overlap. Their true Jaccard similarity is fixed. Each Add hash computes one min-hash for each set: if the two minima match, that’s a vote for similarity. The match rate estimates Jaccard.
0
hashes (k)
–
estimate
0.33
true Jaccard

How it works

Here is why one min-hash matches with probability exactly the Jaccard similarity. Imagine hashing every element of the union A ∪ B and asking which element gets the smallest hash. That “winner” is equally likely to be any element of the union. The two sets share the same minimum if and only if that winning element lies in the intersection A ∩ B. So the match probability is |A ∩ B| / |A ∪ B| — the Jaccard similarity, by definition. Each hash is one Bernoulli trial with that success probability; averaging k of them estimates it.

1

Pick a hash function

Choose a hash that maps every possible element to a number, effectively a random permutation of the elements.

2

Take each set’s minimum

Hash every element of set A and keep the smallest value; do the same for set B. These are the two sets’ min-hashes for this hash function.

3

A match is a vote

If A’s minimum equals B’s minimum, the winning element is in the intersection — count it as a match. The chance of that is exactly the Jaccard similarity.

✓

Average over k hashes

Repeat with k independent hashes to build k-number signatures. The fraction of positions where the signatures agree estimates the Jaccard similarity, with accuracy improving as k grows.

Signature size
k numbers
P(match)
Jaccard
Estimate error
~1/√k
Used in
dedup, LSH

The code

# estimate Jaccard(A, B) with k min-hashes def minhash_estimate(A, B, hashes): matches = 0 for h in hashes: # k independent hash functions min_a = min(h(x) for x in A) # set A's min-hash min_b = min(h(x) for x in B) # set B's min-hash if min_a == min_b: matches += 1 # the winner is in A ∩ B return matches / len(hashes) # ≈ |A∩B| / |A∪B|

Quick check

1. What does MinHash estimate?

2. Why does one min-hash match with probability equal to the Jaccard similarity?

3. How does the estimate improve as you add more hashes?

FAQ

What is MinHash?

A technique for quickly estimating the Jaccard similarity of two sets. Each set is represented by a signature of k values, each the minimum of a hash over the set’s elements. The fraction of signature positions where two sets agree estimates their Jaccard similarity — far faster than comparing elements directly.

What is Jaccard similarity?

The size of the intersection divided by the size of the union: |A∩B| / |A∪B|. It ranges from 0 (nothing shared) to 1 (identical) and measures the fraction of combined elements the two sets share.

How is MinHash used with Locality-Sensitive Hashing (LSH)?

MinHash produces each item’s signature; LSH buckets signatures so similar items likely collide, avoiding all-pairs comparison. The signature is split into bands, and items sharing any band become candidate matches — together they scale near-duplicate detection to billions of items.

Where is MinHash used in practice?

Near-duplicate web page and document detection (search index dedup), plagiarism detection, clustering, recommendation/collaborative filtering, and dataset deduplication — anywhere you must find similar items in a huge collection where exact pairwise comparison is too costly.

The picture: who holds the minimum

AB386120226231334850540861334850540867755687789hash 1: min of A is element 2 (20), min of B is element 7 (7) → no matchA ∩ B = {4, 5, 6}; a match can only happen if the overall minimum is one of them
One hash function, applied to every element of both sets (values scaled to 0–999). Each set keeps only its smallest value. Green means both sets chose the same element, which can only be 4, 5 or 6; red means the minima differ. Repeating this with new seeds gives one vote per hash.

Worked example, step by step

Take A = {1, 2, 3, 4, 5, 6} and B = {4, 5, 6, 7, 8, 9}. The intersection has 3 elements and the union has 9, so the true Jaccard similarity is 3/9 = 0.333. Hash number s is FNV-1a over the decimal digits of the element, seeded with s, finished with the murmur3 mixing steps. For each of 8 seeds, find the element each set gives the smallest hash, and count a vote when the two elements agree.

HashMin of A (element)Min of B (element)Match?Running estimate
186222693 (2)30487999 (7)no0.000
2760498317 (5)760498317 (5)yes0.500
32006135258 (4)1837757736 (7)no0.333
4263244247 (6)263244247 (6)yes0.500
5419643658 (6)419643658 (6)yes0.600
6535183731 (3)599835869 (4)no0.500
7986628126 (6)986628126 (6)yes0.571
8565208416 (3)976527643 (8)no0.500

4 of 8 hashes matched, so the signature estimate is 4/8 = 0.500 against a true 0.333. Every match falls on an element of the intersection: when A and B agree, the shared minimum is always 4, 5 or 6. Every miss is a hash where the overall winner sat in only one set (an element from 1–3 or 7–9). The running estimate in the last column wanders, and with only 8 votes it has to: each vote is a coin flip with probability 0.333. The next section measures how fast that settles.

Complexity and trade-offs

CaseTimeSpaceWhy
MinHash signatureO(k · |S|) to buildO(k) per setBuilt once per set. Two signatures compare in O(k), whatever the set sizes.
Exact Jaccard with hash setsO(|A| + |B|) per pairO(|A| + |B|)Exact, but touches every element of both sets for every pair.
All pairs of n sets, exactO(n² · set size)O(n · set size)Quadratic in the number of documents.
All pairs of n sets, signaturesO(n² · k)O(n · k)Still quadratic in n, but the constant no longer depends on set size.
Signatures + banding (LSH)about O(n · k) to bucketO(n · k)Only sets sharing a band are compared; trades recall of borderline pairs for speed.

Accuracy costs signature length. On the page's sets (true 0.333), 2,000 independent runs per row, each with fresh seeds: k = 8 gives mean 0.338, standard deviation 0.168 (formula √(J(1−J)/k) = 0.167); k = 32: 0.335, 0.084 (0.083); k = 128: 0.336, 0.041 (0.042); k = 512: 0.334, 0.021 (0.021). The estimate is unbiased, and the error falls as 1/√k: four times the signature for half the error. Signatures only beat exact comparison when sets are large or when each signature is reused across many comparisons. For two 6-element sets, comparing them directly is cheaper than 128 hashes.

Common mistakes

  • Reusing one hash for all k positions

    If every position uses the same function, all k votes are the same vote. On the page's sets hash 1 gives no match, so a signature of 8 copies of it reports 0.000; had it matched, it would report 1.000. Across 2,000 different seeds, 31.9% matched (the truth is 33.3%), so a single hash is right only on average, never in one run.

  • Misaligning signature positions

    Position i of one signature must be compared with position i of the other, built with the same seed. Build B's signature with seeds shifted by one and the estimate of the page's sets with k = 128 is 0.000 instead of 0.320, because the votes now compare unrelated hashes.

  • Using a tiny k and trusting it

    With k = 8, 45.8% of runs on the page's sets land more than 0.1 away from the truth. With k = 128 that falls to 1.4%. Pick k from the error you can tolerate, using √(J(1−J)/k).

  • Treating equal signatures as equal sets

    A and A plus one extra element have Jaccard 0.857. With k = 8, all eight positions agree in 28.6% of runs, so identical signatures do not prove identical sets. Verify candidates exactly before acting on a match.

  • Expecting counts to matter

    MinHash estimates set similarity, so repeats are invisible: A with every element repeated three times gives the identical signature. If frequency matters, use a weighted variant or add the count into the element (for example "x#1", "x#2").

Where this shows up

  • Near-duplicate documents

    Break each document into overlapping word groups (shingles), treat them as a set, and compare signatures. Pages that differ by a timestamp or a footer stay highly similar, and the pairs worth comparing can be found without reading every document against every other.

  • Deduplicating training data

    Large text corpora are often filtered for near-duplicates before training. With 1,000,000 documents, exact pairwise comparison would be 499,999,500,000 pairs; banding signatures avoids nearly all of them.

  • Recommendations and similar users

    Two users who liked overlapping sets of items have a high Jaccard similarity. A signature per user lets a service find neighbours without scanning every user's full history.

  • Clustering and sketching sets

    Any task that asks "how much do these two collections overlap?" at scale, such as tags, links, or sets of visited pages, can store k numbers per collection instead of the collection.

SOLVE IT YOURSELF

Solve it: compare two sets without comparing them

Reduce a million-element set to 128 numbers, then estimate how similar any two sets are by counting how many of those numbers match. The probability of a match is the Jaccard similarity. Python or TypeScript.

YOUR TASK

Implement signature(items, num_hashes) — the minimum hash value under each of num_hashes independent hashes — and estimate_jaccard(sig_a, sig_b), the fraction of positions where the two signatures agree.

HINTS — 6 IDEAS
  1. For each hash function, take the minimum over every element of the set. That single number is the signature slot.
  2. Why it works: under a random hash, every element is equally likely to be the smallest. So the minimum agrees for two sets exactly when the overall smallest element is in both — which happens with probability |A∩B| / |A∪B|, the Jaccard similarity.
  3. So estimating similarity is just counting matching slots. No intersection is ever computed.
  4. More hashes means a tighter estimate — the error shrinks as 1/√k, so 128 slots give roughly ±4%.
  5. The hashes must be independent, and each must avalanche. A weak hash makes the minima correlated and the estimate quietly wrong.
  6. Identical sets always score exactly 1.0 and disjoint sets almost always 0.0 — the tests check both, plus accuracy against a real Jaccard in between.
CPython · WebAssembly

Keep going

▶  Watch it explained

MinHash: Similarity from One Coin Flip

Finished this one? 0 / 99 Algorithms done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Algorithms