Proofs: Indexed priority queue
These are the human proofs of Indexed priority queue, at full length.
The indexed priority queue keeps its heap and its position map full
This proof shows: correctness, termination.
Write and for a position . Let be the set of items in the queue.
Invariants. Between operations:
(H) for each ;
(P) lists the items of , each once; for each ; and for each .
Both hold for the empty queue, where and is all .
Lemma 1 (EXCH). If for each before EXCH with , then the same holds after it, and the set of items in is unchanged.
Proof. Line L13 swaps two cells of , so the set is the same. The positions keep their item, and that item's entry in is unchanged, because the two entries written belong to the items now at and , which are distinct from the item at . Those two entries are set to and .
So (P) is kept by any sequence of EXCH calls. SWIM and SINK change only through EXCH, and they change no key.
Lemma 2 (SWIM). Suppose (P) holds, and (H) holds for each , and if then for each child of . Then SWIM stops after at most iterations, and after it (H) and (P) hold.
Lemma 3 (SINK). Suppose (P) holds, and (H) holds for each that is not a child of , and if then for each child of . Then SINK stops after at most iterations, and after it (H) and (P) hold.
Proofs. These are the sift-up and sift-down lemmas of a binary heap applied to the array : by Lemma 1, EXCH swaps and and keeps (P). The invariant of SWIM is that each edge with holds and that , if it exists, is at most each child key of ; a swap at a failing edge moves the possible failure from to . The invariant of SINK is that each edge except those from to its children holds, and that , if it exists, is at most each child key of ; a swap with the smaller child moves the possible failure to the edges out of . Each iteration moves one level, so the bounds on the iterations follow. The details are those of the proofs on the pages heap increase-key and heap extract-max, with the order reversed.
INSERT with . Line L2 appends at position and sets , so (P) holds for . The new cell is a leaf, so (H) holds except at the edge into it, and the new cell has no children; Lemma 2 applies.
DECREASE-KEY with and . Lowering keeps the edges from to its children. The edge into may fail, and is at most the old and so at most each child key. Lemma 2 applies. CHANGE-KEY with a larger key is the mirror case: the edge into still holds, and only the edges out of can fail. The precondition of Lemma 3 asks that is at most each child key of , which holds since is at most the old , which was at most each child key. Lemma 3 applies.
DELETE with . Line L10 swaps the cells and by EXCH and drops the last cell, which now holds ; line L12 sets . So (P) holds for . If , the dropped cell was a leaf, and (H) holds. Else the cell holds the old last item , and only the edges at can fail. If , the children of were at least the old key at , which was at least , so the edges out of hold, and Lemma 2 applies to SWIM; the item then moves up, and the later SINK finds a cell whose children are not smaller, by (H), and stops at once. Else the edge into holds, and is at most the old key at , which was at most each child key, so SWIM does nothing and Lemma 3 applies to SINK.
EXTRACT-MIN. If , (H) and transitivity along the path to the root give for each , so has the smallest key. Lines L5 and L6 are DELETE with , where the root has no parent, so only SINK is needed, by the same case analysis. Line L7 returns and its key.
Theorem. Each operation stops in time, keeps (H) and (P), and does what its name says on the set and the keys; EXTRACT-MIN returns an item of smallest key, and CONTAINS, which tests , is correct by (P).
Proof. This collects the cases above, with Lemmas 2 and 3 for the time.