UBC Divide and Conquer
UBC Divide and Conquer
Reading:
• Finding the closest pair of points 33.4 CLRS
Problem: Given a list (x1 , y1 ), (x2 , y2 ), . . . , (xn , yn ) of points in the plane, nd the pair of points that are
closest together.
Both such points must be in the gray region shown because if one of the points were not, it would be
greater than δ from the dotted dividing line and, hence, greater than δ from the other point.
It is possible for all of the points lie within this gray strip. But do we really need to
δ δ
compare every point in the left half to every point in the right half ? Absolutely not.
A simple bound is that given a point p on the left half, it is sucient to check every
point q in the right half that is vertically within δ of p.
δ
δl
δr Fortunately, not many such q lie in this restricted region be-
cause those points are at least δr ≥ δ apart. The open dots
2δ in the picture show an arrangement of six points that are δ
p q apart. We will now argue that this is the maximum number.
Consider a δ×δ square. We want to cram as many points into this square such that δ
the points are at least δ apart. We rst argue that we can put all of the points on
the boundary of the square: For every point, we replace it with closest point on the
δ
boundary. The drawing on the left partitions the interior by the edge to which each
point is closest.
1
2
We push points to the edge this way because the area forbidden by the new point is strictly contained in
the area forbidden by original point.
To see this, rst consider two circles of the same radius δ (forbidden regions).
Assume without loss of generality that the line through the circle centers is
horizontal. The part of the left circle that is right of the midpoint of the two
circle centers is contained in the right circle.
So we only need to compare each point on the left to at most 6 points on the right. If the rights points
in the gray band are sorted by y -coordinate, we can binary search for the topmost element on the right
to compare to a given left point. The remaining interesting points on the right must follow consecutively
because of the sorting.
Algorithm MinDist(P )
if |P | ≤ 3 then
try all pairs
else
find x median of the points
split P into L and R of about the same size using x
sort BR by y -coordinates
for p ∈ BL do
binary search for highest q ∈ BR in a δ × 2δ box of p
compare at most 6 points starting with q to p
The algorithm is dominated by sorting and recursing, so the run-time recurrence is T (n) = 2T (n/2) +
Θ (n log n). This is case 2 of the Master Theorem in the Goodrich and Tamassia textbook. Hence, the
run-time is Θ(n log2 n). We note that the algorithm in CLRS does some clever bookkeeping to cut this down
to Θ(n log n).