Algorithm Search⌘K

Proofs: Spatial hashing

These are the human proofs of Spatial hashing, at full length.

Spatial hashing keeps one entry per box and cell, and a query reports each box that meets it exactly once full

This proof shows: correctness, termination.

Write C(b)C(b) for the set of cells that the closed box bb meets, and cell⁡(p)\operatorname{cell}(p) for the cell of the point pp. For a closed box b=[x1,x2]×[y1,y2]b = [x_1, x_2] \times [y_1, y_2], a cell (i,j)(i, j) is in C(b)C(b) exactly when ⌊x1/h⌋≤i≤⌊x2/h⌋\lfloor x_1/h \rfloor \le i \le \lfloor x_2/h \rfloor and ⌊y1/h⌋≤j≤⌊y2/h⌋\lfloor y_1/h \rfloor \le j \le \lfloor y_2/h \rfloor; this is the set of cells of the points of bb, since the cell index is a monotone function of each coordinate and takes every integer value between its values at the two ends. In particular, if p∈bp \in b then cell⁡(p)∈C(b)\operatorname{cell}(p) \in C(b).

Invariant (S). For each stored box (id,b)(id, b) and each (i,j)∈C(b)(i, j) \in C(b), bucket H(i,j)H(i, j) holds the entry (i,j,id,b)(i, j, id, b) exactly once, and there are no other entries.

INSERT. Lines L1 and L2 add the entry (i,j,id,b)(i, j, id, b) to bucket H(i,j)H(i, j) for each (i,j)∈C(b)(i, j) \in C(b) and for no other cell. If (id,b)(id, b) was not stored before, this gives (S) for the new set. C(b)C(b) is finite, so INSERT stops.

DELETE. By (S), for each (i,j)∈C(b)(i, j) \in C(b) the bucket H(i,j)H(i, j) holds the entry (i,j,id,b)(i, j, id, b) once, and line L9 removes it; moving the last entry of the list into the free place does not change the multiset of the other entries. After the loop, no entry of (id,b)(id, b) is left and the others are kept, so (S) holds for the set without (id,b)(id, b). If the box was not stored, no entry matches, and nothing changes.

QUERY. Soundness. An id is reported in line L7 only for an entry (i,j,id,b)(i, j, id, b) that passed line L6, so bb meets qq, and by (S) (id,b)(id, b) is stored.

Each stored box that meets qq is reported. Let (id,b)(id, b) be stored with b∩q≠∅b \cap q \ne \emptyset. The intersection of two closed boxes that meet is the closed box [max⁡(qx1,bx1),min⁡(qx2,bx2)]×[max⁡(qy1,by1),min⁡(qy2,by2)][\max(q_{x1}, b_{x1}), \min(q_{x2}, b_{x2})] \times [\max(q_{y1}, b_{y1}), \min(q_{y2}, b_{y2})], so its lower left corner p=(max⁡(qx1,bx1),max⁡(qy1,by1))p = (\max(q_{x1}, b_{x1}), \max(q_{y1}, b_{y1})) lies in both bb and qq. Let (i,j)=cell⁡(p)(i, j) = \operatorname{cell}(p). Since p∈qp \in q, (i,j)∈C(q)(i, j) \in C(q), and line L4 visits it. Since p∈bp \in b, (i,j)∈C(b)(i, j) \in C(b), and by (S) bucket H(i,j)H(i, j) holds (i,j,id,b)(i, j, id, b). Line L5 meets this entry, line L6 lets it pass, and the test of line L7 holds, so idid is reported.

Each is reported once. Line L4 visits each cell of C(q)C(q) once. In a visited cell (i′,j′)(i', j'), by (S) bucket H(i′,j′)H(i', j') holds at most one entry (i′,j′,id,b)(i', j', id, b) for the box, so the box can be reported at most once per cell, and only when cell⁡(p)=(i′,j′)\operatorname{cell}(p) = (i', j'). The point pp has exactly one cell, so the box is reported in at most one cell.

Termination. C(q)C(q) is finite and each bucket is a finite list.

CLOSE PAIRS. For each cell, the code tests each pair of distinct entries of that cell, and reports the pair when the boxes meet and the lower left corner of their intersection lies in the cell. The argument above, with b1b_1 in place of qq and b2b_2 in place of bb, shows that each pair of stored boxes that meet has both entries in the cell of that corner, and that the corner has one cell, so each such pair is reported exactly once, and no other pair is reported. ■\blacksquare

Back to Spatial hashing