no-girlfriend's blog

By no-girlfriend, history, 3 days ago, In English

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

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

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

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

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

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$$$

An algebraic proof

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

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

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

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

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

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.

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

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.

  • Vote: I like it
  • +56
  • Vote: I do not like it

»
3 days ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

what happened to you when you chose your handle? no-girlfriend

  • »
    »
    3 days ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    Just being completely honest, no regrets.

»
3 days ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

This problem can easily be solved using Divide and Conquer.

»
3 days ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

I have no-girlfriend too

»
3 days ago, hide # |
 
Vote: I like it +14 Vote: I do not like it

I think this blog post could be better with some pictures. And maybe a small section about the other algorithms for this problems, and what makes this algorithm nice (doesn't need to be a super practical reason, just because it contains a nice idea is good enough). Currently it feels a bit dry, and doesn't contain much of a reason to read it.

»
3 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by no-girlfriend (previous revision, new revision, compare).

»
3 days ago, hide # |
 
Vote: I like it +20 Vote: I do not like it

can i be your girlfriend

»
3 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by no-girlfriend (previous revision, new revision, compare).

»
2 days ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

I have no-girlfriend too

»
2 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Bro changed his whole lifestyle to match a geometry problem. W blog though

»
2 days ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

Curious about your handle. Is it a manifestation or just your status when you made it?

»
44 hours ago, hide # |
 
Vote: I like it +15 Vote: I do not like it

The comments:

1% about the blog
99% about the user's handle

»
41 hour(s) ago, hide # |
 
Vote: I like it -17 Vote: I do not like it

»
28 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

really cool

»
24 hours ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

If you say you have no-girlfriend , does that mean you have a boyfriend ?

»
17 hours ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

First off, nice solution. Thanks for sharing it.

Now, I don't like to post negative comments on blogs that took some work to put out but I just find this one to be too confusingly written and most of the pictures in it to be distracting.

It also just keeps on going for some reason. It very well could end after stating lemma 1. A proof for it is barely needed because it's pretty intuitive why it's true (and the argument is overcomplicated imo).

The rest of the blog gets into the weeds of maintaining the data structure without first giving any overview of the approach. Any cyan+ coder will find the details redundant if u just gave an overview of the idea. And as much as it gets into the weeds, it does not provide example code...