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

Closest Pair

The document discusses the algorithm for finding the closest pair of points in a plane, utilizing a divide-and-conquer approach. It outlines the steps of dividing the points, recursively finding the closest pairs, and combining the results while ensuring efficiency. The algorithm achieves a time complexity of O(n log n) by optimizing the sorting of points in the strip during the merging process.

Uploaded by

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

Closest Pair

The document discusses the algorithm for finding the closest pair of points in a plane, utilizing a divide-and-conquer approach. It outlines the steps of dividing the points, recursively finding the closest pairs, and combining the results while ensuring efficiency. The algorithm achieves a time complexity of O(n log n) by optimizing the sorting of points in the strip during the merging process.

Uploaded by

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

Closest Pair of Points

The closest pair


Closest Pair of Points

Closest pair. Given n points in the plane, find a pair with smallest
Euclidean distance between them.

Fundamental geometric primitive.



Graphics, computer vision, geographic information systems,
molecular modeling, air traffic control.

Special case of nearest neighbor, Euclidean MST, Voronoi.
fast closest pair inspired fast algorithms for these problems

Brute force. Check all pairs of points p and q with (n2)


comparisons.

1-D version. O(n log n) easy if points are on a line.

Assumption. No two points have same x coordinate.


to make presentation cleaner

2
Closest Pair of Points: First Attempt

Divide. Sub-divide region into 4 quadrants.

3
Closest Pair of Points: First Attempt

Divide. Sub-divide region into 4 quadrants.


Obstacle. Impossible to ensure n/4 points in each piece.

4
Closest Pair of Points

Algorithm.

Divide: draw vertical line L so that roughly ½n points on each
side.

5
Closest Pair of Points

Algorithm.

Divide: draw vertical line L so that roughly ½n points on each
side.

Conquer: find closest pair in each side recursively.

21

12

6
Closest Pair of Points

Algorithm.

Divide: draw vertical line L so that roughly ½n points on each
side.

Conquer: find closest pair in each side recursively. seems like (n )
2


Combine: find closest pair with one point in each side.

Return best of 3 solutions.

8
21

12

7
Closest Pair of Points

Find closest pair with one point in each side, assuming that
distance < .

21

 = min(12,
12 21)

8
Closest Pair of Points

Find closest pair with one point in each side, assuming that
distance < .

Observation: only need to consider points within  of line L.

21

 = min(12,
12 21)

 9
Closest Pair of Points

Find closest pair with one point in each side, assuming that
distance < .

Observation: only need to consider points within  of line L.

Sort points in 2-strip by their y coordinate.

L
7

6
5
4 21

 = min(12,
12 3
21)

 10
Closest Pair of Points

Find closest pair with one point in each side, assuming that
distance < .

Observation: only need to consider points within  of line L.

Sort points in 2-strip by their y coordinate.

Only check distances of those within 11 positions in sorted list!

L
7

6
5
4 21

 = min(12,
12 3
21)

 11
Closest Pair of Points

Def. Let si be the point in the 2-strip, with


the ith smallest y-coordinate.

Claim. If |i – j|  12, then the distance between 39 j


si and sj is at least .
31
Pf.

No two points lie in same ½-by-½ box.

Two points at least 2 rows apart ½
have distance  2(½). ▪ 2 rows 
30
29 ½

i 27
28 ½
Fact. Still true if we replace 12 with 7.

26
25

  12
Closest Pair Algorithm

Closest-Pair(p1, …, pn) {
Compute separation line L such that half the points O(n log n)
are on one side and half on the other side.

1 = Closest-Pair(left half) 2T(n / 2)


2 = Closest-Pair(right half)
 = min(1, 2)
O(n)
Delete all points further than  from separation line L
O(n log n)
Sort remaining points by y-coordinate.

Scan points in y-order and compare distance between O(n)


each point and next 11 neighbors. If any of these
distances is less than , update .

return .
}

13
Closest Pair of Points: Analysis

Running time.

T(n)  2T n/2  O(n logn)  T(n)  O(n log2 n)

Q. Can we achieve O(n log n)?


A. Yes. Don't sort points in strip from scratch each time.

Each recursive returns two lists: all points sorted by y
coordinate, and all points sorted by x coordinate.

Sort by merging two pre-sorted lists.

T(n)  2T n/2  O(n)  T(n)  O(n logn)

14

You might also like