Proofs: Robin Hood hashing
These are the human proofs of Robin Hood hashing, at full length.
Robin Hood hashing keeps its invariant, finds each key, and minimizes the sum of squared distances full
This proof shows: correctness, termination.
All slot indices are taken modulo . A slot is full when it holds a key. For a full slot , write for its key and for the stored distance. A table is valid when (a) no key occurs twice, (b) for each full slot , , (c) at least one slot is EMPTY, and (d) the invariant (R) holds:
(R) For each full slot with , slot is full and .
Lemma 1 (paths are full). In a valid table, let slot hold with distance . Then for each , the slot is full and its distance is at least .
Proof. By induction on . For , slot is full with distance . If slot is full with distance and , then , so by (R) slot is full with distance at least .
Lemma 1 gives full slots, and at most slots are full, so each distance in a valid table is at most .
Lemma 2 (search). In a valid table, RH-SEARCH returns the slot of if is in , and NIL otherwise. It makes at most probes.
Proof. In the iteration with distance , the loop looks at slot (line L1 and line L5). It returns a slot only at line L4, where , so if is absent the result is NIL. Each iteration increases by 1. All stored distances are at most , so in the iteration with line L3 returns, if no earlier line did. So the loop makes at most probes.
Now let be at slot , with distance . For , Lemma 1 with shows that slot is full with distance at least , so line L3 does not return. That slot does not hold , because occurs only once, at (as ). So line L4 does not return either. At , slot holds with , so line L3 does not return and line L4 returns .
Lemma 3 (insertion). If the table is valid, is not in it, and at least two slots are EMPTY, then RH-INSERT ends, the new table is valid, and it holds the old keys and . The slot that becomes full is the first EMPTY slot at or after .
Proof. Call the pair the key in the hand at slot , at the test of line L8. We prove that at each such test these statements hold:
- The keys in the table, with , are the old keys and , each once.
- , and (b) holds for each full slot.
- (R) holds for the table.
- If , slot is full and .
- Exactly the slots that were full before the call are full now.
At the first test, , and , and the table is the old one, so 1 to 5 hold.
Suppose that 1 to 5 hold at the test, and slot is full, so line L8 does not return. Two cases follow.
Case . Line L9 does nothing. Line L10 moves to with distance , which keeps statement 2 for . Statement 4 at the new position needs slot full with , which is the case. Statements 1, 3 and 5 do not change.
Case . Line L9 writes into slot and takes in the hand. Statement 1 is kept, and (b) holds at slot by statement 2. (R) at slot : as , statement 4 gives slot full with . (R) at slot : if it is full with , then the old (R) gave , so . (R) at other slots is unchanged. Line L10 moves to with distance , which is the distance of the new key in the hand at , and statement 4 holds because slot is full with . Statement 5 is kept because the same slots are full.
Now suppose the test of line L8 finds slot EMPTY. Line L8 writes there and returns. Statement 1 gives the key set. (b) holds at by statement 2. (R) at slot holds by statement 4. (R) at slot : before the write, slot was EMPTY, so by (R) a full slot had distance 0, and (R) asks nothing for it. So the table is valid, except possibly (c). By statement 5 only the slot became full, and two slots were EMPTY, so one is still EMPTY.
Termination: between two tests, moves one slot to the right and statement 5 says the full slots do not change. There is an EMPTY slot, so after at most moves the test meets it. Because moves one slot at a time from , the slot that becomes full is the first EMPTY slot at or after .
Lemma 4 (deletion). If the table is valid, RH-DELETE removes if it is present, keeps all other keys, and leaves a valid table.
Proof. By Lemma 2, line L11 finds the slot of or NIL. If NIL, nothing changes. Otherwise let be the longest block of full slots after in which each distance is positive, with . As one slot is EMPTY, . Slot is EMPTY or holds a key with distance 0. The loop of lines L14 to L16 copies the entry of slot to slot with distance one less, for , and line L17 makes slot EMPTY. The key is overwritten (or, if , its slot is emptied), and each other key moves at most once, so the key set is the old one without .
(b): a key moved one slot left gets its distance lowered by 1, which is right, and the old distance was at least 1, so the new one is at least 0.
(R): let be the key that was at , with old distance , for . Inside the block, for , the old (R) gave , and both values drop by 1, so (R) holds between the new slots and . At the new slot (key , distance ): if , the old (R) at gave for the old key ; then the old (R) at gave slot full with distance at least , which is what (R) asks. Slot is not changed by the shift, because . Slot is now EMPTY, and slot is EMPTY or has distance 0, so (R) asks nothing there. Other slots do not change. (c) holds because the number of EMPTY slots grows by one.
Theorem 1 (correctness). Start from an EMPTY table with slots, and apply RH-INSERT (for a key that is not present, with at least two EMPTY slots), RH-DELETE and RH-SEARCH in any order. Then each table is valid, the set of keys is the one given by the operations, and each search answers correctly. Each operation ends after steps.
Proof. The EMPTY table is valid. Lemmas 3 and 4 keep validity and give the key set, by induction on the number of operations, and Lemma 2 gives the answer and the bound for search. The loop of RH-DELETE moves over at most slots.
The sum of the distances and the sum of their squares
Fix a set of keys. Call an assignment of the keys to distinct slots a linear probing layout when, for each key at slot , every slot from to (going right) is full. Its distance is . By Lemma 1, each valid Robin Hood table is a linear probing layout.
Theorem 2. Let and be two linear probing layouts of with the same set of full slots. Then . If is a valid Robin Hood table, then .
Proof. For a slot , let be the number of keys with , let if and 0 otherwise, and let be the number of keys whose path from to its slot contains both and . A key at distance crosses exactly such boundaries, so .
The keys whose path contains are the keys with home and the keys that cross the boundary from to ; these are disjoint, because a path is shorter than . Of these keys, exactly sit at , and the others cross to . So . If , no path contains , so . Pick , which exists because . Then , and the recursion gives from and only. So , and with it the sum of distances, is the same for and .
For the squares, cut the circle at and number the slots as . No path contains , so each path lies in this range and is an interval of integers, with . Suppose a layout has keys at slot and at slot with and . Swap them. Then is at with , and is at , and the set of full slots is the same, so the result is again a linear probing layout with full set . The change in the sum of squares is
The number of layouts is finite, so a layout with the least sum of squares exists, and by the swap it has no such pair: its homes do not decrease from left to right. Call such a layout sorted. In a sorted layout, the slots in are filled from left to right by the keys in order of home, so the multiset of distances is the same for every sorted layout (keys with equal homes can trade places without a change of distances). So every sorted layout has the least sum of squares.
A valid Robin Hood table is sorted. Take two full slots in the range. If all slots from to are full, it is enough to look at . If , (R) gives . If , then . If an EMPTY slot lies between and , the path of does not contain , so .
Consequence. Lemma 3 shows that RH-INSERT fills the first EMPTY slot at or after the home slot, as plain linear probing does. So the two methods, run on the same sequence of insertions, have the same set of full slots after each step. By Theorem 2 they have the same mean distance, and Robin Hood has the least variance of the distance among all linear probing layouts with these full slots.