Closest Pair
Closest Pair
Closest pair. Given n points in the plane, find a pair with smallest
Euclidean distance between them.
2
Closest Pair of Points: First Attempt
3
Closest Pair of Points: First Attempt
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
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.
return .
}
13
Closest Pair of Points: Analysis
Running time.
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.
14