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.
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:
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:
where and are the two sets being compared.
Number of `Hash` Functions
The number of hash functions, , 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:
| Aspect | Effect of More Hash Functions |
| Accuracy | Increases; more samples reduce variance, providing a Jaccard estimate closer to the true value. Small variances in similarity are more detectable. |
| Computation Time | Increases; involves more computations and memory for hash evaluations Larger demand on processing time and resource utilization. |
| Storage | More 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
- How many kinds of Distance Function can we use?
- How many principal components to take?
- How shall we read the Kafka topics in a given time range?
- How should I handle input data with nan values in TensorFlow?
- How many hash functions does my bloom filter need?
- How many palindromes can be formed by selections of characters from a string?
- How many traversals need to be known to construct a BST
- How many ways can you insert a series of values into a BST to form a specific tree?

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 courseTrack 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.