Proofs: Radix heap
These are the human proofs of Radix heap, at full length.
The radix heap returns the minimum, and each item moves at most lg U times full
This proof shows: correctness, time.amortized.
Keys are nonnegative integers below . For an integer , let if , and otherwise. So for , the keys and agree on bits and differ in bit . The invariant is (R1): every key in the heap is at least , and (R2): every item with key is in .
Lemma 1 (order of buckets). Under (R1) and (R2), every key in equals , and if , every key in is smaller than every key in . Also, every key in with is larger than .
Proof. holds only keys with . Let with . Then , and and agree above bit and differ in it. Since by (R1), bit is 1 in and 0 in . Now let and with . Both agree with on bits above . On bit , agrees with , which has a 0 there, while has a 1. So .
Lemma 2 (refill). Let (R1) and (R2) hold, be empty, be the smallest index with not empty, and be the smallest key in . Then is the smallest key in the heap. After line L5 sets , (R1) holds; and for the new , every key of with still has , and every key of the old has .
Proof. By Lemma 1, the keys of are smaller than those of all higher buckets, and the lower buckets are empty. So is the minimum, and every key is at least , which is (R1).
Write for the old . Since , the key agrees with on all bits above , and has a 1 in bit . Let with . Then agrees with , and so with , on all bits above , and differs from in bit . Since , the bit of equals that of , so differs from in bit , and for the new .
Let be in the old . Then and both agree with above bit , and both have a 1 in bit . So , and for the new .
Theorem 1 (correctness). INSERT and EXTRACT-MIN keep (R1) and (R2), and EXTRACT-MIN returns an item with the smallest key.
Proof. INSERT checks and puts in , so both conditions hold. In EXTRACT-MIN, if is not empty, its items have key , which is the minimum by (R1), and removing one keeps both conditions. Otherwise, by Lemma 2, the new is the minimum and (R1) holds. Lines L6 and L7 move every item of the old to its bucket for the new , and by Lemma 2 the items of higher buckets are already in theirs, so (R2) holds. The item with key has , so is not empty, and line L8 returns an item of key .
Theorem 2 (moves). Each item is moved by line L7 at most times, so a sequence of inserts and any number of extract-mins does moves in total.
Proof. By Lemma 2, a move takes an item from to a bucket with a smaller index, and an item in a higher bucket keeps its index. A change of is the only event that can change a bucket index, so the index of each item never grows. It starts at most at , the largest index for keys below , and it is never below 0. Each move lowers it by at least 1, so there are at most moves per item.
The other work of an extract-min is the search of line L4, which takes time by a scan, or with a bit mask of nonempty buckets and an instruction for the lowest set bit. So with the mask, the total time of inserts and extract-mins is : amortized per insert and amortized per extract-min.