Locality Sensitive Hashing
LSH
Data Mining
Algorithms
Hash Functions

How to understand Locality Sensitive Hashing?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Locality Sensitive Hashing (LSH) is a technique designed to perform fast approximate nearest neighbor search in high-dimensional spaces. It is particularly useful in scenarios where data is vast, high-dimensional, and sublinear time complexity is desired. Unlike traditional hashing, where the main objective is to minimize collision for different inputs, LSH aims to maximize the probability of collision for similar items.

Key Concepts and Mechanism

Hash Functions

The ideal LSH function involves creating a family of hash functions where closer items have a higher probability of falling into the same bucket compared to items that are far apart.

  • Hash Family: A family of hash functions H:RdU\mathcal{H}: \mathbb{R}^d \to U.
  • Locality Sensitivity: A hash function hh is (r,c,P1,P2)(r, c, P_1, P_2)-sensitive if for any points p,qp, q:
    • If d(p,q)rd(p, q) \leq r, then Pr[h(p)=h(q)]P1\Pr[h(p) = h(q)] \geq P_1.
    • If d(p,q)crd(p, q) \geq cr, then Pr[h(p)=h(q)]P2\Pr[h(p) = h(q)] \leq P_2, where c>1c > 1, P1>P2P_1 > P_2.

Distance Metrics

Different LSH schemes rely on different types of distance metrics:

  • Hamming Distance: Suitable for binary data.
  • Euclidean Distance: Suitable for continuous data; often uses random hyperplanes.
  • Cosine Similarity: Uses random projection.

LSH Families

  • Random Hyperplane (Cosine Similarity): Projects data points onto random vectors and assigns hash codes based on the sign (positive or negative) of the projection.
    Hash function: h(v)=sign(wv)h(v) = \text{sign}(w \cdot v) where ww is a random vector.
  • Euclidean LSH (Euclidean Distance): Uses random hyperplanes where the probability of collision is aligned with the Euclidean distance.
    Hash function: h(v)=av+bwh(v) = \lfloor \frac{a \cdot v + b}{w} \rfloor where aa is a Gaussian vector, bb is a bias from [0,w][0, w], and ww is the bucket width.
  • MinHash (Jaccard Index): Produces a signature that preserves the similarity between sets represented as binary vectors.

LSH Data Structure

LSH hashing is implemented using hash tables for collision resolution. For high probability of collision among similar items, multiple hash tables are used. The method involves:

  1. Building Hash Tables:
    • Utilize multiple hash functions from the hash family.
    • Construct several hash tables to ensure the same buckets for similar items.
  2. Query Search:
    • Given a query point, compute its hash for each table.
    • Retrieve candidates from buckets and perform nearest neighbor search on these candidates.

Example

Consider a dataset of images represented by feature vectors in R100\mathbb{R}^{100}. Using random hyperplane LSH:

  1. Choose random hyperplanes (vectors) for hashing.
  2. Calculate the hash for each image by evaluating the sign of the dot product with these hyperplanes.
  3. Store these hashes in hash tables.

In querying, evaluate the query image and retrieve similar images from the relevant buckets across hash tables.

Practical Applications

  • Image Retrieval: Efficient searching of visually similar images from a huge database.
  • Document Similarity: Finding documents with similar content in large textual datasets.
  • Bioinformatics: Identifying similar genetic sequences.

Advantages and Limitations

LSH provides significant speed advantages in high-dimensional data by trading off exactitude for speed, but it is not a silver bullet:

  • Advantages:
    • Sublinear time complexity.
    • Easy implementation.
    • Scalable to large datasets.
  • Limitations:
    • Approximate nature may drop very close neighbors.
    • Requires careful selection of hash functions and the number of hash tables.

Conclusion

Locality Sensitive Hashing is a pivotal technique for similarity search in high-dimensional spaces. By employing probabilistic guarantees rather than exact computations, it enhances efficiency in data-heavy applications. To effectively apply LSH, understanding the nature of the data and selecting appropriate hashing families is crucial.

Key Points Summary

AspectDescription
ObjectiveFind approximate nearest neighbors efficiently
Distance MetricsHamming, Euclidean, Cosine
Hash FamiliesRandom Hyperplane, Euclidean LSH, MinHash
Data StructuresMultiple hash tables
ApplicationsImage retrieval, document similarity, bioinformatics
AdvantagesSublinear time complexity, scalability, easy to implement
LimitationsApproximate results, complexity in hash function selection

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.