WIPIVERSE All articles

Dynamic perfect hashing

Dynamic perfect hashing is a hashing scheme that maintains a dictionary (a set of keys) while supporting constant‑time worst‑case look‑ups and expected constant‑time insertions and deletions. Unlike static perfect hashing, which constructs a collision‑free hash function for a fixed set of keys, the dynamic variant adapts the hash structure as keys are added or removed.

Core Idea

The technique combines two levels of hashing:

  1. Top‑level hash table – A primary table of size m (often Θ(n), where n is the current number of stored keys) indexed by a universal hash function h₁. Each entry of this table points to a secondary structure.

  2. Secondary hash tables – For each bucket i of the top level, a secondary hash table of size sᵢ stores the keys that hash to i. The secondary table uses a second universal hash function h₂,i. The size sᵢ is chosen (typically sᵢ = c·(nᵢ)², where nᵢ is the number of keys in bucket i and c is a constant) so that with high probability h₂,i yields a collision‑free arrangement of the keys in that bucket.

Because each secondary table is sized quadratically in the number of its keys, the probability that a randomly chosen h₂,i produces any collision is less than 1/2. If a collision occurs, the secondary hash function is re‑chosen and the table rebuilt. The expected total space over all secondary tables is O(n), and look‑ups require evaluating h₁ and then h₂,i, yielding O(1) worst‑case time.

Dynamization

To support updates, the structure monitors the load factor of the top‑level table. When insertions cause the total number of keys n to exceed a predetermined threshold (e.g., a constant factor of the current table size), the entire structure is rehashed:

  • A new top‑level hash function h₁′ and new table size m′ (usually 2·m) are selected.
  • All keys are reinserted, constructing fresh secondary tables as described above.

Deletion simply removes the key from its secondary table; if a secondary table becomes sufficiently sparse, it may be rebuilt with a smaller size to reclaim space.

The amortized cost of rehashing is O(1) per operation, while each individual operation (lookup, insert, delete) takes O(1) worst‑case time when the hash functions are assumed to be evaluated in constant time.

Historical Development

  • The static version of perfect hashing was introduced by Fredman, Komlós, and Szemerédi (1984) using O(n) space and O(1) worst‑case look‑ups.
  • The dynamic extension, often referred to as “dynamic perfect hashing,” was presented by Dietzfelbinger, Hagerup, and Meyer (1994) and later refined by others (e.g., Pagh & Rodler, 2001). These works showed that the same space bound (Θ(n)) and constant‑time guarantees could be retained under insertions and deletions, with expected amortized update cost.

Performance Summary

Operation Time Complexity (worst case) Expected Amortized Cost
Lookup O(1) O(1)
Insert O(1) (after possible rebuild) O(1)
Delete O(1) O(1)
Space Θ(n) Θ(n)

The constants hidden in the Θ notation depend on the choice of universal hash families and the factor c used for secondary table sizing.

Applications

Dynamic perfect hashing is useful in situations where deterministic constant‑time queries are required and the key set changes over time, such as:

  • Symbol tables in compilers where rapid identifier lookup is critical.
  • Network routers and caches that need fast membership tests for dynamically updating sets of addresses.
  • Algorithmic implementations that require dictionary operations with strict worst‑case guarantees (e.g., certain randomized algorithms that must avoid pathological cases).

Practical Considerations

  • Hash function choice – In practice, simple 2‑universal or 4‑universal families (e.g., multiply‑shift) are employed because they are easy to compute and satisfy the necessary probabilistic bounds.
  • Memory overhead – Although the expected space is linear, the quadratic sizing of secondary tables can cause temporary spikes in memory usage during rebuilds.
  • Parallelism – The independent nature of secondary tables makes the structure amenable to parallel construction and updates on modern multicore processors.

References

  1. Fredman, M. L.; Komlós, J.; Szemerédi, E. (1984). “Storing a Sparse Table with O(1) Worst Case Access Time.” Journal of the ACM, 31(3), 538–544.
  2. Dietzfelbinger, M.; Hagerup, T.; Meyer, U. (1994). “Dynamic Perfect Hashing: Upper Bounds.” Proc. 35th Annual Symposium on Foundations of Computer Science (FOCS), 361–368.
  3. Pagh, R.; Rodler, F. (2001). “Cuckoo Hashing.” Journal of Algorithms, 51(2), 122–144. (Provides related techniques for dynamic hashing with constant‑time operations.)
  4. Cormen, T. H.; Leiserson, C. E.; Rivest, R. L.; Stein, C. (2022). Introduction to Algorithms (4th ed.). MIT Press. – Section on perfect hashing and its extensions.

Dynamic perfect hashing remains a fundamental construction in the theory of data structures, demonstrating that both worst‑case query time and expected amortized update time can be simultaneously optimized while using linear space.

Browse

More topics to explore

    Browse all articles