Skip to content

Hash table with open addressing

storing

Level 2 of 3

Also called Linear probing.

how it works

The idea in a few sentences and three pictures, then one example, moment by moment.

A hash table with open addressing keeps every number in the table's own slots, one number a slot, with no chains of numbers, and each number once. This one has 7 slots, numbered 0 to 6. A number's home is its remainder when divided by 7, the table's hash: 38 is 5 sevens and 3 over, so home 3. To put a number in, you look at its home, then the next slots, round from slot 6 to slot 0, until a free one: linear probing. Each slot looked at is a probe. A take walks the same way to the number and leaves a marker. A search stops at an empty slot, since a put would have filled it, so the marker keeps it walking. A put that passes a marker walks on to a free slot, or round all 7, to be sure the number is not further along. If it is not, it goes in the marker's slot. The example after these pictures puts in 17, 24, 9 and 31, takes 24 and 31 out, then puts in 38.

  1. step 9 of 13

    012345691724

    31 leaves 3 when divided by 7, so its home is slot 3.

    slots looked at: 0

  2. step 10 of 13

    012345691724

    Slot 3 holds 17, not 31.

    slots looked at: 1

  3. step 11 of 13

    012345691724

    Slot 4 holds 24, not 31.

    slots looked at: 2

  4. step 12 of 13

    01234569172431

    Slot 5 is free, so put 31 there.

    slots looked at: 3

Operation 4 puts 31 in, after 17, 24 and 9. Its home, slot 3, holds 17, and slot 4 holds 24, so 31 goes on to slot 5. 3 probes. Yellow is the slot looked at, red the slot just filled.
putnumberhomeits walkprobes
17001
21400, 12
32100, 1, 23
42800, 1, 2, 34
Four numbers with one home, 0. Each one walks past the run of filled slots the others made, one probe further each time.
the tableslot 4slot 5slot 6a search for 39a put of 39
25, 32, 39 put in253239found, 3 probesalready in
32 taken, a marker left25×39walks on: found, 3 probeswalks past ×: already in
32 taken, slot emptied25empty39stops: missing, 2 probesstops: 39 twice
a put that stopped at ×25×39puts 39 in slot 5: twice
25, 32 and 39, all home 4, fill slots 4, 5 and 6. An empty slot ends a walk, so emptying 32's slot would hide 39. A put walks past a marker, as its number may be further along.
Putting in and taking out: 8 moments of its 27 steps, the steps between them left for the watch page
  1. step 3 of 27

    012345617

    Slot 3 is free, so put 17 there.

    what it remembers to know where it is
    doing operation
    1
    number
    17
    slot
    3

    watch from here

  2. step 12 of 27

    01234569172431

    Slot 5 is free, so put 31 there.

    what it remembers to know where it is
    doing operation
    4
    number
    31
    slot
    5

    watch from here

  3. step 16 of 27

    0123456917×31

    Take 24 out of slot 4, and leave a marker so later searches walk past it.

    what it remembers to know where it is
    doing operation
    5
    number
    24
    slot
    4

    watch from here

  4. step 19 of 27

    0123456917×31

    Slot 4 holds a marker left by a number taken out, not 31.

    what it remembers to know where it is
    doing operation
    6
    number
    31
    slot
    4

    watch from here

  5. step 21 of 27

    0123456917××

    Take 31 out of slot 5, and leave a marker so later searches walk past it.

    what it remembers to know where it is
    doing operation
    6
    number
    31
    slot
    5

    watch from here

  6. step 24 of 27

    0123456917××

    Slot 4 holds a marker left by a number taken out. Note it: 38 can go here if it is nowhere further along.

    what it remembers to know where it is
    doing operation
    7
    number
    38
    slot
    4

    watch from here

  7. step 25 of 27

    0123456917××

    Slot 5 holds a marker left by a number taken out, not 38.

    what it remembers to know where it is
    doing operation
    7
    number
    38
    slot
    5

    watch from here

  8. step 26 of 27

    012345691738×

    38 is nowhere further along, so put it in slot 4, where the first marker was.

    what it remembers to know where it is
    doing operation
    7
    number
    38
    slot
    4

    watch from here

the steps, in plain words
  1. start with 7 empty slotsPython line 12
  2. a number's home is the remainder it leaves when divided by 7Python line 14
  3. look at its home slot, then the next ones, round to the startPython lines 17, 18 and 19
  4. to put it in, stop at a free slot or use the first marker passed; a repeat, or no room, is turned awayPython lines 15, 22, 23, 24, 25, 26, 27 and 28
  5. to take it out, stop at it and leave a markerPython lines 29 and 30
  6. an empty slot, or a whole round, ends the search, and the number is not therePython lines 19, 20 and 21
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.

class Marker:
    """What a take leaves in its slot: no number, yet not empty."""


def hash_table_open_addressing(operations):
    """Run a list of operations on a hash table of 7 slots, one number each.

    Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
    out. Returns the numbers held, slot by slot. Each probe looks at one
    slot and compares what it holds with n.
    """
    slots = [None] * 7  # 7 empty slots, numbered 0 to 6
    for action, number in operations:
        home = number % 7  # its remainder when divided by 7
        marker = None  # the first marker a put passes
        stop = None  # the slot that ends the search
        for i in range(7):  # home, then the next slots, round to the start
            s = (home + i) % 7
            if slots[s] == number or slots[s] is None:  # one probe
                stop = s
                break
            if isinstance(slots[s], Marker) and marker is None:
                marker = s
        if action == 'put':
            if stop is not None and slots[stop] is None:  # not there yet
                slots[stop if marker is None else marker] = number
            elif stop is None and marker is not None:  # no empty slot
                slots[marker] = number
        elif stop is not None and slots[stop] is not None:  # found it
            slots[stop] = Marker()  # take it out, leave a marker
    return [x for x in slots if x is not None and not isinstance(x, Marker)]

how it scales

How the probes grow, from ten numbers to a million, each put into an empty table of 7 slots one at a time. Three ways to pick them: homes all different while there is room (1, 2, 3 and on), homes at random, and one home (7, 14, 21 and on). The plot and tables call a probe a slot looked at, as the watch page does.

Each step across or up is ten times the one before, a log-log plot. From 100 numbers on, all three lines climb one step up for each step across: ten times the numbers, a little over ten times the probes. The 7 slots are full by the 7th number, and from then on every put looks at all 7 and is turned away. So the lines nearly meet: 658, about 665 and 679 probes at 100 numbers. What differs is how the table fills, which the grid in section 3 shows.

slots looked at, ten times more at each mark up

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

nn, how many numbers are in the table, ten times more at each mark to the right

  • one home, 7, 14, and on up to 7n7nsolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the work
  • each number's home at random, every slot as likelypoints only, each measured by a real run, since no single formula for the slots looked at of this row is worked out here
  • homes all different while there is room, 1 to nnsolid where measured by running it; dashed where its formula carries it on to ten million
what numbers with one home cost, at a billion probes a second
nnslots looked attime
1,0006,979about 7 microseconds
100,000699,979about 700 microseconds
10,000,00069,999,979about 70 milliseconds

The slots looked at 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 hash table's probes 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 an empty table of 7 slots, one at a time, and prints the
probes for 1 to n (each its own home while there is room, the cheapest),
the average over 20 tables whose numbers each have a home picked at
random, and the probes for 7, 14, up to 7n (every home 0, the dearest).
Past 7 numbers the table is full, and each put probes all 7 slots. A probe
is one slot looked at. The counts are the same on any computer; only the
time differs.
"""
import random


class Counted:
    """A number that counts the probes made with it: one a slot looked at."""

    probes = 0

    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        Counted.probes += 1
        return isinstance(other, Counted) and self.value == other.value

    def __mod__(self, other):
        return self.value % other  # finding the home is no probe


class Marker:
    """What a take leaves in its slot: no number, yet not empty."""


def hash_table_open_addressing(operations):
    """Run a list of operations on a hash table of 7 slots, one number each.

    Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
    out. Returns the numbers held, slot by slot. Each probe looks at one
    slot and compares what it holds with n.
    """
    slots = [None] * 7  # 7 empty slots, numbered 0 to 6
    for action, number in operations:
        home = number % 7  # its remainder when divided by 7
        marker = None  # the first marker a put passes
        stop = None  # the slot that ends the search
        for i in range(7):  # home, then the next slots, round to the start
            s = (home + i) % 7
            if slots[s] == number or slots[s] is None:  # one probe
                stop = s
                break
            if isinstance(slots[s], Marker) and marker is None:
                marker = s
        if action == 'put':
            if stop is not None and slots[stop] is None:  # not there yet
                slots[stop if marker is None else marker] = number
            elif stop is None and marker is not None:  # no empty slot
                slots[marker] = number
        elif stop is not None and slots[stop] is not None:  # found it
            slots[stop] = Marker()  # take it out, leave a marker
    return [x for x in slots if x is not None and not isinstance(x, Marker)]


def cheapest(n):
    """1 to n put in: homes 1, 2, 3, 4, 5, 6, 0, then the table is full."""
    return [('put', number) for number in range(1, n + 1)]


def count(operations):
    """How many probes the table makes on this list of operations."""
    Counted.probes = 0
    hash_table_open_addressing([(action, Counted(number)) for action, number in operations])
    return Counted.probes


if __name__ == "__main__":
    random.seed(1)
    print(f"{'n':>6} {'own homes':>10} {'at random':>10} {'one home':>9}")
    for n in [10, 100, 1000]:
        tables = []
        for _ in range(20):
            row = [7 * i + random.randrange(7) for i in range(1, n + 1)]
            tables.append(count([('put', number) for number in row]))
        dearest = [('put', 7 * i) for i in range(1, n + 1)]
        print(f"{n:>6} {count(cheapest(n)):>10} {sum(tables) / 20:>10.1f} {count(dearest):>9}")

best, worst and average

A put probes from its home to the first free slot. So the homes decide the cost while the table fills, and after that every put costs 7.

A case is one kind of input, the thing the method is given, such as numbers that all have the same home, however many. 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
besthomes all different while there is room, 1 to nnaverageeach number's home at random, every slot as likelyworstone home, 7, 14, and on up to 7n7n
slots looked atnn in all up to 7 numbers, then 7n−427n - 42between the two, about 665 at 1007n−217n - 21 from 7 numbers on
how the slots looked at 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: 7 slots, nothing else

Extra memory is the room it needs on top of the input itself, while it runs.

putone homehomes all differentat random, on average
1111
221about 1.14
331about 1.35
441about 1.65
551about 2.10
661about 2.82
7714
8th on777
The probes of each put as the table fills. At random, a put walks further as the slots fill, 4 on average for the 7th. The 2nd put lands on the 1st number's home 1 time in 7 and then probes 2, so it averages 1 and 1 seventh, about 1.14. The others are worked out over every choice of homes: 7 numbers, 7 homes each, seven 7s multiplied together, 823,543 choices.
probesnumbers put in020406080051015probesnumbers put in020406080051015
  • one home, 7n−217n - 21 from 7 on
  • homes all different, 7n−427n - 42 from 7 on
At 7 the table is full. Up to 7, homes all different climb 1 probe a number, and one home climbs 1, then 2, then 3 and on; after 7, both climb 7 a number.

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 numbers with one home cost 7n−217n - 21 probes from 7 numbers on, the most any nn different numbers can?
  1. the move: price one put

    the line:
    with one home, the kth putprobes k slots, up to 7\begin{gathered} \text{with one home, the } k\text{th put} \\ \text{probes } k \text{ slots, up to 7} \end{gathered}
    1. step 11 of 16

      012345671421

      28 leaves 0 when divided by 7, so its home is slot 0.

      slots looked at: 0

    2. step 12 of 16

      012345671421

      Slot 0 holds 7, not 28.

      slots looked at: 1

    3. step 13 of 16

      012345671421

      Slot 1 holds 14, not 28.

      slots looked at: 2

    4. step 14 of 16

      012345671421

      Slot 2 holds 21, not 28.

      slots looked at: 3

    5. step 15 of 16

      01234567142128

      Slot 3 is free, so put 28 there.

      slots looked at: 4

    Operation 4 puts 28 in, after 7, 14 and 21, all with home 0. 28 walks past all three to slot 3. 4 probes.

    why it is allowed: Count the puts from 1. The numbers before it, one fewer than kk, fill a run from its home, so it looks at each, then at the free slot after them.

  2. the move: the table is full

    the line:
    from the 8th put on, each putprobes all 7 and is turned away\begin{gathered} \text{from the 8th put on, each put} \\ \text{probes all 7 and is turned away} \end{gathered}

    why it is allowed: The 7 slots hold 7 numbers, so the walk goes all the way round and finds no free slot.

  3. the move: no numbers cost more

    the line:
    for any numbers, none repeated,the kth put probes at most kslots, and at most 7\begin{gathered} \text{for any numbers, none repeated,} \\ \text{the } k\text{th put probes at most } k \\ \text{slots, and at most 7} \end{gathered}

    why it is allowed: Its walk passes only slots holding an earlier number, at most one fewer than kk, then stops at a free slot: the 4th put passes at most 3. And a walk never looks at more than the 7 slots.

  4. the move: add the puts

    the line:
    W=(1+2+⋯+7)+7(n−7)W = (1 + 2 + \dots + 7) + 7(n - 7)

    why it is allowed: WW is the probes of nn numbers with one home, from 7 numbers on. By lines 1 and 2, puts 1 to 7 make 1 up to 7, and each later put 7. For 8, 28 + 7.

  5. the move: use the page on adding up

    the line:
    W=7×82+7(n−7)W = \frac{7 \times 8}{2} + 7(n - 7)
    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. Here the last number is 7: 7 times 8, halved, is 28.

  6. the move: tidy

    the line:
    W=7n−21W = 7n - 21

    why it is allowed: In line 5, 7 times 8, halved, is 28, and 7(n−7)7(n - 7) is 7 times nn, less 49. 28 take away 49 is minus 21. For 8, 56 less 21 is 35.

  7. the move: a ceiling

    the line:
    W≤7nW \le 7n

    why it is allowed: Line 6 takes 21 away from 7n7n. For 7, 28 is at most 49.

  8. the move: read it as a bound

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

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

  9. the move: a fact about n

    the line:
    21≤3n for n≥721 \le 3n \text{ for } n \ge 7

    why it is allowed: For a floor, swap the 21 for a multiple of nn. 3 times 7 is 21, and 3n3n only grows. For 8, 21 is at most 24.

  10. the move: a floor

    the line:
    W≥4n for n≥7W \ge 4n \text{ for } n \ge 7

    why it is allowed: In line 6, 7n7n less 21 is at least 7n7n less 3n3n, by line 9, which is 4n4n. For 7, 28 is at least 28.

  11. the move: read it as a bound

    the line:
    W∈Ω(n)W \in \Omega(n)
    probesnumbers put in05010015005101520probesnumbers put in05010015005101520
    • the ceiling, 7n7n
    • WW, one home
    • the floor, 4n4n
    From 7 numbers on, WW stays between 4n4n and 7n7n, two lines of the same growth. At 20, 119 lies between 80 and 140.

    why it is allowed: By line 10, from 7 numbers on.

  12. the move: ceiling and floor together

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

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

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

Why do the numbers 1 to nn cost the fewest probes, 7n−427n - 42 from 7 numbers on?
  1. the move: price the first 7

    the line:
    homes all different, each ofthe first 7 puts probes 1 slot\begin{gathered} \text{homes all different, each of} \\ \text{the first 7 puts probes 1 slot} \end{gathered}
    1. step 6 of 8

      012345612

      3 leaves 3 when divided by 7, so its home is slot 3.

      slots looked at: 0

    2. step 7 of 8

      0123456123

      Slot 3 is free, so put 3 there.

      slots looked at: 1

    Operation 3 puts 3 in, after 1 and 2. Its home, slot 3, is free. 1 probe.

    why it is allowed: 1 to 7 have homes 1, 2, 3, 4, 5, 6 and 0, so each home is free when its number arrives.

  2. the move: no numbers cost less

    the line:
    every put probes at least 1 slot,and from the 8th on, 7\begin{gathered} \text{every put probes at least 1 slot,} \\ \text{and from the 8th on, 7} \end{gathered}

    why it is allowed: A put looks at its home at least. Any 7 different numbers fill all 7 slots, as each walk can go round all 7 to a free one. Then, by line 2 of the one-home question, every later put probes all 7, whatever the homes.

  3. the move: add the puts

    the line:
    B=7+7(n−7)B = 7 + 7(n - 7)

    why it is allowed: BB is the probes of 1 to nn put in, from 7 numbers on. By lines 1 and 2, 7 puts of 1, then 7 each. For 8, 7 + 7 is 14.

  4. the move: tidy

    the line:
    B=7n−42B = 7n - 42

    why it is allowed: In line 3, 7(n−7)7(n - 7) is 7 times nn, less 49, and 7 take away 49 is minus 42. For 8, 56 less 42 is 14.

  5. the move: a ceiling

    the line:
    B≤7nB \le 7n

    why it is allowed: Line 4 takes 42 away from 7n7n. For 7, 7 is at most 49.

  6. the move: read it as a bound

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

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

  7. the move: a fact about n

    the line:
    42≤6n for n≥742 \le 6n \text{ for } n \ge 7

    why it is allowed: For a floor, swap the 42 for a multiple of nn. 6 times 7 is 42, and 6n6n only grows. For 8, 42 is at most 48.

  8. the move: a floor

    the line:
    B≥n for n≥7B \ge n \text{ for } n \ge 7

    why it is allowed: In line 4, 7n7n less 42 is at least 7n7n less 6n6n, by line 7, which is nn. For 7, 7 is at least 7.

  9. the move: read it as a bound

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

    why it is allowed: By line 8, from 7 numbers on.

  10. the move: ceiling and floor together

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

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

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

Here let nn count slots, not numbers put in. A table of nn slots has one free slot. How many probes does a put of a new number make, on average over its home?
  1. the move: walk to the free slot

    the line:
    the put probes from its hometo the free slot: 1 up to n\begin{gathered} \text{the put probes from its home} \\ \text{to the free slot: 1 up to } n \end{gathered}
    homeits walkprobes
    551
    44, 52
    33, 4, 53
    22 to 54
    11 to 55
    00 to 56
    66, 0 to 57
    7 slots, only slot 5 free. Each home gives a different number of probes, 1 to 7.

    why it is allowed: It looks at its home, then the next slots, round to the start, and stops at the one free slot. Its home is any of the nn slots, each as likely, and each home is a different distance away.

  2. the move: average over the homes

    the line:
    L=1+2+⋯+nnL = \frac{1 + 2 + \dots + n}{n}

    why it is allowed: LL is the probes averaged over the nn homes. By line 1, the homes give 1 up to nn, one each. For 7, 28 over 7.

  3. the move: use the page on adding up

    the line:
    L=n(n+1)2nL = \frac{\frac{n(n + 1)}{2}}{n}
    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. Here the last number is nn. For 7, 7 times 8, halved, is 28.

  4. the move: halve and divide at once

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

    why it is allowed: Halved, then over nn, is over twice nn. For 7, 7 times 8 over 14 is 4.

  5. the move: tidy

    the line:
    L=n+12L = \frac{n + 1}{2}

    why it is allowed: The nn on top and below cancel. For 7 slots, 8 over 2 is 4, section 3's average for the 7th put, which finds 6 numbers in 7 slots.

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

Numbers whose homes land at random. How many probes do they cost on average?
  1. the move: no fewer than the cheapest

    the line:
    A≥7n−42A \ge 7n - 42
    numbersone homeat random, on averagehomes all different
    728about 14.067
    100679about 665658
    At 7 numbers, the average over all 823,543 choices of homes. It is section 3's column at random, added up: 1 + 1.14 + 1.35 + 1.65 + 2.10 + 2.82 + 4. At 100, the average of 200 tables picked at random.

    why it is allowed: AA is the probes averaged over every choice of homes. No choice costs fewer than homes all different while there is room, by lines 2 and 4 of the question on the fewest probes, so neither does the average.

  2. the move: read it as a bound

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

    why it is allowed: By line 1, as 7n−427n - 42 is at least nn from 7 numbers on, line 8 of the question on the fewest probes.

  3. the move: no more than the dearest

    the line:
    A≤7n−21A \le 7n - 21

    why it is allowed: No choice costs more than one home, by lines 3 and 6 of the one-home question, so neither does the average.

  4. the move: read it as a bound

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

    why it is allowed: By line 3, as 7n−217n - 21 is at most 7n7n, line 7 of the one-home question, the fixed multiple 7.

  5. the move: ceiling and floor together

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

    why it is allowed: Lines 2 and 4 bound AA by the same growth, nn.

try it on small inputs: over every choice of home for each of 7 numbers, 823,543 in all, the average AA is about 14.06

count it yourself

Count every probe as 7, 14, 21, 28, 35, 42 and 49 go into an empty table. All seven have home 0.

7, 14, 21, 28, 35, 42, 49, every step written down
stepthe slots, numbers onlywhat happensslots looked at
1Start with 7 empty slots, and 7 operations to do.0
27 leaves 0 when divided by 7, so its home is slot 0.0
37Slot 0 is free, so put 7 there.71
4714 leaves 0 when divided by 7, so its home is slot 0.71
57Slot 0 holds 7, not 14.72
67, 14Slot 1 is free, so put 14 there.7, 143
77, 1421 leaves 0 when divided by 7, so its home is slot 0.7, 143
87, 14Slot 0 holds 7, not 21.7, 144
97, 14Slot 1 holds 14, not 21.7, 145
107, 14, 21Slot 2 is free, so put 21 there.7, 14, 216
117, 14, 2128 leaves 0 when divided by 7, so its home is slot 0.7, 14, 216
127, 14, 21Slot 0 holds 7, not 28.7, 14, 217
137, 14, 21Slot 1 holds 14, not 28.7, 14, 218
147, 14, 21Slot 2 holds 21, not 28.7, 14, 219
157, 14, 21, 28Slot 3 is free, so put 28 there.7, 14, 21, 2810
167, 14, 21, 2835 leaves 0 when divided by 7, so its home is slot 0.7, 14, 21, 2810
177, 14, 21, 28Slot 0 holds 7, not 35.7, 14, 21, 2811
187, 14, 21, 28Slot 1 holds 14, not 35.7, 14, 21, 2812
197, 14, 21, 28Slot 2 holds 21, not 35.7, 14, 21, 2813
207, 14, 21, 28Slot 3 holds 28, not 35.7, 14, 21, 2814
217, 14, 21, 28, 35Slot 4 is free, so put 35 there.7, 14, 21, 28, 3515
227, 14, 21, 28, 3542 leaves 0 when divided by 7, so its home is slot 0.7, 14, 21, 28, 3515
237, 14, 21, 28, 35Slot 0 holds 7, not 42.7, 14, 21, 28, 3516
247, 14, 21, 28, 35Slot 1 holds 14, not 42.7, 14, 21, 28, 3517
257, 14, 21, 28, 35Slot 2 holds 21, not 42.7, 14, 21, 28, 3518
267, 14, 21, 28, 35Slot 3 holds 28, not 42.7, 14, 21, 28, 3519
277, 14, 21, 28, 35Slot 4 holds 35, not 42.7, 14, 21, 28, 3520
287, 14, 21, 28, 35, 42Slot 5 is free, so put 42 there.7, 14, 21, 28, 35, 4221
297, 14, 21, 28, 35, 4249 leaves 0 when divided by 7, so its home is slot 0.7, 14, 21, 28, 35, 4221
307, 14, 21, 28, 35, 42Slot 0 holds 7, not 49.7, 14, 21, 28, 35, 4222
317, 14, 21, 28, 35, 42Slot 1 holds 14, not 49.7, 14, 21, 28, 35, 4223
327, 14, 21, 28, 35, 42Slot 2 holds 21, not 49.7, 14, 21, 28, 35, 4224
337, 14, 21, 28, 35, 42Slot 3 holds 28, not 49.7, 14, 21, 28, 35, 4225
347, 14, 21, 28, 35, 42Slot 4 holds 35, not 49.7, 14, 21, 28, 35, 4226
357, 14, 21, 28, 35, 42Slot 5 holds 42, not 49.7, 14, 21, 28, 35, 4227
367, 14, 21, 28, 35, 42, 49Slot 6 is free, so put 49 there.7, 14, 21, 28, 35, 42, 4928
377, 14, 21, 28, 35, 42, 49Done. The table holds 7 numbers.7, 14, 21, 28, 35, 42, 4928

In all, 28 slots looked at. The formula from section 4, 7n−217n - 21, gives W=28W = 28 at n=7n = 7: the same.

where you meet it

Open addressing is how Python's dict and many other tables are built.

  • Python's dict

    dict, Python's own table, keeps its keys, the things it looks up, by open addressing. A deleted key leaves a dummy, its marker, and the table is rebuilt once two thirds of its slots are used, dummies counted, so it is never full.

    source: CPython's Objects/dictobject.c, USABLE_FRACTION and DKIX_DUMMY

  • Java's IdentityHashMap

    Java's IdentityHashMap is a linear-probe table, walking to the next slot as this page does. It leaves no marker: taking a key out empties its slot, then moves back into the gap each later key whose walk the gap would cut short.

    source: Java's documentation, java.util.IdentityHashMap (opens in a new tab); OpenJDK's source, java/util/IdentityHashMap.java in its java.base module, closeDeletion

dict's slotskeys it holds before growing
85
1610
3221
Two thirds of the slots, rounded down. The table is rebuilt before it fills, unlike this page's, which turns its 8th number away.

what is used instead

Java's HashMap and C++'s unordered_map chain instead, as the hash table with chaining does. Sebastian Bourges' books give the rule: 'Reach for open addressing when the keys and values are small and are copied cheaply' (volume two, chapter 18).

how it compares

The two hash tables of this site side by side.

waywhere a number goespast fulltaking outreach for it when
open addressing, 7 slotsits home, or the first marker its walk passed, or the next free slotno room: turned away after 7 probesleaves a markernumbers are small and few are taken out, as markers stay and slow every walk
chaining, 5 bucketsthe end of its bucket's chainchains grow longer, still workingtaken out of its chain, no markermany are taken out, or the table may overfill
The books set the two answers side by side (volume two, chapters 17 and 18).

race them on one row of your own