Proofs: Binomial heap
These are the human proofs of Binomial heap, at full length.
The operations of a binomial heap keep its invariants and return a minimum full
This proof shows: correctness, termination.
We write for the multiset of keys of the heap . A list of roots is valid when each of its trees is a binomial tree with the min-heap property, the parent pointers inside each tree are right, each root has parent nil, and no two trees have the same order . The invariant of a heap is that is valid and is the number of its nodes. The order of a is the degree of its root, by property 3 of the representation section.
Lemma 1 (LINK). Let and be the roots of two heap-ordered binomial trees , with . After LINK, the tree of is a heap-ordered with the same nodes as the two trees, and its children are in increasing order of degree.
Proof. By the definition, a with the root of the other as a new child of its root is a . Lines L1 and L2 do that and line L3 sets the degree to . The children of had the degrees in this order, and is added last with degree . The new edge is ordered because , and the other edges are unchanged.
Lemma 2 (UNION-ROOTS). Let and be valid lists of roots on disjoint sets of nodes. Then UNION-ROOTS stops and returns a valid list of roots on the union of the two sets of nodes.
Proof. We claim that, before each iteration of line L5 and before each test of line L7, the following hold. (a) Each entry is a heap-ordered with its root's parent nil. (b) The trees of , the tree of the current (inside the loop of line L7), and the trees of and not yet visited hold each node of the input exactly once. (c) Inside the loop of line L7, is a heap-ordered and is not in .
At the start, is empty and nothing is visited, so (a) and (b) hold. At line L6, the next root of degree is the root of a heap-ordered , so (c) holds. In an iteration of the loop, line L8 takes , a heap-ordered by (a), out of . Line L9 makes , and then both and are roots of heap-ordered . By Lemma 1, after line L10 the tree of is a heap-ordered , and is now . So (c) holds for the new , and (b) holds because the two trees became one. When the test of line L7 fails, is empty, and line L11 stores the of there, so (a) and (b) hold after the iteration.
Each pass of the loop of line L7 links two trees into one, so the number of trees goes down by 1. There are trees at the start, so there are fewer than links in total, and the procedure stops. At the end, by (a) and (b), the trees of are heap-ordered binomial trees of distinct orders, one for each with , and they hold each node once. The parent of each root is nil, because LINK changes only the parent of the root that becomes a child. So the list of line L12 is valid.
Union. By Lemma 2, becomes a valid list on the nodes of both heaps, and becomes the sum. So becomes the union of the two multisets, and becomes an empty, valid heap.
Insert. Line L14 makes a heap-ordered . We claim that before each test of line L15, with , the node is the root of a heap-ordered , the list is valid on the other nodes, and each root in it has degree at least . This holds at the start with . So a root of degree , if it exists, is the root of least degree, which is the one that line L15 tests. Lines L16 to L18 remove it and, by Lemma 1, make the tree of a heap-ordered . The other roots had distinct degrees larger than , so they have degree at least , and the claim holds again. Each iteration removes a root, so the loop stops. When it stops, each root has a degree larger than , so line L19 makes a valid list on the old nodes and , with of least degree. gains the key of .
Minimum. Let . Each node is in some tree, and by the global form of the heap property, its key is at least the key of the root of that tree. So a root of least key, which line L21 returns, holds a minimum of .
Extract-min. Let be the root that MINIMUM returns, a minimum of , and let be the root of a . Line L23 removes it, and the other roots form a valid list on the other trees. By property 3, the children of are the roots of heap-ordered binomial trees of the orders , which are distinct, and line L24 sets their parents to nil, so is a valid list. The two lists have disjoint nodes. By Lemma 2, line L25 makes a valid list on all nodes except , and drops by 1. So loses one copy of its minimum, which line L26 returns.
Decrease-key. Let , as line L27 checks. Line L28 lowers the key. No line of DECREASE-KEY changes the shape of a tree, so each tree is still a binomial tree, and the list is valid except for the heap order. We claim that before each test of line L29, the only edge that may not be ordered is the edge , and that for each child of when . This is the condition (U) of the sift-up lemma on the binary heap page, and the proof of that lemma works on each rooted tree: line L30 exchanges the contents of and , which plays the swap, and line L31 moves up one level. At the start, after line L28, the claim holds because the edges from to its children were ordered with the old, larger key, and the old key of was at least the key of its parent. Each iteration goes up one level, so the loop stops after at most iterations in a . When it stops, either is a root or the edge is ordered, so each edge is ordered. Line L30 moves the items with their keys and updates the handles, so each handle still names the node of its item. has the old key replaced by .
Delete. Line L32 is a decrease to a key below all others, so by the case above the item reaches the root of its tree, and then this root has the least key, so MINIMUM returns it at line L33, and EXTRACT-MIN removes it. In code, the climb to the root can be done without comparisons, as the Python code does.
Theorem. Starting from empty heaps, after each sequence of operations each heap satisfies its invariant, MINIMUM and EXTRACT-MIN return an item of minimum key, and the multiset of keys of each heap is the one that the meldable priority queue type gives for the same sequence. Each operation stops.
Proof. The empty heap has the empty valid list. Each operation keeps the invariant and changes the keys as the type says, by the cases above, and each stops.