BITS Pilani
CS F364: Design and Analysis of Algorithms

Closest Pair of Points in 2D

Lecture 6 |2026-05-21

Divide and conquer algorithm

  • Divide and conquer algorithm

Closest Pair of Points

Closest pair of points: Algorithm

Divide. Draw vertical line L with \approx n/2 points on each side.

Divide with vertical line

Closest pair of points: Algorithm

Divide. Draw vertical line L with \approx n/2 points on each side.
Conquer. Find closest pair on each side, recursively.

Conquer recursively

Closest pair of points: Algorithm

Divide. Draw vertical line L with \approx n/2 points on each side.
Conquer. Find closest pair on each side, recursively.
Combine. Find closest pair with one point in each side.
Return best of 3 solutions.

Combine step

Closest pair of points: Algorithm

Find closest pair with one point in each side, assuming distance < \delta = \min\{d_1, d_2\}.
Return best of 3 solutions.

Finding crossing pair

Closest pair of points: Algorithm

Find closest pair with one point in each side, assuming distance < \delta = \min\{d_1, d_2\}.
Return best of 3 solutions.

Observation: Suffices to consider points within \delta of line L.

Points within \delta of L

Closest pair of points: Algorithm

Find closest pair with one point in each side, assuming distance < \delta = \min\{d_1, d_2\}.
Return best of 3 solutions.

Observation: Suffices to consider points within \delta of line L.
Almost the one dimensional problem: Sort points in 2\delta-strip by their y coordinate.
Only check points within 8 in sorted list!

Checking points in strip

Closest pair of points: Algorithm

Def. Let p_i have the i^{th} smallest y-coordinate among points in the 2\delta-width-strip.
Claim. If |i-j| > 8, then the distance between p_i and p_j is > \delta.
Proof. No two points lie in the same \delta/2-by-\delta/2 square!

\delta/2 box argument

Closest pair of points: Algorithm

Full algorithm

Closest Pair Analysis

closest pair of points: Analysis

Analysis-1: Let D(n) be the number of pairwise distance calculations in the Closest-Pair Algorithm when run on n \geq 1 points.

D(n) \leq \begin{cases} 0, & n=1\\ 2D(n/2) + 7n, & n > 1. \end{cases}

\implies D(n) = O(n\log{n}).

This is the number of distance computations.

closest pair of points: Analysis

Analysis-2: Let C(n) be the number of comparisons in the Closest-Pair Algorithm when run on n \geq 1 points.

C(n) \leq \begin{cases} 0, & n=1\\ 2C(n/2) + O(n\log{n}), & n > 1. \end{cases}

\implies C(n) = O(n\log^2{n}).

This is the number of comparisons.

Can we reduce the time complexity to O(n\log{n})?

  • Sort all points based on x-coordinate initially (one time only) – O(n\log{n})
  • Sort all points based on y-coordinate initially (one time only) – O(n\log{n})
  • Create the y-sorted list of points in the \delta-strip by merging two sorted lists – O(n)

Thus, total complexity can be reduced to O(n\log{n}).

References:

  • Michael T. Goodrich and Roberto Tamassia, “Algorithm Design Foundations, Analysis, and Internet Examples”, Wiley Student Edition.
  • Jon Kleinberg and Eva Tardos, “Algorithm Design”, Pearson Publishers.
  • Thomas H. Chorman, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein, “Introduction to algorithms”, MIT Press Publishers.
  • Sanjoy Dasgupta, Umesh Vazirani, Christos Papadimitriou, “Algorithms”, McGraw-Hill Education Publishers.