Locality-Sensitive Hashing Explained: The Hash That Keeps Neighbours Together

Locality-sensitive hashing explained in plain terms means understanding the hash that does the opposite of what hashes usually do, a family of functions that puts similar items in the same bucket instead of scattering them. The central question is why a hash would ever want collisions when every other hash tries to avoid them, and the answer is that a collision is a clue, so a hash that collides on purpose turns a search for neighbours into a lookup.

Locality-sensitive hashing, usually shortened to LSH, was introduced by Piotr Indyk and Rajeev Motwani in their 1998 paper Approximate Nearest Neighbors: Towards Removing the Curse of Dimensionality. A family of hash functions is locality-sensitive when close points are likely to share a hash and far points are likely not to, which means a query only needs to check the buckets its own hash points to, and because those buckets hold the neighbours, the search skips the rest of the data.

This article explains what locality-sensitive hashing is, how it works, and where it runs in the real world.

Key takeaways

  • Locality-sensitive hashing collides similar items on purpose.
  • Indyk and Motwani introduced it in 1998.
  • Close points share a bucket, far points usually do not.
  • It answers near-neighbour queries in sublinear time.
  • It powers image search and duplicate detection.

What locality-sensitive hashing is

Locality-sensitive hashing is a framework for finding approximate nearest neighbours in high dimensions, and that approximation is what makes it unusual among search designs. Exact search in hundreds of dimensions is barely faster than scanning everything, which means a system that insists on the exact nearest neighbour pays for it in time, and because an answer that is almost the nearest is good enough in practice, LSH trades a little accuracy for a lot of speed.

Its signature feature is the collision by design. Most hash functions are built to spread inputs evenly, but an LSH family is built so that the chance of two points colliding grows as the distance between them shrinks, which means similar images, similar documents and similar songs land together, and because the collision probability is the signal, the hash is chosen to match the distance being measured.

The scheme was a reaction to the curse of dimensionality. As dimensions grow, every point becomes roughly equally far from every other, so trees and indexes that work in two dimensions stop working, because a search that must visit every branch is a search that has gained nothing, and that practicality is what made locality-sensitive hashing worth building.

It became one of the best-known search methods in the world, and it is named in every discussion of the field. The STOC paper appeared in 1998, the method was refined with MinHash for sets and random hyperplanes for angles, and it is still the first example engineers meet when they learn that a hash can be a similarity detector, which means the scheme survives as a standard even though learned indexes keep arriving.

Our guide to SimHash explains the fingerprint that finds near-duplicate documents with random hyperplanes, and our guide to hashing explains the general hash functions that LSH turns inside out.

How locality-sensitive hashing works

Everything starts with the family, which sets the rule. For a chosen distance, the designer picks a family of hash functions where close points collide often and far points collide rarely, which means bit sampling for Hamming distance, MinHash for Jaccard similarity and random hyperplanes for angles, and because the family matches the metric, the same framework serves many kinds of data.

Each step begins with the AND, which sharpens the signal. Several hash functions are concatenated into one compound key, which means two points share the key only when they collide on every part, and because that pushes the collision chance down for everyone, far pairs almost never survive while close pairs often do.

The OR is what makes the design work as a whole. Many independent tables are built, each with its own compound key, which means a pair of close points only needs to collide in one table to become candidates, and because the tables are independent, the chance of missing a true neighbour falls quickly as tables are added.

The bands are what turn the scheme into a practical system. The compound key is split into bands, and two items become candidates when they agree on a whole band, which means the band count sets the similarity threshold, and because the threshold is tunable, the same index can be strict or generous as the application needs.

The candidate check is what turns the scheme into a correct search. Candidates from the colliding buckets are compared exactly, which means false positives are discarded and only true neighbours are returned, and because the buckets are small, the exact check costs far less than scanning the whole collection.

The result comes from the query, read out as neighbours or nothing. The query hashes itself, gathers the candidates from its buckets and ranks them by true distance, which means the answer is an approximate nearest neighbour with a tunable guarantee, and that verdict is exactly what a search engine needs to deliver quickly.

A dark office with a data scientist checking similar images on a monitor, a laptop and a notebook on the desk
A dark office with a data scientist checking similar images on a monitor, a laptop and a notebook on the desk, shows how locality-sensitive hashing works: similar items take turns landing together, step after step, until neighbours are found.

Comparing locality-sensitive hashing with other search methods

Locality-sensitive hashing is one of several ways to find neighbours in high dimensions, and each was built with a different emphasis. Exact search is the brute-force design built for certainty, KD-trees are the space-partitioning design built for low dimensions, and LSH is the one built to trade accuracy for speed, while each approach trades something for something else.

The table below sets out the main contrasts between the leading approaches.

Aspect LSH Exact Search KD-Tree
Query time Sublinear Linear scan Logarithmic, low dimensions
Accuracy Approximate Exact Exact
Dimensions Hundreds Any, slowly Few
Memory Many tables Data only Tree only
Best for Similar items Small data Low dimensions

The practical result is that LSH is the right choice for similar items, exact search suits small data, and KD-trees fit low dimensions instead.

Where locality-sensitive hashing is used

Image search uses it to find lookalikes. A photo service hashes image fingerprints so that near-duplicates share a bucket, because a user who uploads a photo wants the matching ones, and that matching is the reason LSH earns its keep in media libraries.

Duplicate detection uses it to spot copies. Search engines hash web pages so that pages sharing most of their text land together, because a copy that differs by a few words should still be caught, and that tolerance is the reason the design was built for the web.

Recommendation uses it to find similar users. A service that suggests films or songs hashes taste profiles, because users with similar tastes should share a bucket, and that grouping is the reason LSH appears in recommenders far beyond its search origins.

Genomics uses it to match sequences. DNA reads are hashed so that similar fragments collide, because a genome too large to scan needs a shortcut, and that speed shapes how new sequence tools are configured.

Teaching uses it as the similarity hash example. Courses introduce LSH when they explain that a hash can measure closeness, because it is the clearest case of collisions as a feature, and a student who understands LSH understands why similarity is a search problem.

Strengths and limits

The greatest strength of locality-sensitive hashing is its speed at scale. A query that checks a few buckets instead of a whole collection is a rare achievement in high dimensions, and that strength is real, measured in the services that rely on it and confirmed by the indexes that still use it.

It is also a strength that the method is tunable. The band and table counts set the trade between recall and speed, and that control is worth more than cleverness in a field where a search that cannot be tuned is a search that cannot be shipped.

The limits are real, though, and the biggest is the approximation. LSH can miss a true neighbour, which means a system that needs an exact answer must check more or fall back to a scan, and a design that needs guarantees must accept the extra cost.

Locality-sensitive hashing checklist

  • Use LSH for similar items, never for exact lookups.
  • Remember Indyk and Motwani introduced it in 1998.
  • Note collisions are the signal, not the problem.
  • Compare it with exact search to see speed versus certainty.
  • Study the bands, which are what set the threshold.
  • Check the family, since the hash must match the distance.
  • Keep the tables independent, since variety catches misses.
  • Read the 1998 paper to see how the method was set.
  • Choose LSH when neighbours matter more than perfection.

Frequently asked questions

What is locality-sensitive hashing in simple terms?

It is a hashing method where similar items are likely to land in the same bucket, so finding neighbours becomes a matter of checking a few buckets.

Who created locality-sensitive hashing?

Piotr Indyk and Rajeev Motwani, who introduced it in their 1998 paper on approximate nearest neighbours.

How is LSH different from normal hashing?

Normal hashing spreads similar inputs apart to avoid collisions, while LSH is designed so similar inputs collide and far inputs usually do not.

Is LSH still secure?

It was never meant for security, and its hashes reveal similarity, so it should only be used for search and retrieval tasks.

Where is LSH used?

In image search, in duplicate detection, in recommendation systems, and in genomics.

When should I use LSH?

When you search high-dimensional data, when approximate answers are enough, or when you study similarity search.

The bottom line

Locality-sensitive hashing explained in one line is this: collisions with a purpose, a 1998 framework that turned a hash into a similarity detector and taught the field that the right collision is a shortcut, not a bug.

It is proof that a hash can measure closeness as well as identity, and the reason it still matters is that every similarity search designed since has been judged partly by the lesson LSH taught.

This article is general information about how locality-sensitive hashing works, not advice about choosing a search method for a particular system or workload.

Latest articles

Scroll to Top