Closest Pair of Points in the Plane Inge Li Gørtz Thank you to Kevin Wayne for inspiration to slides. The slides on the deterministic algorithm for finding the closest pair of points is a modification of slides made by Kevin.
Closest Pair of Points • Closest pair of points. Given n points in the plane, find a pair with smallest euclidean distance between them. Thank you to Kevin Wayne for inspiration to slides.
Closest Pair of Points • Closest pair of points. 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 tra ffi c control. • Special case of nearest neighbor, Euclidean MST, Voronoi diagrams. • Brute force. Compare all pairs => O(n 2 ) time. • 1-D version. Sort and scan => O(n log n) time. • Simplifying assumption. No two points coincide (for a simpler presentation). Thank you to Kevin Wayne for inspiration to slides.
Closest pair of points A divide-and-conquer algorithm Thank you to Kevin Wayne for inspiration to slides. The slides on the deterministic algorithm for finding the closest pair of points is a modification of the slides made by Kevin.
Closest pair: Divide-and-Conquer • Divide: • Conquer: • Combine: Thank you to Kevin Wayne for inspiration to slides.
Closest pair: Divide-and-Conquer • Divide: draw vertical line L so that roughly n/2 points on each side. • Conquer: • Combine: L Thank you to Kevin Wayne for inspiration to slides.
Closest pair: Divide-and-Conquer • Divide: draw vertical line L so that roughly n/2 points on each side. • Conquer: find closest pair in each side recursively. • Combine: L 21 12 Thank you to Kevin Wayne for inspiration to slides.
Closest pair: Divide-and-Conquer • Divide: draw vertical line L so that roughly n/2 points on each side. • Conquer: find closest pair in each side recursively. • Combine: • Find closest pair with one point in each side. seems like Θ (n 2 ) • Return best of 3 solutions L 8 21 12 Thank you to Kevin Wayne for inspiration to slides.
Find closest pair with point on each side • Find closest pair with one point in each side, assuming that distance < δ . L 21 δ = min(12, 21) 12 Thank you to Kevin Wayne for inspiration to slides.
Find closest pair with point on each side • Find closest pair with one point in each side, assuming that distance < δ . • Observation: only need to consider points within δ of line L. L 21 δ = min(12, 21) 12 δ Thank you to Kevin Wayne for inspiration to slides.
Find closest pair with point on each side • 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 21 4 δ = min(12, 21) 3 12 2 1 δ Thank you to Kevin Wayne for inspiration to slides.
Find closest pair with point on each side • 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 between these points within 7 positions in sorted list! L 7 6 5 21 4 δ = min(12, 21) 3 12 2 1 δ Thank you to Kevin Wayne for inspiration to slides.
Find closest pair with point on each side • Def. Let s i be the point in the 2 δ -strip, with the i th smallest y-coordinate. • Claim. If |i – j| ≥ 8, then the distance between j 39 s i and s j is at least δ . • Pf. 31 • s j at most δ apart from s i then the di ff erence in y-coordinate is at most δ . • No two points lie in same ½ δ -by- ½ δ box: 30 ½ δ 29 δ s✓ 1 ◆ 2 ◆ 2 r ✓ 1 1 ½ δ 28 2 δ 2 δ 2 δ ≈ 0 . 7 δ < δ + = i 27 26 • At most 7 points within distance δ (one in each 25 other box). ▪ δ δ Thank you to Kevin Wayne for inspiration to slides.
Closest Pair Algorithm Closest-Pair(p 1 , …, p n ) { If n < 4 compute closest pair by comparing all pairs. 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 7 neighbors. If any of these distances is less than δ , update δ . return δ . } Thank you to Kevin Wayne for inspiration to slides.
Closest Pair Algorithm • Analysis: • T(n) = 2T(n/2) + O(n log n), for n > 4. T(n) = O(1), for n ≤ 4. • T(n) = O(n log 2 n). • Can improve this by pre-sorting points: • Start by constructing 2 sorted lists X and Y containing all points sorted after x- and y-coordinate, resp. • Divide: • Split X-array in middle. • Use linear time to split Y-array into 2 (according to x-coordinate). • Combine: • Prune Y-array (only consider points with x-coordinate within δ of L).
Closest Pair Algorithm Presort points into lists X and Y after x- and y-coordinate, respectively. Closest-Pair(X[1…n],Y[1…n]) { If n < 4 compute closest pair by comparing all pairs. Compute separation line L such that half the points O(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) Sort remaining points by y-coordinate. O(n) Scan points in y-order and compare distance between each point and next 7 neighbors. If any of these distances is less than δ , update δ . return δ . }
Closest Pair Algorithm • Analysis: • Total time: T(n) + O(n log n). • T(n) = 2T(n/2) + O(n), n>4. • T(n) = O(1), n ≤ 4. • Thus T(n) = O(n log n). • In total O(n log n).
Closest pair of points A randomized algorithm
Randomized algorithm • Assume wlog that points are in the unit square. • Sort points in random order. • Let δ = d(p 1 ,p 2 ). Check for each point p i (in order) if there exists a point p j, j<i, such that d(p i ,p j ) < δ . • If such a point found. Update δ . 6 12 13 7 14 8 3 1 5 15 δ 1 2 4 11 9 δ 2 10
How to check a point • δ current smallest distance. Divide unit square into subsquares with side lengths δ /2. δ /2 6 12 13 7 14 8 δ /2 3 1 5 15 δ 2 4 11 10 9
How to check a point • δ current smallest distance. Divide unit square into subsquares with side lengths δ /2. • If two points i and j are in the same subsquare then d(i,j) < δ . δ /2 6 12 13 7 14 8 δ /2 3 1 5 15 2 4 11 10 9
How to check a point • δ current smallest distance. Divide unit square into subsquares with side lengths δ /2. • If two points i and j are in the same subsquare then d(i,j) < δ . • If d(i,j) < δ then j is in the 5x5 grid of subsquares around i . δ /2 6 12 13 7 14 8 δ /2 3 1 5 15 2 4 11 10 9
Closest Pair of Points: Randomized algorithm • Use hashtable to store which square a point is in. Only store points already looked at (red points). 6 12 13 7 14 8 3 1 5 15 δ 1 2 4 11 9 δ 2 10
Closest Pair of Points • Use hashtable to store which square a point is in. Only store points already looked at (red points). • When starting new round: rehash all points from 1…i. δ 3 6 12 13 7 14 8 3 1 5 15 2 4 11 9 δ 2 10
Closest Pair of Points • Use hashtable to store which square a point is in. Only store points already looked at (red points). • When starting new round: rehash all points from 1…i. δ 3 6 12 13 7 14 8 3 1 5 15 2 4 11 9 10
Closest Pair of Points: Analysis • Number of lookup operations: • Number of distance calculations: • Number of MakeDictionary operations:
Closest Pair of Points: Analysis • Number of lookup operations: • Number of distance calculations: • Number of MakeDictionary operations: • Number of insertions: • Random variable X = number of insertions • Random variable ( 1 i causes δ to change X i = 0 otherwise • Pr[X i = 1] ≤ 2/i • Expected number of insertions: n n n X X X E [ X ] = n + i · E [ X i ] = n + i · Pr [ X i ] ≤ n + i · 2 /i = n + 2 n = 3 n . i =1 i =1 i =1 • Use hashtable as dictionary: O(n) time in total.
Recommend
More recommend