Algorithm Search⌘K

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 O(log⁡n)O(\log n) amortized time, where nn 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 xx, let s(x)s(x) be the number of nodes in the binary subtree of xx in this form. This is xx and its descendants, together with the right siblings of xx and their descendants. The root has no siblings, so s(root)s(\mathrm{root}) is the number of items. Let

Φ=∑xlg⁡s(x).\Phi = \sum_x \lg s(x).

Each term is at least 00, and the empty heap has Φ=0\Phi = 0. So for any sequence, the sum of the amortized costs, actual cost plus change of Φ\Phi, 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 x,y>0x, y > 0 and x+y≤1x + y \le 1, then lg⁡x+lg⁡y≤−2\lg x + \lg y \le -2.

Proof. For a fixed sum, the product xyxy is largest when x=yx = y, so xy≤1/4xy \le 1/4. □\square

So for positive pp and qq, lg⁡p+lg⁡q≤2lg⁡(p+q)−2\lg p + \lg q \le 2 \lg(p + q) - 2, by Lemma 1 applied to p/(p+q)p/(p+q) and q/(p+q)q/(p+q).

Lemma 2 (a link of two roots). Let aa and bb be roots without siblings, of trees with AA and BB nodes, and let A+B≤nA + B \le n. Then LINK(a,b)\mathrm{LINK}(a, b) raises Φ\Phi by at most 2lg⁡n2 \lg n.

Proof. Say aa wins. Before the link, s(a)=As(a) = A and s(b)=Bs(b) = B. After it, bb is the first child of aa and its right sibling is the old first child of aa, so s(b)=B+A−1s(b) = B + A - 1 and s(a)=A+Bs(a) = A + B. The binary subtree of every other node is the same set as before. So the change is lg⁡(A+B)+lg⁡(A+B−1)−lg⁡A−lg⁡B≤2lg⁡n\lg(A + B) + \lg(A + B - 1) - \lg A - \lg B \le 2 \lg n. □\square

Insert and meld. One link and O(1)O(1) other work, so by Lemma 2 the amortized cost is at most 2lg⁡n+O(1)2 \lg n + O(1).

Decrease-key. Cut the subtree of xx. After the cut, the binary subtree of xx loses the right siblings of xx, and the binary subtree of each node that contained xx loses the nodes of the tree of xx, or stays the same. No binary subtree grows, so Φ\Phi does not go up. The link with the root adds at most 2lg⁡n2 \lg n by Lemma 2. The amortized cost is at most 2lg⁡n+O(1)2 \lg n + O(1).

Delete-min. Let the root rr have the children x1,…,xkx_1, \dots, x_k, left to right, with trees of t1,…,tkt_1, \dots, t_k nodes. In the binary form they make a right chain, and s(xi)=ti+ti+1+⋯+tks(x_i) = t_i + t_{i+1} + \dots + t_k. Write Si=s(xi)S_i = s(x_i) and Sk+1=0S_{k+1} = 0. The actual cost is k+1k + 1 units at most.

Removal of the root. The term lg⁡s(r)=lg⁡n\lg s(r) = \lg n goes away and no other binary subtree changes. The change is −lg⁡n-\lg n.

First pass. Consider the link of xix_i with xi+1x_{i+1} for an odd i<ki < k. Let a=tia = t_i, b=ti+1b = t_{i+1} and c=Si+2c = S_{i+2}, so Si=a+b+cS_i = a + b + c. Before the link, s(xi)=a+b+cs(x_i) = a + b + c and s(xi+1)=b+cs(x_{i+1}) = b + c. After it, the winner stays in the chain with s=a+b+cs = a + b + c, and the loser becomes its first child, with the other children of the winner as its right siblings, so s=a+b−1s = a + b - 1. Nothing else changes. The change is

lg⁡(a+b−1)−lg⁡(b+c)≤lg⁡(a+b)−lg⁡(b+c).\lg(a + b - 1) - \lg(b + c) \le \lg(a + b) - \lg(b + c).

If c>0c > 0, then lg⁡(b+c)≥lg⁡c\lg(b + c) \ge \lg c, and by Lemma 1 lg⁡(a+b)≤2lg⁡(a+b+c)−lg⁡c−2\lg(a + b) \le 2 \lg(a + b + c) - \lg c - 2, so the change is at most 2lg⁡Si−2lg⁡Si+2−22 \lg S_i - 2 \lg S_{i+2} - 2. If c=0c = 0, which happens only for the last pair when kk is even, the change is at most lg⁡(a+b)≤2lg⁡Si\lg(a + b) \le 2 \lg S_i. A last tree without a partner does not change Φ\Phi. Add over the ⌊k/2⌋\lfloor k/2 \rfloor pairs: the terms telescope, and the sum is at most

2lg⁡S1−2⌊k/2⌋+2≤2lg⁡n−2⌊k/2⌋+2.2 \lg S_1 - 2 \lfloor k/2 \rfloor + 2 \le 2 \lg n - 2 \lfloor k/2 \rfloor + 2.

Second pass. Let the trees of the first pass be y1,…,ymy_1, \dots, y_m, left to right, with m=⌈k/2⌉m = \lceil k/2 \rceil. The pass keeps a tree RR at the right end of the chain and links the tree just to its left into it. Let that tree have pp nodes and let RR have qq nodes. Before the link, the left one has s=p+qs = p + q and the root of RR has s=qs = q. After it, the winner has s=p+qs = p + q and the loser has s=p+q−1s = p + q - 1. The binary subtrees of the trees further left hold the same nodes as before. The change is lg⁡(p+q−1)−lg⁡q≤lg⁡(p+q)−lg⁡q\lg(p + q - 1) - \lg q \le \lg(p + q) - \lg q. The new RR has p+qp + q nodes, so the changes telescope, and the sum over the pass is at most lg⁡(n−1)−lg⁡q0≤lg⁡n\lg(n - 1) - \lg q_0 \le \lg n, where q0≥1q_0 \ge 1 is the size of ymy_m.

Total. The amortized cost of delete-min is at most

(k+1)−lg⁡n+(2lg⁡n−2⌊k/2⌋+2)+lg⁡n=2lg⁡n+k−2⌊k/2⌋+3≤2lg⁡n+4.(k + 1) - \lg n + \left(2 \lg n - 2 \lfloor k/2 \rfloor + 2\right) + \lg n = 2 \lg n + k - 2 \lfloor k/2 \rfloor + 3 \le 2 \lg n + 4.

Delete. The cut does not raise Φ\Phi, as for decrease-key. The removal of xx removes the term lg⁡s(x)≥0\lg s(x) \ge 0. The two passes on the children of xx raise Φ\Phi by at most 2lg⁡n−2⌊k/2⌋+2+lg⁡n2 \lg n - 2 \lfloor k/2 \rfloor + 2 + \lg n, and pay for their k−1k - 1 links up to a constant, as above, with 3lg⁡n3 \lg n in place of 2lg⁡n2 \lg n. The final link adds at most 2lg⁡n2 \lg n. So the amortized cost is O(log⁡n)O(\log n).

Conclusion. Every operation takes O(log⁡n)O(\log n) amortized time, and find-min takes O(1)O(1). Since Φ≥0\Phi \ge 0 and Φ=0\Phi = 0 at the start, any sequence of mm operations on an initially empty pairing heap takes O(mlog⁡n)O(m \log n) time in total. ■\blacksquare

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 K(T)K(T) for the multiset of keys of a tree TT. A tree is heap-ordered if each node has a key at least the key of its parent.

Lemma 1 (link). Let AA and BB be heap-ordered trees with roots aa and bb, or let one of them be empty. Then LINK(a,b)\mathrm{LINK}(a, b) returns the root of a heap-ordered tree TT with K(T)=K(A)⊎K(B)K(T) = K(A) \uplus K(B), and the root of TT holds min⁡K(T)\min K(T).

Proof. If one tree is empty, LINK returns the other, and the claim holds. Otherwise, after line L2 we have a.key≤b.keya.key \le b.key. Line L3 adds one edge, from aa to bb, and puts bb at the front of the child list of aa. The new edge is in heap order because b.key≥a.keyb.key \ge a.key. All other parent and child pairs are as before, so they are in heap order. No node is added or removed, so K(T)=K(A)⊎K(B)K(T) = K(A) \uplus K(B). In a heap-ordered tree, each key is at least the key of the root, by induction on the depth, so the root aa holds min⁡K(T)\min K(T). □\square

Lemma 2 (pairing). Let T1,…,TkT_1, \dots, T_k be heap-ordered trees, k≥0k \ge 0. Then MERGE-PAIRS(T1,…,Tk)\mathrm{MERGE\text{-}PAIRS}(T_1, \dots, T_k) returns one heap-ordered tree TT with K(T)=⨄iK(Ti)K(T) = \biguplus_i K(T_i), or NIL if k=0k = 0.

Proof. The first pass (lines L8 and L9) replaces each pair Ti,Ti+1T_i, T_{i+1} by their link and keeps a last tree alone if kk is odd. By Lemma 1, the list YY holds heap-ordered trees whose multisets of keys add up to ⨄iK(Ti)\biguplus_i K(T_i). In the second pass (lines L10 and L11) we use this loop invariant: rr is NIL or the root of a heap-ordered tree whose keys are the union of the keys of the trees of YY already processed. It holds at the start with $r = $ NIL. Each step links the next tree of YY with rr, and by Lemma 1 the invariant holds again. At the end, all trees of YY are processed, and line L12 returns the claimed tree. □\square

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 rr holds a smallest key, so line L7 returns a correct answer. The children of rr are the roots of heap-ordered trees, and these trees hold all keys except one copy of r.keyr.key. 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 k≤x.keyk \le x.key. If xx is the root, only its key goes down, and its children still have keys at least kk, so heap order holds. Otherwise, line L14 cuts the subtree SS of xx out. The rest of the tree is still heap-ordered, because removing a subtree removes edges and adds none. In SS, the children of xx had keys at least the old key of xx, which is at least kk, so SS is heap-ordered. By Lemma 1, the link of the root with xx gives one heap-ordered tree with the old keys, except that the key of xx is now kk.

Delete. If xx is the root, this is a delete-min. Otherwise, line L16 cuts the subtree of xx; as before, the rest is heap-ordered. The children of xx are roots of heap-ordered trees, so by Lemma 2 MERGE-PAIRS gives one heap-ordered tree with the keys of the subtree of xx without xx. By Lemma 1, the final link gives one heap-ordered tree with all keys except one copy of x.keyx.key.

Pointers. LINK puts bb at the front of the child list of aa: b.siblingb.sibling becomes the old first child, the prevprev of that old first child becomes bb, b.prevb.prev becomes aa, and a.childa.child becomes bb. A cut of xx sets the pointer that pointed to xx (the childchild of the parent if xx is a first child, else the siblingsibling of the left sibling) to x.siblingx.sibling, and sets the prevprev of x.siblingx.sibling to x.prevx.prev. 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. ■\blacksquare

Back to Pairing heap