DrawCode Algorithm Visualizer • Divide and Conquer • Hard

Closest Pair of Points

Tags: Divide and Conquer, Geometry, 2D, Recursion

1. One-Liner

Given 2D points, find the minimum distance between any two using divide-and-conquer on the x-sorted order plus a strip merge in y-order.


2. The Problem It Solves

Brute force is O(); the classic D&C algorithm achieves O(n log n) for Euclidean closest pair—foundational in computational geometry and collision detection prep.


3. The Core Idea

Split by a vertical line; closest pair is either both left, both right, or one on each side within distance δ of the line—only O(n) strip candidates need pairwise checks (constant neighbors in y-order).


4. How It Works (Step-by-Step)

StepWhat Happens
1. SortSort points by x (and maintain y-sorted lists or merge).
2. BaseIf n ≤ 3, brute-force min distance.
3. DivideSplit at median x; recurse for δ = min(δ_L, δ_R).
4. CombineBuild strip wherex − midx< δ; scan y-order, compare to next ≤7 points.

5. Dry Run Example

Four corners of a unit square: brute base finds 1 edge length; after split, strip checks cross-pairs near the middle vertical.


6. Key Properties

PropertyValue
TimeO(n log n) with careful merging
SpaceO(n) for auxiliary arrays
MetricEuclidean (typical); extensions vary

7. Where It Is Used

DomainUse
Graphics / GISNearest neighbor preprocessing
RoboticsCollision checks, clustering

8. Interview Tips

Explain why strip is narrow and 7-point constant in y-order; state handling duplicates and integer vs float precision.


9. Comparison with Other Algorithms

MethodTimeNotes
Brute forceO()Simple
D&C closest pairO(n log n)Standard plane algorithm
KD-treeBuild + queryDifferent structure

10. Complexity

MetricValue
TimeO(n log n)
SpaceO(n)

Implementation Example (PYTHON)

import math

def dist(p, q):
    return math.hypot(p[0] - q[0], p[1] - q[1])

def closest_pair(px):
    n = len(px)
    if n <= 3:
        return min((dist(px[i], px[j]) for i in range(n) for j in range(i + 1, n)), default=float('inf'))
    mid = n // 2
    xmid = px[mid][0]
    dl = closest_pair(px[:mid])
    dr = closest_pair(px[mid:])
    d = min(dl, dr)
    strip = [p for p in px if abs(p[0] - xmid) < 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

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer