A Four-Pass Sweep for Closest Pair of Points

Revision en1, by no-girlfriend, 2026-11-25 16:29:40

Let $$$P = \lbrace p_1,\dots,p_n \rbrace \subset \mathbb{Z}^2$$$ be a set of $$$n \ge 2$$$ lattice points in the plane, with $$$p_i = (x_i, y_i)$$$ and Euclidean distance $$$|p_i p_j| = \sqrt{(x_i - x_j)^2 + (y_i - y_j)^2}$$$. We want to compute $$$d^* = \min_{i \lt j} |p_i p_j|$$$. If $$$P$$$ contains duplicate points, then $$$d^* = 0$$$, which can be checked in advance by sorting. Below we assume all points are pairwise distinct.

Maintaining the squared distance $$$(x - a)^2 + (y - b)^2$$$ to a queried point $$$p = (a, b)$$$ directly in a segment tree fails because shifting by $$$(a, b)$$$ adds $$$-2ax - 2by + a^2 + b^2$$$, which depends on each point's coordinates and can change which point attains the minimum. In contrast, the linear quantity $$$x - a$$$ is order-preserving under translation: the minimizer never changes, so no actual translation is needed. Note that even inside a $$$45^\circ$$$ sector, minimizing $$$x$$$ does not necessarily return the Euclidean nearest neighbor of a fixed point $$$p$$$ in that sector. For example, for $$$p = (0, 0)$$$, $$$q = (10, 1)$$$, and $$$r = (9, 8)$$$, both $$$q$$$ and $$$r$$$ lie in the $$$45^\circ$$$ sector of $$$p$$$, and minimizing $$$x$$$ selects $$$r$$$, even though $$$|pq|^2 = 101 \lt 145 = |pr|^2$$$. However, in this case $$$|qr|^2 = 50 \lt 101$$$, so $$$(p, q)$$$ is not a global closest pair. What we actually need is a weaker property: whenever $$$(p, q)$$$ is a global closest pair, $$$q$$$ must be the point with minimum $$$x$$$ in the sector of $$$p$$$.

For any point $$$p = (x_p, y_p)$$$, define its canonical $$$45^\circ$$$ closed sector as

$$$S(p) = \{\, q \in P \setminus \{p\} \mid 0 \le y_q - y_p \le x_q - x_p \,\},$$$

and define a strict total order $$$\prec$$$ on $$$S(p)$$$ by $$$r \prec q$$$ if and only if $$$x_r \lt x_q$$$, or $$$x_r = x_q$$$ and $$$y_r \lt y_q$$$.

Lemma 1. Let $$$(p, q)$$$ be a global closest pair in $$$P$$$ such that $$$q \in S(p)$$$. Then $$$q$$$ is the minimum element of $$$S(p)$$$ with respect to $$$\prec$$$.

Proof. Translate the origin to $$$p = (0, 0)$$$ and write $$$q = (t, s)$$$, where $$$0 \le s \le t$$$ and $$$t \gt 0$$$. Suppose for contradiction that there exists $$$r = (t', s') \in S(p)$$$ with $$$r \prec q$$$. Then $$$0 \le s' \le t'$$$ and $$$t' \gt 0$$$. Computing the difference of squared distances gives

$$$|qr|^2 - |pq|^2 = (t' - t)^2 + (s' - s)^2 - (t^2 + s^2) = t'(t' - 2t) + s'(s' - 2s).$$$

If $$$s' \le s$$$, then $$$0 \lt t' \le t$$$ implies $$$t'(t' - 2t) \lt 0$$$ and $$$s'(s' - 2s) \le 0$$$, so the sum is strictly negative. If $$$s' \gt s$$$, then $$$r \prec q$$$ forces $$$t' \lt t$$$; using $$$0 \le s' \le t'$$$ and $$$s \ge 0$$$, we obtain $$$|qr|^2 - |pq|^2 \le t'(t' - 2t) + (t')^2 = 2t'(t' - t) \lt 0$$$. In either case $$$|qr| \lt |pq|$$$, contradicting that $$$(p, q)$$$ is a closest pair. $$$\blacksquare$$$

Geometrically, $$$r \prec q$$$ means $$$r$$$ lies in the right triangle with vertices $$$p(0, 0)$$$, $$$(t, 0)$$$, and $$$(t, t)$$$. The closed disk centered at $$$q$$$ of radius $$$|pq|$$$ contains all three vertices, whose squared distances to $$$q$$$ are $$$t^2 + s^2$$$, $$$s^2$$$, and $$$(t - s)^2$$$, respectively. By strict convexity of the disk, every point of the triangle except $$$p$$$ and, when $$$s = 0$$$, the vertex $$$(t, t)$$$ lies strictly inside the disk. Since $$$q \prec (t, t)$$$ when $$$s = 0$$$, any point preceding $$$q$$$ under $$$\prec$$$ must lie strictly inside the disk. Because the contradiction relies on $$$|qr| \lt |pq|$$$, Lemma 1 applies only to the unconstrained global closest pair and does not directly extend to the bichromatic closest pair problem: in the earlier example with $$$p$$$ red and $$$q, r$$$ blue, the edge $$$qr$$$ of squared length $$$50$$$ is monochromatic, so the four-pass bichromatic sweep returns $$$145$$$ instead of the true answer $$$101$$$.

The opening angle $$$\pi/4$$$ is tight for Lemma 1. For any $$$\theta \in (\pi/4, \pi/2)$$$, consider the integer point family $$$p = (0, 0)$$$, $$$q = (t, 0)$$$, and $$$r = (t - 1, t + 1)$$$ with integer $$$t \ge 2$$$. Here $$$|pq|^2 = t^2$$$, $$$|qr|^2 = t^2 + 2t + 2 \gt t^2$$$, and $$$|pr|^2 = 2t^2 + 2 \gt t^2$$$, so $$$(p, q)$$$ is the unique closest pair. Meanwhile, the slope of ray $$$pr$$$ is $$$\frac{t+1}{t-1}$$$, which approaches $$$1 = \tan(\pi/4)$$$ from above as $$$t \to \infty$$$; taking $$$t$$$ large enough so that $$$\frac{t+1}{t-1} \le \tan\theta$$$ places $$$r$$$ inside the widened sector. Since $$$x_r = t - 1 \lt t = x_q$$$, querying the $$$\prec$$$-minimum returns $$$r$$$ and misses $$$q$$$. Moreover, the boundaries of the $$$\pi/4$$$ sector are $$$dy = 0$$$ and $$$dx - dy = 0$$$, requiring only integer arithmetic.

Membership $$$q \in S(p)$$$ is equivalent to the 2D dominance constraints $$$y_q \ge y_p$$$ and $$$x_q - y_q \ge x_p - y_p$$$, which together imply $$$x_q \ge x_p$$$. Sweeping all points in descending order of $$$y$$$, breaking ties by descending $$$x$$$, ensures that when $$$p_i$$$ is processed, the already inserted points are exactly those with $$$y_j \gt y_i$$$, or $$$y_j = y_i$$$ and $$$x_j \gt x_i$$$. Coordinate-compressing $$$k_i = x_i - y_i$$$ into ranks $$$\mathrm{rk}_i \in [1, m]$$$ and reversing indices via $$$\mathrm{pos}_i = m + 1 - \mathrm{rk}_i$$$ reduces the suffix query $$$\mathrm{rk}_j \ge \mathrm{rk}_i$$$ to a prefix minimum query on $$$[1, \mathrm{pos}_i]$$$ under $$$\prec$$$. Since only point updates and prefix queries are needed, a Fenwick tree maintains this in $$$O(\log n)$$$ time per point.

A single sweep covers directions in $$$[0, \pi/4]$$$. Running the same sweep under the four isometric transformations $$$(x, y)$$$, $$$(y, x)$$$, $$$(y, -x)$$$, and $$$(x, -y)$$$ covers the original direction intervals $$$[0, \pi/4]$$$, $$$[\pi/4, \pi/2]$$$, $$$[\pi/2, 3\pi/4]$$$, and $$$[-\pi/4, 0]$$$, respectively. Their union is the half-circle $$$[-\pi/4, 3\pi/4]$$$. For any pair $$$(p, q)$$$, the vectors $$$\overrightarrow{pq}$$$ and $$$\overrightarrow{qp}$$$ differ in angle by $$$\pi$$$, so at least one falls in this half-circle, and Lemma 1 guarantees that the closest pair is found. Across all four passes, the total time complexity is $$$O(n \log n)$$$ and the space complexity is $$$O(n)$$$.

Partitioning the plane into fixed angular sectors and selecting one candidate per sector places this algorithm in the same family as $$$\Theta$$$-graphs (Clarkson, 1987; Keil, 1988) and Yao graphs (Yao, 1982). The standard planar $$$\Theta_k$$$-graph divides the plane around each point into $$$k$$$ cones and selects the closest point under orthogonal projection onto each cone's angle bisector; for $$$k \ge 7$$$, the standard $$$\Theta_k$$$-graph is a constant-stretch geometric spanner supporting local greedy routing, with tight spanning ratio bounds studied by Bose et al. (2013). Our selection rule projects onto a cone boundary ray instead, forming a variant of generalized $$$\Theta$$$-graphs that keeps all comparisons purely integer on the original axes, whereas the spanning ratio and routing guarantees of standard $$$\Theta$$$-graphs belong to bisector projection and do not carry over directly. Other classic algorithms arise from different per-sector selection rules: in the $$$L_1$$$ metric, the standard eight-octant sweep minimizes $$$x + y$$$ in each $$$45^\circ$$$ sector to produce $$$O(n)$$$ candidate edges containing the Manhattan minimum spanning tree, which also yields the $$$L_\infty$$$ MST via $$$(x, y) \mapsto (x + y, x - y)$$$; in Euclidean spaces of dimension $$$d \ge 3$$$, where the Delaunay triangulation can have $$$\Omega(n^{\lceil d/2 \rceil})$$$ simplices, Yao (1982) partitioned $$$\mathbb{R}^d$$$ into narrow cones and selected the Euclidean nearest neighbor per cone to build a sparse candidate graph, breaking the $$$O(n^2)$$$ barrier for high-dimensional EMST.

References

  • A. C. Yao, On constructing minimum spanning trees in $$$k$$$-dimensional spaces and related problems, SIAM J. Comput., 11(4):721–736, 1982.

  • K. L. Clarkson, Approximation algorithms for shortest path motion planning, Proc. 19th STOC, pp. 56–65, 1987.

  • J. M. Keil, Approximating the complete Euclidean graph, Proc. 1st SWAT, LNCS 318, pp. 208–213, 1988.

  • P. Bose, A. van Renssen, and S. Verdonschot, On the spanning ratio of $$$\Theta$$$-graphs, Proc. 13th WADS, LNCS 8037, pp. 182–194, 2013.

Tags geometry, closest pair, sweep line

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English no-girlfriend 2026-11-26 04:02:49 50 Tiny change: '# A Four-Pass Sweep for Closest Pair of Points\n\nCan a mini' -> 'Can a mini'
en2 English no-girlfriend 2026-11-26 02:06:38 17930
en1 English no-girlfriend 2026-11-25 16:29:40 7775 Initial revision (published)