Proofs: Rank-pairing heap
These are the human proofs of Rank-pairing heap, at full length.
Amortized cost of the type-2 rank-pairing heap full
This proof shows: time.amortized.
We prove Theorem 6.1 of Haeupler, Sen and Tarjan for type-2 rank-pairing heaps with multipass linking: find-min, insert, meld and decrease-key take amortized time, and delete-min takes amortized time. The proof follows the paper, with the accounting written out.
The rank bound. A child of rank has at least nodes in its subtree, with , . By induction on height: a missing node of rank has nodes, and a leaf has . A 0,-node of rank has a child of rank , and so at least nodes. A 1,1- or 1,2-node of rank has children of ranks at least and , so at least nodes. A root of rank has a left child of rank , so its half tree has at least nodes. So every rank is at most .
The potential. For a node , let be the sum of the rank differences of its children, where a missing child counts with its rank , but the missing right child of a root does not count. Let for every node. For a root, and . Define the potential of a node as for a root, for a 1,1-node, and for every other non-root. This is the potential of the paper: a root with a 1-child has potential 1, a 1,1-node has 0, and an -node other than a 1,1-node has . Every : a 1,2-node has 2, and a 0,-node with has at least 1. Let be the sum over all nodes. The empty heap has .
Count one unit of time for each operation, each half tree processed in delete-min, each link and each iteration of the rank-reduction loop.
Insert, meld, find-min. Insert adds a root, , so the amortized cost is 2. Meld and find-min do not change .
Delete-min. Deleting the root lowers by 1. The disassembly makes each node on the right spine of a root, with . Each spine node that was not a 1,1-node had , and its children do not change, so its potential does not increase. For a 1,1-node on the spine, its right child, the next spine node, has rank . Ranks along the spine never increase, because rank differences are not negative. So the 1,1-nodes on the spine have distinct ranks, all less than , and there are at most of them. The disassembly raises by at most .
Let be the number of half trees after the disassembly. The rest of the operation takes units plus one per link. Each link turns a root () into a 1,1-node () and does not change the children of any other node, except that the winner gets a new left child. The winner is a root, with before and after. So each link lowers by 1, which pays for the link. After multipass linking, the roots have distinct ranks in , so at most remain, and there were at least links. The amortized cost is at most
Decrease-key. If is a root, the cost is 1 and does not change. Otherwise let be the parent of , let be its right child, and let be the non-roots whose ranks decrease, in this order; is the parent of . Let be the decrease at , and let be the node at which the loop ends (a non-root whose rank does not change, or a root).
Claim A: at most one of is a 1,1-node before the operation, and every after the first 1,1-node among them is 1. A 1,1-node of rank has children of rank ; if one of them loses rank, gives (if the child has rank ) or (if less), so a 1,1-node decreases by at most 1. By Lemma 2 of the correctness proof, a node whose child decreased by 1 decreases by at most 1. So once a 1,1-node decreases, every later decrease is by exactly 1. A later 1,1-node whose child decreases by exactly 1 has children of ranks and , so and it does not decrease. So it is , not one of the .
Accounting. Let over all nodes, where for every node, root or not. Let be the number of 1,1-nodes. Since for all nodes except 1,1-nodes and roots, and for a 1,1-node and for a root, we have , where is the number of roots.
- (the root ).
- is at most the number of nodes that stop being 1,1-nodes. Only nodes whose children's rank differences change can change type: , the , and . By Claim A at most one is a 1,1-node. So .
- . Before, , where ; after, is a root, with . When replaces as a child of , the rank difference of that child of grows from to , so grows by . When the rank of drops by , the rank differences of both its children drop by , so drops by , and the rank difference of as a child of (or of ) grows by , which raises of the parent by . If is a root, its rank is reset to make it a 1-node, so before and after the operation. Adding up,
because and every .
So . The actual cost is 1 for the cut and for the iterations of the loop. The amortized cost of decrease-key is at most , which is .
The rank-pairing heap operations keep half order and the type-2 rank rule full
This proof shows: correctness.
We use the definitions of the page. A missing child has rank . For integers and , let if and if . The invariants are (1) half order: the key of each node is at most every key in its left subtree, and every root has no right child; (2) the type-2 rank rule: every root has , and every non-root is a 1,1-, 1,2- or 0,-node with .
Lemma 1 (the rule fixes the rank). A non-root with children of ranks and obeys the type-2 rule if and only if .
Proof. Let . If and , the rank differences are and or and . If and , they are and . Conversely, a 1,1- or 1,2-node has with , and a 0,-node with has with . In each case .
Lemma 2 (monotone). does not decrease when one argument increases by one, and it increases by at most one.
Proof. Fix and compare with . If , both are . If , they are and . If , they are and when , and and when . If , then , so , and . In every case the increase is 0 or 1. The function is symmetric.
Lemma 3 (link). If and are roots of half trees of equal rank that satisfy (1) and (2), LINK returns the root of a half tree of rank that satisfies (1) and (2).
Proof. After line L1, . The new left subtree of holds , the left subtree of , and the old left subtree of . Every key there is at least : the first two because and by half order at , the third by half order at . The left subtree of did not change. The ranks of the two children of are and , so is a 1,1-node. The root now has rank , so it is a 1-node and has no right child.
Theorem 1 (insert, meld, delete-min). These operations keep (1) and (2), and delete-min returns a smallest key.
Proof. Insert adds a one-node half tree of rank . Meld only joins root lists. In delete-min, each root holds the smallest key of its half tree, because the root has no right child and its left subtree holds all other nodes, so holds a smallest key. Line L4 removes and cuts the right spine . Each keeps its left subtree, so half order holds in the new half tree rooted at , which has no right child, and makes it a 1-node. Nodes inside the left subtrees do not change. The links of lines L5 and L6 join only half trees of equal rank, so they keep (1) and (2) by Lemma 3.
Theorem 2 (decrease-key). DECREASE-KEY keeps (1) and (2).
Proof. If is a root, its key is at most every key in its left subtree before the change, and so also after it. Otherwise let be the parent of and its right child.
Half order after line L9. Let be any node other than . If was in the left subtree of , then so were and its subtree; after line L9 the left subtree of is the old one without and the left subtree of . So it lost keys and gained none, and half order at holds. Otherwise the left subtree of did not change. At , every key of its left subtree was at least the old key of , which is at least the new key. So half order holds everywhere, and is a root with no right child.
The rank rule. Line L9 sets , so the new root is a 1-node. The nodes in the subtrees of and did not change. The only node whose children changed is : its child , of rank , was replaced by , of rank , because rank differences are not negative. We prove by induction over the iterations of lines L10 to L14 this claim: at the start of an iteration with node , every node other than obeys the rule, and the ranks of the children of are at most what they were when last obeyed the rule.
If is a root, line L11 makes it a 1-node, and the rule holds everywhere. Otherwise let . When last obeyed the rule, was of the old ranks of its children, by Lemma 1. The rank of has not changed since then, and by Lemma 2 does not increase when the arguments decrease, so . If , line L13 returns, and obeys the rule by Lemma 1. If , line L14 sets , so obeys the rule. Its parent now has a child of smaller rank, and every other node obeys the rule, so the claim holds for the next iteration with the parent of . Each iteration goes one level up, so the loop ends, at a root or at line L13.