Proofs: Grid file
These are the human proofs of Grid file, at full length.
The grid file keeps box-shaped bucket regions that partition the grid, and its operations are correct full
This proof shows: correctness, termination.
We use the names of the pseudocode. The scales and are sorted lists of distinct values, the column of is and the row of is , so column holds the with , with and . The invariant (G) asks: (1) the regions of the buckets partition the cells , and is the bucket whose region holds ; (2) each point of a bucket has its cell in the region of that bucket; (3) each bucket holds at most points. Points are distinct.
LOOKUP. Lines L1 and L2 compute and , and by part 1 line L3 returns the only bucket whose region holds the cell of . By part 2, if is stored, it is in that bucket.
Lemma 1 (split at a line). Let have region with . Replacing by with region and the points with , and with region and the points with , and setting to on its region, keeps parts 1 and 2. The same holds for rows. Proof. The two regions are boxes that partition the region of , so part 1 holds. A point of has , and if and only if , so each point goes to the half whose region holds its cell.
Lemma 2 (refine). Let have a single cell and at least two points with distinct values, and let be the median of their distinct values, for the sorted distinct values , . Inserting into , copying column of to a new column , and adding 1 to each and each keeps parts 1, 2 and 3, and then has columns and , each holding at least one point of . Proof. All points of are in column , so , and is a new value that goes to index of . The old column becomes the columns , for , and , for , and every later column moves up by one. A region that did not contain column moves with its columns or stays; a region that contained it now contains both new columns. The regions are still boxes and still partition the grid, and the copied directory column names the same buckets, so part 1 holds. Each point's new cell lies in the moved region of its bucket, so part 2 holds; no bucket changes its points, so part 3 is unchanged. Finally, is in column and is in column . The case of rows is the same.
INSERT. If is stored, line L4 finds it by LOOKUP. Otherwise line L5 adds it to the bucket of its cell, which keeps parts 1 and 2; part 3 can fail only at that bucket. Consider a pass of the loop with holding points, all other buckets within capacity. If the region of spans two or more columns or rows, lines L8 and L10 split it at a line inside its span by Lemma 1. Otherwise has one cell; its distinct points differ on some axis, and the code refines an axis on which they differ, so by Lemma 2 line L9 keeps (G) up to part 3 at , and line L10 then splits at the new line, by Lemma 1, into two nonempty halves. After line L10, at most one of the two halves can hold more than points, and becomes the larger one. For termination, look at the pair , with the number of points of and the number of cells of its region. In a pass of the first kind, if both halves get points, the larger has at most points; if all points go to one half, is the same and is smaller. In a pass of the second kind, both halves get points, so drops. Refining adds cells to the grid but the pair is taken for the new after line L10, so in every pass the pair drops in the lexicographic order, which has no infinite descending chain, since can drop at most times and between two drops only shrinks. Hence the loop stops, and then part 3 holds at every bucket.
DELETE. Line L12 finds the bucket of , and line L13 removes , which keeps (G). Line L14 merges into only when their regions are boxes with the same rows and adjacent columns, or the same columns and adjacent rows; their union is then a box, and the two regions are replaced by it, so part 1 holds after is set on the cells of . Each point keeps a cell in the region of its bucket, so part 2 holds, and part 3 holds by the size test. The scan of line L14 is over a finite list.
RANGE-SEARCH. Let be stored with . Then and , since and do not decrease, so the cell of is in the block of line L15, and line L16 reads the bucket , which holds by parts 1 and 2. Line L17 reports exactly the points of the read buckets that lie in the box, and each bucket is read once, so each point is reported once. The block is finite, so the search stops.