Hash table that grows
how it works
The idea in a few sentences and three pictures, then one example, moment by moment.
A hash table that grows keeps every number in the table's own slots, one number a slot, as open addressing does. It starts with 2 slots. A number's home is its remainder when divided by how many slots there are: in 8 slots, 25 is 3 eights and 1 over, so home 1. If its home is taken, it looks at the next slot, round to the start. A put looks for its number first, and a repeat is turned away. A new number that would fill more than half the slots then doubles them. Every number then moves into the new slots the same way, from its home to the first empty slot, the lowest old slot first. Then the new number goes in. So the table is never more than half full, and every walk stops, at the number or at an empty slot. In the pictures, a dashed slot is not yet the table's, and a second row holds slots 8 to 15. The example after these pictures puts in 17, 10, 24, 10 again, 25 and 31.
step 6 of 13
10 leaves 0 when divided by 2, so its home is slot 0.
slots looked at: 0
step 7 of 13
Slot 0 is empty, so 10 is not in the table yet.
slots looked at: 1
step 10 of 13
Slot 1 is empty, so put 17 there.
slots looked at: 2
step 11 of 13
Now 10 leaves 2 when divided by 4, so its home among the new slots is slot 2.
slots looked at: 2
step 12 of 13
Put 10 in slot 2.
slots looked at: 3
| new number | slots | with it, more than half? | slots after | numbers moved |
|---|---|---|---|---|
| 1st | 2 | no, 1 of 2 | 2 | 0 |
| 2nd | 2 | yes, 2 of 2 | 4 | 1 |
| 3rd | 4 | yes, 3 of 4 | 8 | 2 |
| 4th | 8 | no, 4 of 8 | 8 | 0 |
| 5th | 8 | yes, 5 of 8 | 16 | 4 |
| 6th to 8th | 16 | no | 16 | 0 |
| 9th | 16 | yes, 9 of 16 | 32 | 8 |
| numbers put in | slots | probes | moves |
|---|---|---|---|
| 1, 2, 3, 4, 5 | 16 | 15 | 7 |
| 1, 17, 33, 49, 65 | 16 | 39 | 7 |
step 8 of 43
With 10, 2 of the 2 slots would hold a number, more than half. Double them to 4, and take the number out to move.
what it remembers to know where it is
- doing operation
- 2
- number
- 10
- slots in the table
- 4
- slot
- not yet
- still to move
- 17
Where it says not yet, nothing is held there at that moment.
step 10 of 43
Slot 1 is empty, so put 17 there.
what it remembers to know where it is
- doing operation
- 2
- number
- 10
- slots in the table
- 4
- slot
- 1
- still to move
- not yet
Where it says not yet, nothing is held there at that moment.
step 12 of 43
Put 10 in slot 2.
what it remembers to know where it is
- doing operation
- 2
- number
- 10
- slots in the table
- 4
- slot
- 2
- still to move
- not yet
Where it says not yet, nothing is held there at that moment.
step 15 of 43
With 24, 3 of the 4 slots would hold a number, more than half. Double them to 8, and take all 2 numbers out to move.
what it remembers to know where it is
- doing operation
- 3
- number
- 24
- slots in the table
- 8
- slot
- not yet
- still to move
- 17, 10
Where it says not yet, nothing is held there at that moment.
step 23 of 43
Slot 2 already holds 10. A number goes in only once, so this one is turned away.
what it remembers to know where it is
- doing operation
- 4
- number
- 10
- slots in the table
- 8
- slot
- 2
- still to move
- not yet
Where it says not yet, nothing is held there at that moment.
step 26 of 43
Slot 2 holds 10, so look at the next slot for 25.
what it remembers to know where it is
- doing operation
- 5
- number
- 25
- slots in the table
- 8
- slot
- 2
- still to move
- not yet
Where it says not yet, nothing is held there at that moment.
step 32 of 43
With 31, 5 of the 8 slots would hold a number, more than half. Double them to 16, and take all 4 numbers out to move.
what it remembers to know where it is
- doing operation
- 6
- number
- 31
- slots in the table
- 16
- slot
- not yet
- still to move
- 24, 17, 10, 25
Where it says not yet, nothing is held there at that moment.
step 42 of 43
Put 31 in slot 15.
what it remembers to know where it is
- doing operation
- 6
- number
- 31
- slots in the table
- 16
- slot
- 15
- still to move
- not yet
Where it says not yet, nothing is held there at that moment.
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- start with 2 empty slotsPython line 11
- a number's home is the remainder it leaves when divided by how many slots there arePython line 15
- look at its home slot, then the next ones, round to the start, until it or an empty slot is found; a repeat is turned awayPython lines 16, 17, 18, 19, 22, 23 and 24
- if one more number would fill more than half the slots, double them and take every number outPython lines 25 and 26
- put each at its home among the new slots, from the lowest up, as in line 3Python lines 27, 28 and 29
- put the new number in the first empty slot from its homePython lines 31 and 32
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
def hash_table_growing(numbers):
"""Put numbers into a hash table that starts with 2 slots and grows.
A number's home is its remainder when divided by how many slots there
are; if that slot is taken, it looks at the next, round to the start.
A new number that would fill more than half the slots doubles them
first, and every number moves into the new slots the same way.
Returns the numbers held, slot by slot. Each probe looks at one slot
and compares what it holds with the number.
"""
slots = [None, None] # 2 empty slots
held = 0 # how many numbers are in
def seek(table, number): # the slot holding it, or the first empty
at = number % len(table) # its home
while True:
if table[at] == number or table[at] is None: # one probe
return at
at = (at + 1) % len(table) # the next slot, round to the start
for number in numbers:
at = seek(slots, number)
if slots[at] is not None: # a repeat is turned away
continue
if 2 * (held + 1) > len(slots): # more than half full with it
bigger = [None] * (2 * len(slots)) # double the slots
for old in slots: # from the lowest slot up
if old is not None:
bigger[seek(bigger, old)] = old # move it
slots = bigger
at = seek(slots, number) # look again from its new home
slots[at] = number
held += 1
return [x for x in slots if x is not None]
how it scales
How the moves grow as more numbers go into a table that starts with 2 slots, from ten numbers to ten million. They are measured up to ten thousand, and found by beyond. Three ways to pick the numbers: 1 to , each at its home; homes at random; and every number with home 1 in every room, the dearest in probes. The plot and tables call a move a number moved, as the watch page does.
Each step across or up is ten times the one before, a log-log plot. The three lines are one: the moves depend on how many numbers go in, never on which. The line climbs in stairs, as the slots double before the 2nd, 3rd, 5th, 9th and 17th numbers and on: 127 moves for 100 numbers, 1,023 for 1,000. So ten times the numbers gives about 8 or about 16 times the moves, as 3 or 4 doublings fall in between. From 10 numbers to 10,000, a thousand times as many, the moves go from 15 to 16,383, about a thousand times. The slope the plot reads, from a million to ten million, crosses 4 doublings: 16 times.
numbers moved, ten times more at each mark up
, how many numbers are in the table, ten times more at each mark to the right
- every number with home 1 in every roomsolid where measured by running it; dashed where its formula carries it on to ten millionslope 1.2: ten times n, 16 times the work
- each number's home at random among the slots it ends insolid where measured by running it; dashed where its formula carries it on to ten million
- 1 to , each number at its homesolid where measured by running it; dashed where its formula carries it on to ten million
| numbers moved | time | |
|---|---|---|
| 1,000 | 1,023 | about 1 microsecond |
| 100,000 | 131,071 | about 131 microseconds |
| 10,000,000 | 16,777,215 | about 17 milliseconds |
The numbers moved are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count a growing hash table's moves yourself, as the plot on this page
does. Run it with Python 3.12 or later. For each size n it puts n
different numbers into a table that starts with 2 slots, one at a time,
and prints the moves for 1 to n, the average over 20 tables whose numbers
each have a home picked at random, and the moves when every number has
home 1 in every room: the same in all three. A move is one number put into
the new slots when they double. The counts are the same on any computer;
only the time differs.
"""
import random
class Homed:
"""A number that notes every home worked out for it, and the sizes."""
homes = 0
sizes = set()
def __init__(self, value):
self.value = value
def __eq__(self, other):
return isinstance(other, Homed) and self.value == other.value
def __mod__(self, size):
Homed.homes += 1
Homed.sizes.add(size)
return self.value % size
def hash_table_growing(numbers):
"""Put numbers into a hash table that starts with 2 slots and grows.
A number's home is its remainder when divided by how many slots there
are; if that slot is taken, it looks at the next, round to the start.
A new number that would fill more than half the slots doubles them
first, and every number moves into the new slots the same way.
Returns the numbers held, slot by slot. Each probe looks at one slot
and compares what it holds with the number.
"""
slots = [None, None] # 2 empty slots
held = 0 # how many numbers are in
def seek(table, number): # the slot holding it, or the first empty
at = number % len(table) # its home
while True:
if table[at] == number or table[at] is None: # one probe
return at
at = (at + 1) % len(table) # the next slot, round to the start
for number in numbers:
at = seek(slots, number)
if slots[at] is not None: # a repeat is turned away
continue
if 2 * (held + 1) > len(slots): # more than half full with it
bigger = [None] * (2 * len(slots)) # double the slots
for old in slots: # from the lowest slot up
if old is not None:
bigger[seek(bigger, old)] = old # move it
slots = bigger
at = seek(slots, number) # look again from its new home
slots[at] = number
held += 1
return [x for x in slots if x is not None]
def count(row):
"""How many numbers the table moves on this row. Each put works out its
home once, and once more after each doubling; every other home worked
out is a move."""
Homed.homes = 0
Homed.sizes = set()
hash_table_growing([Homed(number) for number in row])
return Homed.homes - len(row) - (len(Homed.sizes) - 1)
def room(n):
"""The slots n different numbers end in: 2, doubled while more than half full."""
r = 2
while 2 * n > r:
r *= 2
return r
if __name__ == "__main__":
random.seed(1)
print(f"{'n':>6} {'1 to n':>8} {'at random':>10} {'dearest':>9}")
for n in [10, 100, 1000]:
r = room(n)
homes = [[random.randrange(r) for _ in range(n)] for _ in range(20)]
tables = [count([r * (k + 1) + h for k, h in enumerate(row)]) for row in homes]
dear = count([1 + r * i for i in range(n)])
print(f"{n:>6} {count(range(1, n + 1)):>8} {sum(tables) / 20:>10.1f} {dear:>9}")
best, worst and average
The moves are the same in every case: only how many numbers go in decides when the slots double. The probes differ, as a walk passes every number sitting side by side from its home on.
A case is one kind of input, the thing the method is given, such as every number with home 1 in every room, however long. A bound says how fast that case's cost grows as more numbers go in.
The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.
| best1 to , each number at its home | averageeach number's home at random among the slots it ends in | worstevery number with home 1 in every room | |
|---|---|---|---|
| numbers moved | , the smallest power of 2 at or above | , the same | , the same |
| slots looked at | between the two, about 334 at 100 over 20 tables | at least , 7,978 at 100 | |
| how the numbers moved grow in each case | |||
| tight, | |||
| ceiling, | |||
| floor, | |||
extra memory: twice slots after each put, 32 for 9; while 16 double to 32, both, 48
Extra memory is the room it needs on top of the input itself, while it runs.
| put | moves at this put | moves so far | twice the puts |
|---|---|---|---|
| 1 | 0 | 0 | 2 |
| 2 | 1 | 1 | 4 |
| 3 | 2 | 3 | 6 |
| 4 | 0 | 3 | 8 |
| 5 | 4 | 7 | 10 |
| 6 | 0 | 7 | 12 |
| 7 | 0 | 7 | 14 |
| 8 | 0 | 7 | 16 |
| 9 | 8 | 15 | 18 |
- the ceiling,
- the moves,
- the floor,
| numbers put in | probes | moves |
|---|---|---|
| 1 to 5 | 15 | 7 |
| 5 at random, on average | about 19.05 | 7 |
| 1, 17, 33, 49, 65 | 39 | 7 |
where the formula comes from
Each count above, worked out line by line. Each reason names the line it uses, and each 'where does this come from?' box opens a fact from another page.
the move: the slots double before half is passed
the line:why it is allowed: The table starts with 2 slots and is never more than half full. The 9th new number finds 8 numbers in 16 slots, half; with it, 9 is more than half, so the slots double. In general, with the th new number are in, so it doubles them when is more than half of them. Only the count decides it, never which numbers.
the move: price a doubling
the line:step 30 of 39
Slot 1 is empty, so put 1 there.
slots looked at: 2
step 32 of 39
Slot 2 is empty, so put 2 there.
slots looked at: 3
step 34 of 39
Slot 3 is empty, so put 3 there.
slots looked at: 4
step 36 of 39
Slot 4 is empty, so put 4 there.
slots looked at: 5
Operation 5 puts 5 in. 5 numbers in 8 slots is more than half, so the slots double to 16, and all 4 numbers move. why it is allowed: By line 1, before a new number that doubles them, the th, one fewer than fill at most half the slots, and fill more than half. So half the slots is one fewer than , and all of them move. In 16 slots, half is 8: the numbers in are at most 8, and with one more they are over 8, so they are exactly 8. The slots run 2, 4, 8, so it moves 1, then 2, then 4. Before the 5th number, 4 numbers fill 8 slots to half, and all 4 move.
guess it, thenthe move: the last half
the line:why it is allowed: For 9, the 9th doubled 16 slots to 32, so is 16: 8 is below 9, and 16 is at or above 9. In general, half the slots runs 1, 2, 4, and on, so is a power of 2. The table is at most half full, so is at least . By line 2, the last doubling came at some new number up to the th, when the numbers in, fewer than , filled half the old slots. The doubling made twice slots from , so the numbers in filled half of , and half of is below .
guess it, thenthe move: add the moves
the line:why it is allowed: is the moves of different numbers. By lines 2 and 3, the doublings move 1, 2, 4, and on, up to half of , the last one. For 9, 1 + 2 + 4 + 8, and is 16.
guess it, thenthe move: add the doublings
the line:where does this come from?
question 1 of the page doubling and halving sumsAdd . What is the total? the move: name the sum
the line:why it is allowed: Let be the doubling sum up to . For as 3, it is 1 + 2 + 4 + 8.
the move: double both sides
the line:why it is allowed: Each term doubled is the next one along, and becomes . Doubling both sides keeps them equal. The plan: the two sums share all but their ends.
the move: take line 1 away
the line:why it is allowed: Take from both sides of line 2. On the right, write out as line 1 does.
the move: tidy the left side
the line:why it is allowed: Two lots of less one lot is one lot.
the move: cancel what both brackets share
the line:when n is 3 1 2 4 8 16 first bracket 2 4 8 16 second bracket 1 2 4 8 left over take away 1 16 Line 4 when is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15. why it is allowed: Every term from 2 to is in both brackets and cancels, leaving less 1.
the move: the terms before the last
the line:why it is allowed: The sum of line 5 stopping one term sooner: line 5 with for . For 3, it is 1 + 2 + 4. At as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.
the move: tidy the power
the line:why it is allowed: One more than is . For 3, 1 + 2 + 4 is 7, one less than 8.
the move: read it
the line:why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under , twice the last.
why it is allowed: The box under this line shows a doubling sum from 1 is one less than the next doubling. In the box, is how many times 1 doubles to reach the last number added. It is not the numbers: for 9 numbers it is 3, as 1 doubled 3 times is 8, half of . After half of comes . For 9, 15 is 16 less 1.
guess it, thenthe move: half the slots is under twice the numbers
the line:why it is allowed: By line 3, half of is below . Doubling both, is below . For 9, 8 is below 9, so 16 is below 18.
guess it, thenthe move: a ceiling
the line:why it is allowed: By lines 5 and 6, is less 1, and is below , so is below . For 9, 15 is at most 17.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 7, with the fixed multiple 2.
guess it, thenthe move: half the slots holds every number
the line:why it is allowed: By line 3. For 9, 16 is at least 9.
guess it, thenthe move: a floor
the line:why it is allowed: By lines 5 and 9, less 1 is at least less 1. For 9, 15 is at least 8.
guess it, thenthe move: a simpler floor
the line:why it is allowed: From 2 on, less 1 is at least half of , as half of is at least 1. For 2, 1 is at least 1.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 11, with the fixed multiple one half, from 2 numbers on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 8 and 12 bound by the same growth, .
guess it, thenthe move: share it out
the line:why it is allowed: Line 7 divided by shares the moves over the numbers, the amortised cost. For 9, 15 over 9 is about 1.67, at most 2 less 1 over 9, about 1.89.
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: the same moves
the line:why it is allowed: 1 to are different numbers, so by line 5 of the question on moves they make moves. For 9, 15.
the move: the same ceiling
the line:why it is allowed: As line 7 of the question on moves. For 9, 15 is at most 17.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: the same moves
the line:why it is allowed: Every choice of different numbers makes moves, by line 5 of the question on moves, so their average does too. For 9, 15.
the move: the same ceiling
the line:why it is allowed: As line 7 of the question on moves. For 9, 15 is at most 17.
guess it, then
try it on small rows: over every order of 8 numbers, the average is
the move: count the walks
the line:step 14 of 22
Slot 3 is empty, so 3 is not in the table yet.
slots looked at: 1
step 17 of 22
Slot 1 is empty, so put 1 there.
slots looked at: 2
step 19 of 22
Slot 2 is empty, so put 2 there.
slots looked at: 3
step 21 of 22
Put 3 in slot 3.
slots looked at: 4
Operation 3 puts 3 in. It looks once, the slots double to 8, 1 and 2 move, and 3 looks again. 4 walks, 1 probe each. why it is allowed: Each new number walks once to look for itself. After a doubling it walks again from its new home. Each move is a walk too. Every walk probes at least 1 slot.
the move: the doublings
the line:why it is allowed: Half the slots runs 1, 2, 4, up to , by line 3 of the question on moves. That is doublings, as 1 doubles 4 times to 16.
guess it, thenthe move: on 1 to n, every walk is 1 probe
the line:why it is allowed: Take 3. It looks in 4 slots, where 1 and 2 sit in slots 1 and 2, so its home, slot 3, is empty. Then the slots double to 8: 1 and 2 move to slots 1 and 2, and 3 looks again at slot 3, empty. In general the numbers in are 1 up to one fewer than , each in the slot of its own number. They fill at most half the slots, so there are at least twice that many slots, more than from 3 on. So 's home is slot , empty. A move of a number goes to the slot of its own number too, as only smaller numbers are in yet. Only 2, in 2 slots, has home 0, also empty.
guess it, thenthe move: add the walks
the line:why it is allowed: is the probes of 1 to . By lines 1 and 3, 1 probe a walk. By line 2, walks again, and by line 5 of the question on moves, moves. For 8, 8 + 3 + 7 is 18. No different numbers cost less, as every walk probes at least 1.
guess it, thenthe move: tidy
the line:why it is allowed: Line 4 without its brackets. For 8, still 18.
guess it, thenthe move: a fact about p
the line:doublings 0 1 0 0 1 2 1 1 2 4 2 3 3 8 3 7 4 16 4 15 Each doubling adds 1 to the third column and at least 1 to the fourth, so the fourth never falls behind. why it is allowed: When is 1, both sides are 0. Each doubling adds 1 to , and adds , at least 1, to . For 8, 3 is at most 7.
guess it, thenthe move: a ceiling
the line:why it is allowed: In line 5, swap for , at least as big by line 6. For 8, 18 is at most 22.
guess it, thenthe move: whole numbers
the line:why it is allowed: By line 6 of the question on moves, is below . Both are whole numbers, so is at most . For 8, 8 is at most 15.
guess it, thenthe move: a ceiling in n alone
the line:why it is allowed: By line 8, twice is at most 4 times , less 2. Add and take 2 away, so , from line 7, is at most 5 times , less 4, which is . For 8, 22 is at most 36.
guess it, thenthe move: a simpler ceiling
the line:why it is allowed: Line 9 takes 4 away from . For 1, 1 is at most 5.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 10, with the fixed multiple 5.
guess it, thenthe move: a floor
the line:why it is allowed: In line 5, and are at least 0. For 1, 1 is at least 1.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 12.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 11 and 13 bound by the same growth, .
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: price the looks
the line:step 16 of 29
Slot 1 holds 1, so look at the next slot for 33.
slots looked at: 1
step 17 of 29
Slot 2 holds 17, so look at the next slot for 33.
slots looked at: 2
step 18 of 29
Slot 3 is empty, so 33 is not in the table yet.
slots looked at: 3
step 21 of 29
Slot 1 is empty, so put 1 there.
slots looked at: 4
step 23 of 29
Slot 1 holds 1, so look at the next slot for 17.
slots looked at: 5
step 24 of 29
Slot 2 is empty, so put 17 there.
slots looked at: 6
step 26 of 29
Slot 1 holds 1, so look at the next slot for 33.
slots looked at: 7
step 27 of 29
Slot 2 holds 17, so look at the next slot for 33.
slots looked at: 8
step 28 of 29
Put 33 in slot 3.
slots looked at: 9
Operation 3 puts 33 in. Every number has home 1, so each walk passes all those before it. 9 probes for this one put. why it is allowed: Every number has home 1 in every room, so the numbers sit side by side from slot 1. The th number's look passes every number before it, one fewer than , then an empty slot. No row costs more: every row of different numbers makes the same walks, and a walk passes at most every number in; on this row every walk does.
the move: price a doubling
the line:step 7 of 15
Slot 1 holds 1, so look at the next slot for 17.
slots looked at: 1
step 8 of 15
Slot 0 is empty, so 17 is not in the table yet.
slots looked at: 2
step 11 of 15
Slot 1 is empty, so put 1 there.
slots looked at: 3
step 13 of 15
Slot 1 holds 1, so look at the next slot for 17.
slots looked at: 4
step 14 of 15
Put 17 in slot 2.
slots looked at: 5
Operation 2 puts 17 in, after 1. Slot 1 holds 1, so the look goes round to slot 0, empty. The slots double to 4, 1 moves in 1 probe, and 17 looks again, slot 1 then slot 2. 5 probes: the look 2, then the doubling 1 + 2, with 1. why it is allowed: The moves walk 1, then 2, up to probes, each passing those moved before it. Then the new number looks again, probes. Before 33, is 2, so 1 + 2 + 3.
guess it, thenthe move: add them
the line:why it is allowed: is the probes of the dearest row. By line 1, the looks make 1 up to . By line 2, the doublings make , with 1, 2, 4, up to half of , by line 3 of the question on moves. For 3, the looks 1 + 2 + 3 and the doublings 1 + 2 and 1 + 2 + 3: 6 + 3 + 6 is 15.
guess it, thenthe move: leave out the doublings
the line:why it is allowed: is at least 0. For 3, 15 is at least 6.
guess it, thenthe move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The box under this line shows that 1 up to a last number adds to that number times one more, halved. For 3, 3 times 4, halved, is 6.
guess it, thenthe move: a floor
the line:why it is allowed: In line 5, is more than . For 3, 6 is at least 4.5.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 6, with the fixed multiple one half. So the dearest row's probes grow at least like times , where its moves grow like .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
count it yourself
Count every probe as 1 to 4 go into an empty table of 2 slots. Every walk is 1 probe.
| step | the slots, numbers only | what happens | slots looked at | numbers moved |
|---|---|---|---|---|
| 1 | Start with 2 empty slots, and 4 numbers to put in. | 0 | 0 | |
| 2 | 1 leaves 1 when divided by 2, so its home is slot 1. | 0 | 0 | |
| 3 | Slot 1 is empty, so 1 is not in the table yet. | 1 | 0 | |
| 4 | With 1, 1 of the 2 slots would hold a number. That is no more than half, so the slots stay as they are. | 1 | 0 | |
| 5 | 1 | Put 1 in slot 1.1 | 1 | 0 |
| 6 | 1 | 2 leaves 0 when divided by 2, so its home is slot 0.1 | 1 | 0 |
| 7 | 1 | Slot 0 is empty, so 2 is not in the table yet.1 | 2 | 0 |
| 8 | With 2, 2 of the 2 slots would hold a number, more than half. Double them to 4, and take the number out to move. | 2 | 0 | |
| 9 | Move 1: it leaves 1 when divided by 4, so its new home is slot 1. | 2 | 0 | |
| 10 | 1 | Slot 1 is empty, so put 1 there.1 | 3 | 1 |
| 11 | 1 | Now 2 leaves 2 when divided by 4, so its home among the new slots is slot 2.1 | 3 | 1 |
| 12 | 1, 2 | Put 2 in slot 2.1, 2 | 4 | 1 |
| 13 | 1, 2 | 3 leaves 3 when divided by 4, so its home is slot 3.1, 2 | 4 | 1 |
| 14 | 1, 2 | Slot 3 is empty, so 3 is not in the table yet.1, 2 | 5 | 1 |
| 15 | With 3, 3 of the 4 slots would hold a number, more than half. Double them to 8, and take all 2 numbers out to move. | 5 | 1 | |
| 16 | Move 1: it leaves 1 when divided by 8, so its new home is slot 1. | 5 | 1 | |
| 17 | 1 | Slot 1 is empty, so put 1 there.1 | 6 | 2 |
| 18 | 1 | Move 2: it leaves 2 when divided by 8, so its new home is slot 2.1 | 6 | 2 |
| 19 | 1, 2 | Slot 2 is empty, so put 2 there.1, 2 | 7 | 3 |
| 20 | 1, 2 | Now 3 leaves 3 when divided by 8, so its home among the new slots is slot 3.1, 2 | 7 | 3 |
| 21 | 1, 2, 3 | Put 3 in slot 3.1, 2, 3 | 8 | 3 |
| 22 | 1, 2, 3 | 4 leaves 4 when divided by 8, so its home is slot 4.1, 2, 3 | 8 | 3 |
| 23 | 1, 2, 3 | Slot 4 is empty, so 4 is not in the table yet.1, 2, 3 | 9 | 3 |
| 24 | 1, 2, 3 | With 4, 4 of the 8 slots would hold a number. That is no more than half, so the slots stay as they are.1, 2, 3 | 9 | 3 |
| 25 | 1, 2, 3, 4 | Put 4 in slot 4.1, 2, 3, 4 | 9 | 3 |
| 26 | 1, 2, 3, 4 | Done. The table holds 4 numbers in 8 slots.1, 2, 3, 4 | 9 | 3 |
In all, 9 slots looked at. The formula from section 4, , gives at : the same.
where you meet it
Hash tables that grow as they fill are in most languages.
Python's dict
Python's dict keeps its keys, the things it looks up, by open addressing. It fills at most two thirds of its slots. When those run out, it makes a bigger table and puts every key in again.
source: CPython's source, Objects/dictobject.c, insertion_resize and GROWTH_RATE
Java's HashMap
Java's HashMap doubles its table when it holds more than three quarters of its size, by default, and puts every key in its place in the new table.
source: OpenJDK's source, java/util/HashMap.java, resize and DEFAULT_LOAD_FACTOR
what is used instead
A table whose numbers also come out may shrink as well. Sebastian Bourges' books grow at three quarters full and shrink at a quarter, so a grow and a shrink are never one step apart (volume two, chapter 25). If both came at half full, a put and then a take at that edge would grow and shrink the table every time. Some tables cannot allow one slow put. They move a few numbers each put instead. The books call it 'Copying a bit at a time instead of all at once' (volume two, chapter 25).
how it compares
The table that grows beside open addressing's table of 7 slots.
| numbers put in | open addressing, 7 slots | the table that grows |
|---|---|---|
| 1 to 7 | 7 probes | 17 probes, 7 moves |
| 1 to 8 | the 8th turned away | 18 probes, 7 moves |