Locality-Sensitive Hashing and Approximate Nearest Neighbors
Learning Objectives
- Explain the curse of dimensionality for exact nearest-neighbor search and why approximate answers are the practical goal in high dimensions.
- Define a locality-sensitive hash family by its parameters and give two examples: MinHash for Jaccard similarity and random hyperplanes for angular distance.
- Compute the collision probability of the banding (AND-OR) construction and locate its similarity threshold.
- State the query-time exponent and explain what it buys over a linear scan.
Context & Motivation
algorithms-software/data-structures-ii studied universal hashing, whose goal is to make distinct keys collide as rarely as possible. Many real problems want the opposite. A search engine must find near-duplicate web pages among billions; a recommendation system must find users with similar tastes; the retrieval stage of Track B's retrieval-augmented generation must find the stored vectors closest to a query embedding. All are nearest-neighbor problems: given a query point, find the closest points in a large set.
In low dimensions, space-partitioning trees (kd-trees) answer such queries quickly. In hundreds of dimensions they degrade to scanning nearly everything, because in high dimensions almost all points are roughly equally far from a query, so pruning rarely works. This is the curse of dimensionality. Indyk and Motwani's 1998 answer was locality-sensitive hashing (LSH): design random hash functions under which close points are likely to collide and far points are not, then look only at the query's colliding buckets. The same idea, developed by Broder at AltaVista for duplicate detection (MinHash) and by Charikar for vectors (random hyperplanes), is covered in Stanford's CS168 and MIT's 6.854.
Core Theory
Approximate near neighbors
Exact search cannot beat a scan in the worst case in high dimensions under widely believed conjectures, so the target is relaxed: for a radius and approximation factor , if some point lies within distance of the query, return a point within distance . Allowing a slightly farther answer is what opens the door to sublinear query time.
Locality-sensitive families
A family of hash functions is -sensitive, with and , if for drawn at random from :
- if , then ;
- if , then .
Two classic families:
MinHash (Jaccard similarity). For sets and , . Pick a random permutation of the universe and let . The minimum element of under is equally likely to be any of its elements, and exactly when that element lies in , so . In practice, a random hash function stands in for the permutation.
Random hyperplanes (angular distance). For vectors , draw a random Gaussian vector and let . The hyperplane orthogonal to separates and with probability , where is the angle between them, so . This connects directly to cosine similarity in embedding search.
Amplification: AND then OR
A single hash has a weak gap between and . Amplify it in two steps:
- AND: concatenate independent hashes into one key; two points share the key with probability . This pushes far pairs' probability toward 0, but also hurts near pairs.
- OR: build independent such tables; a pair is a candidate if it collides in at least one. Probability: .
For MinHash with similarity , the candidate probability is an S-shaped curve with its steep part near the threshold . Choosing and places the threshold where the application needs it. Candidates are then checked exactly, so false positives cost only time and false negatives are the true error.
Query time
With the right parameters, LSH answers -approximate near-neighbor queries in time about with , using space. For Hamming distance with bit sampling, ; for Euclidean distance, Andoni and Indyk achieved , which is known to be optimal for this kind of hashing scheme. For in Euclidean space, that is roughly work per query instead of .
Worked Examples
Example 1: tuning bands for near-duplicate detection
Problem: Documents are represented by 100 MinHash values split into bands of rows. What is the probability that a pair with Jaccard similarity 0.8 becomes a candidate? With similarity 0.3? Where is the threshold?
Trace: For : , so . For : , so . Threshold: . True near-duplicates are almost never missed (about 1 in 2,800), and only about 5% of dissimilar pairs need an exact check. Raising to 10 with moves the threshold up to about , useful if "duplicate" should mean very close.
Example 2: random hyperplanes for embeddings
Problem: Embeddings are compared by angle. Each table concatenates hyperplane bits, and there are tables. Compare the probability of retrieving a neighbor at angle 30 degrees with one at 90 degrees (orthogonal, unrelated).
Trace: One bit agrees with probability at 30 degrees and at 90 degrees. Ten bits: versus . Across 20 tables: versus . So a close neighbor is found 97% of the time while an unrelated vector is examined less than 2% of the time, and the query inspects only a small fraction of the database.
Common Misconceptions & Pitfalls
- "LSH is a hash table with a clever hash." A single table almost never works; the guarantees come from the AND-OR amplification across many independent tables, which multiplies memory use. Space, not time, is usually the binding constraint.
- "Approximate search returns wrong answers." It returns candidates verified by exact distance; the approximation is that it may miss the true nearest neighbor with small probability or return one slightly farther. For retrieval and deduplication, this is usually indistinguishable in quality from exact search.
- "LSH is what vector databases use." Many modern systems use graph-based indexes (such as HNSW) or quantization, which often perform better in practice on real data; LSH remains the method with the cleanest worst-case guarantees and is common for set similarity with MinHash.
Summary
High-dimensional exact nearest-neighbor search degrades to a linear scan, so the goal becomes approximate search. Locality-sensitive hash families make close points collide with probability and far points with probability : MinHash collides with probability equal to Jaccard similarity, and random hyperplanes with probability . Concatenating hashes and taking tables turns that gap into a sharp threshold near , and the resulting index answers queries in about time with . The next concept turns from searching to optimizing, and from exact answers to provably good ones for NP-hard problems.