{"id":274,"date":"2026-11-13T12:36:37","date_gmt":"2026-11-13T04:36:37","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-closest-pair-of-points-searches-a-strip\/"},"modified":"2026-11-13T21:54:12","modified_gmt":"2026-11-13T13:54:12","slug":"how-the-closest-pair-of-points-searches-a-strip","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-the-closest-pair-of-points-searches-a-strip\/","title":{"rendered":"How the closest pair of points searches a strip"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> The <strong>closest pair<\/strong> 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 \u03b4 around the cut, where \u03b4 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 <strong>O(n log n)<\/strong>.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 \u03b4 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 \u03b4 by \u03b4 neighborhood: packing disks of radius \u03b4\/2 shows that only a handful of y-neighbors, classically seven, need to be examined.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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\u00b2 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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>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.<\/li>\n<li>Let \u03b4 be the minimum of the two recursive distances. Let mid be the dividing x.<\/li>\n<li>Collect the points, in y order, whose x lies within \u03b4 of mid.<\/li>\n<li>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 \u03b4.<\/li>\n<li>Return the minimum of \u03b4 and the best strip distance, together with the y-ordered list for the caller.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when every pair in the strip is tested<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Compare all pairs within \u03b4 of the cut<\/td>\n<td>The strip can contain every point. The recurrence becomes quadratic. The constant-size y-neighborhood is what keeps the strip linear.<\/td>\n<\/tr>\n<tr>\n<td>Sort by y from scratch at every merge<\/td>\n<td>The distances stay correct and the time becomes O(n log\u00b2 n). Merge the already sorted y lists.<\/td>\n<\/tr>\n<tr>\n<td>Use a strip of width \u03b4\/2<\/td>\n<td>A pair at distance just under \u03b4 can sit almost \u03b4\/2 on each side only if the width is \u03b4 on each side of the cut, a full width of 2\u03b4 centered on the cut. A narrower strip drops that pair.<\/td>\n<\/tr>\n<tr>\n<td>Ignore equal points<\/td>\n<td>Distance zero is the minimum. The algorithm should return it when two points coincide.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment splits the set, solves the two halves, and checks only a strip of width \u03b4 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, <code>1.41<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import math\npts = [(0, 0), (5, 1), (1, 1), (8, 8)]\n\ndef dist(a, b):\n    return math.hypot(a[0] - b[0], a[1] - b[1])\n\ndef closest(points):\n    points = sorted(points)\n    def rec(px):\n        if len(px) &lt;= 3:\n            best = math.inf\n            for i in range(len(px)):\n                for j in range(i + 1, len(px)):\n                    best = min(best, dist(px[i], px[j]))\n            return best\n        mid = len(px) \/\/ 2\n        dl, dr = rec(px[:mid]), rec(px[mid:])\n        d = min(dl, dr)\n        mid_x = px[mid][0]\n        strip = [p for p in px if abs(p[0] - mid_x) &lt; d]\n        strip.sort(key=lambda p: p[1])\n        for i in range(len(strip)):\n            for j in range(i + 1, min(i + 8, len(strip))):\n                d = min(d, dist(strip[i], strip[j]))\n        return d\n    answer = rec(sorted(points))\n    brute = min(dist(a, b) for i, a in enumerate(points) for b in points[i + 1:])\n    assert abs(answer - brute) &lt; 1e-9\n    print(f&quot;{answer:.2f}&quot;)\n\nclosest(pts)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for the closest pair algorithm returns the strip of width \u03b4 and the constant number of y-neighbors that keep the scan linear. <strong>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 \u03b4.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u03b4 around the cut, where&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,40],"tags":[],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-geometry"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=274"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":673,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/274\/revisions\/673"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}