Proofs: Heap increase-key
These are the human proofs of Heap increase-key, at full length.
HEAP-INCREASE-KEY leaves a max-heap with the key replaced full
This proof shows: correctness, termination.
Write . An edge is a pair with , and it holds when . Let be the set of children of that exist.
Invariant. Before each test of line L3:
(U) each edge with holds, and if , then for each .
Initialization. Let be the key at before line L2, so by line L1. Before line L2 each edge held. Line L2 changes only the cell . The edges held with , so , and they still hold. The edges not touching are unchanged. The edge is the one that (U) excludes. If , the old edges give for each . So (U) holds.
Maintenance. Let the test be true, so and, with , . Line L4 swaps them, so and . We check the edges with an end at or , except the edge into , which (U) for the new excludes.
- The edge : .
- The edges for : the second part of (U) gave , and did not change.
- The edge for the other child of : it held before, so .
So after line L5 each edge except the one into the new holds. For the second part of (U) at the new : the edge held before the swap, because it is not the edge into the old , so ; and the children of now hold and . So (U) holds.
Termination. Each iteration halves , so the loop stops after at most iterations. When it stops, either , and the root has no edge into it, or , so the excluded edge holds too. With (U), each edge holds.
Theorem. If is a max-heap, and , then after HEAP-INCREASE-KEY the array is a max-heap, and its multiset of keys is the old one with one copy of the old replaced by . If , it reports an error and changes nothing.
Proof. Line L1 handles . Else, by the invariant and termination, each edge holds at the end. Line L2 replaced one key by , and the loop only swaps.