0% found this document useful (0 votes)
13 views14 pages

Closest Pair

Uploaded by

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

Closest Pair

Uploaded by

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

Closest Points

• A famous algorithmic problem...


Given a set of points in the plane (cities in the U.S., transistors
on a circuit board, computers on a network, etc.) find which
pair of points is the closest together.

• Today we will study algorithms and data structures for


solving this problem.
1
Given a set P of N points, find
Closest Pair p,q ∈ Psuch that the dista
formal definition
d(p,q) is minimum.

q
p

• Algorithms for determining the closest pair:


-brute force O(N2)
-divide-and-conquer O(N log N)
-plane-sweep O(N log N)
2
Brute Force Algorithm
Compute all the distances
d(p,q) and select the minimum
distance.
(x1, y1)
p1 (x2, y2)
p2

d(p1, p2) = (x2 - x1)2 + (y2 - y1) 2

Time Complexity: O( N ) 2 3
Plane-Sweep Algorithm
• Maybe we can avoid checking the distance between every pair...
• Plane-sweep worked for segment intersection...
• Key observation: if the closest pair of points to the left of the sweep
line is distance d apart, the next point encountered can’t be a closest
pair with any point more than d units to the left of the line

d
p

The closest point to the left of p can only be in the red-shaded region
4
Stored Information
• Maintain the following information:
– closest pair (a,b) found so far, and distance d between them.
– ordered dictionary S of the points lying in a strip of width d
to the left of the sweep line, using the y-coordinates as keys

b
d
a p

d
dictionary S

5
Updating
• When the sweep line encounters a point p:
– update dictionary so it only contains points that might be a closest pair with p
– remove all points r such that x(p)-x(r)>d from S
– find the closest point q to p in S
– if d(p,q) < d then update the current closest pair and distance
– insert p into S

d
q

6
Searching the Dictionary d

• How to quickly find the closest point in the


dictionary? (Note: there could be O(N) points
in the dictionary.)
• Have x, y spacing so that y = d/(n-1)
• Good news: not all of the points in the
dictionary can improve d. The only eligible
d
points are in the half circle of radius d centered
p
at p.

7
Searching the Dictionary II
d
• But how to search in a half-circle? p
– a rectangle is almost a half-circle…
2d
– do a range search in the interval [y(p)-d,y(p)+d]
– this will get all the points in the half-circle (and
maybe some others)
• Use brute-force to check the distance to each
point returned by the range query
• But isn’t that still a potentially large number of
points?
– actually, there are at most 6 2d p
– key observation: all of the points in the dictionary are
at least distance d from each other

8
d
Putting It All Together
sort points by x-coordinate and store in ordered sequence X
maintain references to two positions in sequence
-firstInStrip: the leftmost point in S
-lastInStrip: the new point to be added to S
at each step..
lastInStrip  X.after(lastInStrip) // advance lastInStrip
// remove points that are no longer candidates from dictionary
while x(point(firstInStrip)) < x(point(lastInStrip))-d do
S.remove(point(firstInStrip))
firstInStrip  X.after(firstInStrip)
// update closest point information
find point q closest to point(lastInStrip) in S
if d(p,q) < d then update closest pair
d  d(p,q)
9
S.insert(point(lastInStrip)) // insert new point into dictionary
An Example

initial closest pair and


dictionary
d

one point in rectangle but


not half-circle; closest
pair not updated
10
An Example Continued

one point in rectangle but


not half-circle; closest
pair not updated

one point in rectangle but


not half-circle; closest 11
pair not updated
Still Going...

two points in rectangle, one


on border of half-circle;
closest pair not updated

two points in rectangle


and half-circle; closest
pair updated to nearer of 12
the two
Example Completed
d

nothing within
rectangle; closest
pair not updated
d

the final result, with clos- 13


est pair shown
Running Time
• initial sort takes O(N log N) time
• each point is inserted and removed once from S
– S has at most N elements, so each insertion/removal takes O(log N) time
– total insertion/removal time is O(N log N)
• dictionary is searched once each time a point is inserted into S
– each range query takes O(log N + 6) = O(log N) time
– total time for range queries is O(N log N)
• distance computations performed each time a point is inserted into S
– at most 6 computations at each time
– total time for distance computations is O(N)

Time Complexity: O(N log N)


(definitely beats the brute force method!)
14

You might also like