Collisions — two keys hashing to the same slot — are unavoidable by the Pigeonhole Principle - if n items fill m containers and n>m, some container has more than one. Two strategies resolve them.

Separate Chaining

The input set of  elements is divided randomly into  size. A hash function determines which subset an element belongs to and each subset is managed independently as a list.

An array with eight buckets. Bucket 2 links to a chain of two nodes. Bucket 5 links to a single node.

chaining → each nonempty slot points to a linked list. Slot  contains a pointer to the head of the list of all stored elements with hash value 

insertion:  at worst assuming the element is not already present
searching: proportonial to the length of the list
deletion:  if lists are doubly linked

Analysis

Define the load factor  for hash table  as , with  number of slots and  number of elements.

The worst case is in which all keys hash the same slot, creating a list of length . Searching is 

The average case depends on how well the hash function distributes the set of keys. We assume that we are using independent uniform hashing.
Because hashes of distinct keys are assumed to be independent, independent uniform hashing is universal → chance of collide is .
For  denote the length of the list  by  so that . Then .

Theorem 1 (Unsuccessful Search)

In hash table with chaining, an unsuccessful search takes  time on average, with the assumption of independent uniform hashing

Theorem 2 (Successful Search)

In hash table with chaining, a successful search takes  on average

So, concluding:

  • searching →  on average
  • deletion →  at worst
    → we can support all dictionary operations in  time on average

Open Addressing

-> store all entries in a single array
The problem is that when inserting the bucket may be full, so we need to search another bucket. The process of finding an available bucket is called probing, while the bucket order is the probe sequence 1.

Pros:

  • easy
  • cache-friendly
    Cons:
  • prone to clustering

References

See also

Footnotes

  1. Same concept as Multi-Probing in LSH implementations