Hash table with chaining
how it works
The idea in a few sentences and two pictures, then one run, moment by moment.
A hash table works out where a number goes from the number itself. This one has 5 buckets, numbered 0 to 4. A number goes in the bucket of its remainder when divided by 5: 42 is 8 fives and 2 over, so bucket 2. A table holds each number once, so to put a number in, you compare it with each number already in its bucket, oldest first. If none is equal, it goes on the end. To search for a number or take it out, you walk its bucket the same way, up to the number. Only one bucket is ever searched. Numbers sharing a bucket wait there in a chain, so this is a hash table with chaining. This page counts looks: one number in the bucket compared with the number in hand. The run after these pictures puts in 17, 42, 8 and 22, takes 42 out, then puts in 35 and 12.
step 9 of 13
22 leaves 2 when divided by 5, so it belongs in bucket 2.
compared: 0
step 10 of 13
17 is not 22.
compared: 1
step 11 of 13
42 is not 22.
compared: 2
step 12 of 13
22 is not in bucket 2 yet, so put it in.
compared: 2
step 2 of 23
17 leaves 2 when divided by 5, so it belongs in bucket 2.
what it remembers to know where it is
- doing operation
- 1
- number
- 17
- bucket
- 2
step 6 of 23
42 is not in bucket 2 yet, so put it in.
what it remembers to know where it is
- doing operation
- 2
- number
- 42
- bucket
- 2
step 8 of 23
8 is not in bucket 3 yet, so put it in.
what it remembers to know where it is
- doing operation
- 3
- number
- 8
- bucket
- 3
step 12 of 23
22 is not in bucket 2 yet, so put it in.
what it remembers to know where it is
- doing operation
- 4
- number
- 22
- bucket
- 2
step 15 of 23
Here is 42, in bucket 2.
what it remembers to know where it is
- doing operation
- 5
- number
- 42
- bucket
- 2
step 16 of 23
Take 42 out of bucket 2.
what it remembers to know where it is
- doing operation
- 5
- number
- 42
- bucket
- 2
step 18 of 23
35 is not in bucket 0 yet, so put it in.
what it remembers to know where it is
- doing operation
- 6
- number
- 35
- bucket
- 0
step 22 of 23
12 is not in bucket 2 yet, so put it in.
what it remembers to know where it is
- doing operation
- 7
- number
- 12
- bucket
- 2
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- start with 5 empty bucketsPython line 8
- a number's bucket is the remainder it leaves when divided by 5Python line 10
- compare it with each number already in that bucketPython lines 11, 12, 13, 14 and 15
- to put it in, add it to the bucket unless it is already therePython lines 16, 17 and 18
- to take it out, remove it from the bucket if it is therePython lines 19 and 20
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_chaining(operations):
"""Run a list of operations on a hash table of 5 buckets.
Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
out. Returns the 5 buckets, each a list of its numbers, oldest first.
Each look compares one number already in the bucket with n.
"""
buckets = [[] for _ in range(5)] # 5 empty buckets, numbered 0 to 4
for action, number in operations:
bucket = buckets[number % 5] # its remainder when divided by 5
found = None
for place, other in enumerate(bucket): # oldest first
if other == number: # one look
found = place
break
if action == 'put':
if found is None: # not there yet
bucket.append(number) # so add it on the end
elif found is not None: # it is there
del bucket[found] # so take it out
return buckets
how it scales
How the looks grow, from ten numbers to ten thousand, each put into an empty table of 5 buckets one at a time. Spread in turn is 1, 2, 3 and on; at random picks each bucket by chance; all in one bucket is 5, 10, 15 and on. The plot and tables call a look a number compared, 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 two steps up for each step across: ten times the numbers, a hundred times the looks. The 5 buckets never grow, so each chain grows with . Spread in turn starts lower, as its first 5 numbers share no bucket, then nearly meets at random: 950 and 990 looks at 100 numbers. All in one bucket costs 4,950.
compared, ten times more at each mark up
, how many numbers are in the table, ten times more at each mark to the right
- all in one bucket, 5, 10, and on up to solid where measured by running it; dashed where its formula carries it on to ten millionslope 2: ten times n, 100 times the work
- each number's bucket at random, every bucket as likelysolid where measured by running it; dashed where its formula carries it on to ten million
- spread over the buckets in turn, 1 to solid where measured by running it; dashed where its formula carries it on to ten million
| compared | time | |
|---|---|---|
| 1,000 | 499,500 | about 500 microseconds |
| 100,000 | 4,999,950,000 | about 5 seconds |
| 10,000,000 | 49,999,995,000,000 | about 14 hours |
The compared 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 looks 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 5 buckets, one at a time, and prints the
looks for 1 to n (spread over the buckets in turn, the cheapest), the
average over 20 tables whose numbers each land in a bucket picked at
random, and the looks for 5, 10, up to 5n (all in bucket 0, the dearest).
A look is one number in the bucket compared with the number put in. The
counts are the same on any computer; only the time differs.
"""
import random
class Counted:
"""A number that counts the looks made with it: one a comparison."""
looks = 0
def __init__(self, value):
self.value = value
def __eq__(self, other):
Counted.looks += 1
return self.value == other.value
def __mod__(self, other):
return self.value % other # finding the bucket is no look
def hash_table_chaining(operations):
"""Run a list of operations on a hash table of 5 buckets.
Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
out. Returns the 5 buckets, each a list of its numbers, oldest first.
Each look compares one number already in the bucket with n.
"""
buckets = [[] for _ in range(5)] # 5 empty buckets, numbered 0 to 4
for action, number in operations:
bucket = buckets[number % 5] # its remainder when divided by 5
found = None
for place, other in enumerate(bucket): # oldest first
if other == number: # one look
found = place
break
if action == 'put':
if found is None: # not there yet
bucket.append(number) # so add it on the end
elif found is not None: # it is there
del bucket[found] # so take it out
return buckets
def cheapest(n):
"""1 to n put in: the buckets fill in turn, 1, 2, 3, 4, 0, then again."""
return [('put', number) for number in range(1, n + 1)]
def count(operations):
"""How many looks the table makes on this list of operations."""
Counted.looks = 0
hash_table_chaining([(action, Counted(number)) for action, number in operations])
return Counted.looks
if __name__ == "__main__":
random.seed(1)
print(f"{'n':>6} {'spread in turn':>15} {'at random':>10} {'one bucket':>11}")
for n in [10, 100, 1000]:
tables = []
for _ in range(20):
row = [5 * i + random.randrange(5) for i in range(1, n + 1)]
tables.append(count([('put', number) for number in row]))
dearest = [('put', 5 * i) for i in range(1, n + 1)]
print(f"{n:>6} {count(cheapest(n)):>15} {sum(tables) / 20:>10.1f} {count(dearest):>11}")
best, worst and average
A put makes a look for each number already in its bucket. So how the numbers spread over the buckets decides the cost, and their order does not.
A case is one kind of input, the thing the method is given, such as numbers that all leave the same remainder, 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.
| bestspread over the buckets in turn, 1 to | averageeach number's bucket at random, every bucket as likely | worstall in one bucket, 5, 10, and on up to | |
|---|---|---|---|
| compared | , each fifth rounded down; 950 for 100, about a tenth of 100 times 100 | , a fifth of the dearest | , every number each time |
| how the compared grow in each case | |||
| tight, | |||
| ceiling, | |||
| floor, | |||
extra memory: 5 buckets, each a list of its numbers
Extra memory is the room it needs on top of the input itself, while it runs.
| case | numbers put in | their buckets | looks by each put | looks in all |
|---|---|---|---|---|
| spread in turn | 1, 2, 3, 4, 5, 6 | 1, 2, 3, 4, 0, 1 | 0, 0, 0, 0, 0, 1 | 1 |
| the same, back to front | 6, 5, 4, 3, 2, 1 | 1, 0, 4, 3, 2, 1 | 0, 0, 0, 0, 0, 1 | 1 |
| one at random | 13, 27, 8, 41, 19, 33 | 3, 2, 3, 1, 4, 3 | 0, 0, 1, 0, 0, 2 | 3 |
| all in one bucket | 5, 10, 15, 20, 25, 30 | 0, 0, 0, 0, 0, 0 | 0, 1, 2, 3, 4, 5 | 15 |
- all in one bucket,
- at random, on average,
- spread in turn
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: price one put
the line:step 11 of 16
20 leaves 0 when divided by 5, so it belongs in bucket 0.
compared: 0
step 12 of 16
5 is not 20.
compared: 1
step 13 of 16
10 is not 20.
compared: 2
step 14 of 16
15 is not 20.
compared: 3
step 15 of 16
20 is not in bucket 0 yet, so put it in.
compared: 3
Operation 4 puts 20 in, after 5, 10 and 15. All four leave 0, so 20 is compared with all three and goes on the end. 3 looks. 20 waits in hand above the buckets, yellow like each number it meets; red is just put in. why it is allowed: The numbers are all different, so no look finds an equal one, and the walk goes to the bucket's end.
the move: one bucket, every number
the line:why it is allowed: Count the puts from 1, so the th put is put number . 5, 10, 15 and on all leave 0, so it finds the numbers before it in bucket 0. For 4, the fourth put makes 3.
guess it, thenthe move: no numbers cost more
the line:why it is allowed: Its bucket holds at most the numbers already in, so by line 1 it makes at most looks. So no numbers cost more than line 2's.
guess it, thenthe move: add the puts
the line:why it is allowed: is the looks of numbers in one bucket. By line 2, puts 1 to make 0, 1, and on up to . For 4, 0 + 1 + 2 + 3 is 6.
guess it, thenthe move: leave out the 0
the line:why it is allowed: Line 4 without its 0, as adding 0 changes nothing.
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. Here the last number is . For 4, 1 + 2 + 3 is 3 times 4, halved, which is 6.
guess it, thenthe move: tidy
the line:why it is allowed: In line 6, is . For 5, 4 times 5, halved, is 10.
guess it, thenthe move: a ceiling
the line:why it is allowed: In line 7, is less than . For 5, 10 is at most 12.5.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 8, with the fixed multiple one half.
guess it, thenthe move: a fact about n
the line:why it is allowed: Taking 1 from takes at most half of it once is 2 or more. For 5, 4 is at least 2.5.
guess it, thenthe move: a floor
the line:why it is allowed: In line 7, write half of in place of , which by line 10 is no bigger. Half of times , halved, is . For 5, 10 is at least 6.25.
guess it, thenthe move: read it as a bound
the line:- the ceiling,
- the floor,
From 2 numbers on, stays between the floor and the ceiling, two lines of the same growth. At 20, 190 lies between 100 and 200. why it is allowed: By line 11, from 2 numbers on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 9 and 12 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: a look is a pair
the line:7, 12, 3 and 17 put in. Bucket 2 holds 7, 12 and 17, 3 pairs: 7 and 12, 7 and 17, 12 and 17. 12 makes 1 look and 17 makes 2. 3 shares its bucket with nothing. why it is allowed: By line 1 of the one-bucket question, a put compares its number with every number already in its bucket. So two numbers in one bucket meet once, when the later goes in. Two in different buckets never meet.
the move: even out the sizes
the line:17 swapped for 15, which leaves 0. Bucket 2 drops to 7 and 12, 1 pair, and 15 shares bucket 0 with nothing. buckets 2 and 0 hold pairs in each looks 3 and 0 3 and 0 3 2 and 1 1 and 0 1 Line 1's bucket 2 against bucket 2 after the swap. 17 leaves 2 pairs and joins none, so the swap saves 2 looks. why it is allowed: Swap 17 for 15. 17 met 7 and 12, so 2 pairs go; 15 lands in empty bucket 0, so none come: 2 looks saved, by line 1. In general, one fewer than pairs go and at most come.
guess it, thenthe move: the cheapest are even
the line:7 numbers with sizes 3, 2, 1, 1 and 0, largest first. 3 pairs in bucket 2 and 1 in bucket 3, 4 looks. 1 to 7, sizes 2, 2, 1, 1 and 1, largest first. 1 pair in bucket 1 and 1 in bucket 2, 2 looks, the fewest for 7 numbers. why it is allowed: If two sizes are 2 or more apart, line 2's swap saves a look, so that spread is not the cheapest. With no two sizes 2 or more apart, every size is or one more, where is over 5 rounded down. 5 buckets of hold numbers, and the left over make that many buckets one more. For 7, is 1, so 2 buckets hold 2 and 3 hold 1: 2 + 2 + 1 + 1 + 1 is 7. So all such spreads cost the same, by line 1, as 1 to 7 do.
guess it, thenthe move: price the kth put
the line:1 to 12 put in, in turn. 12 lands in bucket 2 and meets 2 and 7, 2 looks. why it is allowed: The numbers before it fill whole rounds of 5, one in each bucket, then part of a round. The part round fills only the buckets before its own in the turn 1, 2, 3, 4, 0. So its bucket holds one from each whole round. For 12, 11 over 5 is 2.2, rounded down 2: the 2 and the 7.
guess it, thenthe move: add the puts
the line:why it is allowed: is the looks of 1 to put in. By line 4, puts 1 to make up to . For 10, five 0s and five 1s, 5.
guess it, thenthe move: count by rounds
the line:puts round looks each looks 1 to 5 first 0 0 6 to 10 second 1 5 11 and 12 part of a third 2 4 in all 9 1 to 12 by rounds. Two whole rounds, so is 2, and 2 puts left over. why it is allowed: Let be the whole rounds of 5 puts, over 5 rounded down. In line 5, the rounds add 0, then 1, and on up to , five times each. The puts left over add each. For 12, is 2: 5 times 1, plus 2 times 2, is 9.
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. Here the last number is , and the 0 adds nothing: times , halved, five times. For 12, 5 times 1, plus 4, is 9.
guess it, thenthe move: drop the rounding
the line:why it is allowed: Line 7's is awkward beside , so for how fast grows, go back to line 5. Rounding down never makes a number bigger. Fifths add their tops: 0 fifths, 1 fifth and 2 fifths make 3 fifths. For 10, 5 is at most 45 over 5, which is 9.
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 same box, with last number : line 8's top is , and a fifth of that is . For 10, 9.
guess it, thenthe move: a ceiling
the line:why it is allowed: In line 9, is less than . For 10, 9 is at most 10.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 10, with the fixed multiple a tenth.
guess it, thenthe move: rounding down loses at most 4 fifths
the line:top top over 5 rounded down lost 5 1 1 0 6 1.2 1 0.2 7 1.4 1 0.4 8 1.6 1 0.6 9 1.8 1 0.8 Rounding a fifth down loses 0, 1, 2, 3 or 4 fifths, never more. why it is allowed: Each of line 5's pieces is its fifth, less what rounding down loses, at most 4 fifths. So pieces lose at most times 4 fifths. For 10, 5 is at least 9 less 8.
guess it, thenthe move: the same sum
the line:why it is allowed: Line 12's sum is line 8's, which line 9 found to be . For 10, 9 less 8, which is 1.
guess it, thenthe move: tidy
the line:why it is allowed: In line 13, 4 fifths of is 8 tenths of . Take out of both tenths: times , less times 8, is times . For 20, 20 times 11 over 10 is 22, and is 30.
guess it, thenthe move: read it as a bound
the line:- the ceiling, a tenth of
- , spread in turn
- the floor, a twentieth of
From 18 numbers on, stays between a twentieth and a tenth of . At 40, 140 lies between 80 and 160. why it is allowed: By line 14. From 18 numbers on, 9 is at most half of , so is at least half of . Then is at least a twentieth of , the fixed multiple. At 20, 30 is at least 20.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 11 and 15 bound by the same growth, .
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand
the move: average one put over its bucket
the line:the third number lands in numbers there looks bucket 0 none 0 bucket 1 none 0 bucket 2 27 1 bucket 3 13 1 bucket 4 none 0 on average 2 over 5 13 and 27 are in. The third number's 5 buckets make 2 looks in all, 2 over 5 on average, numbers over buckets. why it is allowed: Its bucket is any of the 5, each as likely, so average its looks over the 5. By line 1 of the one-bucket question, each bucket's looks are the numbers there. Wherever the numbers already in went, the 5 buckets hold in all. Had 13 and 28 shared bucket 3, it would give 2 and the rest 0: still 2 over 5.
the move: add the puts' averages
the line:why it is allowed: By line 1, whatever the earlier numbers did, puts 1 to average 0 up to over 5. Let be these averages added. For 3, 3 over 5.
guess it, thenthe move: an average adds its pieces
the line:buckets of 3 numbers put 2's looks put 3's looks looks in all 0, 0, 0 1 2 3 0, 0, 1 1 0 1 0, 1, 0 0 1 1 0, 1, 1 0 1 1 1, 0, 0 0 1 1 1, 0, 1 0 1 1 1, 1, 0 1 0 1 1, 1, 1 1 2 3 on average 1 over 2 1 3 over 2 3 numbers into 2 buckets, all 8 choices. Put 2 averages 1 over 2 and put 3 averages 1, the load before each. The totals average 3 over 2, their sum. where does this come from?
question 2 of the page expected costShuffle different numbers at random. How many pairs stand out of order, on average? the move: out of order half the time
the line:Trading places 1 and 3 pairs each shuffle holding them in order with one holding them out of order, 3 and 3. shuffle places 1 and 2 places 1 and 3 places 2 and 3 pairs out of order 1, 2, 3 0 1, 3, 2 yes 1 2, 1, 3 yes 1 2, 3, 1 yes yes 2 3, 1, 2 yes yes 2 3, 2, 1 yes yes yes 3 in all 3 3 3 9 Each shuffle of 1, 2 and 3, a yes where a pair of places is out of order. 9 over 6 is 1.5. why it is allowed: A pair is two places. Trading their numbers pairs each shuffle with another, and trading back undoes it. Each two has the pair out of order in exactly one, and both are as likely: chance one half.
the move: expected counts add
the line:why it is allowed: The table at line 1 adds two ways: down its last column, 9, and along its bottom row, 3 + 3 + 3. Over 6 shuffles, 9 over 6 is the average count, 1.5, and 3 over 6 three times is the pairs' chances added, 1.5 too. Both add the same yeses, so they agree for any , though the pairs depend on each other. With unequal chances, weight each yes by its order's chance. 2, 1, 3 at 3 over 4 and 1, 2, 3 at 1 over 4 give 3 over 4 both ways.
the move: one half for each pair
the line:why it is allowed: By line 2 the expected count adds line 1's one half once per pair.
the move: count the pairs
the line:why it is allowed: Let be the expected count, by line 3 half the number of pairs. The first place pairs with the after it, the next with one fewer, down to 1. For 3 places, 2 + 1 is 3 pairs, so 1.5.
the move: add them the other way round
the line:why it is allowed: The same numbers in the other order.
the 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 page on adding up 1 to adds the numbers from 1 to any whole number. They add up to that number times one more than it, halved: 1 + 2 + 3 is 3 times 4, halved, which is 6. In its working, its sum runs from 1 up to its own last number, which it calls . Here that is .
the move: tidy the bracket
the line:why it is allowed: is .
the move: multiply the halves
the line:why it is allowed: Half of a half is a quarter. For 3, 2 times 3 over 4 is 1.5.
the move: read it
the line:why it is allowed: By lines 4 to 7 there are pairs, and line 8 is half that. For 3, 1.5, as line 1 counted.
why it is allowed: The box under this line shows that an average count adds up each piece's chance, at its line 2. Count the th put's looks as one piece for each number already in, a yes when they share a bucket, with chance 1 over 5. Its pieces add to line 1's average, so is the looks averaged over every choice of buckets. The table shows it on 2 buckets, 8 rows, where 5 buckets would need 125.
guess it, thenthe move: take out the fifth
the line:why it is allowed: Fifths add their tops, as 0 fifths, 1 fifth and 2 fifths make 3 fifths.
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. Here the last number is , so this question's line 4 has on top, and a fifth of that is . For 3, 2 times 3 over 10 is 0.6.
guess it, thenthe move: a ceiling
the line:why it is allowed: In line 5, is less than . For 3, 0.6 is at most 0.9.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 6.
guess it, thenthe move: a floor
the line:why it is allowed: In line 5, write half of in place of , no bigger from 2 numbers on by line 10 of the one-bucket question. Half of times , over 10, is . For 3, 0.6 is at least 0.45.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 8, from 2 numbers on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 7 and 9 bound by the same growth, . At random costs little more than spread in turn, 990 looks against 950 at 100 numbers.
guess it, then
try it on small inputs: over every choice of bucket for each of 3 numbers, 125 in all, the average is
the move: the load with n buckets
the line:put numbers already in looks on average, 4 buckets 1 0 0 2 1 1 over 4 3 2 2 over 4 4 3 3 over 4 in all 6 over 4, which is 1.5 4 numbers into 4 buckets. The load climbs to 3 over 4, never past 1, so no put averages a whole look. why it is allowed: Line 1 of the average question, with buckets in place of 5. The load before the th put is numbers over buckets.
the move: add the puts
the line:why it is allowed: is the looks on average. By line 1, added over the puts, as lines 2 and 3 of the average question add them. For 4, 6 over 4.
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 gives 1 up to as , as line 5 of the average question uses it. Here it is divided by .
guess it, thenthe move: tidy
the line:why it is allowed: The on top and below cancel. For 4, 1.5 looks over 4 puts, under half a look each.
guess it, thenthe move: a ceiling
the line:why it is allowed: In line 4, is less than . For 4, 1.5 is at most 2.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5. Under half a look a put, however many numbers. A table that grows keeps its load at most a fixed number. So by line 1 each put averages at most that many looks: at most 0.75 in Java's (section 6).
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
count it yourself
Count every look as 8, 13, 18, 23 and 28 go into an empty table. All five leave 3.
| step | the table, bucket: numbers | what happens | compared |
|---|---|---|---|
| 1 | Start with 5 empty buckets, and 5 operations to do. | 0 | |
| 2 | 8 leaves 3 when divided by 5, so it belongs in bucket 3. | 0 | |
| 3 | 3: 8 | 8 is not in bucket 3 yet, so put it in.3: 8 | 0 |
| 4 | 3: 8 | 13 leaves 3 when divided by 5, so it belongs in bucket 3.3: 8 | 0 |
| 5 | 3: 8 | 8 is not 13.3: 8 | 1 |
| 6 | 3: 8, 13 | 13 is not in bucket 3 yet, so put it in.3: 8, 13 | 1 |
| 7 | 3: 8, 13 | 18 leaves 3 when divided by 5, so it belongs in bucket 3.3: 8, 13 | 1 |
| 8 | 3: 8, 13 | 8 is not 18.3: 8, 13 | 2 |
| 9 | 3: 8, 13 | 13 is not 18.3: 8, 13 | 3 |
| 10 | 3: 8, 13, 18 | 18 is not in bucket 3 yet, so put it in.3: 8, 13, 18 | 3 |
| 11 | 3: 8, 13, 18 | 23 leaves 3 when divided by 5, so it belongs in bucket 3.3: 8, 13, 18 | 3 |
| 12 | 3: 8, 13, 18 | 8 is not 23.3: 8, 13, 18 | 4 |
| 13 | 3: 8, 13, 18 | 13 is not 23.3: 8, 13, 18 | 5 |
| 14 | 3: 8, 13, 18 | 18 is not 23.3: 8, 13, 18 | 6 |
| 15 | 3: 8, 13, 18, 23 | 23 is not in bucket 3 yet, so put it in.3: 8, 13, 18, 23 | 6 |
| 16 | 3: 8, 13, 18, 23 | 28 leaves 3 when divided by 5, so it belongs in bucket 3.3: 8, 13, 18, 23 | 6 |
| 17 | 3: 8, 13, 18, 23 | 8 is not 28.3: 8, 13, 18, 23 | 7 |
| 18 | 3: 8, 13, 18, 23 | 13 is not 28.3: 8, 13, 18, 23 | 8 |
| 19 | 3: 8, 13, 18, 23 | 18 is not 28.3: 8, 13, 18, 23 | 9 |
| 20 | 3: 8, 13, 18, 23 | 23 is not 28.3: 8, 13, 18, 23 | 10 |
| 21 | 3: 8, 13, 18, 23, 28 | 28 is not in bucket 3 yet, so put it in.3: 8, 13, 18, 23, 28 | 10 |
| 22 | 3: 8, 13, 18, 23, 28 | Done. The table holds 5 numbers.3: 8, 13, 18, 23, 28 | 10 |
In all, 10 compared. The formula from section 4, , gives at : the same.
where you meet it
Chaining is how Java's HashMap and C++'s unordered_map, the usual tables of two programming languages, are built, with more buckets added as the numbers come.
Java's HashMap
Java's HashMap keeps a chain in each bucket, and turns one grown past 8 into a small tree once it has 64 buckets. It starts with 16 buckets and doubles them when the load passes 0.75. The table below follows it.
source: OpenJDK's src/java.base/share/classes/java/util/HashMap.java, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, putVal and resize; Java's documentation, java.util.HashMap (opens in a new tab)
C++'s std::unordered_map
C++'s unordered_map keeps its numbers in buckets and reports its load, which it tries to keep at most 1 unless told otherwise.
source: the C++ standard, unord.req (opens in a new tab), the unordered containers' requirements, and unord.map.cnstr (opens in a new tab), its starting max_load_factor
| numbers held | buckets | load |
|---|---|---|
| 1 | 16 | about 0.06 |
| 12 | 16 | 0.75 |
| 13 | 32 | about 0.41 |
| 24 | 32 | 0.75 |
| 25 | 64 | about 0.39 |
what is used instead
When two numbers want one place, dict, Python's own table, does not chain them. It uses open addressing: the second tries other places of the table itself, as the hash table with open addressing does. Sebastian Bourges' books say so, and still give the rule: 'Chaining is the answer you should have to be argued out of' (volume two, chapter 17).
how it compares
Two hash tables side by side, one with 5 buckets that never grow and one with as many buckets as numbers. Each puts different numbers in, one at a time.
| way | n numbers at random, on average | extra memory | reach for it when |
|---|---|---|---|
| this table, 5 buckets that never grow | looks | 5 buckets, each a list of its numbers | the numbers stay few: 10 numbers cost 9 looks |
| a table with as many buckets as numbers | looks | a bucket for each number, each a list | many numbers: 1,000 cost about 500 looks, not 99,900 |
- 5 buckets,
- as many buckets as numbers,