Algorithm Search⌘K

Proofs: Replacement selection

These are the human proofs of Replacement selection, at full length.

Replacement selection makes sorted runs, each but the last with at least P items full

This proof shows: correctness.

We use the line numbers of the pseudocode, and we write c=∣R∣c = |R| for the number of the current run. When c≥1c \ge 1, let ww be the last item written by line L7. All items of FF are distinct as triples, because their positions ss are distinct.

Invariant. At each test of line L4:

  1. Each run of RR is sorted, and no run is empty.
  2. If c=0c = 0, each item in HH has run number 1. If c≥1c \ge 1, each item in HH has run number cc or c+1c + 1, and each item with run number cc has a key of at least ww.
  3. The items written to RR, the items in HH and the items not yet read are together the items of FF, each one time.

Initialization. Line L2 gives each item in HH the run number 1, and RR is empty. So the invariant is true.

Maintenance. Let (r,x,s)(r, x, s) be the minimum of HH at line L5.

Case c=0c = 0. Then r=1r = 1, line L6 appends run 1, and line L7 makes it (x)(x), which is sorted.

Case c≥1c \ge 1 and r=cr = c. By part 2, x≥wx \ge w, so run cc stays sorted when line L7 appends xx. Line L6 does nothing, because r=∣R∣r = |R|.

Case c≥1c \ge 1 and r=c+1r = c + 1. Because the heap orders by run number first, no item in HH has run number cc. Line L6 appends run c+1c + 1, and line L7 makes it (x)(x). The current run is now c+1c + 1.

In each case, the new current run is rr and the new last item is w=xw = x. We check part 2 for the new values. Each other item in HH has run number rr or r+1r + 1: in the first two cases because the numbers in HH were cc or c+1c+1 with r=cr = c, and in the third case because they were all c+1=rc + 1 = r. Each other item in HH with run number rr is at least xx, because xx is the minimum among the items with run number rr. If line L8 reads an item yy, line L9 gives it the number rr only when y≥xy \ge x, and else r+1r + 1. So part 2 stays true. Line L10 or line L11 removes xx from HH, and xx is now in RR, so part 3 stays true. Part 1 holds by the three cases.

Termination. Each iteration writes exactly one item to RR. By part 3, at most nn items can be written. So the loop stops after at most nn iterations. It stops only when HH is empty. Line L11 runs only when no item is left to read, so at that point all items are written, and the loop ran exactly nn times.

Theorem 1. REPLACEMENT-SELECTION returns sorted, non-empty runs whose items together are the items of FF, each one time.

Proof. At the end, HH is empty and no item is left to read. Parts 1 and 3 of the invariant give the claim. ■\blacksquare

Theorem 2. Each run except the last has at least PP items.

Proof. Take a run cc that is not the last run. Run c+1c + 1 starts at a step where all items in HH have run number c+1c + 1. First let c≥2c \ge 2. Run cc started at a step where all items in HH had run number cc, and all of them are written to run cc. If the input was not empty at that step, line L10 kept the heap at PP items since line L2, so the heap held PP items, and run cc has at least PP items. If the input was empty at that step, then no item is read after it, so no item gets the run number c+1c + 1, and run cc is the last run. This is false. Now let c=1c = 1. When run 2 starts, HH holds at least one item with run number 2. Such an item was read by line L8 at a step that wrote an item to run 1. If the heap held fewer than PP items after line L2, the input was empty, no item was read, and there is no run 2. So the heap held PP items, all of them with run number 1, and they are all written to run 1. ■\blacksquare

Theorem 3 (equal keys). Let aa and bb be items with equal keys, where aa comes before bb in FF. Then the run of aa is at most the run of bb, and if they are in the same run, aa comes first in it.

Proof. Write ρ(z)\rho(z) for the run number of item zz. An item that line L2 reads gets ρ=1\rho = 1, and an item that line L8 reads at a step with current run cc gets ρ=c\rho = c or ρ=c+1\rho = c + 1. The current run never decreases. If aa is read by line L2, then ρ(a)=1≤ρ(b)\rho(a) = 1 \le \rho(b). Else let cac_a and cbc_b be the current runs at the steps that read aa and bb, with ca≤cbc_a \le c_b. If ca<cbc_a < c_b, then ρ(b)≥cb≥ca+1≥ρ(a)\rho(b) \ge c_b \ge c_a + 1 \ge \rho(a). If ca=cb=cc_a = c_b = c and ρ(a)=c+1\rho(a) = c + 1, then the key of aa is less than the item waw_a just written at that step. The items written to run cc are written in sorted order by Theorem 1, so the item wbw_b written at the step that reads bb satisfies wb≥waw_b \ge w_a. The key of bb equals the key of aa, so it is less than wbw_b, and ρ(b)=c+1\rho(b) = c + 1. In all cases ρ(a)≤ρ(b)\rho(a) \le \rho(b). If ρ(a)=ρ(b)\rho(a) = \rho(b), the heap order by run number, then key, then position puts aa before bb, because aa has the smaller position, and both are in the heap or aa was already written when bb was read. ■\blacksquare

Back to Replacement selection