Proofs: Pairing heap
These are the human proofs of Pairing heap, at full length.
Amortized cost of the pairing heap full
This proof shows: time.amortized.
We prove that each operation of the two-pass pairing heap takes amortized time, where is the largest number of items in the heap during the sequence. The argument is the one of Fredman, Sedgewick, Sleator and Tarjan (1986), with the potential function of the splay tree.
The potential. View the tree in the child-sibling form: the left child of a node is its first child, and the right child is its next sibling. For a node , let be the number of nodes in the binary subtree of in this form. This is and its descendants, together with the right siblings of and their descendants. The root has no siblings, so is the number of items. Let
Each term is at least , and the empty heap has . So for any sequence, the sum of the amortized costs, actual cost plus change of , is at least the sum of the actual costs. We count one unit of actual cost per link, plus one unit per operation.
Lemma 1. If and , then .
Proof. For a fixed sum, the product is largest when , so .
So for positive and , , by Lemma 1 applied to and .
Lemma 2 (a link of two roots). Let and be roots without siblings, of trees with and nodes, and let . Then raises by at most .
Proof. Say wins. Before the link, and . After it, is the first child of and its right sibling is the old first child of , so and . The binary subtree of every other node is the same set as before. So the change is .
Insert and meld. One link and other work, so by Lemma 2 the amortized cost is at most .
Decrease-key. Cut the subtree of . After the cut, the binary subtree of loses the right siblings of , and the binary subtree of each node that contained loses the nodes of the tree of , or stays the same. No binary subtree grows, so does not go up. The link with the root adds at most by Lemma 2. The amortized cost is at most .
Delete-min. Let the root have the children , left to right, with trees of nodes. In the binary form they make a right chain, and . Write and . The actual cost is units at most.
Removal of the root. The term goes away and no other binary subtree changes. The change is .
First pass. Consider the link of with for an odd . Let , and , so . Before the link, and . After it, the winner stays in the chain with , and the loser becomes its first child, with the other children of the winner as its right siblings, so . Nothing else changes. The change is
If , then , and by Lemma 1 , so the change is at most . If , which happens only for the last pair when is even, the change is at most . A last tree without a partner does not change . Add over the pairs: the terms telescope, and the sum is at most
Second pass. Let the trees of the first pass be , left to right, with . The pass keeps a tree at the right end of the chain and links the tree just to its left into it. Let that tree have nodes and let have nodes. Before the link, the left one has and the root of has . After it, the winner has and the loser has . The binary subtrees of the trees further left hold the same nodes as before. The change is . The new has nodes, so the changes telescope, and the sum over the pass is at most , where is the size of .
Total. The amortized cost of delete-min is at most
Delete. The cut does not raise , as for decrease-key. The removal of removes the term . The two passes on the children of raise by at most , and pay for their links up to a constant, as above, with in place of . The final link adds at most . So the amortized cost is .
Conclusion. Every operation takes amortized time, and find-min takes . Since and at the start, any sequence of operations on an initially empty pairing heap takes time in total.
Correctness of the pairing heap full
This proof shows: correctness.
We prove that each operation of the pairing heap keeps the invariant (P1) to (P3) of the page and changes the multiset of keys as the priority queue requires. Write for the multiset of keys of a tree . A tree is heap-ordered if each node has a key at least the key of its parent.
Lemma 1 (link). Let and be heap-ordered trees with roots and , or let one of them be empty. Then returns the root of a heap-ordered tree with , and the root of holds .
Proof. If one tree is empty, LINK returns the other, and the claim holds. Otherwise, after line L2 we have . Line L3 adds one edge, from to , and puts at the front of the child list of . The new edge is in heap order because . All other parent and child pairs are as before, so they are in heap order. No node is added or removed, so . In a heap-ordered tree, each key is at least the key of the root, by induction on the depth, so the root holds .
Lemma 2 (pairing). Let be heap-ordered trees, . Then returns one heap-ordered tree with , or NIL if .
Proof. The first pass (lines L8 and L9) replaces each pair by their link and keeps a last tree alone if is odd. By Lemma 1, the list holds heap-ordered trees whose multisets of keys add up to . In the second pass (lines L10 and L11) we use this loop invariant: is NIL or the root of a heap-ordered tree whose keys are the union of the keys of the trees of already processed. It holds at the start with $r = $ NIL. Each step links the next tree of with , and by Lemma 1 the invariant holds again. At the end, all trees of are processed, and line L12 returns the claimed tree.
Theorem. Each operation keeps (P1) to (P3) and is correct.
Proof. Assume the invariant holds before the operation.
Find-min. By (P2) and the remark in Lemma 1, the root holds a smallest key.
Insert and meld. A new node is a heap-ordered tree with one node. By Lemma 1, line L5 or line L1 gives one heap-ordered tree whose keys are the old keys plus the new key, or the keys of both heaps. The count grows by one, or becomes the sum of the counts.
Delete-min. The root holds a smallest key, so line L7 returns a correct answer. The children of are the roots of heap-ordered trees, and these trees hold all keys except one copy of . By Lemma 2, line L6 makes them one heap-ordered tree, so (P1) and (P2) hold, and the count goes down by one.
Decrease-key. Let the new key be . If is the root, only its key goes down, and its children still have keys at least , so heap order holds. Otherwise, line L14 cuts the subtree of out. The rest of the tree is still heap-ordered, because removing a subtree removes edges and adds none. In , the children of had keys at least the old key of , which is at least , so is heap-ordered. By Lemma 1, the link of the root with gives one heap-ordered tree with the old keys, except that the key of is now .
Delete. If is the root, this is a delete-min. Otherwise, line L16 cuts the subtree of ; as before, the rest is heap-ordered. The children of are roots of heap-ordered trees, so by Lemma 2 MERGE-PAIRS gives one heap-ordered tree with the keys of the subtree of without . By Lemma 1, the final link gives one heap-ordered tree with all keys except one copy of .
Pointers. LINK puts at the front of the child list of : becomes the old first child, the of that old first child becomes , becomes , and becomes . A cut of sets the pointer that pointed to (the of the parent if is a first child, else the of the left sibling) to , and sets the of to . These are the updates of a doubly linked list, so (P3) holds after each operation.
Every operation keeps (P1) to (P3), and each answer is a smallest key of the current multiset. So the pairing heap implements the meldable priority queue.