Algorithm Search⌘K

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 X=[x0,x0+nxh)×[y0,y0+nyh)X = [x_0, x_0 + n_x h) \times [y_0, y_0 + n_y h) with the cells ZijZ_{ij}, and the invariant (G) says that each stored copy of a point pp is in the bucket of cell⁡(p)\operatorname{cell}(p), exactly once, and in no other bucket.

Lemma 1 (the cells tile the box). For each p∈Xp \in X, cell⁡(p)=(i,j)\operatorname{cell}(p) = (i, j) is the only pair with p∈Zijp \in Z_{ij}, and 0≤i<nx0 \le i < n_x, 0≤j<ny0 \le j < n_y. Proof. Let t=(px−x0)/ht = (p_x - x_0)/h, so 0≤t<nx0 \le t < n_x. Then i=⌊t⌋i = \lfloor t \rfloor is the only integer with i≤t<i+1i \le t < i + 1, which is the condition x0+ih≤px<x0+(i+1)hx_0 + ih \le p_x < x_0 + (i+1)h, and 0≤i≤nx−10 \le i \le n_x - 1. The same holds for jj. □\square

Lemma 2 (index distance). For real numbers a,ba, b with ∣a−b∣≤h|a - b| \le h, ∣⌊(a−x0)/h⌋−⌊(b−x0)/h⌋∣≤1|\lfloor (a - x_0)/h \rfloor - \lfloor (b - x_0)/h \rfloor| \le 1. Proof. Let s=(a−x0)/hs = (a - x_0)/h and t=(b−x0)/ht = (b - x_0)/h, so ∣s−t∣≤1|s - t| \le 1. Assume s≤ts \le t. Then ⌊t⌋≤t≤s+1<⌊s⌋+2\lfloor t \rfloor \le t \le s + 1 < \lfloor s \rfloor + 2, and ⌊t⌋≥⌊s⌋\lfloor t \rfloor \ge \lfloor s \rfloor. So ⌊t⌋−⌊s⌋∈{0,1}\lfloor t \rfloor - \lfloor s \rfloor \in \{0, 1\}. □\square

INSERT and DELETE keep (G). INSERT appends one copy of pp to the bucket of cell⁡(p)\operatorname{cell}(p) (lines L9 and L10), which by Lemma 1 is a valid bucket, and changes no other bucket. DELETE looks in the bucket of cell⁡(p)\operatorname{cell}(p) (lines L11 and L12). By (G), each stored copy of pp is in that bucket, so if the bucket holds no copy, pp 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 pp be a stored copy with ∥p−q∥≤r\lVert p - q \rVert \le r. Then qx−r≤px≤qx+rq_x - r \le p_x \le q_x + r. The floor is monotone, so the first index ii of cell⁡(p)\operatorname{cell}(p) satisfies ⌊(qx−r−x0)/h⌋≤i≤⌊(qx+r−x0)/h⌋\lfloor (q_x - r - x_0)/h \rfloor \le i \le \lfloor (q_x + r - x_0)/h \rfloor, and by Lemma 1 also 0≤i≤nx−10 \le i \le n_x - 1. So i0≤i≤i1i_0 \le i \le i_1, and in the same way j0≤j≤j1j_0 \le j \le j_1. The loop of line L3 visits each cell in this range once, so by (G) the loop of line L4 meets pp exactly once, and line L5 appends it, since (px−qx)2+(py−qy)2=∥p−q∥2≤r2(p_x - q_x)^2 + (p_y - q_y)^2 = \lVert p - q \rVert^2 \le r^2. Conversely, line L5 appends only points with ∥p−q∥≤r\lVert p - q \rVert \le r, and by (G) each copy is in one bucket, so it is appended at most once. If r<0r < 0 no point qualifies, and the code returns the empty list. So the output is exactly the multiset of stored copies within distance rr.

CLOSE-PAIRS. Order the cells by (j,i)(j, i) in lexicographic order. The forward neighbors of (i,j)(i, j) in line L16 are (i+1,j)(i+1, j), (i+1,j+1)(i+1, j+1), (i,j+1)(i, j+1) and (i−1,j+1)(i-1, j+1). Claim: for two distinct cells aa and bb whose indices differ by at most 1 in each coordinate, exactly one of them is a forward neighbor of the other. Let b−a=(δi,δj)≠(0,0)b - a = (\delta_i, \delta_j) \ne (0, 0) with δi,δj∈{−1,0,1}\delta_i, \delta_j \in \{-1, 0, 1\}. If δj=1\delta_j = 1, or δj=0\delta_j = 0 and δi=1\delta_i = 1, then bb is a forward neighbor of aa, and a−ba - b is not of these forms. Otherwise −(δi,δj)-(\delta_i, \delta_j) is of one of these forms, and aa is a forward neighbor of bb. This covers the eight nonzero vectors.

Now let pp and p′p' be two stored copies with ∥p−p′∥≤h\lVert p - p' \rVert \le h, in the cells aa and bb. Then ∣px−px′∣≤h|p_x - p'_x| \le h and ∣py−py′∣≤h|p_y - p'_y| \le h, so by Lemma 2 the indices of aa and bb differ by at most 1 in each coordinate. If a=ba = b, line L15 tests the pair once. If a≠ba \ne b, 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 hh. Each reported pair passed the test, so it is at distance at most hh. So CLOSE-PAIRS reports each unordered pair of stored copies at distance at most hh exactly once, and nothing else. ■\blacksquare

Back to Uniform grid bucketing