Proofs: d-ary heap
These are the human proofs of d-ary heap, at full length.
The costs of the d-ary heap operations full
This proof shows: time.worst, space.
Let , , and let be the height of the tree, the largest depth of a node.
Lemma 1 (levels). The nodes at depth are the indices with . So is the least integer with , and .
Proof. By induction on . Depth 0 is the index 1. Let depth be the range , with and . The children of these nodes are the indices from to , and and , so depth is the range . A node at depth exists, so , which gives . So .
Insert and decrease-key. SIFT-UP does one comparison and at most one swap per level and goes up from depth at most , so it costs by Lemma 1.
Extract-min. SIFT-DOWN goes down at most levels, and each iteration of line L6 does at most comparisons in lines L8 to L10. So it does at most comparisons, for .
Lemma 2 (sum of heights). Let be the number of nodes of height at least . Then for , and .
Proof. A node has height at least exactly when its first descendant levels down exists. The first child of is , so with we get , and the first descendant levels down, , has . So means , which implies . The nodes with this property are , so . Then
Build. Line L22 runs SIFT-DOWN once at each internal node. At a node of height , each iteration of line L6 that does a comparison is at a node with a child, and there are at most such iterations, one per level. So the sift does at most comparisons. With Lemma 2 and Lemma 1, build does at most
comparisons, and its time is of the same bound, so build is for each .
Space. The array has cells, and each operation uses extra cells.
Theorem. In a -ary heap of keys, insert and decrease-key take time, extract-min takes time, find-min takes time, and build takes time, which is for .
Proof. The cases above.
The operations of a d-ary heap keep the heap property full
This proof shows: correctness, termination.
Let . Write for and for the set of children of that are at most .
The tree. For each , is a child of , and each has : write with ; then with , so . Also for . So the formulas define a rooted tree on with root 1, in which each node has at most children, and the index of each node is larger than the index of its parent.
An edge is a pair with , and it holds when . The invariant (H) says that each edge holds.
Lemma 1 (sift-up). If each edge with holds and, when , for each , then SIFT-UP stops after at most the depth of iterations, only swaps keys, and afterwards (H) holds.
Proof. The proof of Lemma 1 on the binary heap proof page uses only these facts: the node has one parent , the swap of line L4 exchanges and when , the other children of held their edges to , and the edges out of are covered by the second part of the hypothesis. All of them hold in a tree with any number of children per node, with "each other child of " in place of "the other child". Each iteration moves to its parent, so it stops after at most the depth of iterations, at the root or when the edge into holds.
Lemma 2 (sift-down). If each edge with holds and, when , for each , then SIFT-DOWN stops after at most the height of swaps, changes only cells of the subtree of , and afterwards (H) holds.
Proof. Lines L7 to L10 set to an index of a smallest key among and for : the loop goes over all children , skips those beyond , and keeps the index of the least key seen, with at the start. If , the edges out of hold, and with the hypothesis each edge holds. Else let , with for each other child of . After the swap of line L12, the edge holds since , each edge holds since , and the edge , if , holds since the hypothesis gave . The hypothesis holds again at : its parent now holds , and the children of had , since the edges out of were not excluded. Each swap moves one level down in the subtree of the first , so there are at most as many swaps as its height.
Insert. After line L13 the new cell is a leaf, and every old edge holds, so Lemma 1 applies to , and (H) holds after line L14. The keys are the old keys and .
Find-min and extract-min. By (H) and induction along the path from the root, for each . So is a minimum. Line L16 saves it, moves to the root and drops the cell . Every edge not out of the root joins two unchanged cells, so it holds, and the root has no parent. So Lemma 2 applies to , and (H) holds after line L17. The keys are the old keys with one copy of the minimum removed.
Decrease-key. If , the edges out of still hold after line L20 writes , the old edges give for each , and every other edge is unchanged. So Lemma 1 applies, and (H) holds.
Build. Before the iteration of line L22 for , the subtree of each is a heap. If there is no edge and nothing to do. Else, at the start, , and each has no children: , so the first child of is . So the subtree of has no edges. In the iteration for , the children of have larger indices, so their subtrees are heaps, and Lemma 2 applied to the subtree of makes it a heap. Lemma 2 changes only cells of that subtree. A is not an ancestor of , so the subtree of is either inside the subtree of , and then it is part of a heap, or disjoint from it, and then it did not change. After , the whole tree is a heap.
Theorem. Each operation keeps (H), FIND-MIN and EXTRACT-MIN return a minimum, the keys change as the priority queue type says, and each operation stops.
Proof. The cases above with Lemmas 1 and 2.