Docsity

Hash Tables: Implementation and Optimization - Prof. David J. Galles, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Hash Tables: Implementation and Optimization - Prof. David J. Galles and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms^ CS245-2009S-13Hash Tables

David GallesDepartment of Computer ScienceUniversity of San Francisco

13-0:^ Searching & Selecting^ Maintian a Database (keys and associated data)^ Operations:^ Add^ a key / value pair to the database^ Remove^ a key (and associated value) from thedatabase^ Find^ the value associated with a key

13-2:^ Sorted List Implementation If database is implemented as a sorted list:^ Add^ O(n)^ Remove^ O(n)^ Find^ O(lg^ n)

13-3:^ BST Implementation If database is implemented as a Binary Search Tree:^ Add^ Remove^ Find

13-5:^ Unsorted List Maintain an^ unsorted

,^ non-contiguous^ array of

elements^3415

How long does a Find take? How long does a Remove take? How long does an Add take?Does this sound like a good idea?

13-6:^ Hash Function^ What if we had a “magic function” –^ Takes a key as input^ Returns the index in the array where the keycan be found, if the key is in the array^ To add an element^ Put the key through the magic function, to get alocation^ Store element in that location^ To find an element^ Put the key through the magic function, to get alocation^ See if the key is stored in that location

13-8:^ Hash Function^ The “magic function” is called a

Hash function

If^ hash(key) = i, we say that the

key^ hashes to

the value^ i We’d like to ensure that different keys will alwayshash to different values. Why is this not possible?^ Too many possible keys^ If keys are strings of up to 15 letters, there are^21 10 different keys^ 1 sextillion – number of grains of salt it wouldtake to fill this room

one million^ times over.

13-9:^ Integer Hash Function^ When two keys hash to the same value, a

collision occurs. We cannot avoid collisions, but we can minimizethem by picking a hash function that distributeskeys evenly through the array. Example: Keys are integers^ Keys are in range

1... m Array indices are in range^1... n n << m

13-11:^ Integer Hash Function^ What if table size = 10, all keys end in 0?

13-12:^ Integer Hash Function^ What if table size = 10, all keys end in 0?^ What if table size is even, all keys are even?

13-14:^ Integer Hash Function^ What if table size = 10, all keys end in 0?^ What if table size is even, all keys are even?^ In general, what if the table size and many of thekeys share factors?^ What can we do?

13-15:^ Integer Hash Function^ What if table size = 10, all keys end in 0?^ What if table size is even, all keys are even?^ In general, what if the table size and many of thekeys share factors?^ What can we do?^ Prevent keys and table size from sharingfactors.^ No control over the keys.

13-17:^ String Hash Function^ Hash tables are usually used to store string values^ If we can convert a string into an integer, we canuse the integer hash function^ How can we convert a string into an integer?

13-18:^ String Hash Function^ Hash tables are usually used to store string values^ If we can convert a string into an integer, we canuse the integer hash function^ How can we convert a string into an integer?^ Add up ASCII values of the characters in thestring int hash(String key, int tableSize) {int hashvalue = 0;for (int i=0; i<key.length(); i++)hashvalue += (int) key.charAt(i);return hashvalue % tableSize;}