hashing and load
shapes of data
what it is
A hash table works out a key's slot from the key, so a look up goes straight there. The load, keys over slots, says how many keys a look up for a key not there checks, on average.
A hash table keeps keys in slots numbered from 0, here slots 0 to 6. A hash function works out a key's slot number from the key. The simplest takes the remainder after dividing by 7, the number of slots. 17 is 2 sevens and 3 over, so 17 goes in slot 3. A look up for 17 works out 3 again and looks there alone.
A word is no number, so make one. Number the letters from a as 1 to z as 26. Keep a running total from 0, and for each letter in turn multiply it by 26, the base, and add the letter's number. The drawing runs it on cab, the letters 3, 1, 2.
Two keys can want one slot. 31 leaves 3 over, like 17: a collision. Chaining keeps both in slot 3, a chain, the newest on top. With more keys than slots, no hash function avoids it: 8 keys in 7 slots must put two in one. Books call this the pigeonhole principle.
The load is keys over slots, so 14 keys in 7 slots is a load of 2. A look up for a key not there compares it with every key in its slot, one look each. On average, that walk is the load.
Every hash function sends some keys all to one slot: the remainder by 7 sends 0, 7, 14, 21 and 28 to slot 0. Someone who knows it can choose them, and make every look up walk one long chain. So pick the function at random when the table is made, from a family. A member multiplies a key by 1 to 28 and adds 0 to 28: 28 times 29, 812 members, as times 0 would send every key to one slot. Then it takes the remainder by 29, a prime bigger than every key, here 0 to 28, and then by 7. Two keys share a slot under only 92 of the members, about 1 in 9, under 1 in 7 (question 5).
| key | times 4, plus 4 | by 29 | slot |
|---|---|---|---|
| 0 | 4 | 4 | 4 |
| 7 | 32 | 3 | 3 |
| 14 | 60 | 2 | 2 |
| 21 | 88 | 1 | 1 |
| 28 | 116 | 0 | 0 |
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: the sevens that fit
the line:key sevens that fit left over 17 2 3 9 1 2 20 2 6 12 1 5 31 4 3 What is left over is the key's slot. why it is allowed: is over 7 rounded down. 17 over 7 is about 2.4, so 2 sevens fit in 17.
the move: the slot is what is left
the line:why it is allowed: Let be the slot, with the sevens taken out. For 17, 17 take away 14 is 3.
guess it, thenthe move: never below 0
the line:why it is allowed: The sevens that fit add up to at most , so taking them out leaves 0 or more.
guess it, thenthe move: never above 6
the line:why it is allowed: Were 7 or more left over, one more seven would fit, but line 1 already counted every seven that fits.
guess it, thenthe move: read it
the line:why it is allowed: By lines 3 and 4.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: adding forgets the order
the line:word letters added slot bad 2, 1, 4 7 0 dab 4, 1, 2 7 0 Added up, both go in slot 0. why it is allowed: Adding is simpler, but bad and dab hold the same letters, so they always share a slot. A base makes each letter's place count.
the move: each place counts
the line:word 676 26 1 bad 2 1 4 dab 4 1 2 26 times 26 is 676. bad is 2 times 676, plus 26, plus 4, so 1,382, slot 3. dab is 2,732, slot 2. why it is allowed: 312 is 3 times 100, plus 1 times 10, plus 2. Let be cab's number in base . The first letter counts times , the next letter , and the last 1.
guess it, thenthe move: take r out of the first two
the line:why it is allowed: is times , so the first two terms share the factor .
guess it, thenthe move: start from 0
the line:The running total in base 10. why it is allowed: 0 times , plus 3, is 3, so nothing changes. Now read it from the inside out.
guess it, thenthe move: read it
the line:why it is allowed: Line 4, inside out, is the running total. Books call it Horner's rule. It multiplies once a letter, and never works out a power.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: the answer
the line:7 keys fill the 7 slots, one each. The 8th, 31, must join one of them, 17 in slot 3. why it is allowed: is over 7 rounded up: 8 over 7 is about 1.14, rounded up 2. If every slot held fewer, the slots would hold fewer than keys.
the move: suppose fewer
the line:why it is allowed: Pretend line 1 fails, and watch the keys not fit. Then every slot holds fewer than . Counts are whole numbers, so that is at most : for 8 keys, fewer than 2 means at most 1.
guess it, thenthe move: what the 7 slots hold
the line:why it is allowed: Let be the most such slots hold together. For 8 keys, 1 a slot, 7 in all.
guess it, thenthe move: rounding up adds under 1
the line:keys sevenths added 8 2 6 9 2 5 10 2 4 11 2 3 12 2 2 13 2 1 14 2 0 Rounding up adds 6 sevenths at most, at 8 keys. why it is allowed: over 7 is a whole number of sevenths, so rounding it up adds at most 6 sevenths.
guess it, thenthe move: add the sevenths
the line:why it is allowed: Sevenths add as whole things do, so 8 sevenths and 6 sevenths make 14 sevenths, which is 2.
guess it, thenthe move: put lines 4 and 5 in
the line:why it is allowed: Line 3 with for , which by lines 4 and 5 is no smaller. So the right side can only grow, and is at most it: at 9 keys, 7 against 8.
guess it, thenthe move: multiply out
the line:why it is allowed: 7 times is , and 7 times 1 is 7.
guess it, thenthe move: 6 take away 7
the line:why it is allowed: is .
guess it, thenthe move: and no more is needed
the line:Keys 1 to 15 dealt by their remainders by 7. Slot 1 gets the most, 1, 8 and 15. why it is allowed: Deal 15 keys in turn: 1 to 7 one a slot, 8 to 14 one more each, then 15. That is 3 rounds, 15 over 7 rounded up, and a round gives a slot at most 1 key. Any keys take rounds alike.
guess it, thenthe move: read it
the line:why it is allowed: By line 8, slots as line 2 says hold at most keys, so fewer than . So line 2 cannot be, and line 1 holds. By line 9 it is the least the fullest slot can be.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: the whole chain
the line:slot keys there looks 0 35 1 1 22, 15, 29 3 2 9 1 3 17, 31, 3 3 4 none 0 5 12, 40, 26, 33 4 6 20, 48 2 in all 14 keys 14 The 14 keys of section 1, slot by slot. why it is allowed: None of them is the key, so it reads them all.
the move: add the walks
the line:why it is allowed: Each slot is as likely. At line 1 the looks add to 14, and 14 over 7 is 2.
guess it, thenthe move: every key in one chain
the line:why it is allowed: Let be the average walk. Each key is in exactly one chain, so the 7 walks add up to .
guess it, thenthe move: read it
the line:why it is allowed: By line 3. With any number of slots the walks still add up to , so the average is over the slots, the load.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: sort the remainders
the line:Remainders by slot. why it is allowed: Under a member, a key's remainder by 29 is 0 to 28, and that remainder's remainder by 7 is its slot.
the move: pairs in one slot
the line:why it is allowed: Two keys share a slot when their remainders do. So count pairs of different remainders in one slot, in order: 3 then 10, and 10 then 3, are two. Slot 3 holds 3, 10, 17 and 24, giving 4 times 3, 12.
guess it, thenthe move: every pair
the line:why it is allowed: Any two different remainders, in order, are 29, then 28 others.
guess it, thenthe move: the fact
the line:times 1 2 3 4 1 1 2 3 4 2 2 4 6 8 3 3 6 9 12 4 4 8 12 16 5 first: no product is 5, 10 or 15. why it is allowed: Divides means goes in exactly. Lines 5 to 9 prove it.
guess it, thenthe move: say it fails
the line:why it is allowed: Pretend line 4 fails.
guess it, thenthe move: divide by u
the line:why it is allowed: As question 1 does, with for 7: of them fit.
guess it, thenthe move: t works too
the line:why it is allowed: Line 6 times , rearranged. 29 goes in , and in by line 5, so in their gap.
guess it, thenthe move: t is 0
the line:why it is allowed: of 1 or more would work by line 7 and be under , the smallest.
guess it, thenthe move: no u works
the line:why it is allowed: By line 8. is under 29, and 1 would need 29 to divide , which is 1 to 28.
guess it, thenthe move: never one remainder
the line:why it is allowed: Take a member, times , plus , and keys and . Numbers leave one remainder by 29 only when a whole number of 29s apart, as 32 and 3. By line 4 these are not, as and are 1 to 28 in size. A minus sign changes nothing: 29 goes in minus 87 as in 87.
guess it, thenthe move: one pair from two members
the line:why it is allowed: Say times , plus gives the same pair. Then each key's two numbers are a whole number of 29s apart, and so is the gap between those gaps.
guess it, thenthe move: so one member
the line:why it is allowed: Were not 0, line 4 would rule out line 11, as and are 1 to 28 in size. Then and , both 0 to 28, leave one remainder by 29, so they are equal.
guess it, thenthe move: each pair once
the line:A smaller family, 5 for 29 and 2 slots: 20 members, 20 different pairs. 8 is 3 times 2, plus 2 times 1. why it is allowed: By lines 10 to 12, each member gives the two keys its own pair of different remainders.
guess it, thenthe move: why a prime
the line:why it is allowed: With 6 for 29, times 2 takes the keys 0 and 3 to 0 and 6, one remainder by 6: 6 divides 2 times 3. Line 9 needs a prime.
guess it, thenthe move: so 92 of 812
the line:why it is allowed: By line 13, each of line 2's 92 pairs comes from one member, which puts the two keys, even 0 and 7, in one slot.
guess it, thenthe move: add the chances
the line:why it is allowed: Every member is as likely, and is from 0 to 28 too, so by line 15 each key shares 's slot with chance 92 over 812. Let be the chances added.
guess it, thenthe move: an average adds its chances
the line: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 chances are alike. The box under this line shows that an average count adds up each piece's chance, at its line 2. Each key is a piece, so is the average count in 's slot.
guess it, thenthe move: 92 is under a seventh
the line:why it is allowed: 812 over 7 is 116, and 92 is less.
guess it, thenthe move: tidy
the line:why it is allowed: 116 over 812 is 1 over 7.
guess it, thenthe move: read it
the line:why it is allowed: By line 19. The five keys of section 1, all in slot 0 by the remainder by 7, give 5 times 92 over 812, about 0.57. Their load is 5 over 7, about 0.71.
guess it, then
try it with a small : at , line 16 gives , about 0.57
a worked example
Put the words cab, bad, dab and bed in 7 slots, then look up bed and ace.
- cab, bad and dab go in slots 5, 3 and 2, as the working under the drawing shows.
- bed goes in slot 2, where dab is. A collision, so bed joins dab's chain, on top.
- Look up bed. Walk slot 2's chain from its oldest key, at the bottom, dab, then bed. Found in 2 looks.
- Look up ace. It goes 1, 29, 759, which is 108 sevens and 3 over, so slot 3. Compare with bad, the whole chain, so ace is not there, after 1 look.
the trap
check yourself
Which slot does the key 45 go in, among 7?
the answer
Slot 3. 6 sevens, 42, fit in 45, and 3 is left over.
Turn the word fig into a number, letter by letter in base 26, and find its slot among 7.
the answer
f, i and g are 6, 9 and 7. The total goes 6, 165, 4,297, which is 613 sevens and 6 over, so slot 6.
21 keys go in 7 slots. What is the load, and how many keys must the fullest slot hold, at least?
the answer
The load is 21 over 7, 3. By the pigeonhole question, the fullest holds at least 21 over 7 rounded up, 3.