Proofs: Floyd's heap construction
These are the human proofs of Floyd's heap construction, at full length.
Floyd's method uses fewer than n swaps and 2n comparisons full
This proof shows: time.worst, time.best, time.average, space.extra.
Let . For a node , its height is the largest such that . We write for the set of nodes of the subtree of .
Lemma 1. is the largest depth of a node of below .
Proof. The nodes of at depth below are the indices that are at most , by induction on : the children of the indices to are the indices to . So there is a node at depth exactly when the smallest of them, , is at most .
Lemma 2. MAX-HEAPIFY does at most swaps and at most key comparisons.
Proof. Let be the first value of , so the claim is about . Each swap moves one level down inside , so by Lemma 1 there are at most swaps. Each iteration does at most 2 comparisons (lines L5 and L6), and an iteration at a leaf does none, because both tests and fail. Let be the number of swaps. There are iterations. If the last one is at a leaf, the comparisons are at most . Else the last node has a child, so it is at depth , and the comparisons are at most .
Lemma 3. , where is the number of 1 bits of .
Proof. By the definition, exactly when , that is, when . So, counting each once for each with ,
Write with . Then , because the part is less than . Summing over and exchanging the sums,
(The term adds .)
Theorem. BUILD-MAX-HEAP does at most swaps and at most key comparisons. Its time is on each input of size , and its extra space is .
Proof. Line L1 calls MAX-HEAPIFY once for each . The leaves have , so by Lemmas 2 and 3 the swaps are at most and the comparisons at most . Each call costs plus for each iteration, and each iteration other than the last does one swap. So the time is . Line L1 does iterations on every input, so the time is in the best case too. The method keeps only the indices , , and and one key for a swap.
Floyd's method builds a max-heap from the keys of the input full
This proof shows: correctness, termination.
Write for the set of indices in the subtree of node in the complete binary tree on : , its children and when they are at most , their children, and so on. Say that is a heap when for each with . Each index in other than is larger than , because a child has a larger index than its parent.
Lemma (MAX-HEAPIFY). Let , and assume that and are heaps (when they exist). Then MAX-HEAPIFY stops, it permutes the keys of the cells of and changes no other cell, and afterwards is a heap.
Proof. Let be the first value of . We show that this statement is a loop invariant of line L3, for the current value of :
(D) ; each edge with and holds; and if , then for each child of with .
Initialization. . The edges inside that do not start at are the edges inside and , and these hold because the two subtrees are heaps. The last part is empty.
Maintenance. Lines L4 to L6 set to an index of a largest key among , (if ) and (if ): after line L5, , and after line L6, is also at least . If , then each edge out of holds, so with (D) each edge inside holds, and line L7 returns with a heap. Else is a child of , , and for the other child of , if it exists. Line L8 swaps and . Then:
- the edge holds, because ;
- the edge holds, because ;
- if , the edge holds, because the last part of (D) gave ;
- for each child of , the edge held before the swap, because (D) excluded only the edges out of , so , and is now at .
The edges out of may now fail, and (D) for the new value excludes exactly them. The last point gives the last part of (D) for . Also . So line L9 keeps (D).
Termination. Each iteration that does not return replaces by a child, so the depth of below grows by exactly 1. The depth is at most the height of , so there are at most that many swaps, and then line L7 returns, since a leaf gives . Each change is a swap of two cells of .
Theorem. BUILD-MAX-HEAP stops, and afterwards is a permutation of its input and satisfies for each .
Proof. We show this invariant of the loop of line L1:
(B) before the iteration for , the subtree is a heap for each with .
Initialization. The first value is . Each has , so it has no child, and is a heap. If the loop does not run, and the array is a heap.
Maintenance. Let . The children and are larger than , so by (B) their subtrees are heaps. The lemma applies: after line L2, is a heap, and only cells of changed. Take with . If , is a heap. If , then , and a subset of a heap of this form is a heap, because its edges are edges of . Else and are disjoint: is not an ancestor of , because an ancestor of has an index smaller than . So no cell of changed, and is still a heap. This is (B) for .
Termination. The loop runs times, and each call stops by the lemma. After the iteration for , (B) with says that is a heap. Each change is a swap, so is a permutation of its input.