3.3.6 · D1Hashing

Foundations — Load factor — when to resize, rehashing cost

2,146 words10 min readBack to topic

This page assumes you have seen none of the notation on the parent note. We build every symbol, one at a time, each resting on the previous. If you can count and share sweets among friends, you can follow every line.


0. The picture we keep returning to

Before any symbol, hold this image: a row of buckets (think laundry baskets in a corridor), and a pile of keys (think named socks) that must each be dropped into exactly one basket by a rule.

Figure — Load factor — when to resize, rehashing cost

Everything below is a label for something in this picture.


1. — the number of keys stored

If you insert one more key, goes up by one. If you delete one, goes down by one. Nothing subtle — but every later formula uses this letter, so we name it first.


2. — the number of buckets (slots)


3. The division bar and — the load factor

We have a count of stuff () and a count of space (). The natural question "how crowded?" is answered by dividing one by the other.

Figure — Load factor — when to resize, rehashing cost

Why the topic needs : it is the single dial the whole page turns. Small = fast + wasteful; large = cramped + slow. Resizing is nothing but keeping this one number in a safe range.


4. The hash function and

How does a key know which bucket to enter? A hash function decides.

But might print a number bigger than the number of baskets you own. We fix that with the modulo operation.

Figure — Load factor — when to resize, rehashing cost

5. A collision


6. — Big-O, the "how does cost grow" notation

The parent note keeps writing , , , . This is not multiplication — it is a way of describing how work scales.


7. Expected value and "average"

The parent writes . This just means average, weighted by how likely each case is.


8. The geometric series

The parent derives two results from the same mathematical fact: adding up a shrinking chain of powers.

Figure — Load factor — when to resize, rehashing cost

9. Summation sign


10. Amortized cost


How the foundations feed the topic

n = items stored

alpha = n over m

m = number of buckets

hash function h of k

bucket index

mod = wrap remainder

collisions

speed of operations

expected value

Big O growth

geometric series

open addressing probes

doubling total work

summation sign

amortized O of 1

when to resize

Load Factor and Rehashing

Trace any arrow: no box is used before the boxes feeding it were defined above. That is the whole promise of this page.


Equipment checklist

Cover the right side; say your answer aloud before revealing.

means
the number of keys currently stored in the table.
means
the number of buckets (slots), full or empty.
is
the load factor — average items per bucket / how crowded the table is.
Why divide by instead of subtracting?
crowdedness is a ratio; division gives the same answer for proportionally-scaled tables.
is
the hash function — a rule turning a key into a fixed number.
means
the remainder of divided by ; it wraps any number into .
Why can't you copy buckets on resize?
the bucket is ; changing changes the remainder, so keys must be re-placed.
A collision is
two different keys sent to the same bucket.
versus
constant work (doesn't grow) versus linear work (grows in proportion to size).
means
the average outcome over many repetitions.
The geometric series for sums to
.
means
add the term for .
Amortized cost is
average cost per operation over a run where rare operations are expensive.

Ready? Then head back to the parent topic (or its English version) and every symbol will already be an old friend. See also Hashing and Dynamic Arrays for where doubling first appears.