Proofs: Sift-down
These are the human proofs of Sift-down, at full length.
Sift-down restores the heap property in a subtree full
This proof shows: correctness, termination.
The children of are and when they are less than . Let be the set of indices in the subtree of . An edge is a pair with a child of . It is right when . Let be the input index.
Theorem. Assume and that each edge of whose parent is not is right. Then SIFT-DOWN ends after at most iterations, where is the number of levels below . At the end, all edges of are right, the items of are a permutation of the old ones, and no cell outside has changed.
Proof. We show that this invariant holds each time line L2 starts:
(I) . (D1) Each edge of whose parent is not is right. (D2) If and is the parent of , then for each child of . Only cells of have changed, by swaps.
Initialization. At the first iteration, . Then (D1) is the hypothesis, (D2) does not apply, and nothing has changed.
Maintenance. Assume (I). If line L3 or line L6 returns, we treat it under termination. Else has a child, lines L4 and L5 set to a child of with for each child of , and . Write and before line L7, so . After line L7, and . We check each edge with an end at or . All other edges keep both values.
- The edge : , so it is right.
- The edge to the other child of , if there is one: by the choice of , so it is right.
- The edge from the parent of , if : by (D2), , because is a child of . So it is right. If , this edge is not in , and we do not need it.
- The edges for the children of : these are the edges that (D1) for the new index leaves out.
So each edge of whose parent is not is right, which is (D1) for . For (D2) with the new index , whose parent is : before the swap, each edge was right by (D1), since , so . The new value at is , so for each child of . A swap of two cells of keeps the multiset of , and . Line L8 sets , so (I) holds at the next iteration.
Termination. Each iteration that does not return moves to a child, so the number of levels below drops by exactly 1. The number of levels below is , and a node with no level below it is a leaf, at which line L3 returns. So there are at most iterations that do not return, and one more that returns.
If line L3 returns, is a leaf, so there is no edge with parent , and (D1) covers all edges of . If line L6 returns, then for each child of , so the edges with parent are right, and with (D1) all edges of are right. By (I), only cells of changed, and their items are a permutation of the old ones.
EXTRACT-MIN. Let be a min-heap with . Line L9 saves the root, which is a smallest item because each item is reached from the root by right edges. After line L10 the heap has items, and each edge whose parent is not joins two cells that did not change, so it is right. If , the theorem with makes the whole array a min-heap. The items are the old items less one copy of the returned minimum.
Increase-key. If is a min-heap and the value at grows, each edge of whose parent is not keeps both values, so the theorem applies to . The edge from the parent of stays right: the final value at is the new value, which is larger than the old one, or a value from , which was at least the old .
Floyd's construction. Call SIFT-DOWN for . Claim: after the call for , the subtrees of are min-heaps. The indices are leaves, so the claim holds before the first call. When the call for starts, its children are larger indices, so their subtrees are heaps, which is the hypothesis of the theorem. The call changes only cells of , and for the subtree is either inside , where the call leaves all edges right, or disjoint from it, where nothing changes. So the claim holds after the call, and after the call for the whole array is a heap.