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 points in a plane, find the two points with the smallest Euclidean distance between them.
Problem Parameters
Scatter
Brute Force vs Divide and Conquer
Operations as n Grows (log scale)
The vertical axis is logarithmic: each step up is ten times more work. Brute force, growing like , keeps climbing; divide and conquer, , grows barely faster than itself. The gap between the two dots is the logarithm of the speedup, and it keeps widening as grows.
| Algorithm | Time | Operations (n = |
|---|---|---|
| Brute force | ||
| Divide & conquer | ||
| Speedup | -- |
The brute force approach checks all =
Divide and Conquer Algorithm
- Sort points by x-coordinate:
- Divide: Split points into left and right halves at the median x
- Conquer: Recursively find the closest pair in each half
- Combine: Check for closer pairs spanning the dividing line -- only points within of the line need inspection, and each point checks at most 7 neighbors
The Split, the Two Halves and the Strip
Here is the top level of the recursion on
| Step | Work |
|---|---|
| Sort | = |
| Recursive calls | |
| Strip merge | per level |
| Levels | |
Expected Distances
For
| Metric | Value |
|---|---|
| Area | |
| Average density | |
| Mean nearest-neighbor distance | ~ |
| Expected closest-pair distance | ~ |
| Average spacing | ~ |
With only
The Strip Insight
The key to the bound is the merge step. When combining, only points within of the dividing line are candidates, and each point checks at most 7 neighbors inside a rectangle. That keeps the merge at instead of .
Increase
Operations as n Grows (log scale)
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
Angle Relationships Explorer
Explore complementary, supplementary, vertical, and corresponding angles
Arc Length & Sector Area Explorer
Calculate arc length and sector area with real-world examples like pizza slices and clock hands
Area of Polygons Explorer
See how all polygon areas relate to rectangles
Circle Properties Explorer
Discover how radius, diameter, circumference, and area relate to each other
You might also like
Delaunay Triangulation
ExplorationExplore Delaunay triangulation properties, the empty circumcircle condition, and its dual relationship with Voronoi diagrams
Distance & Midpoint Calculator
Calculate the distance between two points and find the midpoint, fundamental tools for coordinate geometry and navigation
Distance & Midpoint Explorer
ExplorationCalculate distance and midpoint between two points using coordinate geometry
Line Intersection Detector
ExplorationExplore how two line segments intersect using parametric equations and cross products from computational geometry