advanced algorithms coms31900 hashing part three cuckoo
play

Advanced Algorithms COMS31900 Hashing part three Cuckoo Hashing - PowerPoint PPT Presentation

Advanced Algorithms COMS31900 Hashing part three Cuckoo Hashing Rapha el Clifford Slides by Benjamin Sach Back to the start (again) A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key )


  1. Back to the start (again) � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) Hash table T of size m � n . Universe U of u keys. Collisions are fixed by chaining bucketing We require that we can recover any key from its bucket in O ( s ) time Locating the bucket containing where s is the number of keys in the bucket a given key takes O (1) time n arbitrary operations arrive online, one at a time . If our construction has the property that, for any two keys x, y ∈ U (with x � = y ), � 1 � the probability that x and y are in the same bucket is O m For any n operations, the expected run-time is O (1) per operation.

  2. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time

  3. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time What does amortised expected O (1) time mean?!

  4. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time What does amortised expected O (1) time mean?! let’s build it up. . .

  5. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time What does amortised expected O (1) time mean?! let’s build it up. . . “ O (1) worst-case time per operation” means every operation takes constant time

  6. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time What does amortised expected O (1) time mean?! let’s build it up. . . “ O (1) worst-case time per operation” means every operation takes constant time “The total worst-case time complexity of performing any n operations is O ( n ) ”

  7. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time What does amortised expected O (1) time mean?! let’s build it up. . . “ O (1) worst-case time per operation” means every operation takes constant time “The total worst-case time complexity of performing any n operations is O ( n ) ” this does not imply that every operation takes constant time

  8. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time What does amortised expected O (1) time mean?! let’s build it up. . . “ O (1) worst-case time per operation” means every operation takes constant time “The total worst-case time complexity of performing any n operations is O ( n ) ” this does not imply that every operation takes constant time However, it does mean that the amortised worst-case time complexity of an operation is O (1)

  9. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time What does amortised expected O (1) time mean?! let’s build it up. . . expected “ O (1) worst-case time per operation” means every operation takes constant time in expectation expected “The total worst-case time complexity of performing any n operations is O ( n ) ” this does not imply that every operation takes constant time in expectation expected However, it does mean that the amortised worst-case time complexity of an operation is O (1)

  10. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 .

  11. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) .

  12. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) . h 1 ( x ) h 2 ( x )

  13. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) . x h 1 ( x ) h 2 ( x )

  14. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) . x h 1 ( x ) h 2 ( x )

  15. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) . x h 1 ( x ) h 2 ( x )

  16. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) . x h 1 ( x ) h 2 ( x )

  17. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) . Important: We never store multiple keys at the same position x h 1 ( x ) h 2 ( x )

  18. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) . Important: We never store multiple keys at the same position x h 1 ( x ) h 2 ( x ) Therefore, as claimed, lookup takes O (1) time. . .

  19. Dynamic perfect hashing � A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) , lookup ( key ) (which returns value ) and delete ( key ) T HEOREM In the Cuckoo hashing scheme: • Every lookup and every delete takes O (1) worst-case time, • The space is O ( n ) where n is the number of keys stored • An insert takes amortised expected O (1) time In Cuckoo hashing there is a single hash table but two hash functions: h 1 and h 2 . Each key in the table is either stored at position h 1 ( x ) or h 2 ( x ) . Important: We never store multiple keys at the same position x h 1 ( x ) h 2 ( x ) Therefore, as claimed, lookup takes O (1) time. . . but how do we do inserts?

  20. Inserts in Cuckoo hashing x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x )

  21. Inserts in Cuckoo hashing x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop (and congratulate yourself on a job well done)

  22. Inserts in Cuckoo hashing x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop

  23. Inserts in Cuckoo hashing x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop

  24. Inserts in Cuckoo hashing x y h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop

  25. Inserts in Cuckoo hashing x y h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x )

  26. Inserts in Cuckoo hashing x y h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x

  27. Inserts in Cuckoo hashing y x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x

  28. Inserts in Cuckoo hashing y x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x where should we put key y ?

  29. Inserts in Cuckoo hashing y x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x where should we put key y ? in the other position it’s allowed in

  30. Inserts in Cuckoo hashing y h 2 ( y ) h 1 ( y ) x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x where should we put key y ? in the other position it’s allowed in

  31. Inserts in Cuckoo hashing y h 2 ( y ) h 1 ( y ) x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x Step 3 : Let pos be the other position y is allowed to be in i.e pos = h 2 ( y ) if h 1 ( x ) = h 1 ( y ) and pos = h 1 ( y ) otherwise

  32. Inserts in Cuckoo hashing y h 2 ( y ) h 1 ( y ) x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x Step 3 : Let pos be the other position y is allowed to be in i.e pos = h 2 ( y ) if h 1 ( x ) = h 1 ( y ) and pos = h 1 ( y ) otherwise Step 4 : Attempt to put y in position pos if that position is empty, stop

  33. Inserts in Cuckoo hashing h 2 ( y ) h 1 ( y ) y x h 1 ( x ) h 2 ( x ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x Step 3 : Let pos be the other position y is allowed to be in i.e pos = h 2 ( y ) if h 1 ( x ) = h 1 ( y ) and pos = h 1 ( y ) otherwise Step 4 : Attempt to put y in position pos if that position is empty, stop

  34. Inserts in Cuckoo hashing y h 2 ( y ) h 1 ( y ) x z h 2 ( z ) h 1 ( x ) h 2 ( x ) h 1 ( z ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x Step 3 : Let pos be the other position y is allowed to be in i.e pos = h 2 ( y ) if h 1 ( x ) = h 1 ( y ) and pos = h 1 ( y ) otherwise Step 4 : Attempt to put y in position pos if that position is empty, stop

  35. Inserts in Cuckoo hashing y h 2 ( y ) h 1 ( y ) x z h 2 ( z ) h 1 ( x ) h 2 ( x ) h 1 ( z ) Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x Step 3 : Let pos be the other position y is allowed to be in i.e pos = h 2 ( y ) if h 1 ( x ) = h 1 ( y ) and pos = h 1 ( y ) otherwise Step 4 : Attempt to put y in position pos if that position is empty, stop Step 5 : Let z be the key currently in position pos evict key z and replace it with key y

  36. Inserts in Cuckoo hashing h 2 ( y ) h 1 ( y ) y x h 2 ( z ) h 1 ( x ) h 2 ( x ) h 1 ( z ) z Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x Step 3 : Let pos be the other position y is allowed to be in i.e pos = h 2 ( y ) if h 1 ( x ) = h 1 ( y ) and pos = h 1 ( y ) otherwise Step 4 : Attempt to put y in position pos if that position is empty, stop Step 5 : Let z be the key currently in position pos evict key z and replace it with key y

  37. Inserts in Cuckoo hashing h 2 ( y ) h 1 ( y ) y x h 2 ( z ) h 1 ( x ) h 2 ( x ) h 1 ( z ) z Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x Step 3 : Let pos be the other position y is allowed to be in i.e pos = h 2 ( y ) if h 1 ( x ) = h 1 ( y ) and pos = h 1 ( y ) otherwise Step 4 : Attempt to put y in position pos if that position is empty, stop Step 5 : Let z be the key currently in position pos evict key z and replace it with key y

  38. Inserts in Cuckoo hashing h 2 ( y ) h 1 ( y ) y x h 2 ( z ) h 1 ( x ) h 2 ( x ) h 1 ( z ) z Step 1 : Attempt to put x in position h 1 ( x ) if that position is empty, stop Step 2 : Let y be the key currently in position h 1 ( x ) evict key y and replace it with key x Step 3 : Let pos be the other position y is allowed to be in i.e pos = h 2 ( y ) if h 1 ( x ) = h 1 ( y ) and pos = h 1 ( y ) otherwise Step 4 : Attempt to put y in position pos if that position is empty, stop Step 5 : Let z be the key currently in position pos evict key z and replace it with key y and so on. . .

  39. Pseudocode add( x ): � pos ← h 1 ( x ) � Repeat at most n times: � If T [ pos ] is empty then T [ pos ] ← x . � Otherwise, y ← T [ pos ] , T [ pos ] ← x , pos ← the other possible location for y . (i.e. if y was evicted from h 1 ( y ) then pos ← h 2 ( y ) , otherwise pos ← h 1 ( y ) .) x ← y . Repeat � Give up and rehash the whole table. i.e. empty the table, pick two new hash functions and reinsert every key h 2 ( y ) h 1 ( y ) y x h 2 ( z ) h 1 ( x ) h 2 ( x ) h 1 ( z ) z

  40. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash.

  41. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash. What does rehashing involve? �

  42. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash. What does rehashing involve? Suppose that the table contains the k keys x 1 , . . . , x k at the time of we fail to insert key x . �

  43. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash. What does rehashing involve? Suppose that the table contains the k keys x 1 , . . . , x k at the time of we fail to insert key x . To rehash we: �

  44. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash. What does rehashing involve? Suppose that the table contains the k keys x 1 , . . . , x k at the time of we fail to insert key x . To rehash we: Randomly pick two new hash functions h 1 and h 2 . (More about this in a minute.) �

  45. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash. What does rehashing involve? Suppose that the table contains the k keys x 1 , . . . , x k at the time of we fail to insert key x . To rehash we: Randomly pick two new hash functions h 1 and h 2 . (More about this in a minute.) Build a new empty hash table of the same size �

  46. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash. What does rehashing involve? Suppose that the table contains the k keys x 1 , . . . , x k at the time of we fail to insert key x . To rehash we: Randomly pick two new hash functions h 1 and h 2 . (More about this in a minute.) Build a new empty hash table of the same size Reinsert the keys x 1 , . . . , x k and then x , one by one, using the normal add operation. �

  47. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash. What does rehashing involve? Suppose that the table contains the k keys x 1 , . . . , x k at the time of we fail to insert key x . To rehash we: Randomly pick two new hash functions h 1 and h 2 . (More about this in a minute.) Build a new empty hash table of the same size Reinsert the keys x 1 , . . . , x k and then x , one by one, using the normal add operation. If we fail while rehashing. . . we start from the beginning again �

  48. Rehashing If we fail to insert a new key x , (i.e. we still have an “evicted” key after moving around keys n times) then we declare the table “rubbish” and rehash. What does rehashing involve? Suppose that the table contains the k keys x 1 , . . . , x k at the time of we fail to insert key x . To rehash we: Randomly pick two new hash functions h 1 and h 2 . (More about this in a minute.) Build a new empty hash table of the same size Reinsert the keys x 1 , . . . , x k and then x , one by one, using the normal add operation. If we fail while rehashing. . . we start from the beginning again This is rather slow. . . but we will prove that it happens rarely �

  49. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page).

  50. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions:

  51. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions: h 1 and h 2 are independent i.e. h 1 ( x ) says nothing about h 2 ( x ) , and vice versa.

  52. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions: h 1 and h 2 are independent i.e. h 1 ( x ) says nothing about h 2 ( x ) , and vice versa. h 1 and h 2 are truly random i.e. each key is independently mapped to each of the m positions in the hash table with probability 1 m .

  53. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions: h 1 and h 2 are independent i.e. h 1 ( x ) says nothing about h 2 ( x ) , and vice versa. h 1 and h 2 are truly random i.e. each key is independently mapped to each of the m positions in the hash table with probability 1 m . Computing the value of h 1 ( x ) and h 2 ( x ) takes O (1) worst-case time

  54. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions: h 1 and h 2 are independent i.e. h 1 ( x ) says nothing about h 2 ( x ) , and vice versa. h 1 and h 2 are truly random i.e. each key is independently mapped to each of the m positions in the hash table with probability 1 m . Computing the value of h 1 ( x ) and h 2 ( x ) takes O (1) worst-case time There are at most n keys in the hash table at any time.

  55. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions: REASONABLE h 1 and h 2 are independent ASSUMPTION i.e. h 1 ( x ) says nothing about h 2 ( x ) , and vice versa. h 1 and h 2 are truly random i.e. each key is independently mapped to each of the m positions in the hash table with probability 1 m . Computing the value of h 1 ( x ) and h 2 ( x ) takes O (1) worst-case time There are at most n keys in the hash table at any time.

  56. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions: REASONABLE h 1 and h 2 are independent ASSUMPTION i.e. h 1 ( x ) says nothing about h 2 ( x ) , and vice versa. h 1 and h 2 are truly random UNREASONABLE i.e. each key is independently mapped to each of the m positions ASSUMPTION in the hash table with probability 1 m . Computing the value of h 1 ( x ) and h 2 ( x ) takes O (1) worst-case time There are at most n keys in the hash table at any time.

  57. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions: REASONABLE h 1 and h 2 are independent ASSUMPTION i.e. h 1 ( x ) says nothing about h 2 ( x ) , and vice versa. h 1 and h 2 are truly random UNREASONABLE i.e. each key is independently mapped to each of the m positions ASSUMPTION in the hash table with probability 1 m . QUESTIONABLE Computing the value of h 1 ( x ) and h 2 ( x ) takes O (1) worst-case time ASSUMPTION There are at most n keys in the hash table at any time.

  58. Assumptions We will follow the analysis in the paper Cuckoo hashing for undergraduates , 2006, by Rasmus Pagh (see the link on unit web page). We make the following assumptions: REASONABLE h 1 and h 2 are independent ASSUMPTION i.e. h 1 ( x ) says nothing about h 2 ( x ) , and vice versa. h 1 and h 2 are truly random UNREASONABLE i.e. each key is independently mapped to each of the m positions ASSUMPTION in the hash table with probability 1 m . QUESTIONABLE Computing the value of h 1 ( x ) and h 2 ( x ) takes O (1) worst-case time ASSUMPTION N O T A C T U A L There are at most n keys in the hash table at any time. ASSUMPTION L Y A N

  59. Cuckoo graph Hash table (size m )

  60. Cuckoo graph Hash table The cuckoo graph : (size m )

  61. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table.

  62. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. m vertices

  63. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . m vertices

  64. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . h 2 ( x 1 ) x 1 h 1 ( x 1 ) m vertices

  65. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . h 2 ( x 1 ) x 1 h 1 ( x 1 ) x 2 m vertices

  66. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . h 2 ( x 1 ) x 1 h 1 ( x 1 ) x 3 x 2 m vertices

  67. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . h 2 ( x 1 ) x 1 h 1 ( x 1 ) x 3 x 2 m vertices

  68. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 x 3 x 2 m vertices

  69. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 x 3 x 5 x 2 m vertices

  70. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 h 2 ( x 5 ) x 3 x 5 x 2 h 1 ( x 5 ) m vertices

  71. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 There is no space for x 5 . . . h 2 ( x 5 ) x 3 x 5 x 2 h 1 ( x 5 ) m vertices

  72. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 There is no space for x 5 . . . h 2 ( x 5 ) so we make space x 3 by moving x 2 and then x 3 x 5 x 2 h 1 ( x 5 ) m vertices

  73. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 There is no space for x 5 . . . h 2 ( x 5 ) so we make space x 3 by moving x 2 and then x 3 x 5 x 2 h 1 ( x 5 ) m vertices

  74. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 There is no space for x 5 . . . h 2 ( x 5 ) so we make space x 3 by moving x 2 and then x 3 x 5 x 2 h 1 ( x 5 ) m vertices

  75. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 There is no space for x 5 . . . h 2 ( x 5 ) so we make space x 3 by moving x 2 and then x 3 x 5 x 2 h 1 ( x 5 ) m vertices

  76. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 There is no space for x 5 . . . h 2 ( x 5 ) so we make space x 3 by moving x 2 and then x 3 x 5 x 2 h 1 ( x 5 ) m vertices The number of moves performed while adding a key is the length of the corresponding path in the cuckoo graph

  77. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 1 x 3 x 5 x 2 m vertices The number of moves performed while adding a key is the length of the corresponding path in the cuckoo graph

  78. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph x 3 x 5 x 2 m vertices

  79. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 6 The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph x 3 x 5 x 2 m vertices

  80. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 6 The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph Inserting key x 6 creates a cycle. x 3 x 5 x 2 m vertices

  81. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 6 The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph Inserting key x 6 creates a cycle. x 3 Cycles are dangerous. . . x 5 x 2 m vertices

  82. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 6 The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph Inserting key x 6 creates a cycle. x 3 Cycles are dangerous. . . x 5 x 2 x 7 m vertices

  83. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 6 The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph Inserting key x 6 creates a cycle. x 3 Cycles are dangerous. . . x 5 When key x 7 is inserted where does it go? x 2 x 7 m vertices

  84. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 6 The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph Inserting key x 6 creates a cycle. x 3 Cycles are dangerous. . . x 5 When key x 7 is inserted where does it go? x 2 x 7 there are 6 keys but only 5 spaces m vertices

  85. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 6 The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph Inserting key x 6 creates a cycle. x 3 Cycles are dangerous. . . x 5 When key x 7 is inserted where does it go? x 2 x 7 there are 6 keys but only 5 spaces m vertices The keys would be moved around in an infinite loop but we stop and rehash after n moves. . .

  86. Cuckoo graph Hash table The cuckoo graph : (size m ) A vertex for each position of the table. x 4 For each key x there is an undirected edge between h 1 ( x ) and h 2 ( x ) . x 6 The number of moves performed while adding a key is x 1 the length of the corresponding path in the cuckoo graph Inserting key x 6 creates a cycle. x 3 Cycles are dangerous. . . x 5 When key x 7 is inserted where does it go? x 2 x 7 there are 6 keys but only 5 spaces m vertices The keys would be moved around in an infinite loop but we stop and rehash after n moves. . . Inserting a key into a cycle always causes a rehash

Recommend


More recommend