Proofs: Weak heap
These are the human proofs of Weak heap, at full length.
The weak-heap operations keep weak-heap order full
This proof shows: correctness.
The array with bits and defines a tree: the left child of is and the right child is , where they exist, and node 0 has only the child 1. For a node , is the set of nodes of the subtree of , and and are the sets of nodes of its left and right subtrees. Flipping exchanges and and changes no other subtree as a set. Write for the condition for all . The heap is in weak-heap order if holds for every node . A set of nodes is ordered if holds for every . For a set , write if for every .
Lemma 1 (distinguished ancestors). For , let be the value of D-ANCESTOR. Then , and for every node , if and only if is one of . If has a left child , then .
Proof. Line L1 goes up while the current node is a left child: is the left child of its parent exactly when . It stops at a node that is a right child, and is its parent, so . Every ancestor of strictly between and is on the walk, so is in , not in . So is the lowest ancestor with . If and , then is a proper ancestor of and , and the claim follows by induction on depth. The converse holds because for each on the chain. For the left child of , the walk from first goes up to , and then continues as the walk from .
Lemma 2 (join). Let . Before JOIN, assume that is ordered and . After the join, is ordered and . The join changes only , and , and it does not increase .
Proof. If , nothing changes, and by , so .
Otherwise line L4 swaps the keys and flips . Let and be the old and , with . The new is less than , it is at most every key of the old by the old , and it is less than the new . So the new . The new right subtree of is the old , and the new the old , so holds. For with , the set and its keys did not change, because , so still holds.
Theorem 1 (build). After lines L5 and L6, the heap is in weak-heap order, with exactly comparisons.
Proof. A join at changes only , and , and . So once has been processed, no later step (which has a smaller index) changes a key or a bit in : every node of has an index at least , and the later steps change only nodes with smaller indices. We prove by induction downward on : when is processed, is ordered and .
is ordered: for every with , the step at came earlier and left ordered by Lemma 2, and has not changed since. For : if has a right child , then by Lemma 1, so the step at gave . After it, did not change, and changed only at steps for other , which do not increase it.
: if has a left child , then by Lemma 1, and the step at gave . Since then, has not changed, and changed only as the first argument of joins, which do not increase it, because and is processed after .
So Lemma 2 applies at every step. After the step at , is ordered and , so also holds. Each of the steps makes one comparison.
Theorem 2 (sift-down). Assume holds for every among the nodes , with . After SIFT-DOWN, the heap is in weak-heap order.
Proof. Line L8 finds the path , where is the left child of and has no left child. By Lemma 1, for every . Lines L9 and L10 call JOIN, then JOIN, up to JOIN, because . We show by induction, for down to 1, that after JOIN the set is ordered and .
For : is ordered by assumption, and is empty, so Lemma 2 applies. For : the earlier joins changed only and nodes in , and the bits there. So still holds, because and have not changed, and the nodes of are still ordered. With the induction hypothesis, is ordered, and , so . Lemma 2 applies again.
At the end, is ordered and , so every node satisfies .
Theorem 3 (delete-min). DELETE-MIN returns a smallest key, and the remaining heap of keys is in weak-heap order.
Proof. The root is a smallest key, because holds all other nodes. Removing the last node, a leaf, removes one element from some sets and keeps every . Moving its key to the root changes no for , because node 0 is in no . With at least two nodes left, Theorem 2 gives the result; with one node, there is nothing to check.
Theorem 4 (insert). After INSERT, the heap is in weak-heap order and holds the old keys and .
Proof. Line L13 puts in the new leaf . If is even, is the first child of its parent , which was a leaf, so setting changes no subtree of the old tree and makes the left child of .
We show that the loop keeps this invariant : for every node and every with , . In words, the only possible violations are between and the nodes with . After line L13, holds, because the old heap was in weak-heap order and the new leaf has .
Let , and assume . If JOIN does not swap, then . By Lemma 1, every other with has , so by . Every holds, and the loop stops.
If the join swaps, let and be the old and , with . We check .
- : the new is the old , which is part of and does not contain , so the old by .
- : is the same set. Its keys other than did not change and are at least . The new .
- , : and its keys did not change, and .
- with : these are proper ancestors of , so as well. The pair is the exception allowed by . For , the new key is , and held before, because and .
- Every other : if meets , then , and by Lemma 1 or , which are the cases above. Otherwise contains neither nor and did not change.
So holds, and the loop continues with . Each step moves to a proper ancestor, so the loop ends: either at a join without a swap, which gives weak-heap order as shown, or with . Node 0 is in no , so is the weak-heap order itself. Joins only exchange keys, so the multiset of keys is the old keys and .