Closest Pair of Points

Explore the divide-and-conquer algorithm for finding the closest pair of points in a plane

Try these examples:

Closest Pair of Points

Given nn points in a plane, find the two points with the smallest Euclidean distance between them.

Problem Parameters

Scatter 50 points across a 100 ×\times 100 space and compare two algorithms for finding the closest pair.

Brute Force vs Divide and Conquer

Operations as n Grows (log scale)

1,225282speedup 4.3×02004006008001,0000246number of points nlog₁₀(operations)Brute force n(n − 1)/2Divide & conquer n log₂ n

The vertical axis is logarithmic: each step up is ten times more work. Brute force, growing like n2n^2, keeps climbing; divide and conquer, nlog⁡nn \log n, grows barely faster than nn itself. The gap between the two dots is the logarithm of the speedup, and it keeps widening as nn grows.

AlgorithmTimeOperations (n = 50)
Brute forceO(n2)O(n^2)1,225
Divide & conquerO(nlog⁡n)O(n \log n)282
Speedup--4.3x

The brute force approach checks all (n2)\binom{n}{2} = 1,225 pairs. The divide-and-conquer algorithm achieves O(nlog⁡n)O(n \log n) -- the same complexity as sorting.

Divide and Conquer Algorithm

  1. Sort points by x-coordinate: O(nlog⁡n)O(n \log n)
  2. Divide: Split points into left and right halves at the median x
  3. Conquer: Recursively find the closest pair in each half
  4. Combine: Check for closer pairs spanning the dividing line -- only points within δ\delta of the line need inspection, and each point checks at most 7 neighbors

The Split, the Two Halves and the Strip

median xclosest pair050100020406080100xy

Here is the top level of the recursion on 50 scattered points. The dashed line splits them at the median xx. The closest pair on the left is 2.60 apart and on the right 2.81 (thin blue segments), so δ\delta = 2.60. Any closer pair must have one point on each side within δ\delta of the line, inside the shaded strip, which holds only 4 of the 50 points. Here the strip matters: the closest pair (red, 1.55 apart) straddles the line, and neither half would have found it.

StepWork
SortO(nlog⁡n)O(n \log n) = 282
Recursive calls2T(n/2)2T(n/2)
Strip mergeO(n)O(n) per level
Levelslog⁡2(n)\log_2(n) ≈\approx 5.60

Expected Distances

For 50 random points in a 100 ×\times 100 area, two different distances matter. A typical point's nearest neighbor is about 12ρ\frac{1}{2\sqrt{\rho}} away (where ρ\rho is the density), but the closest pair is the smallest of all (n2)\binom{n}{2} pair distances, and that shrinks much faster: each pair falls within distance dd with probability about πd2/A\pi d^2 / A, so the smallest of them averages 12A/(n2)\tfrac{1}{2}\sqrt{A / \binom{n}{2}} (ignoring edge effects).

MetricValue
Area10,000
Average density0.01 points per unit
Mean nearest-neighbor distance~7.07
Expected closest-pair distance~1.43
Average spacing~14.14

With only 50 points, brute force (1,225 comparisons) is manageable, and the speedup from divide and conquer is just 4.3x.

The Strip Insight

The key to the O(nlog⁡n)O(n \log n) bound is the merge step. When combining, only points within δ\delta of the dividing line are candidates, and each point checks at most 7 neighbors inside a δ×2δ\delta \times 2\delta rectangle. That keeps the merge at O(n)O(n) instead of O(n2)O(n^2).

Increase 50 and watch the brute-force dot in the chart above pull away from the divide-and-conquer dot.

Operations as n Grows (log scale)

1,225282speedup 4.3×02004006008001,0000246number of points nlog₁₀(operations)Brute force n(n − 1)/2Divide & conquer n log₂ n

Try it in Code

import math

# Closest pair: brute force vs divide-and-conquer complexity
n = 50
width = 100
height = 100

pairs = n * (n - 1) // 2
log_n = math.log2(n) if n > 1 else 0
brute_ops = pairs
dc_ops = round(n * log_n)
speedup = round(brute_ops / dc_ops, 1) if dc_ops > 0 else 1
area = width * height
density = n / area
expected_dist = round(1 / (2 * math.sqrt(density)), 2) if density > 0 else 0

print(f"Brute force O(n^2): {brute_ops} comparisons")
print(f"Divide & conquer O(n log n): {dc_ops} operations")
print(f"Speedup: {speedup}x")
print(f"Expected nearest distance: {expected_dist}")

What to explore next

You might also like