Cuckoo Hashing -...

245
Cuckoo Hashing

Transcript of Cuckoo Hashing -...

Page 1: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

Page 2: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Outline for Today

● Towards Perfect Hashing● Reducing worst-case bounds

● Cuckoo Hashing● Hashing with worst-case O(1) lookups.

● Random Graph Theory● Just how fast is cuckoo hashing?

Page 3: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Perfect Hashing

Page 4: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Collision Resolution

● Last time, we mentioned three general strategies for resolving hash collisions:● Closed addressing: Store all colliding elements in

an auxiliary data structure like a linked list or BST.● Open addressing: Allow elements to overflow out

of their target bucket and into other spaces.● Perfect hashing: Choose a hash function with no

collisions.● We have not spoken on this last topic yet.

Page 5: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Why Perfect Hashing is Hard

● For fixed, constant load factors, the expected cost of a lookup in a chained hash table or linear probing table is O(1).

● However, the expected cost of a lookup in these tables is not the same as the expected worst-case cost of a lookup in these tables.

Page 6: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Expected Worst-Case Bounds

● Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in a chained hash table isΘ(log n / log log n), assuming the number of slots is Θ(n).

● Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in a linear probing hash table is Ω(log n).

● Proofs: Exercises 11-1 and 11-2 from CLRS. ☺

Look at just this bucket.

Look at just this bucket.

Page 7: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Expected Worst-Case Bounds

● Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in a chained hash table isΘ(log n / log log n), assuming the number of slots is Θ(n).

● Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in a linear probing hash table is Ω(log n).

● Proofs: Exercises 11-1 and 11-2 from CLRS. ☺

Look at just this bucket.

Look at just this bucket.

How can we get from these

bounds down to O(1)?

How can we get from these

bounds down to O(1)?

Page 8: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Technique 1: Multiple-Choice Hashing

Page 9: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 10: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 11: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 12: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 13: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 14: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 15: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 16: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 17: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 18: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 19: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 20: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 21: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 22: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 23: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 24: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 25: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Suppose that we distribute n balls into Θ(n) bins using the following strategy:● For each ball, choose two bins totally at random.● Put the ball into the bin with fewer balls in it; tiebreak randomly.

● Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n).

● Proof: Nontrivial; see “Balanced Allocations” by Azar et al.

Page 26: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Idea: Build a chained hash table with two hash functions h₁ and h₂.

● To insert an element x, compute h₁(x) and h₂(x) and place x into whichever bucket is less full.

● To perform a lookup, compute h₁(x) and h₂(x) and search both buckets for x.

Page 27: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Idea: Build a chained hash table with two hash functions h₁ and h₂.

● To insert an element x, compute h₁(x) and h₂(x) and place x into whichever bucket is less full.

● To perform a lookup, compute h₁(x) and h₂(x) and search both buckets for x.

Page 28: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Idea: Build a chained hash table with two hash functions h₁ and h₂.

● To insert an element x, compute h₁(x) and h₂(x) and place x into whichever bucket is less full.

● To perform a lookup, compute h₁(x) and h₂(x) and search both buckets for x.

Page 29: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Idea: Build a chained hash table with two hash functions h₁ and h₂.

● To insert an element x, compute h₁(x) and h₂(x) and place x into whichever bucket is less full.

● To perform a lookup, compute h₁(x) and h₂(x) and search both buckets for x.

Page 30: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Second-Choice Hashing

● Theorem: The expected cost of a lookup in such a hash table is O(1 + α). ). (Why?)

● Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in such a hash table is O(log log n). (Why?)

● Open problem: What is the smallest k for which there are k-independent hash functions that match the bounds using truly random hash functions?

Page 31: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Technique 2: Hashing with Relocation

Page 32: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

14

1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

Page 33: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

14

1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

13

Page 34: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

14

1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

13

Page 35: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

14

1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

13

Page 36: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

14

Page 37: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

14

Page 38: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

Page 39: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

15

Page 40: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

15

Page 41: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

1515

Page 42: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

412

12

13

14 1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

150

Page 43: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

412

12

13

14 1

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

0

Page 44: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

1

Page 45: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

1

Page 46: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

1

Page 47: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

412

12

13

14 0

1

1

0 1

2

3

4

5

67

89

10

11

12

13

14

15

2

Page 48: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

412

12

13

14 0

1

1

0 1

2

3

4

5

67

89

10

11

12

13

14

15

2

Page 49: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

212

12

13

14 0

1

1

0 1

2

3

4

5

67

89

10

11

12

13

14

15

4

Page 50: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

212

12

13

14 0

1

1

0 1

2

3

4

5

67

89

10

11

12

13

14

15

4

Page 51: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

15

212

12

13

14 0

1

1

4

0 1

2

3

4

5

67

89

10

11

12

13

14

15

Page 52: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

Page 53: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

15

Page 54: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

15

Page 55: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

1515

Page 56: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed.

● When an existing key is found during an insertion that's closer to its “home” location than the new key, it's displaced to make room for it.

● This dramatically decreases the variance in the expected number of lookups.

● It also makes it possible to terminate searches early.

0

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

1515

Page 57: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Theorem: The expected cost of a lookup in Robin Hood hashing, using 5-independent hashing, is O(1), assuming a constant load factor.

● Proof idea: Each element is hashed into the same run as it would have been hashed to in linear probing.

0

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

Page 58: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Robin Hood Hashing

● Theorem: Assuming truly random hash functions, the variance of the expected number of probes required in Robin Hood hashing is O(log log n).

● Proof: Tricky; see Celis' Ph.D thesis.

0

412

12

13

14 0

1

2

0 1

2

3

4

5

67

89

10

11

12

13

14

15

Page 59: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Where We Stand

● We now have two interesting ideas for improving performance:● Second-choice hashing: Give each element multiple

homes to pick from.● Relocation hashing: Move elements after placing

them.● Each idea, individually, exponentially decreases the

worst-case cost of a lookup by decreasing the variance in the element distribution.

● What happens if we combine these ideas together?

Page 60: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

Page 61: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Maintain two tables, each of which has m elements.

● We choose two hash functions h₁ and h₂ from to [ 𝒰 to [ m].

● Every element x ∈ 𝒰 to [will either be at position h₁(x) in the first table or h₂(x) in the second.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 62: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

Deletions take time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 63: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

Deletions take time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 64: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

Deletions take time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 65: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

Deletions take time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 66: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

Deletions take time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 67: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

Deletions take time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 68: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

● Deletions take worst-case time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 69: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

● Deletions take worst-case time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

41

T₁ T₂

Page 70: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

● Deletions take worst-case time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

T₁ T₂

Page 71: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

● Deletions take worst-case time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

T₁ T₂

Page 72: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

● Deletions take worst-case time O(1) because only two locations must be checked.

32

93

58

84

59

97

23

53

26

T₁ T₂

Page 73: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

● Deletions take worst-case time O(1) because only two locations must be checked.

32

93

58

84

97

23

53

26

T₁ T₂

Page 74: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● Lookups take worst-case time O(1) because only two locations must be checked.

● Deletions take worst-case time O(1) because only two locations must be checked.

32

93

58

84

97

23

53

26

T₁ T₂

Page 75: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

93

58

84

97

23

53

26

T₁ T₂

Page 76: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

93

58

84

97

23

53

26

T₁ T₂

75

Page 77: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

93

58

84

97

23

53

26

T₁ T₂

75

Page 78: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

93

58

84

75

97

23

53

26

T₁ T₂

Page 79: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

93

58

84

75

97

23

53

26

T₁ T₂

Page 80: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

93

58

84

75

97

23

53

26

T₁ T₂

10

Page 81: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

93

58

84

75

97

23

53

26

T₁ T₂

10

Page 82: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

93

58

84

75

97

23

53

26

T₁ T₂

10

Page 83: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10 93

58

84

75

97

23

53

26

T₁ T₂

Page 84: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

84

75

97

23

53

26

T₁ T₂

93

Page 85: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

84

75

97

23

53

26

T₁ T₂

93

Page 86: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

84

75

97

23

53

26

T₁ T₂

93

Page 87: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

84

75

97

23

53

26

93

T₁ T₂

Page 88: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

84

75

97

23

53

26

93

T₁ T₂

Page 89: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

84

75

97

23

53

26

93

T₁ T₂

6

Page 90: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

84

75

97

23

53

26

93

T₁ T₂

6

Page 91: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

6 84

75

97

23

53

26

93

T₁ T₂

Page 92: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

6

75

97

23

53

26

93

T₁ T₂

84

Page 93: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

6

75

97

23

53

26

93

T₁ T₂

84

Page 94: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

6

75

97

23

53

26

93

T₁ T₂

84

Page 95: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

6

75

97

23

53 84

26

93

T₁ T₂

Page 96: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

6

75

97

23

84

26

93

T₁ T₂

53

Page 97: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

6

75

97

23

84

26

93

T₁ T₂

53

Page 98: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

32

10

58

6

75

97

23

84

26

93

T₁ T₂

53

Page 99: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

53 32

10

58

6

75

97

23

84

26

93

T₁ T₂

Page 100: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

53

10

58

6

75

97

23

84

26

93

T₁ T₂

32

Page 101: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

53

10

58

6

75

97

23

84

26

93

T₁ T₂

32

Page 102: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

53

10

58

6

75

97

23

84

26

93

T₁ T₂

32

Page 103: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

53

10

58

6

75

97

23

84

26

32

93

T₁ T₂

Page 104: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

53

10

58

6

75

97

23

84

26

32

93

T₁ T₂

Page 105: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● To insert an element x, start by inserting it into table 1.

● If h₁(x) is empty, place x there.

● Otherwise, place x there, evict the old element y, and try placing y into table 2.

● Repeat this process, bouncing between tables, until all elements stabilize.

Page 106: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

53

10

58

6

75

97

23

84

26

32

93

T₁ T₂

Page 107: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

32

93

T₁ T₂

91

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 108: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

32

93

T₁ T₂

91

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 109: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

91 75

97

23

84

26

32

93

T₁ T₂

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 110: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

91

97

23

84

26

32

93

T₁ T₂

75

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 111: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

91

97

23

84

26

32

93

T₁ T₂

75

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 112: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

91

97

23

84

26

32

93

T₁ T₂

75

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 113: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

91

97

23

84

26

32 75

93

T₁ T₂

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 114: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

91

97

23

84

26

75

93

T₁ T₂

32

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 115: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

91

97

23

84

26

75

93

T₁ T₂

32

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 116: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

91

97

23

84

26

75

93

T₁ T₂

32

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 117: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

32 91

97

23

84

26

75

93

T₁ T₂

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 118: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

32

97

23

84

26

75

93

T₁ T₂

91

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 119: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

32

97

23

84

26

75

93

T₁ T₂

91

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 120: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

32

97

23

84

26

75

93

T₁ T₂

91

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 121: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

32

97

23

84

26

75 91

93

T₁ T₂

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 122: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

32

97

23

84

26

91

93

T₁ T₂

75

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 123: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

32

97

23

84

26

91

93

T₁ T₂

75

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 124: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

32

97

23

84

26

91

93

T₁ T₂

75

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 125: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75 32

97

23

84

26

91

93

T₁ T₂

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 126: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

91

93

T₁ T₂

32

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 127: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

91

93

T₁ T₂

32

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 128: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

91

93

T₁ T₂

32

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 129: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

91 32

93

T₁ T₂

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 130: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

32

93

T₁ T₂

91

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 131: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

32

93

T₁ T₂

91

● An insertion fails if the displacements form an infinite cycle.

If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 132: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

53

10

58

6

75

97

23

84

26

32

93

T₁ T₂

91

● An insertion fails if the displacements form an infinite cycle.

● If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

Page 133: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

58 75

97

23

84

26

32

93

T₁ T₂

● An insertion fails if the displacements form an infinite cycle.

● If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

Multiple rehashes might be necessary before this succeeds.

53

10

6

91

Page 134: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Cuckoo Hashing

58 75

97

23

84

26

32

93

T₁ T₂

● An insertion fails if the displacements form an infinite cycle.

● If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables.

● Multiple rehashes might be necessary before this succeeds.

53

10

6

91

Page 135: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

How efficient is cuckoo hashing?

Page 136: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Pro tip: When analyzing a data structure, it never hurts to get some empirical

performance data first.

Page 137: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

0

10

20

30

40

50

60

70

80

90

100

Suppose we store n total elements in two tables of m slots each.

What’s probability all insertions succeed, assuming m = α). n?

If m ≤ (1 – ε))n, we almost

certainly fail.

If m ≤ (1 – ε))n, we almost

certainly fail.

If m ≥ (1+ε))n, we almost certainly

succeed.

If m ≥ (1+ε))n, we almost certainly

succeed.

Idea: Going forward, set m = (1+ε))n for some small ε) > 0.

Idea: Going forward, set m = (1+ε))n for some small ε) > 0.

Page 138: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Suppose we store n total elements with m = (1+ε))n.

How many total displacements occur across all insertions?

1000

0

6000

0

1100

00

1600

00

2100

00

2600

00

3100

00

3600

00

4100

00

4600

00

5100

00

5600

00

6100

00

6600

00

7100

00

7600

00

8100

00

8600

00

9100

00

9600

000

100000

200000

300000

400000

500000

600000

Wow! That’s surprisingly

linear!

Wow! That’s surprisingly

linear!

Goal: Show each insertion takes

expected time O(1).

Goal: Show each insertion takes

expected time O(1).

Page 139: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Goal: Show that insertions take expected time O(1), under the assumption that

m = (1+ε))n for some ε) > 0.

Page 140: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Analyzing Cuckoo Hashing

● The analysis of cuckoo hashing presents some challenges.

● Challenge 1: We may have to consider hash collisions across multiple hash functions.

● Challenge 2: We need to reason about chains of displacement, which can be fairly complicated.

● To resolve these challenges, we’ll need to bring in some new techniques.

12

1

6

5

3

T₁ T₂

13

9

7

Page 141: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● The cuckoo graph is a bipartite multigraph derived from a cuckoo hash table.

● Each table slot is a node.

● Each element is an edge.

● Edges link slots where each element can be.

● Each insertion introduces a new edge into the graph.

12

1

6

5

3

T₁ T₂

13

9

7

613

712

5

1

3

9

Page 142: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

12

1

6

5

3

T₁ T₂

13

9

7

613

712

5

1

3

9

6

13

7

12

5

1

93

Page 143: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

12

1

6

5

3

T₁ T₂

13

9

7

613

712

5

1

3

9

6

13

7

12

5

1

93

Arrowheads indicate which slots elements

is stored in.

Arrowheads indicate which slots elements

is stored in.

Each node has at most one incoming

edge.

Each node has at most one incoming

edge.

Page 144: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

12

1

6

5

3

T₁ T₂

13

9

7

613

712

5

1

3

9

6

13

7

12

5

1

93

Insertions correspond to sequences of

flipping arrowheads.

Insertions correspond to sequences of

flipping arrowheads.

Page 145: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

12

1

6

5

3

T₁ T₂

13

9

7

613

712

5

1

3

9

6

13

7

12

5

1

93

Insertions correspond to sequences of

flipping arrowheads.

Insertions correspond to sequences of

flipping arrowheads.

2 2

Page 146: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

12

1

6

5

3

T₁ T₂

13

9

7

613

712

5

1

3

9

6

13

7

12

5

1

93

Insertions correspond to sequences of

flipping arrowheads.

Insertions correspond to sequences of

flipping arrowheads.

2 2

Page 147: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

12

1

2 6

5

3

T₁ T₂

13

9

7

613

712

5

1

3

9

6

13

7

12

5

1

93

Insertions correspond to sequences of

flipping arrowheads.

Insertions correspond to sequences of

flipping arrowheads.

2 2

Page 148: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

12

1

2

5

3

T₁ T₂

13

9

7 6

613

712

5

1

3

9

6

13

7

12

5

1

93

Insertions correspond to sequences of

flipping arrowheads.

Insertions correspond to sequences of

flipping arrowheads.

2 2

Page 149: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

7 12

1

2

5

3

T₁ T₂

13

9

6

613

712

5

1

3

9

6

13

7

12

5

1

93

Insertions correspond to sequences of

flipping arrowheads.

Insertions correspond to sequences of

flipping arrowheads.

2 2

Page 150: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

7

1

12

2

5

3

T₁ T₂

13

9

6

613

712

5

1

3

9

6

13

7

12

5

1

93

Insertions correspond to sequences of

flipping arrowheads.

Insertions correspond to sequences of

flipping arrowheads.

2 2

Page 151: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

7

1

12

2

5

3

T₁ T₂

13

9

6

613

712

5

1

3

9

6

13

7

12

5

1

93

Insertions correspond to sequences of

flipping arrowheads.

Insertions correspond to sequences of

flipping arrowheads.

2 2

Page 152: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 153: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 154: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 155: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 156: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 157: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 158: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 159: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 160: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 161: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 162: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 163: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 164: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 165: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 166: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 167: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 168: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 169: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 170: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 171: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 172: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 173: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 174: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 175: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

Page 176: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 1: If x is inserted into a cuckoo hash table, the insertion succeeds if the connected component containing x contains either no cycles or only one cycle.

We either stabilize inside the cycle, avoid the cycle, or get

kicked out of the cycle.

We either stabilize inside the cycle, avoid the cycle, or get

kicked out of the cycle.

Page 177: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 2: If x is inserted into a cuckoo hash table, the insertion fails if the connected component containing x contains more than one cycle.

No cycles: The graph is a directed tree. A tree with k

nodes has k – 1 edges.

No cycles: The graph is a directed tree. A tree with k

nodes has k – 1 edges.

Page 178: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 2: If x is inserted into a cuckoo hash table, the insertion fails if the connected component containing x contains more than one cycle.

One cycle: We’ve added an edge,

giving k nodes and k edges.

One cycle: We’ve added an edge,

giving k nodes and k edges.

Page 179: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● Claim 2: If x is inserted into a cuckoo hash table, the insertion fails if the connected component containing x contains more than one cycle.

Two cycles: There are k nodes and k+1 edges. There are too many arrowheads to

place at most one arrowhead per

node.

Two cycles: There are k nodes and k+1 edges. There are too many arrowheads to

place at most one arrowhead per

node.

Page 180: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Cuckoo Graph

● A connected component of a graph is called complex if it contains two or more cycles.

● Theorem: Insertion into a cuckoo hash table succeeds if and only if the resulting cuckoo graph has no complex connected components.

● Questions we still need to answer:● The number of nodes in a connected component can

be used to bound the cost of an insertion. On average, how big are those connected components?

● What is the probability that an insertion fails because we create a complex connected component?

Page 181: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Time-Out for Announcements!

Page 182: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Project Checkpoints

● Project checkpoints were due today at 2:30PM.

● We’ll be reviewing them over the weekend with the aim of getting back to you by early next week.

● In general, please feel free to reach out to us if you have any questions about the project or your topic! We’re happy to help out.

Page 183: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Problem Set Four

● Problem Set Four is due next Tuesday at 2:30PM.

● You know the drill! Ask questions if you have them, get in touch with us if there’s anything we can assist with, and have a lot of fun working through these exercises!

Page 184: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Back to CS166!

Page 185: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Two Major Questions

How big are the connected componentsin the cuckoo graph?

How likely is it for an insertion to fail?

Page 186: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Step One: Sizing Connected Components

Page 187: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Analyzing Connected Components

● The cost of inserting x into a cuckoo hash table is proportional to the size of the CC containing x.

● Question: What is the expected size of a CC in the cuckoo graph?

Page 188: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Pick some starting table slot.

There are n elements in the table, so this graph has n

edges.

Assume, for now, that our hash functions are truly random.

Each edge has a ¹/ₘ chance of touching this table slot.

The number of adjacent nodes, which will be visited in the

next step of BFS, is aBinom(n, ¹/ₘ) variable.

Idea: Count the number of nodes in a connected

component by simulating a BFS.

Idea: Count the number of nodes in a connected

component by simulating a BFS.

Page 189: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Each new node kinda sorta ish also touches a number of new nodes on the other side that

can be modeled as aBinom(n, ¹/ₘ) variable.

This ignores double-counting nodes.

This ignores existing edges.

This ignores correlations between edge counts.

However, it conservatively bounds the next BFS step.

Idea: Count the number of nodes in a connected

component by simulating a BFS.

Idea: Count the number of nodes in a connected

component by simulating a BFS.

Page 190: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Modeling the BFS

● Simulate a BFS tree using Binom(n, ¹/ₘ) variables!● Begin with a root node.● Each node has children

distributed as a Binom(n, ¹/ₘ) variable.

● Question: How many total nodes will this simulated BFS discover before terminating?

Page 191: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Modeling the BFS

● A tree-shaped process where each node has an i.i.d. number of children is called a Galton-Watson process.

● A subcritical process is one where the expected number of children of each node is less than one.

● Question: Assuming each node’s child count is an i.i.d. copy of some random variable ξ, how many nodes should we expect to see in the tree?

Page 192: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

http://1.bp.blogspot.com/-f3gzOHMBM6I/TWcdXn-R1SI/AAAAAAAAAEA/G8uu8scBj1I/s1600/1868-02%2Bgazette%2Bof%2Bfashion.jpg

Page 193: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Subcritical Galton-Watson Processes

● Denote by Xₙ the number of nodes alive at depth n. This gives a series of random variables X₀, X₁, X₂, … .

● These variables are defined by the following randomized recurrence:

● Here, each ξᵢ,ₙ is an i.i.d. copy of ξ.

Xn+1=∑i=1

Xn

ξi ,nX0=1

X₀ = 1

X₁ = 3

X₂ = 4

X₃ = 0

Page 194: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

X0=1 E[ξ] < 1

Lemma 1: E[Xᵢ] = E[ξ]i.

(Induction and conditional expectation.)

Lemma 1: E[Xᵢ] = E[ξ]i.

(Induction and conditional expectation.)

Xn+1=∑i=1

Xn

ξi ,n

Lemma 2:

(Linearity of expectation; sum of a geometric series.)

Lemma 2:

(Linearity of expectation; sum of a geometric series.)

E[∑i=0∞ X i]=

11−E[ξ ]

Theorem: The expected number of nodes in a

connected component of the cuckoo graph is O(1), assuming that

m = (1+ε))n.

Proof: ξ in this case is a Binom(n, ¹/ₘ) variable. So E[ξ] = ⁿ/ₘ = O(1).

Page 195: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Story So Far

● The expected size of a connected component in the cuckoo graph is O(1).

● Therefore, each successful insertion takes expected time O(1).

● Question: What happens in an unsuccessful insertion? And what does that do for our expected cost of any insertion?

Page 196: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Step Two:Exploring the Graph Structure

Page 197: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Exploring the Graph Structure

● Cuckoo hashing will always succeed in the case where the cuckoo graph has no complex connected components.

● If there are no complex CC's, then we will not get into a loop and insertion time will depend only on the sizes of the CC's.

● It's reasonable to ask, therefore, how likely we are to not have complex components.

Page 198: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Exploring the Graph Structure

● Question: What is the probability that a randomly-chosen bipartite multigraph with 2m nodes and n edges will contain a complex connected component?

● Directly answering this question is challenging and requires some fairly detailed combinatorics.

● However, there’s a very clever technique we can use to bound this probability indirectly.

Page 199: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

Page 200: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

Page 201: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

Page 202: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

Page 203: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

Page 204: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

Page 205: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

We’re right back where we started. This

pattern will continue indefinitely.

We’re right back where we started. This

pattern will continue indefinitely.

Page 206: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

h₁(x)

c₁c₂ l₁l₂

Question: What’s the probability that we end up with a configuration

like this one?

Question: What’s the probability that we end up with a configuration

like this one?

Page 207: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

h₁(x)

c₁c₂ l₁l₂

This next proof comes from a CS166 final project byNoah Arthurs, Joseph Chang, and Nolan Handali. It’s inspired by

another argument due to Charles Chen (another Stanford student), which is a modification of one by Sanders and Vöcking,

which was an improvement of one by Pagh and Rodler.

Key idea: Use a traditional, CS109-style counting argument. Admittedly, it’s a nontrivial counting argument, but it’s a

counting argument nonetheless!

This next proof comes from a CS166 final project byNoah Arthurs, Joseph Chang, and Nolan Handali. It’s inspired by

another argument due to Charles Chen (another Stanford student), which is a modification of one by Sanders and Vöcking,

which was an improvement of one by Pagh and Rodler.

Key idea: Use a traditional, CS109-style counting argument. Admittedly, it’s a nontrivial counting argument, but it’s a

counting argument nonetheless!

Page 208: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Insertion fails if we have a complex connected component.What specifically happens in that case?

h₁(x)

c₁c₂ l₁l₂

∑k=1

n

( (k+1)4 mk−1 nk

m2 k m )

Ways to split k nodes into c₁, l₁, c₂, and l₂.

(upper bound)

Ways to pick k nodes (table slots) given the first is h₁(x).

(upper bound) Ways to assign k keys to those slots.

(upper bound)

Ways h₁ and h₂ can be chosen for those keys.

Ways h₂(x) can be chosen.

Sum over all possible numbers of other

keys being displaced.

Page 209: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Page 210: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Page 211: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Page 212: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Page 213: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Page 214: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

m = (1 + ε))nm = (1 + ε))n

Page 215: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

m = (1 + ε))nm = (1 + ε))n

Page 216: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Page 217: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Numerator grows polynomially as a

function of k.

Numerator grows polynomially as a

function of k.

Denominator grows exponentially as a

function of k.

Denominator grows exponentially as a

function of k.

Page 218: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Numerator grows polynomially as a

function of k.

Numerator grows polynomially as a

function of k.

Denominator grows exponentially as a

function of k.

Denominator grows exponentially as a

function of k.

Page 219: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Page 220: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

∑k=1

n

((k+1)4 mk−1 nk

m2 k m ) = ∑k=1

n

((k+1)4 nk mk−1−2 k−1)

= ∑k=1

n

((k+1)4 nk mk−2 )

=1

m2 ∑k=1

n

((k+1)4 nk m−k )

=1

m2 ∑k=1

n

(k+1)4 ( n

m )k

=1

m2 ∑k=1

n(k+1)

4

(1+ε))k

=1

m2⋅O (1)

= O (1

m2 )

Page 221: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Question 1: What is the probability at least one insert fails if we do n

total insertions?

Question 1: What is the probability at least one insert fails if we do n

total insertions?

The probability that a single insertionfails is O(1 / m2) if m = (1+ε))n.

Page 222: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Pr [some insert fails ]

≤ ∑k=1

n

Pr [the k th insert fails ]

= ∑k=1

n

O(1

m2)

= O(n

m2 )

= O(1m

)

Question 1: What is the probability at least one insert fails if we do n

total insertions?

Question 1: What is the probability at least one insert fails if we do n

total insertions?

The probability that a single insertionfails is O(1 / m2) if m = (1+ε))n.

Page 223: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Pr [some insert fails ]

≤ ∑k=1

n

Pr [the k th insert fails ]

= ∑k=1

n

O(1

m2)

= O(n

m2 )

= O(1m

)

Question 1: What is the probability at least one insert fails if we do n

total insertions?

Question 1: What is the probability at least one insert fails if we do n

total insertions?

The probability that a single insertionfails is O(1 / m2) if m = (1+ε))n.

Page 224: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Pr [some insert fails ]

≤ ∑k=1

n

Pr [the k th insert fails ]

= ∑k=1

n

O(1

m2)

= O(n

m2 )

= O(1m

)

Question 1: What is the probability at least one insert fails if we do n

total insertions?

Question 1: What is the probability at least one insert fails if we do n

total insertions?

The probability that a single insertionfails is O(1 / m2) if m = (1+ε))n.

Page 225: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Pr [some insert fails ]

≤ ∑k=1

n

Pr [the k th insert fails ]

= ∑k=1

n

O(1

m2)

= O(n

m2 )

= O(1m

)

Question 1: What is the probability at least one insert fails if we do n

total insertions?

Question 1: What is the probability at least one insert fails if we do n

total insertions?

The probability that a single insertionfails is O(1 / m2) if m = (1+ε))n.

Page 226: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Pr [some insert fails ]

≤ ∑k=1

n

Pr [the k th insert fails ]

= ∑k=1

n

O(1

m2)

= O(n

m2 )

= O(1m

)

Question 1: What is the probability at least one insert fails if we do n

total insertions?

Question 1: What is the probability at least one insert fails if we do n

total insertions?

The probability that a single insertionfails is O(1 / m2) if m = (1+ε))n.

Page 227: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

If an insertion fails, we rehash by building a brand-new table, with

new hash functions, and inserting all old elements.

If an insertion fails, we rehash by building a brand-new table, with

new hash functions, and inserting all old elements.

The probability that a series ofn insertions fail is O(1 / m).

It’s possible that, when we do a rehash, one of the insertions fails. Therefore, we keep rehashing until we find a working table.

It’s possible that, when we do a rehash, one of the insertions fails. Therefore, we keep rehashing until we find a working table.

Question 2: On expectation, how many

rehashes are needed per insertion?

Question 2: On expectation, how many

rehashes are needed per insertion?

Page 228: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The probability that a series ofn insertions fail is O(1 / m).

Question 2: On expectation, how many rehashes are needed per insertion?

Question 2: On expectation, how many rehashes are needed per insertion?

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

X is geometrically distributed with success probability

1 – O(1 / m).

X is geometrically distributed with success probability

1 – O(1 / m).

E[ X ] =1

1−O(1 /m)= O(1)

Page 229: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The probability that a series ofn insertions fail is O(1 / m).

Question 2: On expectation, how many rehashes are needed per insertion?

Question 2: On expectation, how many rehashes are needed per insertion?

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

X is geometrically distributed with success probability

1 – O(1 / m).

X is geometrically distributed with success probability

1 – O(1 / m).

E[ X ] =1

1−O(1 /m)= O(1)

E[#rehashes]

= E[ X ]⋅Pr [#rehashes > 0]

= O(1)⋅O(1/m2)

= O(1 /m2)

Page 230: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The probability that a series ofn insertions fail is O(1 / m).

Question 2: On expectation, how many rehashes are needed per insertion?

Question 2: On expectation, how many rehashes are needed per insertion?

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

X is geometrically distributed with success probability

1 – O(1 / m).

X is geometrically distributed with success probability

1 – O(1 / m).

E[ X ] =1

1−O(1 /m)= O(1)

E[#rehashes]

= E[ X ]⋅Pr [#rehashes > 0]

= O(1)⋅O(1/m2)

= O(1 /m2)

Page 231: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The probability that a series ofn insertions fail is O(1 / m).

Question 2: On expectation, how many rehashes are needed per insertion?

Question 2: On expectation, how many rehashes are needed per insertion?

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

X is geometrically distributed with success probability

1 – O(1 / m).

X is geometrically distributed with success probability

1 – O(1 / m).

E[ X ] =1

1−O(1 /m)= O(1)

E[#rehashes]

= E[ X ]⋅Pr [#rehashes > 0]

= O(1)⋅O(1/m2)

= O(1 /m2)

Page 232: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The probability that a series ofn insertions fail is O(1 / m).

Question 2: On expectation, how many rehashes are needed per insertion?

Question 2: On expectation, how many rehashes are needed per insertion?

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

Let X be a random variable counting the number of

rehashes assuming at least one rehash occurs.

X is geometrically distributed with success probability

1 – O(1 / m).

X is geometrically distributed with success probability

1 – O(1 / m).

E[ X ] =1

1−O(1 /m)= O(1)

E[#rehashes]

= E[ X ]⋅Pr [#rehashes > 0]

= O(1)⋅O(1/m2)

= O(1 /m2)

Page 233: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The expected number of rehasheson any insertion is O(1 / m2).

Question 3: What is the expected cost of an insertion

into a cuckoo hash table?

Question 3: What is the expected cost of an insertion

into a cuckoo hash table?

O(1) + O(1 / m2) · O(m)

Expected cost of successful

insertion.

Expected cost of successful

insertion.

Expected number of rehashes.

Expected number of rehashes.

Cost of doing one rehash.

Cost of doing one rehash.

Page 234: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The expected number of rehasheson any insertion is O(1 / m2).

Question 3: What is the expected cost of an insertion

into a cuckoo hash table?

Question 3: What is the expected cost of an insertion

into a cuckoo hash table?

O(1) + O(1 / m2) · O(m)

Page 235: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The expected number of rehasheson any insertion is O(1 / m2).

Question 3: What is the expected cost of an insertion

into a cuckoo hash table?

Question 3: What is the expected cost of an insertion

into a cuckoo hash table?

O(1) + O(1 / m)

Page 236: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The expected number of rehasheson any insertion is O(1 / m2).

Question 3: What is the expected cost of an insertion

into a cuckoo hash table?

Question 3: What is the expected cost of an insertion

into a cuckoo hash table?

O(1)

Page 237: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

The Overall Analysis

● Cuckoo hashing gives worst-case lookups and deletions.

● Insertions are expected, amortized O(1).

● The hidden constants are small, and this is a practical technique for building hash tables.

Cuckoo Hashing:● lookup: O(1)● insert: O(1)*

● delete: O(1)

* expected, amortized

Page 238: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

More to Explore

Page 239: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Hash Function Strength

● We analyzed cuckoo hashing assuming our hash functions were truly random. That’s often too strong of an assumption.

● What we know:● 6-independent hashing isn't sufficient for expected

O(1) insertion time, but that O(log n)-independence is.● Some simple classes of hash functions (e.g.

2-independent polynomial) perform poorly for cuckoo hashing.

● Some simple classes of hash functions (e.g.3-independent simple tabulation) perform very well.

● Open problem: Determine the strength of hash function needed for cuckoo hashing to work efficiently.

Page 240: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Multiple Tables

● Cuckoo hashing works well with two tables. So why not 3, 4, 5, …, or k tables?

● In practice, cuckoo hashing with k ≥ 3 tables tends to perform much better than cuckoo hashing with k = 2 tables:● The load factor can increase substantially; with k=3,

it's only around α). = 0.91 that you run into trouble with the cuckoo graph.

● Displacements are less likely to chain together; they only occur when all hash locations are filled in.

● Open problem: Determine where these phase transition thresholds are for arbitrary k.

Page 241: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Increasing Bucket Sizes

● What if each slot in a cuckoo hash table can store multiple elements?

● When displacing an element, choose a random one to move and move it.

● This turns out to work remarkably well in practice, since it makes it really unlikely that you'll have long chains of displacements.

● Open problem: Quantify the effect of larger bucket sizes on the overall runtime of cuckoo hashing.

Page 242: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Restricting Moves

● Insertions in cuckoo hashing only run into trouble when you encounter long chains of displacements during insertions.

● Idea: Cap the number of displacements at some fixed factor, then store overflowing elements in a secondary hash table.

● In practice, this works remarkably well, since the auxiliary table doesn't tend to get very large.

● Open problem: Quantify the effects of “hashing with a stash” for arbitrary stash sizes and displacement limits.

Page 243: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Other Dynamic Schemes

● There is another famous dynamic perfect hashing scheme called dynamic FKS hashing.

● It works by using closed addressing and resolving collisions at the top level with a secondary (static) perfect hash table.

● In practice, it's not as fast as these other approaches. However, it only requires2-independent hash functions.

● Check CLRS for details!

Page 244: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Lower Bounds?

● Open Problem: Is there a hash table that supports amortized O(1) insertions, deletions, and lookups?

● You'd think that we'd know the answer to this question, but, sadly, we don't.

Page 245: Cuckoo Hashing - web.stanford.eduweb.stanford.edu/class/archive/cs/cs166/cs166.1196/lectures/13/Slides13.pdf13 14 15 13. Robin Hood Hashing Robin Hood hashing is a variation of open

Next Time

● Approximate Membership Queries● Educated guesses about whether things have been seen

before.● Bloom Filters

● The original – and one of the most popular – solutions to this problem.

● Quotient Filters● Adapting linear probing for AMQ.

● Cuckoo Filters● Adapting cuckoo hashing for AMQ.