Skip to content

Hash table that grows

storing

Level 2 of 3

Also called Resizing a hash table, Rehashing and Dynamic hash table.

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.

  1. step 6 of 13

    012317

    10 leaves 0 when divided by 2, so its home is slot 0.

    slots looked at: 0

  2. step 7 of 13

    012317

    Slot 0 is empty, so 10 is not in the table yet.

    slots looked at: 1

  3. step 10 of 13

    012317

    Slot 1 is empty, so put 17 there.

    slots looked at: 2

  4. step 11 of 13

    012317

    Now 10 leaves 2 when divided by 4, so its home among the new slots is slot 2.

    slots looked at: 2

  5. step 12 of 13

    01231710

    Put 10 in slot 2.

    slots looked at: 3

Operation 2 puts 10 in, after 17. Its home in 2 slots, slot 0, is empty. But 2 numbers in 2 slots is more than half, so the slots double to 4. 17 moves to slot 1, and 10 goes in slot 2. 10 looks at slot 0, 17 moves in 1 probe, and 10 looks again at slot 2. 3 probes, 1 move.
new numberslotswith it, more than half?slots afternumbers moved
1st2no, 1 of 220
2nd2yes, 2 of 241
3rd4yes, 3 of 482
4th8no, 4 of 880
5th8yes, 5 of 8164
6th to 8th16no160
9th16yes, 9 of 16328
The slots double before the 2nd, 3rd, 5th and 9th different numbers, moving 1, 2, 4 and 8. 9 numbers make 15 moves in all, 1 less than 16.
numbers put inslotsprobesmoves
1, 2, 3, 4, 516157
1, 17, 33, 49, 6516397
Five numbers, two ways. 1 to 5 each land at home, 1 probe a walk: 5 first looks, 3 looks again after the 3 doublings and 7 moves: 15 walks. 1, 17, 33, 49 and 65 each leave 1 when divided by 2, 4, 8 or 16. So all have home 1 in every room, and each walk passes every number before it. The probes differ; the moves are the same.
Growing, and a repeat: 8 moments of its 43 steps, the steps between them left for the watch page
  1. step 8 of 43

    0123456708

    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.

    watch from here

  2. step 10 of 43

    012345670817

    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.

    watch from here

  3. step 12 of 43

    01234567081710

    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.

    watch from here

  4. step 15 of 43

    0123456708

    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.

    watch from here

  5. step 23 of 43

    0123456708241710

    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.

    watch from here

  6. step 26 of 43

    0123456708241710

    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.

    watch from here

  7. step 32 of 43

    0123456708

    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.

    watch from here

  8. step 42 of 43

    01234567081724251031

    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.

    watch from here

the steps, in plain words
  1. start with 2 empty slotsPython line 11
  2. a number's home is the remainder it leaves when divided by how many slots there arePython line 15
  3. 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
  4. if one more number would fill more than half the slots, double them and take every number outPython lines 25 and 26
  5. put each at its home among the new slots, from the lowest up, as in line 3Python lines 27, 28 and 29
  6. 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 p−1p - 1 beyond. Three ways to pick the numbers: 1 to nn, 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

slope 1.2102104106108101102103104105106107slope 1.2101102103104105106107108101102103104105106107slope 1.2101102103104105106107108101102103104105106107

nn, 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 nn, each number at its homesolid where measured by running it; dashed where its formula carries it on to ten million
what the dearest row costs in moves, at a billion moves a second
nnnumbers movedtime
1,0001,023about 1 microsecond
100,000131,071about 131 microseconds
10,000,00016,777,215about 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.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
best1 to nn, each number at its homeaverageeach number's home at random among the slots it ends inworstevery number with home 1 in every room
numbers movedp−1p - 1, pp the smallest power of 2 at or above nnp−1p - 1, the samep−1p - 1, the same
slots looked atn+log⁡2 p+p−1n + \log_2\, p + p - 1between the two, about 334 at 100 over 20 tablesat least n22\frac{n^2}{2}, 7,978 at 100
how the numbers moved grow in each case
tight, Θ\ThetaΘ(n)\Theta(n)Θ(n)\Theta(n)Θ(n)\Theta(n)
ceiling, OOO(n)O(n)O(n)O(n)O(n)O(n)
floor, Ω\OmegaΩ(n)\Omega(n)Ω(n)\Omega(n)Ω(n)\Omega(n)

extra memory: twice pp 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.

putmoves at this putmoves so fartwice the puts
1002
2114
3236
4038
54710
60712
70714
80716
981518
Each put's moves, and the moves so far. They stay under twice the puts.
movesnumbers put in01020304005101520movesnumbers put in01020304005101520
  • the ceiling, 2n−12n - 1
  • the moves, p−1p - 1
  • the floor, n−1n - 1
The moves climb in stairs, flat between doublings. They stay between n−1n - 1 and 2n−12n - 1, two lines of the same growth.
numbers put inprobesmoves
1 to 5157
5 at random, on averageabout 19.057
1, 17, 33, 49, 65397
The probes of 5 numbers, three ways. 5 numbers end in 16 slots. The average is worked out over every choice of homes among those 16 for each of 5 numbers, 16 times itself 5 times, 1,048,576 choices.

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.

Why do nn different numbers make p−1p - 1 moves, whichever they are, and under 2n2n?
  1. the move: the slots double before half is passed

    the line:
    a new number that would fillmore than half doubles them\begin{gathered} \text{a new number that would fill} \\ \text{more than half doubles them} \end{gathered}

    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 kkth new number kk are in, so it doubles them when kk is more than half of them. Only the count decides it, never which numbers.

  2. the move: price a doubling

    the line:
    a doubling moves everynumber in, exactly halfthe slots: 1, 2, 4, and on\begin{gathered} \text{a doubling moves every} \\ \text{number in, exactly half} \\ \text{the slots: 1, 2, 4, and on} \end{gathered}
    1. step 30 of 39

      01234567081

      Slot 1 is empty, so put 1 there.

      slots looked at: 2

    2. step 32 of 39

      012345670812

      Slot 2 is empty, so put 2 there.

      slots looked at: 3

    3. step 34 of 39

      0123456708123

      Slot 3 is empty, so put 3 there.

      slots looked at: 4

    4. step 36 of 39

      01234567081234

      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 kkth, one fewer than kk fill at most half the slots, and kk fill more than half. So half the slots is one fewer than kk, 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.

  3. the move: the last half

    the line:
    after n numbers, halfthe slots is p, the smallestpower of 2 at or above n\begin{gathered} \text{after } n \text{ numbers, half} \\ \text{the slots is } p \text{, the smallest} \\ \text{power of 2 at or above } n \end{gathered}

    why it is allowed: For 9, the 9th doubled 16 slots to 32, so pp 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 pp is a power of 2. The table is at most half full, so pp is at least nn. By line 2, the last doubling came at some new number up to the nnth, when the numbers in, fewer than nn, filled half the old slots. The doubling made twice pp slots from pp, so the numbers in filled half of pp, and half of pp is below nn.

  4. the move: add the moves

    the line:
    M=1+2+4+⋯+p2M = 1 + 2 + 4 + \dots + \frac{p}{2}

    why it is allowed: MM is the moves of nn different numbers. By lines 2 and 3, the doublings move 1, 2, 4, and on, up to half of pp, the last one. For 9, 1 + 2 + 4 + 8, and pp is 16.

  5. the move: add the doublings

    the line:
    M=p−1M = p - 1
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

      the line:
      S=1+2+4+⋯+2nS = 1 + 2 + 4 + \dots + 2^n

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      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 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    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, nn 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 pp. After half of pp comes pp. For 9, 15 is 16 less 1.

  6. the move: half the slots is under twice the numbers

    the line:
    p<2np < 2n

    why it is allowed: By line 3, half of pp is below nn. Doubling both, pp is below 2n2n. For 9, 8 is below 9, so 16 is below 18.

  7. the move: a ceiling

    the line:
    M≤2n−1M \le 2n - 1

    why it is allowed: By lines 5 and 6, MM is pp less 1, and pp is below 2n2n, so MM is below 2n−12n - 1. For 9, 15 is at most 17.

  8. the move: read it as a bound

    the line:
    M∈O(n)M \in O(n)

    why it is allowed: By line 7, with the fixed multiple 2.

  9. the move: half the slots holds every number

    the line:
    p≥np \ge n

    why it is allowed: By line 3. For 9, 16 is at least 9.

  10. the move: a floor

    the line:
    M≥n−1M \ge n - 1

    why it is allowed: By lines 5 and 9, pp less 1 is at least nn less 1. For 9, 15 is at least 8.

  11. the move: a simpler floor

    the line:
    M≥n2 for n≥2M \ge \frac{n}{2} \text{ for } n \ge 2

    why it is allowed: From 2 on, nn less 1 is at least half of nn, as half of nn is at least 1. For 2, 1 is at least 1.

  12. the move: read it as a bound

    the line:
    M∈Ω(n)M \in \Omega(n)

    why it is allowed: By line 11, with the fixed multiple one half, from 2 numbers on.

  13. the move: ceiling and floor together

    the line:
    M∈Θ(n)M \in \Theta(n)

    why it is allowed: Lines 8 and 12 bound MM by the same growth, nn.

  14. the move: share it out

    the line:
    Mn≤2−1n\frac{M}{n} \le 2 - \frac{1}{n}

    why it is allowed: Line 7 divided by nn shares the moves over the nn numbers, the amortised cost. For 9, 15 over 9 is about 1.67, at most 2 less 1 over 9, about 1.89.

try it with a small nn: at n=8n = 8, line 4 gives M=7M = 7, which you can work out by hand

How many moves do 1 to nn make?
  1. the move: the same moves

    the line:
    M=p−1M = p - 1

    why it is allowed: 1 to nn are nn different numbers, so by line 5 of the question on moves they make p−1p - 1 moves. For 9, 15.

  2. the move: the same ceiling

    the line:
    M≤2n−1M \le 2n - 1

    why it is allowed: As line 7 of the question on moves. For 9, 15 is at most 17.

try it with a small nn: at n=8n = 8, line 1 gives M=7M = 7, which you can work out by hand

Numbers at random. How many moves do they make on average?
  1. the move: the same moves

    the line:
    M=p−1M = p - 1

    why it is allowed: Every choice of nn different numbers makes p−1p - 1 moves, by line 5 of the question on moves, so their average does too. For 9, 15.

  2. the move: the same ceiling

    the line:
    M≤2n−1M \le 2n - 1

    why it is allowed: As line 7 of the question on moves. For 9, 15 is at most 17.

try it on small rows: over every order of 8 numbers, the average is M=7M = 7

Why do 1 to nn cost n+log⁡2 p+p−1n + \log_2\, p + p - 1 probes, the fewest any nn different numbers can?
  1. the move: count the walks

    the line:
    n walks to look, one moreafter each doubling, one a move\begin{gathered} n \text{ walks to look, one more} \\ \text{after each doubling, one a move} \end{gathered}
    1. step 14 of 22

      0123456712

      Slot 3 is empty, so 3 is not in the table yet.

      slots looked at: 1

    2. step 17 of 22

      012345671

      Slot 1 is empty, so put 1 there.

      slots looked at: 2

    3. step 19 of 22

      0123456712

      Slot 2 is empty, so put 2 there.

      slots looked at: 3

    4. step 21 of 22

      01234567123

      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.

  2. the move: the doublings

    the line:
    the slots double  log⁡2 p times\begin{gathered} \text{the slots double } \,\log_2\, p \text{ times} \end{gathered}

    why it is allowed: Half the slots runs 1, 2, 4, up to pp, by line 3 of the question on moves. That is log⁡2 p\log_2\, p doublings, as 1 doubles 4 times to 16.

  3. the move: on 1 to n, every walk is 1 probe

    the line:
    on 1 to n, each numberlands at its home\begin{gathered} \text{on 1 to } n \text{, each number} \\ \text{lands at its home} \end{gathered}

    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 kk, 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 kk from 3 on. So kk's home is slot kk, 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.

  4. the move: add the walks

    the line:
    B=n+log⁡2 p+(p−1)B = n + \log_2\, p + (p - 1)

    why it is allowed: BB is the probes of 1 to nn. By lines 1 and 3, 1 probe a walk. By line 2, log⁡2 p\log_2\, p walks again, and by line 5 of the question on moves, p−1p - 1 moves. For 8, 8 + 3 + 7 is 18. No nn different numbers cost less, as every walk probes at least 1.

  5. the move: tidy

    the line:
    B=n+log⁡2 p+p−1B = n + \log_2\, p + p - 1

    why it is allowed: Line 4 without its brackets. For 8, still 18.

  6. the move: a fact about p

    the line:
    log⁡2 p≤p−1\log_2\, p \le p - 1
    doublingspplog⁡2 p\log_2\, pp−1p - 1
    0100
    1211
    2423
    3837
    416415
    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 pp is 1, both sides are 0. Each doubling adds 1 to log⁡2 p\log_2\, p, and adds pp, at least 1, to p−1p - 1. For 8, 3 is at most 7.

  7. the move: a ceiling

    the line:
    B≤n+2p−2B \le n + 2p - 2

    why it is allowed: In line 5, swap log⁡2 p\log_2\, p for p−1p - 1, at least as big by line 6. For 8, 18 is at most 22.

  8. the move: whole numbers

    the line:
    p≤2n−1p \le 2n - 1

    why it is allowed: By line 6 of the question on moves, pp is below 2n2n. Both are whole numbers, so pp is at most 2n−12n - 1. For 8, 8 is at most 15.

  9. the move: a ceiling in n alone

    the line:
    B≤5n−4B \le 5n - 4

    why it is allowed: By line 8, twice pp is at most 4 times nn, less 2. Add nn and take 2 away, so n+2p−2n + 2p - 2, from line 7, is at most 5 times nn, less 4, which is 5n−45n - 4. For 8, 22 is at most 36.

  10. the move: a simpler ceiling

    the line:
    B≤5nB \le 5n

    why it is allowed: Line 9 takes 4 away from 5n5n. For 1, 1 is at most 5.

  11. the move: read it as a bound

    the line:
    B∈O(n)B \in O(n)

    why it is allowed: By line 10, with the fixed multiple 5.

  12. the move: a floor

    the line:
    B≥nB \ge n

    why it is allowed: In line 5, log⁡2 p\log_2\, p and p−1p - 1 are at least 0. For 1, 1 is at least 1.

  13. the move: read it as a bound

    the line:
    B∈Ω(n)B \in \Omega(n)

    why it is allowed: By line 12.

  14. the move: ceiling and floor together

    the line:
    B∈Θ(n)B \in \Theta(n)

    why it is allowed: Lines 11 and 13 bound BB by the same growth, nn.

try it with a small nn: at n=4n = 4, line 4 gives B=9B = 9, which you can work out by hand

Every number with home 1 in every room. Why do its probes grow at least like n2n^2?
  1. the move: price the looks

    the line:
    the kth number’s lookprobes k slots\begin{gathered} \text{the } k\text{th number's look} \\ \text{probes } k \text{ slots} \end{gathered}
    1. step 16 of 29

      01234567117

      Slot 1 holds 1, so look at the next slot for 33.

      slots looked at: 1

    2. step 17 of 29

      01234567117

      Slot 2 holds 17, so look at the next slot for 33.

      slots looked at: 2

    3. step 18 of 29

      01234567117

      Slot 3 is empty, so 33 is not in the table yet.

      slots looked at: 3

    4. step 21 of 29

      012345671

      Slot 1 is empty, so put 1 there.

      slots looked at: 4

    5. step 23 of 29

      012345671

      Slot 1 holds 1, so look at the next slot for 17.

      slots looked at: 5

    6. step 24 of 29

      01234567117

      Slot 2 is empty, so put 17 there.

      slots looked at: 6

    7. step 26 of 29

      01234567117

      Slot 1 holds 1, so look at the next slot for 33.

      slots looked at: 7

    8. step 27 of 29

      01234567117

      Slot 2 holds 17, so look at the next slot for 33.

      slots looked at: 8

    9. step 28 of 29

      0123456711733

      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 kkth number's look passes every number before it, one fewer than kk, then an empty slot. No row costs more: every row of nn different numbers makes the same walks, and a walk passes at most every number in; on this row every walk does.

  2. the move: price a doubling

    the line:
    a doubling with m numbers inprobes 1+2+⋯+(m+1)\begin{gathered} \text{a doubling with } m \text{ numbers in} \\ \text{probes } 1 + 2 + \dots + (m + 1) \end{gathered}
    1. step 7 of 15

      01231

      Slot 1 holds 1, so look at the next slot for 17.

      slots looked at: 1

    2. step 8 of 15

      01231

      Slot 0 is empty, so 17 is not in the table yet.

      slots looked at: 2

    3. step 11 of 15

      01231

      Slot 1 is empty, so put 1 there.

      slots looked at: 3

    4. step 13 of 15

      01231

      Slot 1 holds 1, so look at the next slot for 17.

      slots looked at: 4

    5. step 14 of 15

      0123117

      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 mm 1.

    why it is allowed: The moves walk 1, then 2, up to mm probes, each passing those moved before it. Then the new number looks again, m+1m + 1 probes. Before 33, mm is 2, so 1 + 2 + 3.

  3. the move: add them

    the line:
    W=(1+2+⋯+n)+DW = (1 + 2 + \dots + n) + D

    why it is allowed: WW is the probes of the dearest row. By line 1, the looks make 1 up to nn. By line 2, the doublings make DD, with mm 1, 2, 4, up to half of pp, 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.

  4. the move: leave out the doublings

    the line:
    W≥1+2+⋯+nW \ge 1 + 2 + \dots + n

    why it is allowed: DD is at least 0. For 3, 15 is at least 6.

  5. the move: use the page on adding up

    the line:
    W≥n(n+1)2W \ge \frac{n(n + 1)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    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.

  6. the move: a floor

    the line:
    W≥n22W \ge \frac{n^2}{2}

    why it is allowed: In line 5, n+1n + 1 is more than nn. For 3, 6 is at least 4.5.

  7. the move: read it as a bound

    the line:
    W∈Ω(n2)W \in \Omega(n^2)

    why it is allowed: By line 6, with the fixed multiple one half. So the dearest row's probes grow at least like nn times nn, where its moves grow like nn.

try it with a small nn: at n=3n = 3, line 3 gives W=15W = 15, 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.

1, 2, 3, 4, every step written down
stepthe slots, numbers onlywhat happensslots looked atnumbers moved
1Start with 2 empty slots, and 4 numbers to put in.00
21 leaves 1 when divided by 2, so its home is slot 1.00
3Slot 1 is empty, so 1 is not in the table yet.10
4With 1, 1 of the 2 slots would hold a number. That is no more than half, so the slots stay as they are.10
51Put 1 in slot 1.110
612 leaves 0 when divided by 2, so its home is slot 0.110
71Slot 0 is empty, so 2 is not in the table yet.120
8With 2, 2 of the 2 slots would hold a number, more than half. Double them to 4, and take the number out to move.20
9Move 1: it leaves 1 when divided by 4, so its new home is slot 1.20
101Slot 1 is empty, so put 1 there.131
111Now 2 leaves 2 when divided by 4, so its home among the new slots is slot 2.131
121, 2Put 2 in slot 2.1, 241
131, 23 leaves 3 when divided by 4, so its home is slot 3.1, 241
141, 2Slot 3 is empty, so 3 is not in the table yet.1, 251
15With 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.51
16Move 1: it leaves 1 when divided by 8, so its new home is slot 1.51
171Slot 1 is empty, so put 1 there.162
181Move 2: it leaves 2 when divided by 8, so its new home is slot 2.162
191, 2Slot 2 is empty, so put 2 there.1, 273
201, 2Now 3 leaves 3 when divided by 8, so its home among the new slots is slot 3.1, 273
211, 2, 3Put 3 in slot 3.1, 2, 383
221, 2, 34 leaves 4 when divided by 8, so its home is slot 4.1, 2, 383
231, 2, 3Slot 4 is empty, so 4 is not in the table yet.1, 2, 393
241, 2, 3With 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, 393
251, 2, 3, 4Put 4 in slot 4.1, 2, 3, 493
261, 2, 3, 4Done. The table holds 4 numbers in 8 slots.1, 2, 3, 493

In all, 9 slots looked at. The formula from section 4, n+log⁡2 p+p−1n + \log_2\, p + p - 1, gives B=9B = 9 at n=4n = 4: 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 inopen addressing, 7 slotsthe table that grows
1 to 77 probes17 probes, 7 moves
1 to 8the 8th turned away18 probes, 7 moves
A table of 7 slots fills at 7 and turns the next number away. The table that grows always has room, and pays in moves.