Proofs: Brodal queue
These are the human proofs of Brodal queue, at full length.
The teaching version of the Brodal queue keeps the heap order and finds the minimum full
This proof shows: correctness, termination.
This proof is about the teaching version on the page, the skew binomial queue with a pointer to the minimum, which is the code. The full Brodal queue is proved correct in Brodal's paper. We use the invariants of the page:
- (O) For each node with a parent , .
- (K) Each child of a node has a rank smaller than ; a node of rank 0 has no children; and a node of rank has at least nodes in its subtree.
- (L) The roots are sorted by rank, and only the first two may have the same rank.
- (M) If the queue is not empty, is a root, and no root has a smaller key; else .
- (I) Each item in the queue is in exactly one node, is that node, and each node holds an item of the queue.
Lemma 1. If (O) and (M) hold, the item of has the minimum key.
Proof. By (O), keys do not decrease along each path down from a root, so each key is at least the key of its root, which is at least by (M).
Lemma 2. LINK of two roots and of the same rank that satisfy (O) and (K) returns a root of rank whose tree holds the nodes of both and satisfies (O) and (K).
Proof. After line L1, , so the new edge from to satisfies (O). The child has the rank , and the old children of have ranks below . The tree has at least nodes.
Lemma 3. Say that a node is pending if (O) holds on every edge except possibly the edge from the parent of to , and for every child of . If is pending and , then after the exchange of the items of and , the node is pending, or (O) holds everywhere if is a root.
Proof. Let before the exchange; after it, holds and holds . The edges from to its children hold, since each child has a key at least . The edge from to holds, since . The edges from to its other children hold, since these had keys at least by (O). The only edge that may fail is the one from the parent of to . The children of now hold , at , and keys at least , and held by (O) before the exchange, so is pending.
Insert
Let the roots before be . Line L3 makes a node of rank 0 with no children, which satisfies (K).
Case 1, line L4 is true. The first two roots have the same rank . Line L5 links them, which gives a root of rank with (O) and (K), by Lemma 2. If was or , then , so keeps (M) on the new root list. Line L6 makes a leaf child of : its rank 0 is below , and the size of the tree only grows, so (K) holds. If , line L7 exchanges the items; then holds a key smaller than its old key, which was at most every key of its tree, and the leaf , which has no children, holds the old key of , which is at least the new one. So (O) holds. By (L), the third root has a rank at least , and all later roots have distinct ranks larger than that of the third, so the new list and the rest is sorted, and only and the next root may share a rank: (L) holds.
Case 2. goes to the front. Its rank 0 is at most every rank, and the first two roots of had different ranks, or there were fewer than two, so the only pair of equal ranks is now at most and the next root: (L) holds.
In both cases, line L9 sets to the new front root if it has a smaller key, which keeps (M), since the new front root is the only root whose key is new. The item of is in exactly one node after the exchanges, and the exchange updates both handles, so (I) holds.
Remove-root, delete-min and delete
REMOVE-ROOT, for a root . Line L12 makes the other roots and the children of into roots; each is a tree with (O) and (K), and the children lose their only parent, so they are roots. Line L13 processes the trees one by one and keeps the map with at most one tree per rank: while a tree of the same rank is in , the two are linked by Lemma 2 into one tree of a rank one higher. Each link reduces the number of trees by one, so the inner loop ends, and at the end the trees of hold all nodes except , with distinct ranks. Line L14 lists them by rank, which gives (L), and picks a root of smallest key, which gives (M). (I) holds for all items but the item of , which leaves the queue.
DELETE-MIN removes , whose item has the minimum key by Lemma 1, and returns that item.
DELETE. Line L18 moves the item of to the root of its tree by exchanges with the parent. Give the symbolic key , smaller than every key. Then is pending, since its children have keys at least the old key of , which is at least the key of its parent, and Lemma 3 applies to each exchange. When the loop stops, is at the root of its tree and (O) holds everywhere with the symbolic key. The symbolic key appears only on the edges from that root, which REMOVE-ROOT then removes, so (O) holds for the real keys on all the other edges. The item of leaves the queue, and the rest of the argument is that of REMOVE-ROOT.
Decrease-key
Line L16 lowers the key of to . Then (O) can fail only on the edge from the parent of to : the children of had keys at least the old key, which is at least . So is pending: its parent had a key at most the old key of , which is at most the keys of its children. Each exchange of the loop is Lemma 3, so the only possible failure moves up one edge, and when the loop stops, either the edge above holds or is a root; in both cases (O) holds everywhere. The nodes and ranks do not change, so (K) and (L) hold. If is now a root, its key is the only root key that changed, and it went down, so line L17 restores (M); if is not a root, no root key changed. The exchanges keep the handles right, so (I) holds.
Meld
MELD joins the two root lists as in line L13. As in REMOVE-ROOT, the result holds all nodes of both queues, with distinct ranks, (O) and (K). The smaller of the two minima has the smallest key among all roots: each root of the result is a root of one of the queues or a link of roots, and a linked root carries the smaller key of the two. So (M) holds.
Termination
INSERT, FIND-MIN and the error checks do steps. Each loop of exchanges moves up one level per iteration and stops at a root, so it ends, after at most iterations by (K). Each iteration of the inner loop of line L13 removes one tree, so the loop ends. So every operation ends.
By (I) and Lemma 1, after each operation the queue holds exactly the items inserted and not yet removed, with their current keys, and FIND-MIN and DELETE-MIN return an item of minimum key.