3.3.4 · D1Hashing

Foundations — Open addressing — linear probing, quadratic probing, double hashing

2,021 words9 min readBack to topic

0. The picture behind everything: a table of slots

Before any symbol, look at the object we are storing things in.

We will call the number of slots . In the picture .


1. The key

The whole game is a function from keys (possibly huge numbers) to slots (only of them). Many keys, few slots — collisions are inevitable. Hold that thought.


2. The mod operation — the heart of hashing

Everything uses . If you are shaky here, nothing else lands. So we build it slowly.

The picture that makes mod obvious is a clock.


3. The base hash

Now we name the rule that turns a key into its home slot.

The little subscript just means "hash number one" — we keep the name free because double hashing will introduce a second hash, , later. (Why the division method and other choices exist is a separate story: see Hash functions — division & multiplication methods.)


4. Collision — two keys, one box

Collisions are the entire reason open addressing exists. The parent contrasts two cures:

  • Hashing — chaining — hang colliding keys in a list outside the table.
  • Open addressing — keep everyone inside the table, using a probe rule (next section).

5. The probe index and the offset


6. The probe sequence and "permutation"

This is precisely why the parent worries about being coprime to and about being prime — those conditions are what force the sequence to be a full permutation. (Coprime = "share no common factor except "; more on prime sizes in Prime table sizes.)


7. Load factor


8. The summation symbol

The parent derives expected probes with a . If that squiggle is unfamiliar, here it is from zero.


Prerequisite map

Table of slots

Base hash h1 of k

mod operation

Key k

Collision

Probe index i and offset f

Probe sequence template

Permutation of all slots

Load factor alpha

Expected probes

Summation sigma

Open Addressing topic

Read it as: the table, mod, and key combine into the base hash; a base hash makes collisions possible; collisions demand a probe rule; the probe rule must form a permutation; and separately, plus the give the cost formulas. All roads feed the topic.


Equipment checklist

Cover the right side. If you can answer every one, you are ready for the parent topic.

What does index refer to in a table of slots?
The very first slot; computers count from zero, so valid indices are through .
Compute .
(since , remainder ).
Why does a hash function use mod m?
mod m folds any key into the range , which is always a legal slot index.
What is a collision?
Two different keys map to the same home slot: .
What must equal, and why?
, so the first probe () always lands on the home slot .
Write the general probe template.
.
What does "permutation of all slots" guarantee?
Every slot is eventually visited, so an insert never fails while an empty slot still exists.
Define the load factor .
, the fraction of slots that are filled; .
What does equal for ?
(the geometric series sum).
Why start counting slots at instead of ?
So slot indices and mod m remainders are the exact same set of numbers .