A Four-Pass Sweep for Closest Pair of Points
Difference between en1 and en2, changed 17930 character(s)
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 < 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 < 145 = |pr|^2$. However, in this case $|qr|^2 = 50 < 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 < x_q$, or $x_r = x_q$ and $y_r < 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 > 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' > 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 < t' \le t$ implies $t'(t' - 2t) < 0$ and $s'(s' - 2s) \le 0$, so the sum is strictly negative. If $s' > s$, then $r \prec q$ forces $t' < 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) < 0$. In either case $|qr| < |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| < |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 > t^2$, and $|pr|^2 = 2t^2 + 2 > 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 < 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 > y_i$, or $y_j = y_i$ and $x_j > 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.
# A Four-Pass Sweep for Closest Pair of Points↵
↵
Can a minimum-coordinate query help us find the Euclidean closest pair? Put a $45^\circ$ sector at each point and select the point with the smallest $x$ in that sector. For an individual query point, this selection can miss its nearest neighbor. For a global closest pair, however, if the selection skips one endpoint, the two candidates are closer to each other than the supposed closest pair. That observation is enough to solve the problem with four Fenwick-tree sweeps.↵
↵
Given $n\ge 2$ planar lattice points, we want to find the minimum Euclidean distance $d^*$ between two distinct input entries. We can compare squared distances throughout. If two input points coincide, the answer is zero, which we can check by sorting. From now on, assume the points are pairwise distinct and let $P$ be their set.↵
↵
Consider $p=(0,0)$, $q=(10,1)$, and $r=(9,8)$. Both $q$ and $r$ lie in the sector between the positive $x$-axis and the ray $y=x$. The minimum-$x$ point is $r$, although $|pq|^2=101\lt 145=|pr|^2$. The shorter edge is $qr$, with $|qr|^2=50$. Thus the local choice skips $q$, and $pq$ was never a global minimum in the first place.↵
↵
![Three points in a 45-degree sector](https://i.ibb.co/tpJx0rrm/01-local-neighbor-counterexample.png)↵
↵
*Figure 1. The upper point $r$ has smaller $x$ than $q$. The thick segment $qr$ is the global closest pair.*↵
↵
For $p=(x_p,y_p)$, define its closed sector by↵
↵
$$S(p)=\{q\in P\setminus\{p\}\mid 0\le y_q-y_p\le x_q-x_p\}.$$↵
↵
Within $S(p)$, compare points lexicographically by $(x,y)$. Write $r\prec q$ when $x_r\lt x_q$, or when $x_r=x_q$ and $y_r\lt y_q$.↵
↵
![Membership in the closed 45-degree sector](https://i.ibb.co/xS9sh1FJ/06-closed-sector-membership.png)↵
↵
*Figure 2. The square marks the query point. The filled dots show that points on either boundary ray qualify.*↵
↵
Lemma 1. If $(p,q)$ is a global closest pair and $q\in S(p)$, then $q$ is the lexicographic minimum of $S(p)$.↵
↵
Translate $p$ to the origin and write $q=(t,s)$, with $0\le s\le t$ and $t\gt 0$. Suppose $r=(t',s')\in S(p)$ precedes $q$. Since $0\lt t'\le t$ and $0\le s'\le t'$, the point $r$ lies in the triangle with vertices $p$, $A=(t,0)$, and $B=(t,t)$.↵
↵
![The proof triangle inside a disk](https://i.ibb.co/p6SgDPf9/02-lemma-disk-and-triangle.png)↵
↵
*Figure 3. The dashed circle is centered at $q$ and passes through $p$. The thick segment joins $q$ to a point $r$ that precedes it.*↵
↵
The disk centered at $q$ with radius $|pq|$ contains all three vertices. Their squared distances from $q$ are $t^2+s^2$, $s^2$, and $(t-s)^2$, respectively. Strict convexity puts every point of the triangle other than its vertices strictly inside the disk. The vertex $A$ is also strictly inside. The vertex $p$ is excluded from the sector.↵
↵
![The tie-break when two points share the same x-coordinate](https://i.ibb.co/tw2Ftscv/07-equal-x-tie-break.png)↵
↵
*Figure 4. On the right edge of the triangle, the lower point comes first in lexicographic order.*↵
↵
The final vertex $B$ can lie on the circle only when $s=0$. In that case $q=(t,0)$ comes before $B=(t,t)$ by the tie-break on $y$, so $r$ cannot be $B$.↵
↵
![The disk boundary when s is zero](https://i.ibb.co/b5C4x2cS/08-disk-boundary-exception.png)↵
↵
*Figure 5. When $s=0$, the upper vertex and $p$ are both on the circle. The upper vertex comes after $q$.*↵
↵
Therefore $r$ lies strictly inside the disk and $|qr|\lt |pq|$, contradicting the global minimality of $(p,q)$. $\square$↵
↵
<spoiler summary="An algebraic proof">↵
↵
With the same notation,↵
↵
$$|qr|^2-|pq|^2=t'(t'-2t)+s'(s'-2s).$$↵
↵
If $s'\le s$, the first term is negative because $0\lt t'\le t$, and the second is nonpositive. If $s'\gt s$, the condition $r\prec q$ gives $t'\lt t$. Using $0\le s'\le t'$, the right-hand side is at most $t'(t'-2t)+(t')^2=2t'(t'-t)\lt 0$. Both cases give $|qr|\lt |pq|$.↵
↵
</spoiler>↵
↵
The opening angle $45^\circ$ is tight for this selection rule if we keep the positive $x$-axis as one boundary and widen the sector upward. Take $p=(0,0)$, $q=(t,0)$, and $r=(t-1,t+1)$ for an integer $t\ge 2$. Then↵
↵
$$|pq|^2=t^2,\qquad |qr|^2=t^2+2t+2,\qquad |pr|^2=2t^2+2.$$↵
↵
Thus $pq$ is the unique closest pair. The slope of $pr$ is $(t+1)/(t-1)$, which approaches $1$ from above. For any fixed opening $\theta\in(45^\circ,90^\circ)$, a large enough $t$ puts $r$ inside the widened sector, where its smaller $x$ makes it win the query at $p$.↵
↵
![A counterexample for a wider sector](https://i.ibb.co/4nc0HpCM/03-angle-tightness.png)↵
↵
*Figure 6. For $t=8$, the open point $r=(7,9)$ enters the hatched region when the opening reaches $55^\circ$. The horizontal segment $pq$ is still the unique closest pair.*↵
↵
To implement one pass, set $k=x-y$. Then $q\in S(p)$ is equivalent to $q\ne p$ together with↵
↵
$$y_q\ge y_p,\qquad k_q\ge k_p.$$↵
↵
The change of coordinates turns the sector into an upper-right quadrant in the $(k,y)$ plane.↵
↵
![A sector becomes a dominance quadrant](https://i.ibb.co/Mk4x993H/04-dominance-and-fenwick-sweep.png)↵
↵
*Figure 7. The left panel uses $(x,y)$, and the right panel uses $(k,y)$. The ring marks the candidate with the smallest original $x$.*↵
↵
Process the points in decreasing order of $y$, breaking ties by decreasing $x$. Query before inserting the current point. At $p$, all points strictly above it have already been inserted. Among points on the same horizontal line, exactly those to its right have been inserted. This is the correct tie order because when $y_q=y_p$, the condition $k_q\ge k_p$ reduces to $x_q\ge x_p$, and distinctness makes the inequality strict.↵
↵
![The sweep order on a horizontal tie](https://i.ibb.co/SXCj3QdM/09-sweep-order-and-horizontal-tie.png)↵
↵
*Figure 8. The ring marks the current query point. Filled points have already been inserted. The sweep visits the current horizontal line from right to left.*↵
↵
Compress the values $k_i=x_i-y_i$ into increasing ranks $\mathrm{rk}_i\in[1,m]$ and reverse them by setting $\mathrm{pos}_i=m+1-\mathrm{rk}_i$. The condition $k_q\ge k_p$ now becomes a Fenwick prefix query on $[1,\mathrm{pos}_p]$. Each tree entry stores the lexicographically smallest inserted point in its block. The query combines the block minima, and each insertion updates its blocks by taking a minimum. The coordinate used for comparison can stay fixed because subtracting $x_p$ from every candidate's $x$ preserves their order.↵
↵
![A Fenwick prefix split into three blocks](https://i.ibb.co/GQnB6DFX/10-fenwick-prefix-blocks.png)↵
↵
*Figure 9. A prefix of length seven is queried from right to left using blocks of lengths one, two, and four.*↵
↵
For each nonempty query, evaluate the squared distance to the returned point and update the answer. Run this same pass on the four coordinate systems below.↵
↵
| Coordinates used in the pass | Directions covered in the original plane |↵
| --- | --- |↵
| $(x,y)$ | $[0,\pi/4]$ |↵
| $(y,x)$ | $[\pi/4,\pi/2]$ |↵
| $(y,-x)$ | $[\pi/2,3\pi/4]$ |↵
| $(x,-y)$ | $[-\pi/4,0]$ |↵
↵
![The four sweeps cover a half-circle](https://i.ibb.co/Kj1zJ3k7/05-four-sweep-directions.png)↵
↵
*Figure 10. The hatched sectors cover one closed half-circle. The arrows show the two opposite orientations of the same segment.*↵
↵
The union is $[-\pi/4,3\pi/4]$. For any unordered pair, at least one of the two orientations of its segment lies in this half-circle. Each coordinate transformation preserves distances, so Lemma 1 applies in the pass that covers that orientation. In particular, every global closest pair is returned by at least one query. There are at most $4n$ candidate edges before deduplication. Sorting, compression, and the Fenwick operations take $O(n\log n)$ time in total and use $O(n)$ space.↵
↵
The standard divide-and-conquer solution splits the points by $x$ and checks a narrow strip around the dividing line at each merge. The standard plane sweep keeps an active window and uses the current best distance to restrict both that window and the range of $y$-coordinates it examines. Both take $O(n\log n)$ time. [This Codeforces post gives a short description of the standard sweep.](https://codeforces.me/blog/entry/58747)↵
↵
The four-pass sweep has the same asymptotic running time. Its candidate at each point is fixed by the sector and the coordinate order, independently of the best distance found so far. The interesting part is that a query can miss a point's nearest neighbor inside the sector, yet the candidate set still contains every global closest pair. The distance is computed only after the Fenwick query returns a candidate.↵
↵
The proof uses the fact that a shorter edge between two candidates would contradict global minimality. This matters in the bichromatic problem. Color $p$ red and $q,r$ blue in the opening example. The query at $p$ picks $r$, with squared distance $145$. The true bichromatic minimum is $pq$, with squared distance $101$. The shorter edge $qr$ joins points of the same color.↵
↵
![The bichromatic version of the opening example](https://i.ibb.co/6cj4xr64/11-bichromatic-counterexample.png)↵
↵
*Figure 11. The square and the filled dots represent the two colors. The thick edge is the true bichromatic minimum. The dotted edge joins points of the same color.*↵
↵
The sector-based candidate rule is related to $\Theta$-graphs and Yao graphs. A standard $\Theta$-graph selects a point by its projection onto the cone bisector. This sweep compares projections onto a boundary ray of the canonical sector. Yao graphs select a Euclidean nearest neighbor in each cone. A familiar competitive-programming example of this general approach is the Manhattan MST sweep, which uses $x+y$ as the selection key in a suitably oriented octant.↵
↵
**References**↵
↵
- A. C. Yao, “On constructing minimum spanning trees in $k$-dimensional spaces and related problems,” SIAM Journal on Computing, 11(4), 721–736, 1982.↵
↵
- K. L. Clarkson, “Approximation algorithms for shortest path motion planning,” STOC, 56–65, 1987.↵
↵
- J. M. Keil, “Approximating the complete Euclidean graph,” SWAT, 208–213, 1988.↵
↵
- P. Bose, A. van Renssen, and S. Verdonschot, “On the spanning ratio of $\Theta$-graphs,” WADS,
 182–194, 2013.↵

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)