How the closest pair of points searches a strip

Short answer: The closest pair algorithm finds the minimum Euclidean distance in a planar set by divide and conquer. It sorts by x, splits the set in half, solves both halves, and then checks only the points that lie in a strip of width δ around the cut, where δ is the best distance found so far. Inside the strip, each point is compared with a constant number of candidates sorted by y. The time is O(n log n).


What the problem actually asks

Testing every pair is quadratic and correct. A cut at the median x coordinate reduces the problem to two halves plus a strip. Any pair closer than δ and split across the cut must have both points inside the strip. The geometry of the plane limits how many strip points can sit inside a δ by δ neighborhood: packing disks of radius δ/2 shows that only a handful of y-neighbors, classically seven, need to be examined.

The constant 7 is a proof constant, not a tuning knob. Checking a few more neighbors is still linear in the strip and still correct. Checking none of them misses a cross pair.


What the algorithm guarantees

The algorithm returns the minimum distance, and with a little extra bookkeeping the pair that attains it. Time is O(n log n) after an initial sort by x, provided the y-order of each subset is produced by a merge rather than by a fresh sort. Re-sorting by y at every node makes the merge cost n log² n. Extra memory is the recursion and the y-ordered buffers. Duplicate points have distance zero and are the answer as soon as they are seen.

The argument uses the Euclidean plane. Manhattan distance has a similar strip proof with a different neighbor count. The three-dimensional problem has a different, more expensive recurrence.


The working steps

  1. Sort the points by x. Recurse on the left half and the right half, and also return the points of each half sorted by y, merged as in mergesort.
  2. Let δ be the minimum of the two recursive distances. Let mid be the dividing x.
  3. Collect the points, in y order, whose x lies within δ of mid.
  4. For each point in that list, compute distances to the next several points in y order, classically up to 7, and keep the minimum if it is smaller than δ.
  5. Return the minimum of δ and the best strip distance, together with the y-ordered list for the caller.

The n log n bound needs the linear strip scan and the merged y-order. A strip scan that tests every pair inside the strip is quadratic when every point falls in the strip, which happens. The neighbor limit is the algorithm. A one-dimensional closest pair is a sort and a scan of adjacent points, and it does not need a strip.


Approaches that look simpler and fail

Approach Why it fails when every pair in the strip is tested
Compare all pairs within δ of the cut The strip can contain every point. The recurrence becomes quadratic. The constant-size y-neighborhood is what keeps the strip linear.
Sort by y from scratch at every merge The distances stay correct and the time becomes O(n log² n). Merge the already sorted y lists.
Use a strip of width δ/2 A pair at distance just under δ can sit almost δ/2 on each side only if the width is δ on each side of the cut, a full width of 2δ centered on the cut. A narrower strip drops that pair.
Ignore equal points Distance zero is the minimum. The algorithm should return it when two points coincide.

A planar hull is Andrew or Graham. The closest pair is the strip divide-and-conquer. Higher dimensions need a different algorithm. The one-dimensional case is adjacent points after a sort.


A working example

This Python fragment splits the set, solves the two halves, and checks only a strip of width δ on each side of the cut. The closest pair among (0, 0), (5, 1), (1, 1), (8, 8) is the pair at distance sqrt(2). It prints that distance rounded, 1.41.

import math
pts = [(0, 0), (5, 1), (1, 1), (8, 8)]

def dist(a, b):
    return math.hypot(a[0] - b[0], a[1] - b[1])

def closest(points):
    points = sorted(points)
    def rec(px):
        if len(px) <= 3:
            best = math.inf
            for i in range(len(px)):
                for j in range(i + 1, len(px)):
                    best = min(best, dist(px[i], px[j]))
            return best
        mid = len(px) // 2
        dl, dr = rec(px[:mid]), rec(px[mid:])
        d = min(dl, dr)
        mid_x = px[mid][0]
        strip = [p for p in px if abs(p[0] - mid_x) < d]
        strip.sort(key=lambda p: p[1])
        for i in range(len(strip)):
            for j in range(i + 1, min(i + 8, len(strip))):
                d = min(d, dist(strip[i], strip[j]))
        return d
    answer = rec(sorted(points))
    brute = min(dist(a, b) for i, a in enumerate(points) for b in points[i + 1:])
    assert abs(answer - brute) < 1e-9
    print(f"{answer:.2f}")

closest(pts)

Where it is used

The closest-pair divide and conquer finds the nearest two points in a GIS layer or a collision pre-pass. The strip search is the step that keeps the time at n log n. A k-d tree answers nearest-neighbor queries of a different shape.


When you can drop this algorithm

Drop the quadratic pair loop for any large planar set. Use the strip. Keep a library or a careful merge of the y-order, because a fresh sort at each level quietly loses a log.


Why this stays published

We publish this so a search for the closest pair algorithm returns the strip of width δ and the constant number of y-neighbors that keep the scan linear. The closest pair of planar points is found in O(n log n) time by divide and conquer plus a linear scan of a constant number of y-neighbors inside a strip of width δ.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *