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

Universal hashing, beyond the book [see HashFunctions.h for the fixed functions of section 7.1]: a class \( H \) of functions \( U \rightarrow \{ 0 , \ldots , m - 1 \} \) is universal if two distinct keys collide under at most \( | H | / m \) of its functions, and \( c \)-universal if under at most \( c | H | / m \); a function drawn at random from it gives every set of keys the expected lists of length \( 1 + \alpha \) of the simple uniform hashing, \( \alpha = n / m \), and the longest of them is \( O( \log n / \log \log n ) \) in expectation when \( m = \Theta( n ) \), whatever the keys, while for every fixed function there are \( n \) keys that fall in one list. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/Portable.h"
#include "ADS/dictionary/FastHash.h"
#include "ADS/dictionary/RandomHashing.h"
#include "ADS/numeric/NumberTheory.h"
#include "ADS/numeric/RandomNumbers.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iterator>
#include <optional>
#include <span>
#include <string_view>
#include <utility>
#include <vector>
#include "ADS/dictionary/impl/UniversalHashing.tpp"

Go to the source code of this file.

Classes

class  ADS::CarterWegmanHash
 \( h( k ) = ( ( a k + b ) \bmod p ) \bmod m \), of the class \( H_{p,m} \) of Carter and Wegman More...
class  ADS::DotProductHash
 \( h( k ) = \sum_i a_i k_i \bmod m \), the \( k_i \) being the digits of \( k \) in base \( m \) More...
class  ADS::MultiplyShiftHash
 \( h( k ) = ( a k \bmod 2^h ) \operatorname{div} 2^{h - l} \), with \( a \) odd More...
class  ADS::StringHash
 \( h( s ) = \sum_i ( s_i + 1 ) x^i \bmod ( 2^{61} - 1 ) \), at a random point \( x \) More...
class  ADS::MultiplicativeHash< K, Code >
 the multiplicative hashing of section 5.1.1 of Morin's book, \( \mathrm{hash}( x ) = ( z x \bmod 2^w ) \operatorname{div} 2^{w-d} \) on a table of \( 2^d \) places, \( z \) a random odd word and \( x \) the hash code Code( k ) of the key More...
class  ADS::CompoundHash
 the hash code of an object of \( r \) parts of section 5.3.2 of Morin's book, \( h( x_0 , \ldots , x_{r-1} ) = ( ( z \sum_i z_i x_i ) \bmod 2^{2w} ) \operatorname{div} 2^w \) with \( w = 64 \), the \( z_i \) random words and \( z \) a random odd integer of \( 2 w \) bits: two distinct tuples collide with probability at most \( 3 / 2^w \) (Theorem 5.3 of Morin's book), where the XOR or the sum of the parts make large sets of tuples collide (exercises 5.7 to 5.9) More...
class  ADS::SequenceHash
 the hash code of the arrays and of the strings of section 5.3.3 of Morin's book, a polynomial over the prime field of \( p = 2^{61} - 1 \) with an end marker, \( h( x_0 , \ldots , x_{r-1} ) = ( \sum_i x_i z^i + ( p - 1 ) z^r ) \bmod p \) More...
class  ADS::DLeftHashTable< K, V, H >
 a DICTIONARY on \( d \) tables of lists, a key in the shortest of its \( d \) lists More...
class  ADS::DLeftHashTable< K, V, H >::const_iterator
 the iterator on the pairs, table after table and list after list More...

Namespaces

namespace  ADS

Detailed description

Universal hashing, beyond the book [see HashFunctions.h for the fixed functions of section 7.1]: a class \( H \) of functions \( U \rightarrow \{ 0 , \ldots , m - 1 \} \) is universal if two distinct keys collide under at most \( | H | / m \) of its functions, and \( c \)-universal if under at most \( c | H | / m \); a function drawn at random from it gives every set of keys the expected lists of length \( 1 + \alpha \) of the simple uniform hashing, \( \alpha = n / m \), and the longest of them is \( O( \log n / \log \log n ) \) in expectation when \( m = \Theta( n ) \), whatever the keys, while for every fixed function there are \( n \) keys that fall in one list.

The classes are

  • CarterWegmanHash, \( h_{a,b}( k ) = ( ( a k + b ) \bmod p ) \bmod m \) with \( p \) a prime larger than the keys, \( 1 \le a < p \) and \( 0 \le b < p \), universal for every \( m \); with \( p = 2^{61} - 1 \) the product mod \( p \) is made of four products of 32 bits and a few shifts, with no integer of 128 bits [see mulMod61() and mulMod() in NumberTheory.h];
  • DotProductHash, \( h_a( k ) = \sum_i a_i k_i \bmod m \) with \( m \) prime, the key cut into its \( r \) digits \( k_i \) in base \( m \) and \( a \) a vector of \( r \) digits: universal, since for two keys that differ in a digit the collision fixes that digit of \( a \) once the others are chosen;
  • MultiplyShiftHash, the class of Dietzfelbinger, Hagerup, Katajainen and Penttonen, \( h_a( k ) = ( a k \bmod 2^h ) \operatorname{div} 2^{h-l} \) with \( a \) odd, for keys of \( h \) bits and \( m = 2^l \): 2-universal (two keys collide with probability at most \( 2 / m \)), and only a product and a shift, the fastest of the three;
  • StringHash, the polynomial \( \sum_i ( s_i + 1 ) x^i \bmod p \) of the characters of a string at a random point \( x \), \( p = 2^{61} - 1 \), each character counted one more so that the character 0 counts too (the strings of zeros of different lengths, or a string and the same followed by zeros, would collide otherwise): two distinct strings of length at most \( L \) collide with probability at most \( L / ( p - 1 ) \), since their difference is a polynomial of degree at most \( L \), not zero, with at most \( L \) roots among the \( p - 1 \) points \( x \ne 0 \) drawn; it turns a string into a key for the classes above;
  • MultiplicativeHash, the multiplicative hashing of section 5.1.1 of Morin's book, the class above with \( h = 64 \) and \( l \) chosen by the table: its place( k , 2^d ) takes the \( d \) high bits of \( z x \bmod 2^{64} \), so that a table of \( 2^d \) places that grows and shrinks keeps one function [see PlaceHash in TableSize.h];
  • CompoundHash, the hash code of an object of \( r \) parts of section 5.3.2 of Morin's book, a sum of products by random words reduced by a multiplicative hash of 128 bits, two distinct tuples colliding with probability at most \( 3 / 2^{64} \);
  • SequenceHash, the hash code of the arrays and of the strings of section 5.3.3 of Morin's book, the polynomial of StringHash with the end marker \( p - 1 \) in place of the shift of each character by one, so that it takes the words of 64 bits, reduced to 60 bits, and not only the bytes.

Against the code of Morin's book: the words are of 64 bits, where the book has \( w = 32 \), and the prime of SequenceHash is \( 2^{61} - 1 \), whose products are reduced by shifts and masks [see mulMod61()], where the book uses \( 2^{32} - 5 \) and a division; the collision bounds are \( 2^{32} \) times smaller for the same work per word.

DLeftHashTable is the d-left hashing of Vöcking on top of them: \( d \) tables of \( m / d \) lists, each with a function of its own, a key going to the shortest of its \( d \) lists and to the leftmost one on a tie; a search looks at the \( d \) lists. The longest list is then \( \ln \ln n / ( d \ln \phi_d ) + O( 1 ) \) with high probability for \( n = m \) keys, \( \phi_d \) being the ratio of the generalized Fibonacci numbers, against \( \Theta( \log n / \log \log n ) \) with one function: the power of two choices of Azar, Broder, Karlin and Upfal, an exponential gain for one more probe, which lets each list be a bucket of fixed size \( O( \log \log n ) \) instead of a linked list. Its functions are simple tabulation [see TabulationHash in RandomHashing.h], for which Dahlgaard, Knudsen, Rotenberg and Thorup proved a longest list of \( O( \log \log n ) \) with high probability.

The contracts [see Contract.h]: a key below the universe of its class (\( p \) for CarterWegmanHash, \( m^r \) for DotProductHash, \( 2^h \) for MultiplyShiftHash), the parameters of a class in its range (a prime \( p \) or \( m \) is the caller's duty, not checked), each hash below its \( m \); insert() of DLeftHashTable ensures that the key is there and remove() that it is not, and its invariant is that each key is in one of its \( d \) lists and is counted once, checked on the key of the operation only past 1024 keys, so that a long sequence of operations at the level 3 stays linear. The functions are defined in UniversalHashing.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 8, The Dictionary Problem, doi:10.1017/9781009128933
J. L. Carter, M. N. Wegman, Universal classes of hash functions, Journal of Computer and System Sciences 18(2), 143-154, 1979, doi:10.1016/0022-0000(79)90044-8
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, sections 5.1.1 and 5.3
M. Dietzfelbinger, T. Hagerup, J. Katajainen, M. Penttonen, A reliable randomized algorithm for the closest-pair problem, Journal of Algorithms 25(1), 19-51, 1997, doi:10.1006/jagm.1997.0873
Y. Azar, A. Z. Broder, A. R. Karlin, E. Upfal, Balanced allocations, SIAM Journal on Computing 29(1), 180-200, 1999, doi:10.1137/S0097539795288490
B. Vöcking, How asymmetry helps load balancing, Journal of the ACM 50(4), 568-589, 2003, doi:10.1145/792538.792546
S. Dahlgaard, M. B. T. Knudsen, E. Rotenberg, M. Thorup, The power of two choices with simple tabulation, Proceedings of SODA 2016, 1631-1642, 2016, doi:10.1137/1.9781611974331.ch111
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa