Skip over navigation

Hash Tables

Problems

What is a Hash Table?

Hash Functions

Problem : How does a hash table allow for O(1) searching? What is the worst case efficiency of a look up in a hash table using separate chainging?

A hash table uses hash functions to compute an integer value for data. This integer value can then be used as an index into an array, giving us a constant time access to the requested data. However, using separate chaining, we won't always achieve the best and average case efficiency of O(1) . If we have too small a hash table for the data set size and/or a bad hash function, elements can start to build in one index in the array. Theoretically, all n element could end up in the same linked list. Therefore, to do a search in the worst case is equivalent to looking up a data element in a linked list, something we already know to be O(n) time. However, with a good hash function and a well created hash table, the chances of this happening are, for all intents and purposes, ignorable.

Problem : The bigger the ratio between the size of the hash table and the number of data elements, the less chance there is for collision. What is a drawback to making the hash table big enough so the chances of collision is ignorable?

Wasted memory space

Problem : How could a linked list and a hash table be combined to allow someone to run through the list from item to item while still maintaining the ability to access an individual element in O(1) time?

Create a hash table where the elements of the array are pointers to linked list objects. You have your linked list as normal, but when you add an element to the linked list, you also add a pointer to that element from the correct place in the hash table specified by the hash value of the data in the new linked list element.

Problem : How could a hash table be used to implement a spell checker?

Create a hash table of all the words in the dictionary. When you encounter a word, whose spelling you want to check, just hash it and see if it exists in the table. If it does, then you've spelled it correctly. If not, then you haven't. This allows you to look up a word in O(1) time rather than O(n) time, which, in a dictionary on the order of 800,000 words, is a big time saver.

Problem : How long would a deletion operation take from hash table implemented using separate chaining?

O(1) . Lookup is O(1) and deleting from a linked list is O(1) , so the total efficiency is O(1) .

Follow Us