Minhash
\`Hash\` Functions
Algorithm
Data Science
Set Similarity

How many hash functions are required in a minhash algorithm

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

The MinHash algorithm is a significant technique in estimating the similarity between large sets, primarily used for applications like near-duplicate detection in web crawls and clustering similar documents. A critical component of the MinHash algorithm is the use of hash functions. Understanding the number of hash functions required is vital for balancing accuracy and computational efficiency.

Understanding MinHash

The MinHash algorithm seeks to efficiently approximate the Jaccard similarity between two sets. Jaccard similarity is defined as the size of the intersection divided by the size of the union of the two sets:

J(A,B)=ABAB\text{J}(A, B) = \frac{|A \cap B|}{|A \cup B|}

To compute this similarity without directly comparing elements between sets, MinHash uses random permutations of elements to estimate the probability that a randomly chosen element from the union of the sets is in the intersection.

Role of `Hash` Functions in MinHash

`Hash` functions simulate random permutations for large datasets, as directly computing a permutation would be computationally expensive. The idea is to apply a family of independent hash functions to each element of a set and record the minimum hash value. The basic property used is:

Pr[min(hash(S_A))=min(hash(S_B))]=J(S_A,S_B)\text{Pr}[\min(\text{hash}(S\_A)) = \min(\text{hash}(S\_B))] = \text{J}(S\_A, S\_B)

where SAS_A and SBS_B are the two sets being compared.

Number of `Hash` Functions

The number of hash functions, kk, directly influences the accuracy of the MinHash approximation. Each hash function provides an independent estimate of the Jaccard similarity. The law of large numbers suggests that as the number of hash functions increases, the average of these estimates converges to the actual Jaccard similarity.

Key Factors:Accuracy: More hash functions generally yield a more accurate approximation of the Jaccard similarity. • Computational Cost: More hash functions increase the computation and space required for storing hash values.

Practical Considerations and Example

Example Scenario:

Suppose we are dealing with two web documents with large bag-of-words representations. To find near-duplicates, a threshold for acceptable Jaccard similarity is set to 0.8. A common practical choice is to use 100 to 200 hash functions for a balance of speed and accuracy.

Let's summarize the impact:

AspectEffect of More Hash Functions
AccuracyIncreases; more samples reduce variance, providing a Jaccard estimate closer to the true value. Small variances in similarity are more detectable.
Computation TimeIncreases; involves more computations and memory for hash evaluations Larger demand on processing time and resource utilization.
StorageMore storage is required to maintain the hash signatures for each set.

Advanced Considerations

`Hash` Function Design:

Choosing appropriate hash functions is crucial. `Hash` functions must: • Be independent or weakly dependent. • Distribute elements uniformly to avoid hash collisions.

Universal hash functions or simple hash functions like those based on modular arithmetic are often employed in practice.

Scaling with Approximation Techniques

In large-scale implementations, techniques like Locality Sensitive Hashing (LSH) are often combined with MinHash to further speed up the search by partitioning the hash values in a way that similar items are more likely to map to the same bucket.

By tuning the number of hash functions and considering computational resources, MinHash can provide an efficient balance between precision and performance for large-scale similarity searches.


Understanding the influence of the number of hash functions is a cornerstone for implementing efficient and effective MinHash algorithms suitable for diverse applications in information retrieval, data mining, and beyond.


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.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.