ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PerfectHashing.h File Reference

Perfect hashing of a static set, beyond the book: a function is perfect on a set \( S \) of \( n \) keys if it has no collision on \( S \), minimal if its range is \( \{ 0 , \ldots , n - 1 \} \), and order preserving if it maps the keys in their order; a table built on it searches in \( O( 1 ) \) time in the worst case, as the table with direct access [see DirectTable.h], but in \( O( n ) \) space instead of the size of the universe. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/dictionary/RandomHashing.h"
#include "ADS/dictionary/UniversalHashing.h"
#include "ADS/external/MappedFile.h"
#include <algorithm>
#include <cmath>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <stdexcept>
#include <string_view>
#include <vector>
#include "ADS/dictionary/impl/PerfectHashing.tpp"

Go to the source code of this file.

Classes

class  ADS::PerfectHashTable
 a static SET of integers on the two-level perfect hashing More...
struct  ADS::PerfectHashTable::Places
 the places of the second level, for SlotIterator More...
class  ADS::OrderedMinimalPerfectHash< K >
 the function of Czech, Havas and Majewski that maps \( \mathit{keys}[ i ] \) to \( i \) More...

Namespaces

namespace  ADS

Detailed description

Perfect hashing of a static set, beyond the book: a function is perfect on a set \( S \) of \( n \) keys if it has no collision on \( S \), minimal if its range is \( \{ 0 , \ldots , n - 1 \} \), and order preserving if it maps the keys in their order; a table built on it searches in \( O( 1 ) \) time in the worst case, as the table with direct access [see DirectTable.h], but in \( O( n ) \) space instead of the size of the universe.

  • PerfectHashTable is the two-level scheme of Fredman, Komlós and Szemerédi: the \( n \) keys are hashed into \( n \) lists by a function \( h \) of the class of Carter and Wegman [see CarterWegmanHash], drawn again until \( L = \sum_j n_j^2 < 4 n \), \( n_j \) being the keys of the list \( j \) (the expected \( L \) is below \( 2 n \), since \( \sum_j \binom{n_j}{2} \) counts the collisions of \( h \), so that by the inequality of Markov on \( L - n \ge 0 \), of expectation below \( n \), \( L \ge 4 n \) with probability below \( 1 / 3 \) and \( h \) is drawn fewer than \( 3 / 2 \) times on average); the list \( j \) becomes a table of \( n_j^2 \) places with a function \( h_j \) of its own, drawn again until it has no collision, which happens with probability at least \( 1 / 2 \) since the expected collisions of \( n_j \) keys in \( n_j^2 \) places are below \( 1 / 2 \). A search computes \( h \) and \( h_j \) and compares one key: two accesses in the worst case, \( O( n ) \) expected time to build, below \( 5 n \) places in all. Its dynamic version, by Dietzfelbinger et al., also inserts and removes in \( O( 1 ) \) amortized expected time; the compressed schemes of today (CHD of Belazzougui, Botelho and Dietzfelbinger, RecSplit of Esposito, Mueller Graf and Vigna) need about 2 and 1.6 bits per key instead of words.
  • OrderedMinimalPerfectHash is the scheme of Czech, Havas and Majewski: \( r = 2 \) functions map each key \( t \) to the edge \( ( h_1( t ) , h_2( t ) ) \) of a random graph of \( m' = c n \) vertices, labeled by the value wanted for \( t \) (its place in the input, i.e., its rank if the input is sorted); if the graph is acyclic, a visit of each tree from a root with \( g = 0 \) solves \( g( u ) + g( v ) = h( t ) \bmod n \) on each edge (labelAcyclicGraph()), and \( h( t ) = ( g( h_1( t ) ) + g( h_2( t ) ) ) \bmod n \) is the function. For \( c > 2 \) the graph is acyclic with probability about \( \sqrt{( c - 2 ) / c} \), i.e., about two graphs for \( c = 3 \); with \( r = 3 \) functions into three blocks of vertices the edges are triples, the hypergraph is solved in the reverse order of its peeling (the removal of the vertices of degree 1, labelByPeeling()), and \( c > 1.23 \) is enough, i.e., \( 1.23 n \) values instead of \( 3 n \). It stores no key: \( O( m' ) \) words, \( O( 1 ) \) time to evaluate, \( O( n ) \) expected time to build; unlike a trie [see PatriciaTrie.h] it gives nothing meaningful for a key out of \( S \), but its space does not depend on the length of the keys.

The keys of PerfectHashTable are integers below \( 2^{61} - 1 \), the prime of its functions; those of OrderedMinimalPerfectHash are integers or strings, turned into integers by StringHash [see UniversalHashing.h] or splitmix64() before the functions of Carter and Wegman.

The contracts [see Contract.h]: the keys distinct (an audit), below \( 2^{61} - 1 \) for PerfectHashTable and fewer than \( 2^{32} \) for OrderedMinimalPerfectHash, \( r = 2 \) with \( c > 2 \) or \( r = 3 \) with \( c > 1.23 \), the vertices of an edge distinct for labelByPeeling(); after a build each key is found, or mapped to its value, an audit; a labeling satisfies every edge, a postcondition. A build that draws 1000 functions of the first level, or 1000 graphs, with no success throws std::runtime_error, which for distinct keys has a probability below \( 3^{-1000} \) for the first level and, for \( r = 2 \) and \( c = 3 \), one that tends to \( ( 1 - 1 / \sqrt{3} )^{1000} \) for the graphs. The functions are defined in PerfectHashing.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 8, The Dictionary Problem, doi:10.1017/9781009128933
M. L. Fredman, J. Komlós, E. Szemerédi, Storing a sparse table with O(1) worst case access time, Journal of the ACM 31(3), 538-544, 1984, doi:10.1145/828.1884
Z. J. Czech, G. Havas, B. S. Majewski, An optimal algorithm for generating minimal perfect hash functions, Information Processing Letters 43(5), 257-264, 1992, doi:10.1016/0020-0190(92)90220-P
B. S. Majewski, N. C. Wormald, G. Havas, Z. J. Czech, A family of perfect hashing methods, The Computer Journal 39(6), 547-554, 1996, doi:10.1093/comjnl/39.6.547
M. Dietzfelbinger, A. Karlin, K. Mehlhorn, F. Meyer auf der Heide, H. Rohnert, R. E. Tarjan, Dynamic perfect hashing: upper and lower bounds, SIAM Journal on Computing 23(4), 738-761, 1994, doi:10.1137/S0097539791194094
D. Belazzougui, F. C. Botelho, M. Dietzfelbinger, Hash, displace, and compress, Proceedings of ESA 2009, LNCS 5757, 682-693, 2009, doi:10.1007/978-3-642-04128-0_61
E. Esposito, T. Mueller Graf, S. Vigna, RecSplit: minimal perfect hashing via recursive splitting, Proceedings of ALENEX 2020, 175-185, 2020, doi:10.1137/1.9781611976007.14
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa