Proofs: Thin heap
These are the human proofs of Thin heap, at full length.
Amortized cost of the thin heap full
This proof shows: time.amortized.
We prove that insert, meld, find-min and decrease-key take amortized time and that delete-min takes amortized time, where is the number of items at the time of the operation.
Cost units. Count one unit for each operation, one unit for each root that delete-min puts into the buckets, one unit for each link, and one unit for each iteration of the loop of decrease-key. Every other step of an operation takes constant time per unit.
Potential. Let be the number of trees and the number of thin nodes, and let . The empty heap has , and always, so the total actual cost of any sequence that starts from an empty heap is at most the total amortized cost.
Insert and meld. Insert has actual cost 1 and adds one tree, so its amortized cost is . Meld has actual cost 1 and does not change or of the union. Find-min has actual cost 1.
Decrease-key. If is a root, the actual cost is 1 and does not change. Otherwise the cut at line L9 adds one tree, (and more if was thin). Consider the iterations of the loop.
- An iteration that ends at line L14 cuts a thin node (a parent defect with means that was thin before the operation started the iteration, with first child of rank ) and makes it a normal root: , , . With the actual cost of 1, the amortized cost is .
- An iteration at line L16 makes the thin node normal: , amortized .
- The last iteration returns at line L12, L13 or L15. Line L12 changes only the rank of a root, which stays normal. Lines L13 and L15 make thin. So , and the amortized cost is at most 5.
So the amortized cost of decrease-key is at most .
Delete-min. Let be the number of trees before, let be the number of children of the minimum , and let be the number of trees after. Lemma 1 of the correctness proof gives . The operation puts roots into the buckets, and each link removes one tree, so it makes links. It may make thin children normal (line L4), and it makes no node thin, so . The actual cost is , where the last term pays for building the new root list, and
After the links, the roots have distinct ranks, all at most by Lemma 1, so . The amortized cost is at most .
The thin-heap operations keep the thin-heap invariants full
This proof shows: correctness.
We use the invariants of the page. (1) Heap order: each key is at least the key of its parent. (2) The children of each node , in list order, have the ranks for ( normal) or ( thin), where an empty list has . (3) Every root is normal. Write .
Lemma 1 (rank bound). A node of rank has at least nodes in its subtree, where and . So in a heap of items every rank is at most .
Proof. By induction on . For the subtree has at least node. For , by (2) the node has children of ranks at least , so by induction its subtree has at least nodes, using . Since , and .
Lemma 2 (link). If and are normal roots of rank that satisfy (1) and (2) in their trees, LINK returns a normal root of rank whose tree satisfies (1) and (2).
Proof. Line L1 makes , so heap order holds at the new edge. The children of had the ranks ; now , of rank , comes first, so they are and is normal with rank . The node keeps its children and its rank, and it was normal.
Theorem 1 (insert, meld, delete-min). These operations keep (1) to (3), and delete-min returns a smallest key.
Proof. Insert adds a root of rank 0 with no child, which is normal. Meld only joins two lists of roots. In delete-min, the minimum root holds a smallest key: by heap order, every key is at least the key of its root, and is the smallest root. The children of become roots. A thin child gets the rank at line L4. This changes no child list, and it makes normal, so (2) and (3) hold for all roots. Heap order has no parent condition for a root. Lemma 2 shows that each link keeps (1) to (3). At the end the smallest root is found again.
The defect of decrease-key. Say the heap has a sibling defect at if is not a root, (1) holds, and (2) and (3) hold except that the node after in its sibling list has rank (with rank if is the last child). Say it has a parent defect at if (1) holds, and (2) and (3) hold except at , whose children have the ranks with . A parent defect with is not a defect at all when is not a root: is then thin.
Lemma 3 (the hole left by a cut). Suppose (1) to (3) hold except for one defect that is not at or below , and let be a non-root node with rank and left neighbor , that is, its parent if is a first child and its left sibling otherwise. After and its subtree are cut out of the list and becomes a root with rank , the heap has a sibling defect at if was the left sibling of , and a parent defect at if was the parent of . The subtree of satisfies (1) to (3).
Proof. The node after in the list had rank , or was the last child and . If is the left sibling, its rank is , and after the cut its next node has rank : a sibling defect. If is the parent, its first child had rank and ; now the first child has rank , so : a parent defect. All other lists are unchanged. The new rank of makes it normal and does not change its children. Heap order inside the subtree of holds as before, and has no parent.
Lemma 4 (one step of the loop). If the heap has a sibling or parent defect at before an iteration of lines L11 to L16, then after it either (1) to (3) hold, or there is a defect of one of the two kinds at the old left neighbor of .
Proof. Heap order is not changed by a change of rank. The only move of a node from one parent to another is line L15, which moves the first child of to be a sibling of , so the new parent of is the parent of , and .
Parent defect, line L12. If is a root, setting makes normal, and as a root it has no sibling condition. All invariants hold.
Parent defect, line L13. If is not a root and , then is thin, which (2) allows. The rank of did not change, so its own list position is correct. All invariants hold.
Parent defect, line L14. If and is not a root, line L14 cuts and makes it a normal root. By Lemma 3, applied with , the heap has a defect at the old left neighbor of .
Sibling defect, line L15. Let . If is normal, its first child has rank , and its next child has rank . Line L15 moves right after . Now the list of 's parent reads , which is correct, and the children of start at rank , so is thin, which is allowed for a non-root. All invariants hold.
Sibling defect, line L16. If is thin, its first child has rank . Line L16 sets , so is normal, and its next node, of rank , is now correct. In the list that holds , had rank and now has rank . If has a left sibling , then : a sibling defect at . If is a first child of , then the first child of now has rank , one less than before, and : a parent defect at .
Theorem 2 (decrease-key). DECREASE-KEY keeps (1) to (3), and the heap holds the same items with the new key at .
Proof. If is a root, lowering its key keeps heap order, because a root has no parent, and line L8 updates . Otherwise line L9 cuts . Inside its subtree, every key was at least the old key of , which is at least the new key, so heap order holds there, and is a root. By Lemma 3 there is now one defect at the old left neighbor . By Lemma 4, each iteration either removes the defect, and the procedure returns, or moves it to the left neighbor of . Moving to a left sibling or to a parent goes along a path that cannot repeat a node: each move increases the rank of the current node or goes up a level, and there are finitely many nodes. The move from a parent defect at a root always returns at line L12. So the loop ends, and when it ends, (1) to (3) hold. No node is removed or created.