Proofs: Deap
These are the human proofs of Deap, at full length.
The deap operations keep the two heaps and the partner condition full
This proof shows: correctness, termination.
We use the notation of the page. The keys are in ; the level of is ; is the place of a min node ; is if and else; and for a max node . The invariants are (L), (R) and (C) of the page. For a max node , let be the set of min nodes whose partner is .
Facts about partners
Fact 1. for each min node , when .
Proof. If , the partner is the parent of , which is at level . The tree is complete and is at level , so every cell of level is at most .
Fact 2. Let be a min node with parent . Then is or its parent. So, under (R), .
Proof. , since is even. If , then , so the partner of is the parent of the partner of . Else the partner of is , by Fact 1, and that is also the partner of .
Fact 3. For a max node , . And is , together with the children of when is a leaf.
Proof. The first part is arithmetic, as in Fact 2. For the second, let ; then , so . Another min node has the partner only if and , so , which means is a child of and has no child at . If has a child , then and ; this is unless , so a non-leaf has the extra member only when . We record this case: it occurs only when . For a leaf , both children of , when they exist, have places and beyond , so they are in . Grandchildren of do not exist, since they would be at level , beyond the level of the children of , which do not exist.
Lemma 0. If (L), (R) and (C) hold and , then is a smallest key and a largest key. If , the one key is both.
Proof. A min key is at least by (L), and at most the key of its partner, which is at most by (R). A max key is at most by (R), and at least by (C), which is at least .
The sifts
Say that the invariants hold with if they hold for the array in which the cell holds the key ; the code may treat as a hole.
Lemma 1. Let be a min node, and let the invariants hold with . If , then MIN-SIFT-UP stops after at most iterations, and afterwards the invariants hold with the keys of changed as follows: is in some cell on the path from up to 2, and the keys on the path from to the parent of have moved one cell down, so that the multiset is the old one with replaced by .
Proof. Before each test of line L6, for the current hole , the invariants hold with for some ; this holds at the start with . A min node has one lower bound, its parent's key, and its upper bounds are the keys of its children and of its partner. If the test is true, moves into . Then meets the lower bound of by (L) at , and meets the upper bounds of . The parent cell becomes the hole with , and no constraint at it changed. If the test is false, then or , and meets the upper bounds of . Each iteration moves up one level, and the loop stops at level 1.
Lemma 1'. The mirror statement holds for MAX-SIFT-UP at a max node with . A max node has one upper bound, its parent's key, and its lower bounds are the keys of its children and of the nodes of ; the proof is the same with the order reversed.
INSERT
Let the invariants hold for , and let be the new cell, with the key . If , there is nothing to check.
A max node . Let . Before the insert, was beyond the end, so the partner of was (or nothing if ), and (C) gave . After the insert, , because has no children (they would be at level , beyond ). So the invariants hold with : it meets the upper bound and the lower bound .
If , line L3 sets , and the invariants hold with the hole and . Lemma 1 applies. Else , and Lemma 1' applies to MAX-SIFT-UP with .
A min node . Its place is beyond the end, so its partner is , which is also the partner of the parent of , so (C) gives . So the invariants hold with : has no children, and meets its lower bound and its partner bound.
If , line L5 moves to , and the invariants hold with the hole and , because the only new member of is , which holds . Lemma 1' applies. Else Lemma 1 applies to MIN-SIFT-UP with .
Removing the last cell
Lemma 2. If the invariants hold for , they hold for .
Proof. (L) and (R) lose one edge. If is a min node, no partner changes. If is a max node, the only min node whose partner changes is , whose new partner is , and by (C) and (R).
EXTRACT-MIN
Let be the size after line L10, and the key of the removed cell . By Lemma 0, is a smallest key. By Lemma 2, the invariants hold for with the old key at 2.
The descent of line L12. Claim: before each test, with the hole at , the invariants hold for all cells other than , where (L) is read with skipped, and the key that held before the descent reached it is at least the key now at its parent. When the smaller child moves up to , it is at most its sibling, and at least the key now above , which was above before, by (L). For (C) at , Fact 2 gives . The max side does not change. The loop stops at a leaf after at most iterations.
The placement. Let . The key now at , if , is the old key of , which was at most by (C) for . So the invariants hold with . If , line L14 moves to ; the invariants hold with the hole and , since the cell holds , and Lemma 1' applies. Else Lemma 1 applies to MIN-SIFT-UP with . For , line L11 puts at 2.
EXTRACT-MAX
Let be the size after line L16, the key of the removed cell , and , a largest key by Lemma 0. If was a max node, let and ; then before the removal, and has no children (as in INSERT).
The descent of line L18. Let be the path of the hole, where is the larger child of , and is a leaf. After the descent, holds the old key of for . These keys decrease along the path, and each is at most the old key above it, so (R) holds on the max side with as a hole. For (C), consider a min node whose partner is some with . If , then by Fact 3, is a child of , so the old , which is the new . Else, by Fact 3, is not a leaf, , and is the child with place ; so and . So after the descent, (C) can fail only for , and .
The choice of line L19. By Fact 3, is and its children; by (L), its largest key is at the larger child if there is one. So , the node chosen in line L19, holds the largest key of . The key now at the parent of is the old key of , which is at least every key of , by (C) for (for a node of other than , the partner did not change with the removal).
Case . The invariants other than (C) at hold with , because is at least the keys of and at most the key above , unless , where also allows . Lemma 1' applies to MAX-SIFT-UP with . Afterwards, the cells of the path from up to the final cell of hold their old keys again, below , then at , then the keys above that are at least , since the loop stopped. So the partner of , which is on the path or off it, holds a key that is at least or its old key, and the old key was at least ; in both cases it is at least , so (C) holds at too.
Case . Then , since . Line L20 moves to ; it is at most the key above , as shown, and it is at least the keys of . The node , if it exists, has a partner whose key is now at least if it is on the path, and its old key, which is at least , if it is not. So the invariants hold with the hole and , which is a min leaf whose partner holds . Lemma 1 applies to MIN-SIFT-UP.
For , lines L15 and L17 are correct by Lemma 0 and Lemma 2.
Theorem
Starting from the empty deap, each sequence of INSERT, EXTRACT-MIN and EXTRACT-MAX keeps (L), (R) and (C), stops after steps per operation, and keeps in exactly the current multiset; FIND-MIN and FIND-MAX return a smallest and a largest key.
Proof. The invariants and the multiset are the cases above, with Lemmas 1 and 1' for the sifts; each loop moves one level per iteration. The answers of the finds and extracts follow from Lemma 0.