Hash Tables 6/13/2018 Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Cuckoo Hashing.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Hash Tables
Advertisements

© 2004 Goodrich, Tamassia Hash Tables1  
© 2004 Goodrich, Tamassia Dictionaries   
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
Dictionaries1 © 2010 Goodrich, Tamassia m l h m l h m l.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Data Structures Lecture 13 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
CS 221 Guest lecture: Cuckoo Hashing Shannon Larson March 11, 2011.
© 2004 Goodrich, Tamassia Dictionaries   
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Dictionaries 4/17/2017 3:23 PM Hash Tables  
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Hash Tables1   © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Maps1 © 2010 Goodrich, Tamassia. Maps2  A map models a searchable collection of key-value entries  The main operations of a map are for searching, inserting,
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Hash Maps Rem Collier Room A1.02 School of Computer Science and Informatics University College Dublin, Ireland.
Maps Rem Collier Room A1.02 School of Computer Science and Informatics
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Maps 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Sections 10.5 – 10.6 Hashing.
Hashing (part 2) CSE 2011 Winter March 2018.
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Skip Lists 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Design & Analysis of Algorithm Map
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Binary Search Trees < > =
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Hashing CSE 2011 Winter July 2018.
B-Trees 7/5/2018 4:26 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Chapter 10.1 Binary Search Trees
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries 9/14/ :35 AM Hash Tables   4
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Skip Lists S3 + - S2 + - S1 + - S0 + -
Priority Queues © 2014 Goodrich, Tamassia, Goldwasser Priority Queues
Data Structures Maps and Hash.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Hashing II CS2110 Spring 2018.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Dictionaries 11/23/2018 5:34 PM Hash Tables   Hash Tables.
Copyright © Aiman Hanna All rights reserved
Maps.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Ordered Maps & Dictionaries
Copyright © Aiman Hanna All rights reserved
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Dictionaries 4/5/2019 1:49 AM Hash Tables  
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
Dictionaries and Hash Tables
CSE 373: Data Structures and Algorithms
Presentation transcript:

Hash Tables 6/13/2018 Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Cuckoo Hashing xkcd. http://xkcd.com/1065/. “Shoes.” Used with permission under Creative Commons 2.5 License. © 2015 Goodrich and Tamassia Hash Tables

The Power of Two Choices In the cuckoo hashing scheme, we use two lookup tables, T0 and T1, each of size N, where N is greater than n, the number of items in the map, by at least a constant factor, say, N ≥ 2n. We use a hash function, h0, for T0, and a different hash function, h1, for T1. For any key, k, there are only two possible places where we are allowed to store an item with key k, namely, either in T0[h0(k)] or T1[h1(k)]. This freedom turns out to allow us to achieve O(1) worst-case running time for table lookups. © 2015 Goodrich and Tamassia Hash Tables

An Example of Cuckoo Hashing Each of the keys in the set S = {2, 3, 5, 8, 9} has two possible locations it can go, one in the table T1 and one in the table T2. Note that there is a collision of 2 and 8 in T2, but that is okay, since there is no collision for 2 in its alternative location, in T1. © 2015 Goodrich and Tamassia Hash Tables

Recall the Map Operations get(k): if the map M has an entry with key k, return its associated value; else, return null put(k, v): insert entry (k, v) into the map M; if key k is not already in M, then return null; else, return old value associated with k remove(k): if the map M has an entry with key k, remove it from M and return its associated value; else, return null size(), isEmpty() © 2015 Goodrich and Tamassia Hash Tables

Pseudo-code for get and remove The algorithms for get and remove are simple and run in O(1) time in the worst case. © 2015 Goodrich and Tamassia Hash Tables

Intuition for the Name The name “cuckoo hashing” comes from the way the put(k, v) operation is performed in this scheme, because it mimics the breeding habits of the Common Cuckoo bird. The Common Cuckoo is a brood parasite—it lays its egg in the nest of another bird after first evicting an egg out of that nest. © 2015 Goodrich and Tamassia Hash Tables

Intuition for the Name - put Similarly, if a collision occurs in the insertion operation in the cuckoo hashing scheme, then we evict the previous item in that cell and insert the new one in its place. This forces the evicted item to go to its alternate location in the other table and be inserted there, which may repeat the eviction process with another item, and so on. Eventually, we either find an empty cell and stop or we repeat a previous eviction, which indicates an eviction cycle. If we discover an eviction cycle, then we stop the insertion process and rehash all the items in the two tables using new, hopefully better, hash functions. © 2015 Goodrich and Tamassia Hash Tables

Pseudo-code for put Note that the above pseudo-code has a condition for detecting a cycle in the sequence of insertions. There are several ways to formulate this condition. For example, we could count the number of iterations for this loop and consider there to be a cycle if we go over a certain threshold, like n or log n. © 2015 Goodrich and Tamassia Hash Tables

Example Eviction Sequence An eviction sequence of length 3: © 2015 Goodrich and Tamassia Hash Tables

Long Eviction Sequences are Rare © 2015 Goodrich and Tamassia Hash Tables

Long Eviction Sequences are Rare © 2015 Goodrich and Tamassia Hash Tables

The Expected Number of Evictions © 2015 Goodrich and Tamassia Hash Tables

Performance of Cuckoo Hashing We can also show that, since long eviction sequences are rare, we will have to rehash only with very low probability. Thus, the expected amortized time to perform any single insertion in a cuckoo table is O(1). So a cuckoo hash table achieves worst-case O(1) time for lookups and removals, and expected O(1) time for insertions. This performance is primarily because there are exactly two possible places for any item to be in a cuckoo hash table, which shows the power of two choices. © 2015 Goodrich and Tamassia Hash Tables