Proofs: Analysis of linear probing
These are the human proofs of Analysis of linear probing, at full length.
The exact expected cost of a search in linear probing full
This proof shows: statement.
This proof covers parts (a), (b) and (d) of the statement. Part (d) uses part (c), which is Knuth's result (The Art of Computer Programming, Volume 3, Section 6.4) and is not proved here.
Setting. Slots are , and all slot arithmetic is modulo . A hash sequence is a word , where is the home of the -th key. Running linear probing on (insert key at the first empty slot of , for ) gives a final table , which says which key is in each slot. We assume , so every insert finds an empty slot. The path of key is the list of slots from to its final slot.
Fact 1. A slot that is empty in is on no path, and the home of each key is full in .
Proof. When key is inserted, each slot of its path before the last one is full, and the last one becomes full. No slot is ever emptied. So every slot of every path is full at the end. The home is the first slot of the path of key .
Lemma 1 (rotation). For integers and , let be the number of hash sequences of keys in a circle of slots after which a fixed slot is empty. Then , and this number does not depend on .
Proof. Let add 1 to each entry of a sequence. By induction on the number of inserts, the run on puts each key one slot further than the run on , because each probe is shifted by one and sees a shifted table. So slot is empty after exactly when slot is empty after . Since is a bijection of the sequences, every slot is empty after the same number of sequences. Count the pairs (sequence, empty slot) in two ways: each sequence leaves exactly empty slots, so , and . For this is , the empty sequence.
Lemma 2 (splitting). Let and . Let be the number of hash sequences of length such that in slot 0 is empty, slots are full, and slot is empty. Then
Proof. Let and , a set of slots. We build a bijection between the sequences counted by and the triples , where is a set of of the keys, is a hash sequence for the keys of (in their order) in a circle of slots that leaves slot empty, and is a hash sequence for the other keys in a circle of slots that leaves slot empty. By Lemma 1 there are triples, and .
From a sequence to a triple. Take counted by . By Fact 1, no path contains slot 0 or slot . A path is a run of consecutive slots, so each path lies inside or inside , and so does its home. Let be the keys with home in ; they fill exactly the slots of , so . Let give each key of the home , and give each other key the home , which lies in . We claim that the run on in the small circle places each key of in its slot in minus 1, and leaves slot empty. By induction on the inserts: before each insert of a key of , the full slots of in the run on are the shifted full slots of in the run on , and slot of the small circle is empty. The insert of a key in the run on follows a path inside , and the same steps, shifted by one, form the first empty slot after in the small circle. Keys outside do not change . In the same way, the run on copies the run on restricted to , shifted by , and its last slot , which stands for slot 0 of the big table, stays empty, since no path of the big run reaches slot 0. So is a triple of the kind above.
From a triple to a sequence. Take a triple . By Fact 1 for the small circle, no key of has home , so its home in the big table, , is in . By Fact 1 for the other circle, no key has home , so its home is in . Define this way. By induction on the inserts, as above, the run on keeps slot 0 and slot empty and copies the two small runs into and : an insert of a key of meets only slots of before it stops, because the small run stops before slot , and an insert of another key meets only slots of , because its small run stops before slot . At the end, the keys of fill , so is counted by .
The two maps are inverse to each other, because each one only shifts the homes and selects the keys by the block of their home.
Proof of (a). Fix a hash sequence and its table . A failed search that starts at slot probes up to and including the first empty slot. If is empty, it costs 1. Assume first , so the table has at least two empty slots. Then the full slots form maximal runs, and each run of slots has an empty slot just before it and a different empty slot just after it. A search that starts at the -th slot from the end of a run, for , costs . So the total cost over all start slots is
where is the number of runs of length . A run of length is a slot such that is empty, are full and is empty. By the rotation of Lemma 1, the number of sequences for which this holds at is the same for every , so it is for each , and . The search key has a uniform start slot, independent of . Hence
and Lemma 2 gives the formula of (a). A run has at most slots, so Lemma 2 applies to each in the sum.
If , the table has exactly one empty slot, and the full slots form one run. The total cost is , and for every sequence.
Proof of (b). Fix . The search for key probes until it finds key . The slots before the final slot of key were full when key was inserted and hold other keys, and slot holds key , so the search makes exactly as many probes as the insert of key did. The insert of key is a failed search for a key with home in the table , and it costs the same number of probes, the final empty slot counted. The value is uniform and independent of , which are themselves independent and uniform. So the expected cost of the search for key is . Averaging over uniform in gives (b).
Proof of (d), from (c). Let with . Fix with . For all large , . For and a fixed , the term
tends to , since each factor tends to . For all large and all , , because each factor is at most (factors with make the term 0). The bound is summable, since by the negative binomial series. By dominated convergence for series (Tannery's theorem),
The same holds for , since too. With (c), and .