Data Structures · Hashing

Hash tables: constant-time lookup

A hash table stores records in an array and uses a hash function to turn each key straight into an array index. Done well, insert, search and delete all take constant time on average. These notes build the idea from a phone directory, and the lab at the bottom lets you watch keys land in a table, collide, and get found again.

The problem: a phone directory

Suppose we store employee records keyed by phone number, and we need three operations to be fast:

  1. Insert a phone number and its record.
  2. Search for a phone number and fetch its record.
  3. Delete a phone number and its record.

Here is how the structures we already know compare, for n records:

StructureSearchInsertDeleteSpace
Unsorted arrayO(n)O(1)O(n)O(n)
Sorted arrayO(log n)O(n)O(n)O(n)
Linked listO(n)O(1)O(n)O(n)
Balanced binary search treeO(log n)O(log n)O(log n)O(n)
Direct access tableO(1)O(1)O(1)O(10d) for d-digit keys
Hash tableO(1) averageO(1) averageO(1) averageO(m + n)

The sorted array searches fast with binary search but pays O(n) to keep its order on every insert and delete. The balanced tree guarantees O(log n) for everything. Can we do better?

Direct access table. Make one huge array and use the phone number itself as the index. The entry is empty (NIL) if that number is not present, otherwise it points to the record. Every operation is one array access: O(1). The trouble is size. A 10-digit phone number needs an array of 1010 slots, ten billion. At 8 bytes per pointer that is 80 GB, almost all of it empty. Worse, a 10-digit number does not even fit in a 32-bit int, whose largest value is about 2.1 billion.

Hashing keeps the O(1) idea but uses a small array. In practice it beats arrays, linked lists and balanced trees: O(1) on average under reasonable assumptions, and O(n) in the worst case.

The three parts of hashing

1. Hash table. An array of m slots that holds the records (or pointers to them). A slot is empty if no key stored so far hashes to it. A hash table is a generalization of an ordinary array: instead of indexing by position, you index by key.

2. Hash function. A function that turns a big key, like a phone number, into a small index from 0 to m − 1. In the lab below we use h(k) = k % m. If every key gets its own slot the function is called a perfect hash function, but that is rarely possible; the realistic goal is as few collisions as possible. A good hash function:

For phone numbers, the first three digits make a bad hash: everyone in the same area code lands in the same slot. The last three digits are much better because they vary almost randomly. Even that may not be the best choice.

3. Collision handling. A small array cannot give every possible key its own slot, so two keys will sometimes hash to the same index. Keys with the same hash value are called synonyms, and when a new key's slot is already taken we have a collision. There are two standard fixes:

Chaining

Each slot points to a linked list of all the records that hash there. A collision just adds another node to the list. Simple, never fills up, but it needs extra memory outside the table.

Open addressing (linear probing)

Every record lives in the table itself. If a key's home slot is taken, try the next slot, then the next, wrapping around, until an empty one turns up. Searching follows the same path until it finds the key or an empty slot.

Questions from class

With h(x) = x % 10, the values 11, 12, 13, 14, 15 land in slots 1, 2, 3, 4 and 5.

Where will 34, 65, 23, 56 and 12 go?

34 % 10 = 4, 65 % 10 = 5, 23 % 10 = 3, 56 % 10 = 6, and 12 % 10 = 2. Each lands in a different slot, so there are no collisions. Load the preset in the lab to check.

How do we look things up?

Compute the same hash again. To find 56, compute 56 % 10 = 6 and look in slot 6. With chaining, walk the list in that slot; with linear probing, check slot 6, then 7, 8, … until you find 56 or reach an empty slot, which proves it is not there.

What is the Big-O?

Computing the hash and indexing the array are O(1). Collisions add work: with chaining, an average search looks at about 1 + α items, where α = n / m. If m grows with n so α stays small, that is O(1) on average. The worst case is O(n), when every key hashes to the same slot and the table degenerates into a single list.

Lab: build a hash table

Insert keys and watch where they land. Switch between chaining and linear probing, or change the table size m: the same keys are re-inserted so you can compare. The class example (k % 20 with key/value pairs) is loaded to start.

Presets:

The table

What is happening

Statistics

Mapping

HashTable.cpp

    Calls