0% found this document useful (0 votes)
12 views2 pages

UBC Divide and Conquer

Uploaded by

supriopaul461
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

UBC Divide and Conquer

Uploaded by

supriopaul461
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Closest Pair of Points

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.

Simple Solution: Test each of the


 
n
∈ Θ(n2 ) pairs of points to see if they are the closest pair.
2
Sophisticated Solution: We can do better with a divide-and-conquer algorithm that exploits the geometry
of distance. The idea is to split the point set into two halves with a vertical line, nd the closest
pair within each half, and then nd the closest pair between the two halves. The result of nding
the closest pair within each half speeds up nding the closest pair between the halves. The closest
pair is the minimum of the closest pairs within each half and the closest pair between the two
halves.

To split the point set in two, we nd the x-median of


δ δ the points and use that as a pivot. Finding the closest
pair of points in each half is subproblem that is solved
recursively. Let δl be the minimum distance in the
left half, and let δr be the minimum distance in the
right half. Then the minimum distance between every
δr pair of points is less than or equal to δ = min {δl , δr }.

δl If the minimum distance between all pairs of


points is equal to δ, we have found the pair and we
are done. Otherwise, the minimum distance must be
between a point on the left half and a point on the
right half.

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.

The region forbidden by the new point can represented


δ δ
as the union of two regions, both of which are forbid-
den by the old point. Hence, the region forbidden by
δ δ
the new point is contained in the old forbidden region.
Therefore, given a set ofk points at least δ apart that

are contained in aδ × δ square, we push each point to
boundary so that the k points are still at least δ apart.

Clearly we cannot place more than two points on a


δ δ square edge. The only feasible arrangement of two
points per edge is on the corners. If one point is on
δ δ the interior of a square edge, the remainder of that
edge is forbidden, and the best we can hope for the
remaining edges is one per edge. If one point is on a
corner, the interiors of two edges are forbidden, and
the best we can do with the remaining two edges is
three points.

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

δ = min {MinDist(L), MinDist(R)}


BL =points in L within δ of x median
BR =points in R within δ of x median

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).

You might also like