Proofs: Uniform grid bucketing
These are the human proofs of Uniform grid bucketing, at full length.
The uniform grid keeps each point in its cell and reports exactly the near points and the close pairs full
This proof shows: correctness.
We use the names of the pseudocode. The grid covers the box with the cells , and the invariant (G) says that each stored copy of a point is in the bucket of , exactly once, and in no other bucket.
Lemma 1 (the cells tile the box). For each , is the only pair with , and , . Proof. Let , so . Then is the only integer with , which is the condition , and . The same holds for .
Lemma 2 (index distance). For real numbers with , . Proof. Let and , so . Assume . Then , and . So .
INSERT and DELETE keep (G). INSERT appends one copy of to the bucket of (lines L9 and L10), which by Lemma 1 is a valid bucket, and changes no other bucket. DELETE looks in the bucket of (lines L11 and L12). By (G), each stored copy of is in that bucket, so if the bucket holds no copy, is not stored and the answer false is right. Otherwise line L13 removes one copy. In both operations the other copies stay where they were, so (G) holds after the operation.
RADIUS-QUERY. Let be a stored copy with . Then . The floor is monotone, so the first index of satisfies , and by Lemma 1 also . So , and in the same way . The loop of line L3 visits each cell in this range once, so by (G) the loop of line L4 meets exactly once, and line L5 appends it, since . Conversely, line L5 appends only points with , and by (G) each copy is in one bucket, so it is appended at most once. If no point qualifies, and the code returns the empty list. So the output is exactly the multiset of stored copies within distance .
CLOSE-PAIRS. Order the cells by in lexicographic order. The forward neighbors of in line L16 are , , and . Claim: for two distinct cells and whose indices differ by at most 1 in each coordinate, exactly one of them is a forward neighbor of the other. Let with . If , or and , then is a forward neighbor of , and is not of these forms. Otherwise is of one of these forms, and is a forward neighbor of . This covers the eight nonzero vectors.
Now let and be two stored copies with , in the cells and . Then and , so by Lemma 2 the indices of and differ by at most 1 in each coordinate. If , line L15 tests the pair once. If , by the claim exactly one of the cells has the other as a forward neighbor, so line L17 tests the pair once, and no other step tests it, since a test of line L15 or L17 involves only the two cells of the points. The test reports the pair because the distance is at most . Each reported pair passed the test, so it is at distance at most . So CLOSE-PAIRS reports each unordered pair of stored copies at distance at most exactly once, and nothing else.