3.3.3 · D1Hashing

Foundations — Chaining — linked lists in buckets, load factor, resizing

3,346 words15 min readBack to topic

Before you can read the parent note Chaining — linked lists in buckets, load factor, resizing, every squiggle it uses has to mean something. This page builds each one from nothing. Read top to bottom; each block only uses ideas from the blocks above it.


1. A "key" and the "universe"

Picture. Imagine a giant bag holding one ticket for every possible username on Earth. That whole bag is ; a single ticket you pull out is a . The tickets you actually saved today are just a handful pulled out of it.

Why the topic needs it. The whole difficulty of hashing comes from one fact: is enormous (all possible IDs), but your storage is tiny (a small array). We need language to say "far more possible keys than boxes." That is what (and its members ) give us.


2. The array and its indices

Figure 1 (below) shows the row of cells with their indices. Notice the leftmost cell is labelled .

Figure — Chaining — linked lists in buckets, load factor, resizing

Why start at 0? Because the hash rule (next sections) will hand us a remainder, and remainders naturally start at 0. In Figure 1 the amber arrow points at cell , not .

Why the topic needs it. The magic of an array is that jumping to cell takes the same time no matter which — the computer computes an address by arithmetic and goes straight there. This "instant jump" is the reason hashing can be fast. Walking a list, by contrast, is one-step-at-a-time (see §7).

  • ::: the number of buckets / array slots, a positive integer (). Read it as "how many boxes on the wall."

3. The symbol and "much greater than"

Picture. is a huge bag of tickets next to a tiny row of boxes. There is no way each ticket gets its own box.

Why the topic needs it. is the reason collisions are unavoidable — the seed of the whole Pigeonhole Principle argument. If you have more pigeons () than holes (), some hole gets two pigeons. Hold that thought for §8.


4. The modulo operation

Picture. Count around a clock face with hours. Start at 0 and step forward times; where you stop is . A clock is a mod- machine.

Why the topic needs it. We introduce mod before the hash function because the hash function is built on top of it. It is what squeezes any big integer into the box range — and it is why resizing changes where keys go: if changes from 4 to 8, then but — the same value moves boxes. That is the whole point of §"Resizing" in the parent note.


5. From arbitrary keys to integers (feeding the hash)

Picture. Two machines in a row: the first stamps every letter with a numeric barcode; the second folds that barcode onto the clock face of boxes.

Why the topic needs it. Without this stage the parent note's "" would only ever handle integer keys. For the rest of this page we assume keys are already integers (or already turned into ), so we can write cleanly. See Hash Functions for how is built well.


6. The hash function as a map, and where collisions come from

Figure 2 (below) draws as a sorting clerk: you hand it a key, it points to exactly one mailbox. Same key → same box, always. Watch the two amber arrows in Figure 2 — two different keys getting sent to the same box.

Figure — Chaining — linked lists in buckets, load factor, resizing

Why the topic needs it. is the "instant jump" rule. Instead of searching, you compute and land on the right box in one step. Everything else in the parent note is about what to do when two keys land in the same box.


7. Linked lists and the pointer (node arrow)

Figure 3 (below) shows a three-node chain. Follow the cyan node-arrows left to right: to reach the 3rd key you must hop through the 1st and 2nd first.

Figure — Chaining — linked lists in buckets, load factor, resizing

Picture. Paper clips linking letters into a string. To read the 3rd letter you must clip-walk from the 1st, to the 2nd, to the 3rd — one hop at a time. There is no jumping.

Why the topic needs it. In chaining, each array cell is a head pointer to one of these chains — the "bucket." Because reading a list is one-hop-at-a-time, the cost of a lookup is how long that chain is. Keeping chains short = keeping lookups fast. See Linked Lists for the full data structure.


8. The Pigeonhole Principle (a picture, not a formula)

Figure 4 (below) shows 5 pigeons forced into 3 holes — one hole must take two.

Figure — Chaining — linked lists in buckets, load factor, resizing

Here the "pigeons" are all possible keys () and the "holes" are the boxes. Since (§3), some box is forced to receive more than one key.

Why the topic needs it. This is the proof that collisions can never be fully avoided — so we don't try. Chaining and Open Addressing are both just answers to "given collisions must happen, how do we cope?" See Pigeonhole Principle.


9. Counting symbols: , expectation , and the load factor

Picture. Total letters divided by total boxes = the average height of a chain. That average height is what you'll walk through on a lookup — so is a direct estimate of search cost.

Why the topic needs it. is the single knob the whole parent note tunes. Small → short chains → fast. Big → long chains → slow. Resizing exists purely to keep from growing.


10. Big-O notation and "amortized"

Picture. is a flat line. is a straight rising line. As the variable doubles, stays put while doubles.

Picture. A rent of $1200 paid once a month feels like $40 a day when spread across 30 days. Resizing is the once-a-month big bill; spread over many cheap inserts it averages to . Full treatment in Amortized Analysis; the same trick powers Dynamic Arrays.

Why the topic needs it. Without "amortized," resizing looks like an disaster on some inserts. Amortized language is what lets us honestly call insert "."


The prerequisite map

Universe U of possible keys

U is much bigger than m

Array T with m boxes

Pigeonhole forces collisions

Turn any key into an integer code

Hash function h

Modulo remainder

Collision h k1 equals h k2

Linked lists and pointers

Chaining stores a list per box

n stored keys

Load factor alpha equals n over m

Uniform hashing assumption

Expected cost O of 1 plus alpha

Expected value E

Big-O and amortized

Worst case O of n if all collide

Resize to keep alpha small


Equipment checklist

Cover the right side and answer aloud before revealing.

What symbols stand for one key, all possible keys, and how many boxes?
= one key, = the universe of all possible keys, = number of boxes.
Why must be a positive integer?
You cannot have zero or a fraction of an array cell; .
Why is ?
There are far more possible keys than boxes, which forces collisions.
What does mean?
The contents of cell number in the array ; here it is the head pointer of a bucket's linked list.
What is and why is its result always a legal index?
The remainder after dividing by ; remainders lie in , exactly the index range.
Why must a real key become an integer before mod?
mod only works on whole numbers, so first turns strings/UUIDs into a non-negative integer .
In the string hash , what does index?
The position of a character in the string (0 for the first, 1 for the second, ...).
What do the two different arrows and mean?
is a function mapping (a rule input-to-output); is a linked-list pointer (a physical link node-to-node).
Is deterministic?
Yes — the same key always maps to the same box, or you could never find it again.
What is and ?
and — the same value can move boxes when changes.
State the Pigeonhole Principle and what plays the pigeons/holes here.
More pigeons than holes forces a shared hole; keys are pigeons, boxes are holes, so some box gets keys.
What is a collision?
Two different keys with .
Define , , and .
= stored keys, = number of boxes, = average keys per box.
What does mean?
The expected (long-run average) value of a random quantity over all the ways the randomness could turn out.
What assumption makes average chain length equal , and why?
SUHA — each key is equally likely to fall in any box independently, so .
In , which variable is growing?
The load factor (the average chain length), not directly.
What is the worst-case lookup cost and when does it happen?
, when all keys collide into a single chain (SUHA violated).
What does "amortized " mean?
Total cost over many operations divided by their count is constant, even if one operation is occasionally expensive.

Connections

  • Chaining — linked lists in buckets, load factor, resizing — the topic these foundations feed.
  • Hash Functions — how and are actually built; whether SUHA holds.
  • Linked Lists — the bucket data structure.
  • Pigeonhole Principle — why collisions must happen.
  • Open Addressing — the rival collision strategy.
  • Amortized Analysis · Dynamic Arrays — the doubling/averaging idea behind resize.