Download presentation
Presentation is loading. Please wait.
1
CS 221 Guest lecture: Cuckoo Hashing Shannon Larson March 11, 2011
2
Learning Goals Describe the cuckoo hashing principle Analyze the space and time complexity of cuckoo hashing Apply the insert and lookup algorithms in a cuckoo hash table Construct the graph for a cuckoo table
3
Remember Graphs?
4
Graph Cycles A graph cycle is a path of edges such that the first and last vertices are the same
5
Recall Hashing
6
Cuckoo Hashing: the idea
7
Why is this cool? Perfect hashing guarantees – O(1) lookup, O(1) insert Cuckoo hashing guarantees – O(1) lookup – O(1) insert** Other hashing strategies can’t guarantee this! Also, it’s an option for your final project ** There’s a caveat here, but we’ll see it later
8
Cuckoo Hashing: Two Nests
9
Cuckoo Hashing: Example x
10
y x
11
z x y oh no!
12
Cuckoo Hashing : Example z x y NOW we’re fine!
13
Cuckoo Hashing : Example x y z
14
Why two tables? Two tables, one for each hash function Simple to visualize, simple to implement But, why two? One table works just as well! Just as simple to implement (all one table)
15
One Table Example x
16
y x
17
z x y oh no!
18
One Table Example z x y
19
x y z
20
x y z
21
Graph Representation How can we represent our table? Why not a graph? – Nodes are every possible table entry – Edges are inserted entries This is a directed graph Direction from current location TO alternate location
22
Graph Example Remember our one-table example? x y z 1 2 3 4 12 34
23
Infinite Insert Suppose we insert something, and we end up in an infinite loop – Or, “too many” displacements – Some pre-defined maximum based on table size
24
Example: Loops Remember our one-table example? x y z 1 2 3 4 12 34
25
Example: Loops x y z 1 2 3 4 12 34 w
26
x y z 1 2 3 4 12 34 a w
27
a y x 1 2 3 4 12 34 z w
28
a y x 1 2 3 4 12 34 w z
29
Notice what happens to the graph We keep going and going and going…. 12 34
30
Analysis: Loops
31
What can we do once we get a loop? – Rebuild, same size (ok solution) – Double table size (better solution) We’ll need new hash functions for both
32
Analysis Lookup has O(1) time – At MOST two places to look, ever – One location per hash function Insert has amortized O(1) time – Think of this as “in the long run” – In practice we see O(1) time insert – You’ll see amortized analysis in CPSC 320 Remember the “grass and trees” analysis?
33
Lookup: The Code
34
Insert: The Code
35
Analysis: Load Factor
36
More hash functions What would this look like? We would have three tables (simple case) – One hash function per table Or, we would have two alternates (one table)
37
More hash functions x y z
39
Even better load? Currently we’ve only put one item per bucket What if we had two cells per bucket? x,w y,a z
40
Even better load? Currently we’ve only put one item per bucket What if we had two cells per bucket? What about collision strategies? – Round-robin (cells take turns swapping out) – FIFO (oldest resident gets kicked out)
41
Even better load?
42
Links & Resources http://en.wikipedia.org/wiki/Cuckoo_hashing http://www.ru.is/faculty/ulfar/CuckooHash.pdf http://www.it-c.dk/people/pagh/papers/cuckoo- undergrad.pdf http://www.it-c.dk/people/pagh/papers/cuckoo- undergrad.pdf No neat animations on the internet…yet! – Possible personal project? – Brownie points? – Pre-coop project?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.