Loading...

PPT – Closest Points PowerPoint presentation | free to download - id: 23fbaa-ZDc1Z

The Adobe Flash plugin is needed to view this content

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.

Closest Pairformal definition

- Algorithms for determining the closest pair
- -brute force O(N2)
- -divide-and-conquer O(N log N)
- -plane-sweep O(N log N)

Brute Force Algorithm

(x1, y1)

p1

(x2, y2)

p2

Time Complexity O( N2 )

d(p1, p2)

(x2 - x1)2 (y2 - y1) 2

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 cant be a closest

pair with any point more than d units to the left

of the line

The closest point to the left of p can only be in

the red-shaded region

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

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)gtd from S
- find the closest point q to p in S
- if d(p,q) lt d then update the current closest

pair and distance - insert p into S

Searching the Dictionary

- 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

points are in the half circle of radius d

centered at p.

d

p

Searching the Dictionary II

d

- But how to search in a half-circle?
- a rectangle is almost a half-circle
- 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 isnt that still a potentially large number

of points? - actually, there are at most 6
- key observation all of the points in the

dictionary are at least distance d from each

other

p

2d

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

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) lt d then update closest pair
- d ? d(p,q)
- S.insert(point(lastInStrip)) // insert new

point into dictionary

An Example

An Example Continued

Still Going...

Example Completed

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)