CSCE 500 – Design & Analysis of Algorithms, Spring 2026
University of Louisiana at Lafayette
Author: Sabbir Rahman
Given n integer-valued points in a 2-D plane, find the pair of points with the minimum Manhattan distance:
| Parameter | Bound |
|---|---|
| n | 1 ≤ n ≤ 10⁶ |
| coordinates | −10⁹ ≤ x, y ≤ 10⁹ |
| time limit | 10 seconds |
Key Insight: Rotating the coordinate axes by 45° converts Manhattan distance into Chebyshev distance.
Let:
u = x + y
v = x - y
Then:
Manhattan(P, Q) = |xP - xQ| + |yP - yQ|
= max(|uP - uQ|, |vP - vQ|) (Chebyshev distance)
Algorithm:
- Transform all points:
(x, y) → (u, v) = (x+y, x-y)— O(n) - Sort by
u— O(n log n) - Sweep right: for each new point R, maintain a
SortedListof active points (those with|u - uR| < δ). Query the v-window[vR−δ, vR+δ]withbisect— O(log n) per point. - Update
δwhenever a closer pair is found.
Total: O(n log n)
Classic D&C closest-pair adapted for Manhattan distance:
- Sort by x — O(n log n)
- Divide at median x — T(n) = 2T(n/2) + O(n log n)
- Merge: collect strip of width
δ, sort by y, compare each point against O(1) neighbours.
| Algorithm | Time | Space |
|---|---|---|
| Sweep Line (Chebyshev) | O(n log n) | O(n) |
| Divide & Conquer | O(n log² n) | O(n) |
| Brute Force (reference) | O(n²) | O(1) |
Input:
n
x1 y1
x2 y2
...
xn yn
Output:
<minimum Manhattan distance>
<index_i> <index_j>
Points are 1-indexed. If multiple pairs achieve the minimum, any one is acceptable.
Example:
Input: Output:
7 2
0 0 3 5
1 2
5 5
-2 -3
4 6
7 8
9 2
Points 3 (5,5) and 5 (4,6): |5−4| + |5−6| = 1 + 1 = 2 ✓
pip install sortedcontainerspython closest_pair.py < input.txtpip install pytest
python -m pytest tests/ -v.
├── closest_pair.py # Main solution (primary + fallback algorithms)
├── tests/
│ └── test_closest_pair.py # Unit + stress + performance tests
├── requirements.txt
└── README.md
| n | Time (Python) |
|---|---|
| 10,000 | < 0.1 s |
| 100,000 | < 0.3 s |
| 500,000 | ≈ 2.4 s |
| 1,000,000 | ≈ 5 s (est.) |
All within the 10-second limit.
- Cormen, T. H., et al. Introduction to Algorithms, 4th ed. MIT Press, 2022.
- Preparata, F. P. & Shamos, M. I. Computational Geometry: An Introduction. Springer, 1985.
- SortedContainers documentation