7.7 Hashing 7.7 Hashing

Dictionary: Definitions: ñ S.insert(x): Insert an element x. ñ Universe U of keys, e.g., U N0. U very large. ñ S.delete(x): Delete the element pointed to by x. ⊆ ñ Set S U of keys, S m n. ñ S.search(k): Return a pointer to an element e with ⊆ | | = ≤ key[e] k in S if it exists; otherwise return null. ñ Array T[0, . . . , n 1] hash-table. = − ñ Hash function h : U [0, . . . , n 1]. → − So far we have implemented the search for a key by carefully choosing split-elements. The hash-function h should fulfill: ñ Fast to evaluate. Then the memory location of an object x with key k is determined ñ Small storage requirement. by successively comparing k to split-elements. ñ Good distribution of elements over the whole table. Hashing tries to directly compute the memory location from the given key. The goal is to have constant search time.

EADS EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 201 c Ernst Mayr, Harald Räcke 202

7.7 Hashing 7.7 Hashing

Ideally the hash function maps all keys to different memory Suppose that we know the set S of actual keys (no insert/no locations. delete). Then we may want to design a simple hash-function that maps all these keys to different memory locations. k1 œ k1 k1 k7 œ k7 k1 U œ k7 universe k6 k3 x k7 of keys œ U œ universe k6 k3 x k3 of keys œ

k6 S (actual keys) k3

œ k6 œ This special case is known as Direct Addressing. It is usually very unrealistic as the universe of keys typically is quite large, and in Such a hash function h is called a perfect hash function for set S. particular larger than the available memory.

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 203 c Ernst Mayr, Harald Räcke 204 7.7 Hashing 7.7 Hashing

Typically, collisions do not appear once the size of the set S of actual keys gets close to n, but already once S ω(√n). If we do not know the keys in advance, the best we can hope for | | ≥ is that the hash function distributes keys evenly across the table. Lemma 21 The probability of having a collision when hashing m elements Problem: Collisions into a table of size n under uniform hashing is at least Usually the universe U is much larger than the table-size n. m(m 1) m2 − 1 e− 2 1 e− 2n . − ≈ − Hence, there may be two elements k1, k2 from the set S that map to the same memory location (i.e., h(k1) h(k2)). This is called a = collision. Uniform hashing: Choose a hash function uniformly at random from all functions f : U [0, . . . , n 1]. → −

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 205 c Ernst Mayr, Harald Räcke 206

7.7 Hashing f (x)

Proof.

Let Am,n denote the event that inserting m keys into a table of size n does not generate a collision. Then

m m 1 Y n ` 1 Y−  j  Pr[Am,n] − + 1 = n = − n ` 1 j 0 = = m 1 Y− Pm 1 j m(m 1) j/n j −0 n 2 − e− e− = e− n . ≤ = = j 0 = x fg(x) (x) 1e− x x Here the first equality follows since the `-th element that is = − n ` 1 hashed has a probability of − + to not generate a collision n under the condition that the previous elements did not induce

collisions. x The inequality 1 x e− is derived by stopping the − ≤x tayler-expansion of e− after the second term.

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 207 c Ernst Mayr, Harald Räcke 208 Resolving Collisions Hashing with Chaining

Arrange elements that map to the same position in a linear list. ñ Access: compute h(x) and search list for key[x]. ñ Insert: insert at the front of the list. The methods for dealing with collisions can be classified into the two main types k1 k4 œ ñ open addressing, aka. closed hashing œ k1 ñ hashing with chaining. aka. closed addressing, open k4 k5 k5 k2 k7 œ k7 hashing. U k2 œ universe k6 k3 k8 x of keys œ

S (actual keys) k3 œ

k8 k6 œ œ

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 209 c Ernst Mayr, Harald Räcke 210

7.7 Hashing Hashing with Chaining

Let A denote a strategy for resolving collisions. We use the following notation: The time required for an unsuccessful search is 1 plus the length of the list that is examined. The average length of a list is α m . ñ A+ denotes the average time for a successful search when = n using A; Hence, if A is the collision resolving strategy “Hashing with Chaining” we have ñ A− denotes the average time for an unsuccessful search A− 1 α . when using A; = + ñ We parameterize the complexity results in terms of α : m , = n the so-called fill factor of the hash-table. Note that this result does not depend on the hash-function that is used. We assume uniform hashing for the following analysis.

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 211 c Ernst Mayr, Harald Räcke 212 Hashing with Chaining Hashing with Chaining

For a successful search observe that we do not choose a list at random, but we consider a random key in the hash-table and k  m  m  m  m  1 X X 1 X X   ask for the search-time for k. E 1 Xij 1 E Xij m + = m + i 1 j i 1 i 1 j i 1 = = + = = + This is 1 plus the number of elements that lie before k in k’s list. 1 Xm  Xm 1  1 = m + n Let k` denote the `-th key inserted into the table. i 1 j i 1 = = + 1 Xm Let for two keys ki and kj, Xij denote the event that i and j hash 1 (m i) = + mn − to the same position. Clearly, Pr[X 1] 1/n for uniform i 1 ij = = = hashing. 1  m(m 1) 1 m2 + = + mn − 2 The expected successful search cost is m 1 α α keys before ki 1 − 1 . m m = + 2n = + 2 − 2m  1 X  X  E 1 Xij m + Hence, the expected cost for a successful search is A 1 α . i 1 j i 1 + ≤ + 2 = = + cost for key ki

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 213 c Ernst Mayr, Harald Räcke 214

Open Addressing Open Addressing

All objects are stored in the table itself.

Define a function h(k, j) that determines the table-position to be Choices for h(k, j): examined in the -th step. The values 0 ,. . . , 1 form j h(k, ) h(k, n ) ñ h(k, i) h(k) i mod n. . − = + a permutation of 0,..., n 1. 2 − ñ h(k, i) h(k) c1i c2i mod n. Quadratic probing. = + + Search : Try position 0 ; if it is empty your search fails; ñ h(k, i) h1(k) ih2(k) mod n. . (k) h(k, ) = + otw. continue with h(k, 1), h(k, 2),.... For quadratic probing and double hashing one has to ensure that Insert(x): Search until you find an empty slot; insert your the search covers all positions in the table (i.e., for double

element there. If your search reaches h(k, n 1), and this slot is hashing h2(k) must be relatively prime to n; for quadratic − non-empty then your table is full. probing c1 and c2 have to be chosen carefully).

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 215 c Ernst Mayr, Harald Räcke 216 Linear Probing Quadratic Probing

ñ Advantage: Cache-efficiency. The new probe position is very ñ Not as cache-efficient as Linear Probing.

likely to be in the cache. ñ Secondary clustering: caused by the fact that all keys ñ Disadvantage: . Long sequences of mapped to the same position have the same probe sequence. occupied table-positions get longer as they have a larger probability to be hit. Furthermore, they can merge forming larger sequences. Lemma 23 Let Q be the method of quadratic probing for resolving collisions:

Lemma 22  1  α Q+ 1 ln ≈ + 1 α − 2 Let L be the method of linear probing for resolving collisions: − 1 1  1  1  L+ 1 Q− ln α ≈ 2 + 1 α ≈ 1 α + 1 α − − − − 1 1  L− 1 ≈ 2 + (1 α)2 −

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 217 c Ernst Mayr, Harald Räcke 218

Double Hashing 7.7 Hashing

ñ Any probe into the hash-table usually creates a cash-miss.

Lemma 24 Some values: Let A be the method of double hashing for resolving collisions: α Linear Probing Quadratic Probing Double Hashing 1  1  L+ L− Q+ Q− D+ D− D+ ln ≈ α 1 α 0.5 1.5 2.5 1.44 2.19 1.39 2 − 0.9 5.5 50.5 2.85 11.40 2.55 10 1 0.95 10.5 200.5 3.52 22.05 3.15 20 D− ≈ 1 α −

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 219 c Ernst Mayr, Harald Räcke 220 i Pr[X i] =

7.7 Hashing Analysis of Idealized Open Address Hashing

#probes Let X denote a random variable describing the number of probes in an unsuccessful search.

10 Let Ai denote the event that the i-th probe occurs and is to a non-empty slot.

Pr[A1 A2 A ] ∩ ∩ · · · ∩ i1 Pr[A1] Pr[A2 A1] Pr[A3 A1 A2] = · | · | ∩ · 5 ... Pr[Ai1 A1 Ai 2] · | ∩ · · · ∩ −

m m 1 m 2 m i 2 Pr[X i] − − ... − + ≥ = n · n 1 · n 2 · · n i 2 − − − + 0 LD−+−+ α  i 1 m − i 1 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 α − . ≤ n =

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 221 c Ernst Mayr, Harald Räcke 222

Analysis of Idealized Open Address Hashing i = 3

Pr[X i] =

X X i Pr[X i] Pr[X i] i = = i ≥ ∞ ∞ ∞ X X i 1 X i 1 E[X] Pr[X i] α − α . = ≥ ≤ = = 1 α i 1 i 1 i 0 = = = −

1 1 α α2 α3 ... 1 α = + + + + i − 1 2 3 4 5 6 7

The j-th rectangle appears in both sums j times. (j times in the first due to multiplication with j; and j times in the second for summands i 1, 2, . . . , j) = EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 223 c Ernst Mayr, Harald Räcke 224 Pr[X i] ≥

i = 4 Analysis of Idealized Open Address Hashing

Pr[X i] = The number of probes in a successful for k is equal to the number of probes made in an unsuccessful search for k at the time that k is inserted. X X i Pr[X i] Pr[X i] i = = i ≥ Let k be the i 1-st element. The expected time for a search for k + is at most 1 n . 1 i/n n i − = −

m 1 m 1 n 1 X− n n X− 1 1 X 1 i m n i = m n i = α k 1 2 3 4 5 6 7 i 0 i 0 k n m 1 = − = − = − +

1 Z n 1 1 n 1 1 The j-th rectangle appears in both sums j times. (j times in the dx ln ln . ≤ α n m x = α n m = α 1 α first due to multiplication with j; and j times in the second for − − − summands i 1, 2, . . . , j) = EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 224 c Ernst Mayr, Harald Räcke 225

7.7 Hashing

f (x)

How do we delete in a hash-table?

Xn 1 Z n 1 ñ For hashing with chaining this is not a problem. Simply dx k ≤ m n x k m n 1 − search for the key, and delete the item in the corresponding = − + list.

ñ For open addressing this is difficult. 1 f (x) x 1 1 1 = m n 1 m n 2 n − + − + ··· x m n m n 1 n − − +

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 226 c Ernst Mayr, Harald Räcke 227 7.7 Hashing 7.7 Hashing

Regardless, of the choice of hash-function there is always an input Definition 25 (a set of keys) that has a very poor worst-case behaviour. A class of hash-functions from the universe U into the set H 0, . . . , n 1 is called universal if for all u1, u2 U with u1 ≠ u2 Therefore, so far we assumed that the hash-function is random so { − } ∈ that regardless of the input the average case behaviour is good. 1 Pr[h(u1) h(u2)] , = ≤ n However, the assumption of uniform hashing that h is chosen where the probability is w. r. t. the choice of a random randomly from all functions f : U [0, . . . , n 1] is clearly U → − hash-function from set . unrealistic as there are n| | such functions. Even writing down H

such a function would take U log n bits. 1 | | Note that this means that Pr[h(u1) h(u2)] . = = n Universal hashing tries to define a set of functions that is H much smaller but still leads to good average case behaviour when selecting a hash-function uniformly at random from . H

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 228 c Ernst Mayr, Harald Räcke 229

7.7 Hashing 7.7 Hashing

Definition 26 Definition 27 A class of hash-functions from the universe U into the set A class of hash-functions from the universe U into the set H H 0, . . . , n 1 is called 2-independent (pairwise independent) if 0, . . . , n 1 is called k-independent if for any choice of ` k { − } { − } ≤ the following two conditions hold distinct keys u1, . . . , u` U, and for any set of ` not necessarily 1 ∈ ñ For any key u U, and t 0, . . . , n 1 Pr[h(u) t] , distinct hash-positions t1, . . . , t`: ∈ ∈ { − } = = n i.e., a key is distributed uniformly within the hash-table. 1 Pr[h(u1) t1 h(u ) t ] , ñ For all u1, u2 U with u1 ≠ u2, and for any two = ∧ · · · ∧ ` = ` ≤ ` ∈ n hash-positions t1, t2: where the probability is w. r. t. the choice of a random 1 hash-function from set . Pr[h(u1) t1 h(u2) t2] . H = ∧ = ≤ n2 Note that the probability is w. r. t. the choice of a random hash-function from set . H This requirement clearly implies a universal hash-function.

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 230 c Ernst Mayr, Harald Räcke 231 7.7 Hashing 7.7 Hashing

Let U : 0, . . . , p 1 for a prime p. Let Z : 0, . . . , p 1 , and Definition 28 = { − } p = { − } let Z : 1, . . . , p 1 denote the set of invertible elements in Z . A class of hash-functions from the universe U into the set p∗ = { − } p H 0, . . . , n 1 is called (µ, k)-independent if for any choice of { − } Define ` k distinct keys u1, . . . , u` U, and for any set of ` not ≤ ∈ ha,b(x) : (ax b mod p) mod n necessarily distinct hash-positions t1, . . . , t`: = + Lemma 29  µ ` Pr[h(u1) t1 h(u ) t ] ) , = ∧ · · · ∧ ` = ` ≤ n The class h a Z∗, b Z where the probability is w. r. t. the choice of a random H = { a,b | ∈ p ∈ p} hash-function from set . is a universal class of hash-functions from U to 0, . . . , n 1 . H { − }

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 232 c Ernst Mayr, Harald Räcke 233

7.7 Hashing ñ The hash-function does not generate collisions before the (mod n)-operation. Furthermore, every choice (a, b) is Proof. mapped to different hash-values t : h (x) and x = a,b Let x, y U be two distinct keys. We have to show that the t : h (y). ∈ y = a,b probability of a collision is only 1/n. This holds because we can compute a and b when given tx ñ ax + b 6≡ ay + b (mod p) and ty : t ax b (mod p) If x ≠ y then (x y) 0 (mod p). x ≡ + − 6≡ t ay b (mod p) y ≡ + Multiplying with a 0 (mod p) gives 6≡ t t a(x y) (mod p) x − y ≡ − a(x y) 0 (mod p) − 6≡ ty ay b (mod p) ≡ + ˇ where we use that Zp is a field (KÃC½urper)and, hence, has 1 a (t t )(x y)− (mod p) no zero divisors (nullteilerfrei). ≡ x − y − b ay t (mod p) ≡ − y

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 234 c Ernst Mayr, Harald Räcke 235 7.7 Hashing 7.7 Hashing

There is a one-to-one correspondence between hash-functions As ty ≠ tx there are (pairs (a, b), a ≠ 0) and pairs (tx, ty ), tx ≠ ty . l p m p n 1 p 1 1 − 1 − − ≤ + − ≤ Therefore, we can view the first step (before the (modn)- n n n n

operation) as choosing a pair (tx, ty ), tx ≠ ty uniformly at random. possibilities for choosing ty such that the final hash-value creates a collision. What happens when we do the (modn) operation? This happens with probability at most 1 . n Fix a value t . There are p 1 possible values for choosing t . x − y

From the range 0, . . . , p 1 the values t , t n, t 2n, . . . map − x x + x + to t after the modulo-operation. These are at most p/n values. x d e

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 236 c Ernst Mayr, Harald Räcke 237

7.7 Hashing Perfect Hashing

Suppose that we know the set S of actual keys (no insert/no It is also possible to show that is an (almost) pairwise delete). Then we may want to design a simple hash-function that H independent class of hash-functions. maps all these keys to different memory locations.

2 2 k1 j p k l p m  tx mod n h1  œ n = n k1 Prt ≠t Z2 x y p ∧ 7 p(p 1) ≤ ∈ t mod n h2 ≤ p(p 1) k − y = − k7 U œ universe k6 k3 x Note that the middle is the probability that h(x) h1 and of keys = œ h(y) h2. The total number of choices for (t , t ) is p(p 1). = x y − S (actual keys) k3 The number of choices for tx (ty ) such that tx mod n h1 k6 p p = (t mod n h2) lies between and . y = b n c d n e œ

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 238 c Ernst Mayr, Harald Räcke 239 Perfect Hashing Perfect Hashing

Let m S . We could simply choose the hash-table size very = | | large so that we don’t get any collisions. We can find such a hash-function by a few trials.

Using a universal hash-function the expected number of collisions However, a hash-table size of n m2 is very very high. = is ! m 1 We construct a two-level scheme. We first use a hash-function that E[#Collisions] . = 2 · n maps elements from S to m buckets. If we choose n m2 the expected number of collisions is strictly 1 = Let mj denote the number of items that are hashed to the j-th less than 2 . bucket. For each bucket we choose a second hash-function that Can we get an upper bound on the probability of having 2 maps the elements of the bucket into a table of size mj . The collisions? second function can be chosen such that all elements are mapped to different locations. 1 The probability of having 1 or more collisions can be at most 2 as 1 otherwise the expectation would be larger than 2 .

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 240 c Ernst Mayr, Harald Räcke 241

Perfect Hashing Perfect Hashing

P 2 The total memory that is required by all hash-tables is j mj . We need only (m) time to construct a hash-function h with P 2 O j m (4m).    !  j = O X 2 X mj X E m E 2 mj j = 2 + Then we construct a hash-table h for every bucket. This takes j j j j ! expected time (m ) for every bucket.  X m   X  O j 2 E j E m = 2 + j j j We only need that the hash-function is universal!!!

The first expectation is simply the expected number of collisions, for the first level. ! m 1 2 m 2m 1 = 2 m + = −

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 242 c Ernst Mayr, Harald Räcke 243 Cuckoo Hashing

Insert:

Goal: œ œ

Try to generate a perfect hash-table (constant worst-case search œ xœ1

time) in a dynamic scenario. x xx7 x9 1 œ x œ ñ Two hash-tables T1[0, . . . , n 1] and T2[0, . . . , n 1], with x7 − − xœ xœ hash-functions h1, and h2. x4 x6 x76 ñ An object x is either stored at location T1[h1(x)] or x16 œ T2[h2(x)]. œ x3 ñ A search clearly takes constant time if the above constraint is œ œ met. T1 T2

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 244 c Ernst Mayr, Harald Räcke 245

Cuckoo Hashing Cuckoo Hashing

Algorithm 16 Cuckoo-Insert(x) 1: if T1[h1(x)] x T2[h2(x)] x then return = ∨ = 2: steps 1 What is the expected time for an insert-operation? ← 3: while steps maxsteps do ≤ 4: exchange x and T1[h1(x)] We first analyze the probability that we end-up in an infinite loop 5: if x null then return = (that is then terminated after maxsteps steps). 6: exchange x and T2[h2(x)] 7: if x null then return = Formally what is the probability to enter an infinite loop that 8: rehash() // change table-size and rehash everything touches ` different keys (apart from x)? 9: Cuckoo-Insert(x)

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 246 c Ernst Mayr, Harald Räcke 247 Cuckoo Hashing Cuckoo Hashing

Insert: A cycle-structure is defined by

ñ ` keys a1, a2, . . . a , ` 2, a `a a ≥ ñ An index j 1 . . . , ` 1 that defines how much the last a ∈ { a − } item “jumps back” in the sequence. a`a

2 ñ keys . 0. b `b b1, b2, . . . b`b b b1 ≥ ñ An index jb 1 . . . , `a `b that defines how much the last x ∈ { + } x x item “jumps back” in the sequence. b`b a1 b 3 a1 x a2 a3 ñ An assignment of positions for the keys in both tables. a a4 8 a5 Formally we have positions p1, . . . , p , and p0 , . . . , p0 . `a 1 `b a6 a7 ñ The size of a cycle-structure is defined as ` ` . a + b

T1 T2

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 248 c Ernst Mayr, Harald Räcke 249

Cuckoo Hashing Cuckoo Hashing

We say a cycle-structure is active for key x if the hash-functions are chosen in such a way that the hash-function results match the pre-defined key-positions.

ñ h1(x) h1(a1) p1 = = ñ h2(a1) h2(a2) p2 = = Observation If we end up in an infinite loop there must exist a ñ h1(a2) h1(a3) p3 = = cycle-structure that is active for x. ñ ...

ñ if ` is even then h1(a ) p , otw. h2(a ) p a ` = sa ` = sa ñ h2(x) h2(b1) p0 = = 1 ñ h1(b1) h1(b2) p0 = = 2 ñ ...

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 250 c Ernst Mayr, Harald Räcke 251 Cuckoo Hashing Cuckoo Hashing

Proof. A cycle-structure is defined without knowing the hash-functions. All positions are fixed by the cycle-structure. Therefore we ask for the probability of mapping s 1 keys (the a-keys, the b-keys and Whether a cycle-structure is active for key x depends on the + hash-functions. x) to pre-specified positions in T1, and to pre-specified positions in T2. Lemma 30 A given cycle-structure of size s is active for key x with The probability is  s 1  s 1 probability at most µ + µ +  2(s 1) , µ + n · n , n since h1 and h2 are chosen independently. if we use (µ, s 1)-independent hash-functions. +

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 252 c Ernst Mayr, Harald Räcke 253

Cuckoo Hashing Cuckoo Hashing

The number of cycle-structures of size s is small: Hence, there are at most s3(mn)2 cycle-structures of size s.

ñ There are at most s ways to choose `a. This fixes `b. The probability that there is an active cycle-structure of size s is ñ There are at most s2 ways to choose j , and j . a b at most ñ There are at most ms possibilities to choose the keys  2(s 1) 3  s 1 2 s 1 and . 3 s µ + s + µ + a1, . . . , a`a b1, . . . , b`b s (mn) mn · n = mn n2 ñ There are at most ns choices for choosing the positions p1, . . . , p and p0 , . . . , p0 . 3  2 s 1 `a 1 `a s µ m + = mn n

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 254 c Ernst Mayr, Harald Räcke 255 Cuckoo Hashing Now assume that the insert operation takes t steps and does not create an infinite loop. If we make sure that n (1 δ)µ2m for a constant δ (i.e., the ≥ + hash-table is not too full) we obtain Consider the sequences and x, a1, a2, . . . , a`a x, b1, b2, . . . , b`b where the ai’s and bi’s are defined as before (but for the Pr[there exists an active cycle-structure] construction we only use keys examined during the while loop) X∞ Pr[there exists an act. cycle-structure of size s] ≤ If the insert operation takes steps then s 2 t = X∞ s3 µ2m s 1 + t 2`a 2`b 2 ≤ ≤ + + s 2 mn n = 1 X∞  1 s as no key is examined more than twice. s3 ≤ 1 mn s 0 δ = + Hence, one of the sequences x, a1, a2, . . . , a and 1 `a (1). x, b1, b2, . . . , b` must contain at least t/4 keys (either `a 1 or ≤ m2 ·O b + ` 1 must be larger than t/4). b +

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 256 c Ernst Mayr, Harald Räcke 257

Cuckoo Hashing Define a sub-sequence of length ` starting with x, as a sequence x1, . . . , x of keys with x1 x, together with ` 1 positions ` = + p0, p1, . . . , p from 0, . . . , n 1 . ` { − }

We say a sub-sequence is right-active for h1 and h2 if h1(x) h1(x1) p0, h2(x1) h2(x2) p1, = = = = Observation: h1(x2) h1(x3) p2, h2(x3) h2(x4) p3,... . = = = = If the insert takes t 4` steps there must either be a left-active or ≥ a right-active sub-sequence of length ` starting with x. We say a sub-sequence is left-active for h1 and h2 if h2(x1) p0, = h1(x1) h1(x2) p1, h2(x2) h2(x3) p2, = = = = h1(x3) h1(x4) p3,... . = =

For an active sequence starting with x the key x is supposed to have a collision with the second element in the sequence. This collision could either

be in the table T1 (left) or in the table T2 (right). Therefore the above definitions differentiate between left-active and right-active.

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 258 c Ernst Mayr, Harald Räcke 259 Cuckoo Hashing Cuckoo Hashing

` 1 ` 1 The number of sequences is at most m − p + as we can choose ` 1 keys (apart from x) and we can choose ` 1 positions The probability that a given sub-sequence is left-active − + p0, . . . , p`. (right-active) is at most  µ 2` , The probability that there exists a left-active or right-active n sequence of length ` is at most if we use (µ, `)-independent hash-functions. This holds since there are ` keys whose hash-values (two values per key) have to Pr[there exists active sequ. of length `] map to pre-specified positions. 2` ` 1 ` 1  µ  2 m − n + ≤ · · · n  1 ` 2 ≤ 1 δ +

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 260 c Ernst Mayr, Harald Räcke 261

Cuckoo Hashing Cuckoo Hashing

The expected time for an insert under the condition that If the search does not run into an infinite loop the probability that maxsteps is not reached is it takes more than 4` steps is at most X Pr[search takes at least ` steps iteration successful] | ` 0  1 ` ≥ 2 X  1 ` 1 δ 8 (1). + ≤ 1 δ = O ` 0 We choose maxsteps 4(1 2 log m)/ log(1 δ). Then the ≥ + = + + probability of terminating the while-loop because of reaching More generally, the above expression gives a bound on the cost in maxsteps is only 1 ( 1 2 because of reaching an ( m2 ) ( /m ) the successful iteration of an insert-operation (there is exactly one O 2 O infinite loop and 1/m because the search takes maxsteps steps successful iteration). without running into a loop). An iteration that is not successful induces cost (m) for doing a O complete rehash.

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 262 c Ernst Mayr, Harald Räcke 263 Cuckoo Hashing Cuckoo Hashing

The expected number of unsuccessful operations is 1 . ( m2 ) What kind of hash-functions do we need? O 1 Hence, the expected cost in unsuccessful iterations is only ( ). O m Since maxsteps is Θ(log m) it is sufficient to have (µ, Θ(log m))-independent hash-functions. Hence, the total expected cost for an insert-operation is constant.

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 264 c Ernst Mayr, Harald Räcke 265

Cuckoo Hashing

How do we make sure that n ≥ µ2(1 + δ)m? Definition 31 ñ Let α : 1/(µ2(1 δ)). d 1 = + Let d N; q n be a prime; and let a~ 0, . . . , q 1 + . Define ñ Keep track of the number of elements in the table. Whenever ∈ ≥ ∈ { − } for x 0, . . . , q m αn we double n and do a complete re-hash ∈ { } ≥ (table-expand).  Xd  h (x) : a xi mod q mod n . ñ Whenever m drops below α n we divide n by 2 and do a a~ = i 4 i 0 rehash (table-shrink). = α d d 1 d ñ Note that right after a change in table-size we have m n. Let n : ha~ a~ 0, . . . , q + . The class n is = 2 H = { | ∈ { } } H α (2, d 1)-independent. In order for a table-expand to occur at least 2 n insertions + α are required. Similar, for a table-shrink at least 4 deletions must occur.

ñ Therefore we can amortize the rehash cost after a change in table-size against the cost for insertions and deletions.

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 266 c Ernst Mayr, Harald Räcke 267 Fix ` d 1; let x1, . . . , x 0, . . . , q 1 be keys, and let ≤ + ` ∈ { − } t1, . . . , t` denote the corresponding hash-function values.

Let ` for all 1 d 1 A ha¯ ha¯(xi) ti i , . . . , ` For the coefficients a¯ 0, . . . , q 1 let f¯ denote the = { ∈ H | = ∈ { }} ∈ { − } + a Then polynomial  d  ` X i ha¯ A aha¯ fa¯ mod n and f¯(x) a x mod q a = i ∈ = i 0 = q f¯(x ) t α n α 0,..., 1 a i ∈ { i + · | ∈ { d n e − }} The polynomial is defined by d 1 distinct points. + Therefore I have

d ` 1 q ` d ` 1 B1 ... B q − + q − + | | · · | `| · ≤ dne ·

possibilities to choose a¯ such that h¯ A . a ∈ `

EADS 7.7 Hashing EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 268 c Ernst Mayr, Harald Räcke 269

Therefore the probability of choosing ha¯ from A` is only

q 1 ` qd `  2 ` d n e · − + d 1 q + ≤ n

EADS 7.7 Hashing c Ernst Mayr, Harald Räcke 270