Proofs: Soft heap
These are the human proofs of Soft heap, at full length.
The soft heap returns an item of smallest current key and has at most 18n/2^T corrupted items full
This proof shows: correctness.
We use the definitions of the page: is the number of insertions, the target sizes are for and for , and the current key of an item in is . The invariants are (I1) for every child of ; (I2) for every item in ; (I3) every node that is not a leaf has a non-empty list, and every leaf in a tree has a non-empty list; (I4) each child of a node of rank has rank ; (I5) every item that was inserted and not deleted is in exactly one list.
Lemma 1. SIFT, called on a node whose children satisfy (I1) to (I4) in their subtrees, ends with (I1) to (I4) in the subtree of , and with at least its old value if was not empty. It does not change any current key except by raising it.
Proof. By induction on the rank of ; the recursive call is on a child, of smaller rank. Take one pass of the loop. By (I3) the child that line L4 chooses has a non-empty list, and it has the smallest among the children. Line L5 sets . If was not empty, the old was at most by (I1), so it does not decrease, and the items of get a current key that is not smaller. The items of keep their current key . So (I2) holds at : old items had old , and the new ones had . The other child has , so (I1) holds for . Now is empty. If is a leaf, line L6 removes it, and (I3) holds. Otherwise SIFT runs; by induction it ends with (I1) to (I4) in the subtree of , its list is not empty, because is not a leaf, so the loop makes at least one pass, and each pass adds a non-empty list; and is at least the value that took. So (I1) holds for . When the loop ends, is non-empty or is a leaf; started with a child, and the first pass adds a non-empty list, so is not empty. Ranks do not change, so (I4) holds.
Lemma 2. INSERT, MELD and DELETE-MIN keep (I1) to (I5), and the suffix-min pointers point to a root of smallest among the roots of equal or larger rank.
Proof. A new node of rank 0 has one item with . LINK makes a node of rank with two children of rank , so (I4) holds, and SIFT gives (I1) to (I3) by Lemma 1. Insert and meld only add roots and link them, so each item stays in one list (I5). In DELETE-MIN, line L11 removes one item, which keeps (I1), (I2) and (I5) for the others. If is not a leaf, SIFT restores (I3) by Lemma 1, and (I1) still holds for its parent because is a root. If is a leaf whose list is empty, line L14 removes it. The suffix-min pointers can change only for the roots whose changed or that were added or removed, and for the roots of smaller rank, which all come after them in the list. The code recomputes all of them from the first changed position, so they are right.
Lemma 3. FIND-MIN and DELETE-MIN return an item whose current key is the smallest current key of all items, and the returned current key is at least the key of the item.
Proof. By (I1) and induction on depth, the of a root is at most the of every node in its tree. Every root has a non-empty list by (I3), so the root of smallest holds an item with the current key , and every item in the heap has a current key at least . By (I2) the item has .
Lemma 4. A node of rank holds at most one item, and that item is not corrupted.
Proof. At creation, a node of rank 0 holds one item with its own key. A node of rank starts with an empty list. In SIFT the loop runs only while , so only when the list is empty, and then it takes the list of a child of rank , which by induction on and on time is at most one item that is not corrupted, and sets to the current key of that item, which is its own key. Lists only shrink otherwise.
Lemma 5. For every node of rank , , where for and for .
Proof. First, : for this is , and . A list grows only in line L5, while it has fewer than items, by one list of a child, of rank . So , with for by Lemma 4. Then for , .
Lemma 6. At most nodes of rank are ever made.
Proof. Nodes of rank 0 are made only by insert, so there are . A node of rank is made only by LINK of two roots of rank , and these two nodes are no longer roots afterwards, and a node never becomes a root again. So each node of rank takes part in at most one link, and the number of nodes of rank is at most half the number of rank .
Theorem. At any time the heap holds exactly the items inserted and not deleted (I5); delete-min returns an item of smallest current key (Lemma 3); current keys never decrease (Lemma 1); and the number of corrupted items is at most . With this is less than .
Proof. The first three claims are Lemmas 2, 3 and 1. By Lemma 4, every corrupted item is in a node of rank . By Lemmas 5 and 6, these nodes hold at most
items. If , then , so the count is at most .