advanced algorithms coms31900 hashing part two static
play

Advanced Algorithms COMS31900 Hashing part two Static Perfect - PowerPoint PPT Presentation

Advanced Algorithms COMS31900 Hashing part two Static Perfect Hashing Rapha el Clifford Slides by Benjamin Sach Dictionaries and Hashing recap A dynamic dictionary stores ( key , value ) -pairs and supports: add ( key , value ) ,


  1. Advanced Algorithms – COMS31900 Hashing part two Static Perfect Hashing Rapha¨ el Clifford Slides by Benjamin Sach

  2. Dictionaries and Hashing recap � 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 were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . n arbitrary operations arrive online, one at a time .

  3. Dictionaries and Hashing recap � 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 were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . n arbitrary operations arrive online, one at a time . A set H of hash functions is weakly universal if for any two keys x, y ∈ U (with x � = y ), 1 � � Pr h ( x ) = h ( y ) � m ( h is picked uniformly at random from H )

  4. Dictionaries and Hashing recap � 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 were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . n arbitrary operations arrive online, one at a time . Using weakly universal hashing: A set H of hash functions is weakly universal if for any two keys x, y ∈ U (with x � = y ), For any n operations, the expected 1 run-time is O (1) per operation. � � Pr h ( x ) = h ( y ) � m ( h is picked uniformly at random from H )

  5. Dictionaries and Hashing recap � 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 were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . n arbitrary operations arrive online, one at a time . Using weakly universal hashing: A set H of hash functions is weakly universal if for any two keys x, y ∈ U (with x � = y ), For any n operations, the expected 1 run-time is O (1) per operation. � � Pr h ( x ) = h ( y ) � m But this doesn’t tell us much about the ( h is picked uniformly at random from H ) worst-case behaviour

  6. Static Dictionaries and Perfect hashing � A static dictionary stores ( key , value ) -pairs and supports: lookup ( key ) (which returns value ) - no inserts or deletes are allowed Hash table T of size m � n . Universe U of u keys. Collisions were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . we are given n different ( key , value ) -pairs and want to pick a good h

  7. Static Dictionaries and Perfect hashing � A static dictionary stores ( key , value ) -pairs and supports: lookup ( key ) (which returns value ) - no inserts or deletes are allowed Hash table T of size m � n . Universe U of u keys. Collisions were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . we are given n different ( key , value ) -pairs and want to pick a good h T HEOREM The FKS hashing scheme: • Has no collisions • Every lookup takes O (1) worst-case time, • Uses O ( n ) space, • Can be built in O ( n ) expected time.

  8. Static Dictionaries and Perfect hashing � A static dictionary stores ( key , value ) -pairs and supports: lookup ( key ) (which returns value ) - no inserts or deletes are allowed Hash table T of size m � n . Universe U of u keys. Collisions were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . we are given n different ( key , value ) -pairs and want to pick a good h T HEOREM The rest of this lecture is devoted to the The FKS hashing scheme: FKS scheme • Has no collisions • Every lookup takes O (1) worst-case time, • Uses O ( n ) space, • Can be built in O ( n ) expected time.

  9. Static Dictionaries and Perfect hashing � A static dictionary stores ( key , value ) -pairs and supports: lookup ( key ) (which returns value ) - no inserts or deletes are allowed Hash table T of size m � n . Universe U of u keys. Collisions were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . we are given n different ( key , value ) -pairs and want to pick a good h T HEOREM The rest of this lecture is devoted to the The FKS hashing scheme: FKS scheme • Has no collisions • Every lookup takes O (1) worst-case time, The construction is based on weak • Uses O ( n ) space, universal hashing • Can be built in O ( n ) expected time.

  10. Static Dictionaries and Perfect hashing � A static dictionary stores ( key , value ) -pairs and supports: lookup ( key ) (which returns value ) - no inserts or deletes are allowed Hash table T of size m � n . Universe U of u keys. Collisions were fixed by chaining (building linked lists) A hash function maps a key x to position h ( x ) - i.e T [ h ( x )] = ( key , value ) . we are given n different ( key , value ) -pairs and want to pick a good h T HEOREM The rest of this lecture is devoted to the The FKS hashing scheme: FKS scheme • Has no collisions • Every lookup takes O (1) worst-case time, The construction is based on weak • Uses O ( n ) space, universal hashing • Can be built in O ( n ) expected time. (with an O (1) time hash function)

  11. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m

  12. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n

  13. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n (where any h ( x ) can be computed in O (1) time)

  14. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n

  15. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n Step 2: Check for collisions

  16. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n Step 2: Check for collisions Step 3: Profit!

  17. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n Step 2: Check for collisions Step 3: Repeat if necessary

  18. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average?

  19. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U ( x � = y ), 1 � � Pr h ( x ) = h ( y ) where h is picked uniformly at random from H � m Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? number of collisions 1 � n · 1 � � � � � � E ( C ) = E I x,y = E ( I x,y ) � m = 2 m x,y ∈ T,x<y x,y ∈ T, x<y x,y ∈ T, x<y where indicator random variable I x,y = 1 iff h ( x ) = h ( y ) .

Recommend


More recommend