An O(n log n) Algorithm for Finding Minimal Perfect Hash Functions

Edward A. Fox, Lenwood S. Heath, Qi ChenVTechWorks 1989

Abstract

We describe the first practical algorithm for finding minimal perfect hash functions (MPHFs) that can be used to access very large databases. This method extends earlier work by improving the expected time complexity from $O(n^3)$ to $O(n \log n)$. Our algorithm makes use of three key insights: applying randomness wherever possible, ordering our search for hash functions based on the degree of the vertices in a dependency graph, and viewing hash value assignment in terms of adding circular patterns of related words to a partially filled disk. Tests with datasets exceeding 1.2 million keys demonstrate the algorithm's scalability and efficiency for modern data access needs.

Introduction

In computer science, a hash function maps keys (such as words or identifiers) to array indices. While standard hashing often results in "collisions"—where two keys map to the same index—a perfect hash function eliminates these collisions entirely, allowing for $O(1)$ access time. A minimal perfect hash function (MPHF) goes a step further by mapping $n$ keys to exactly $n$ slots, ensuring zero wasted space.

Historically, finding MPHFs for large sets was computationally prohibitive. Previous algorithms, such as those by Sager, operated at $O(n^4)$ or $O(n^3)$ complexity, making them unsuitable for the massive static datasets emerging in the late 1980s. This paper presents a breakthrough $O(n \log n)$ expected-time algorithm that makes indexing millions of keys a practical reality.

The MOS Framework

The authors utilize a three-step framework known as MOS: Mapping, Ordering, and Searching. This structured approach allows the algorithm to handle the constraints of minimal perfect hashing by breaking the problem into manageable phases.

The three-step MOS framework: Mapping, Ordering, and Searching.

1. Mapping

The first phase involves using three random auxiliary hash functions ($h_0, h_1, h_2$) to map each key into a triple of integers. These triples are used to construct a dependency graph—a bipartite graph where edges represent keys and vertices represent possible hash components. This graph captures the relationships and potential conflicts between keys.

2. Ordering

A critical innovation of this algorithm is the ordering of vertices based on their degree in the dependency graph. The authors observed that the degree distribution is highly skewed; a few "popular" vertices have many connections, while most have few. By using a heuristic similar to Prim’s algorithm to process high-degree vertices first, the algorithm tackles the most constrained parts of the problem while the search space is still open.

3. Searching

In the final phase, the hash table is viewed as a circular "disk." Assigning hash values becomes a process of "fitting" patterns of related words into empty slots on this disk. Because the keys were ordered by dependency, the most difficult patterns are placed first. As the disk fills, the remaining low-degree keys—which are easier to fit—are slotted into the remaining gaps. If a set of triples proves too difficult to fit, the algorithm simply "rolls the dice" with new random functions and restarts the process.

Experimental Results and Scalability

The algorithm was stress-tested on several significant real-world datasets to prove its practical utility:

  • Collins English Dictionary: 130,198 words.
  • French Word List: 420,878 entries.
  • Library Catalog: Over 1.2 million keys.

The performance gains were dramatic. For a set of 1,000 keys, where older algorithms took over 5 hours, the new algorithm completed the Ordering step in just 0.63 seconds. Furthermore, the resulting hash functions require very little storage—roughly a few computer words per key—making them ideal for memory-constrained environments like the CD-ROM systems of the era.

Conclusion

By shifting the complexity of finding minimal perfect hash functions to $O(n \log n)$, this research transformed a theoretical curiosity into a vital tool for large-scale data management. While designed for static datasets, its efficiency in construction and $O(1)$ lookup performance made it a cornerstone for early large-scale semantic networks and hypertext collections, such as the Virginia Disc One project.

Summary updated

Abstract

We describe the first practical algorithm for finding minimal perfect hash functions that can be used to access very large databases. This method extends earlier work wherein an O(n3) algorithm was devised, building upon prior work by Sager that described an O(n4) algorithm. Our new O(n log n) expected time algorithm makes use of three key insights: applying randomness whereever possible, ordering our search for hash functions based on the degree of the vertices in a graph that represents word dependencies, and viewing hash value assignment in terms of adding circular patterns of related words to a partially filled disk. While ultimately applicable to a wide variety of data and file access needs, this algorithm has already proven useful in aiding our work in improving performance of CD-ROM systems and our construction of a Large External Network Database (LEND) for semantic networks and hypertext/hypermedia collections. Virginia Disc One includes a demonstration of a minimal perfect hash function running on a PC to access a 130,198 word list on that CD-ROM, and several other microcomputer, minicomputer, and parallel processor versions and applications of our algorithm have also been developed. Tests with a French word list of 420,878 entries and a library catalog key set with over 1.2 million keys have shown that our methods work with very large databases.
BibTeX
@inproceedings{fox1989o,
  author = {Edward A. Fox and Lenwood S. Heath and Qi Chen},
  title = {{An O(n log n) Algorithm for Finding Minimal Perfect Hash Functions}},
  booktitle = {VTechWorks},
  year = {1989},
}
Figure