Proofs: Relaxed heap
These are the human proofs of Relaxed heap, at full length.
The relaxed heap of this page keeps the binomial shape and invariants (a) to (c), and FIX ends full
This proof shows: correctness.
We use the definitions of the page. A non-root node is a violation if for its parent . A stale mark is a mark on a node that is not a violation. The invariants are (S) every node of rank has children of the ranks in the slots , the parent pointers agree with the slots, the roots have different ranks and are not active, and the table of active nodes holds exactly the active nodes under their current ranks; (a) at most one active node of each rank; (b) every active node is a last child; (c) every non-root node that is not active is not a violation.
Lemma 1 (links). Let and be roots of equal rank that are not active and are not in any tree. LINK returns a tree of rank that satisfies (S). The loser is not active and not a violation. The only node whose last-child status changes, other than the loser, is the old last child of the winner, and the code pushes it on .
Proof. Say . Then becomes the child of slot of , and has the children of the ranks . The code removes the mark of , and . The children of keep their parent. The old child of rank of was the last child and is not any more; the code pushes it. The rank of changes, and the code moves in the table of active nodes if is active.
Lemma 2 (shape). Every operation keeps (S), and every rank is at most .
Proof. Look at each change of the trees. INSERT and the carries of delete-min only link roots of equal rank (Lemma 1) and put the result in the root table, carrying while the rank is taken, as in a binary counter. PARENT: is the last child of , so . After the cut , so the link of under gives the rank , which is the old rank of , and goes to the slot of . TRIPLE: and have rank , and has rank ; after the cut has rank and goes to slot , and LINK has rank and goes to slot . CLEAN exchanges two nodes of rank . PAIR: and have rank , since and are their last children of rank ; after the cuts both have rank , goes to slot of , and LINK of rank goes to the old slot of . The parents differ, and neither of and is a descendant of the other, because a subtree of a node of rank holds only nodes of smaller rank below its root. So no cycle forms. In DELETE-MIN, if of rank is not a root after line L13, then has rank 0, and the links with the children of of the ranks , in this order, give ranks ; the result fills slot of the parent of . If is a root, its children become roots. So the shape is always binomial. A binomial tree of rank has nodes by induction (), so . Every move to the root table or a call of PLACE with no parent removes the mark, so roots are not active. Every change of the rank of a node goes through the helper that moves the node in the table of active nodes.
Lemma 3 (heap order of the moves). No transformation and no link makes a node a violation, except the nodes that it pushes on .
Proof. We list the nodes that get a new parent or a new key. LINK: the loser is not a violation (Lemma 1). PARENT with : goes under , and ; gets the parent of and is pushed. TRIPLE: the condition of line L8 is false when line L9 runs, because either it was false for the first , or PARENT ran; in the second case PARENT either makes the last child of (when moves up, loses its last child and becomes the last child), or puts back with . So , and in the slot of is not a violation; it is pushed anyway. The tree LINK gets the parent and is pushed. CLEAN: is not active and , so under is not a violation; under is pushed. PAIR: under has ; LINK in the old place of is pushed. DELETE-MIN: the children of are linked under or under each other as losers, and the result is pushed. A decrease of the key of keeps the children of in order, and itself is pushed at line L4.
Lemma 4 (the work list). At the top of the loop of FIX the following hold: (W1) every non-root node that is a violation and not active is on ; (W2) every active node that is not a last child is on ; (W3) for each rank , at most one active node of rank is not on . When is empty, (a), (b) and (c) hold.
Proof. When an operation calls FIX, (a), (b) and (c) held before the operation. The operation changes parents, slots, or the key of , and by Lemmas 1 and 3 it pushes every node whose parent, key or last-child status changed and that can be a violation or active. Line L13 makes children of roots, and roots satisfy all three properties. So (W1) to (W3) hold at the first test.
Now take one pass of the loop with the node . The nodes that the pass changes are , the nodes that a transformation moves, and the nodes whose last-child status changes; by Lemmas 1 and 3 all these are pushed, except the losers of links and the nodes put in place without a violation, and these are not active and not violations. So (W1) and (W2) can fail only for itself. Line L5: is a root or not a violation, and it becomes not active, so it breaks nothing. Lines L8 to L10 end the pass with pushed again (lines L8 and L10) or with not active, inside the pushed tree LINK (line L9), except when PARENT makes the last child; then the pass goes on to line L11 with active and a last child. At line L11, is an active last child. If no other active node has rank , then is the only active node of rank off , so (W3) holds. If another active node of rank exists and line L11 pushes and , then (W3) holds since is on . If PAIR runs, the marks of and go away, and by (W3) before the pass, at most one active node of rank was off other than ; after the pass that is still true. A change of the rank of an active node happens only to a node that the same step pushes (the node of TRIPLE, the nodes of PARENT and PAIR, which lose their mark or get their old rank back), so it cannot break (W3).
If is empty, (W1) gives (c), (W2) gives (b), and (W3) gives (a).
Lemma 5 (termination). FIX ends.
Proof. Let be the set of violations and define , where is the number of violations that are not last children and is the number of stale marks. Every component is a bounded integer, since and all ranks are at most . Keys do not change in FIX, and a node changes its parent only in a transformation or a link. We show that each transformation lowers in lexicographic order.
- PAIR: and are violations before. After, is under and not a violation, the loser of LINK is not a violation, and only the winner can be one. If was a violation, it is replaced by the winner. So drops by at least 1.
- TRIPLE: is a violation. If is a violation too, the two are replaced by at most the winner, so drops. If has a stale mark, either the winner is not a violation and drops, or of rank is replaced in by the winner of rank , so the second component drops. The node is not a violation before or after.
- PARENT with : is a violation before, and under is not after. If was a violation, drops. Otherwise either is not a violation in the place of , and drops, or stays in with the rank of , which is one larger, and the second component drops.
- CLEAN: is a violation before and after, with the same rank; is not a violation before or after. was not a last child and now is, and no other node changes its last-child status in a way that matters: is not a violation. So drops. Line L5 removes a mark from a node that is not a violation, or does nothing, so drops or stays. Line L6 marks a violation, so does not change. So never grows in FIX, and it drops at each transformation. So there are finitely many transformations.
Between two transformations, does not grow, and passes that do not change are of three kinds: a pop that ends at line L5 with no change, a pop that ends at line L11 with nothing to do, and a pass that pushes two nodes at line L8 or line L11. A push at line L8 puts a violation of rank that is not a last child on top of . The next pass handles : it reaches line L7, and either a transformation runs or line L8 pushes a node of rank . So a chain of such pushes has length at most and ends with a transformation. A push at line L11 puts on top. The next pass handles : either is not a violation, and line L5 removes its mark, which lowers ; or is a violation that is not a last child, and then a chain as above ends with a transformation. So between two drops of there are at most pushes, and so passes. Since drops finitely often, FIX ends.
Lemma 6 (find-min). When (c) holds, the smallest key is the key of a root or of an active node.
Proof. Take a node of smallest key. While the current node is not a root and not active, go to its parent; by (c) the key of the parent is at most the key of the node. The walk ends at a root or an active node, whose key is at most , so it is also a smallest key.
Theorem. After each operation, the heap holds exactly the inserted keys that were not deleted, with their current values, and (S), (a), (b), (c) hold; FIND-MIN and DELETE-MIN return a smallest key.
Proof. Lemma 2 gives (S), Lemma 5 that FIX ends, and Lemma 4 that (a), (b) and (c) hold when it ends. Insert adds one node. Decrease-key changes one key. Transformations and links only move nodes. In DELETE-MIN the node is removed from the trees: if is not a root, it is not in the tree of rank that fills its slot, and is in that tree; if becomes a root, it is removed from the root table and is added back. So exactly leaves, and has a smallest key by Lemma 6, applied to the state before the operation, when (c) held. The returned key was saved before the change.