Proofs: Interval heap
These are the human proofs of Interval heap, at full length.
The interval heap operations keep the interval invariants full
This proof shows: correctness, termination.
We use the notation of the page: the node has the low key and the high key , which is if it exists and else; . The invariants are (N) , and (I) and for . For an edge , call the first inequality of (I) its low side and the second its high side.
Lemma 0. If (N) and (I) hold, then every key lies in . So FIND-MIN and FIND-MAX are correct.
Proof. By induction on the depth of a node : its keys lie in by (N), and by (I) this interval lies inside the interval of .
Lemma 1 (sift-up on the low side). Let be the last node, and suppose that (N) holds, that (I) holds except perhaps on the low side of the edge into , and that implies . Then SIFT-UP-MIN stops after at most iterations, and after it (N) and (I) hold.
Proof. We show that before each test of line L4, for the current node :
(U) (N) holds; (I) holds except perhaps on the low side of the edge into ; and if , then , for each child of , and either holds one key or .
Start. is the last node, so it has no children. If it holds two keys, one of them is the old key of the node, which lay in the interval of before the insert, and is the larger of the two keys, so . The rest is the hypothesis.
Step. Let the test be true, with , and , so . Line L5 swaps and .
- (N) at : the new .
- (N) at : if holds one key, it is , and there is nothing to show. Else by the last part of (U).
- The low side of : . The low side of for the other child of : it held with , so .
- The low sides of for the children of : by (U).
- The high sides: no high key moved, except that a single-key node now has .
So (N) holds, and (I) holds except perhaps on the low side of the edge into . For the rest of (U) at the new : ; the children of have low keys and , and the old low side of the edge into gives ; and holds two keys, since it is not the last node, with .
End. Each iteration moves to the parent, so there are at most iterations. When the test is false, either , which has no edge into it, or the low side of the edge into holds. With (U), (N) and (I) hold.
Lemma 1'. The mirror statement holds for SIFT-UP-MAX on the high side, with the same proof and the order reversed.
INSERT. Before line L1 the invariants hold. Let be the new key and the last node.
Line L2 orders the pair of if it has two keys, so (N) holds. The old key of , if any, lay in by (I). The node has no children. So (I) can fail only at the edge , and only through : if , then is now and only the low side fails, and ; if , then is now and only the high side fails; else nothing fails.
In the first case, Lemma 1 applies to SIFT-UP-MIN, and afterwards (N) and (I) hold, so SIFT-UP-MAX finds its test false at once, because each edge holds. The second case is symmetric with Lemma 1'. In the third case both sifts stop at once.
EXTRACT-MIN. By Lemma 0, is a smallest key. If , the queue becomes empty. Else the last key is removed and written to . Removing the last key keeps (N) and (I) for all other nodes, since it deletes the high key of the last node or the whole last node; a node with one key left has , and its key still lies in the parent's interval. The new is at most by Lemma 0, so (N) holds at the root.
The claim before each test of line L11, for the current : (N) holds everywhere; (I) holds except perhaps on the low sides of the edges from to its children; and if , then for each child of . At the start, , and the last part is empty.
Let be the child with the smaller low key, and let the test of line L12 be true: . Line L13 swaps the two low keys. At , the new low key is at most the low key of the other child, so the low sides at hold; it is at least by the last part of the claim; and it is less than , so (N) holds at . At , the low slot now holds . The high side of still holds, since did not move. If , line L13 swaps the two keys of , so holds ; then (N) holds at , and the high side of holds because , which was true before since was the low key of . The new low key of is at least , which is now , so the low side of holds. For the children of , , which is the last part of the claim at the new ; their low sides may now fail, and their high sides held and did not decrease. So the claim holds after line L14.
When the loop returns at line L11 or L12, the low sides at hold, so (N) and (I) hold. Each iteration moves one level down, so the loop stops after at most the height of the tree.
EXTRACT-MAX is the mirror image for . For , the last key is the largest by (N), and removing it leaves a valid heap.
Theorem. Starting from the empty array, each sequence of INSERT, EXTRACT-MIN and EXTRACT-MAX keeps (N) and (I), each operation stops in steps, and the extracts and finds return a smallest or a largest key of the multiset. The array always holds exactly the current multiset.
Proof. The invariants and termination are the cases above, and the answers follow from Lemma 0. Each operation only appends, removes the last key, overwrites the removed extreme key with the removed last key, or swaps keys, so the multiset changes as the operation says.