closest pair of points in the plane
play

Closest Pair of Points in the Plane Inge Li Grtz Thank you to Kevin - PowerPoint PPT Presentation

Closest Pair of Points Closest pair of points. Given n points in the plane, find a pair with smallest euclidean distance between them. Closest Pair of Points in the Plane Inge Li Grtz Thank you to Kevin Wayne for inspiration to slides. The


  1. Closest Pair of Points • Closest pair of points. Given n points in the plane, find a pair with smallest euclidean distance between them. 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 Thank you to Kevin Wayne for inspiration to slides. Kevin. 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. Closest pair of points • Special case of nearest neighbor, Euclidean MST, Voronoi diagrams. • Brute force. Compare all pairs => O(n 2 ) time. A divide-and-conquer algorithm • 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. The slides on the deterministic algorithm for finding the closest pair of points is a modification of the slides Thank you to Kevin Wayne for inspiration to slides. made by Kevin.

  2. Closest pair: Divide-and-Conquer Closest pair: Divide-and-Conquer • Divide: • Divide: draw vertical line L so that roughly n/2 points on each side. • Conquer: • Conquer: • Combine: • Combine: L Thank you to Kevin Wayne for inspiration to slides. Thank you to Kevin Wayne for inspiration to slides. Closest pair: Divide-and-Conquer Closest pair: Divide-and-Conquer • Divide: draw vertical line L so that roughly n/2 points on each side. • Divide: draw vertical line L so that roughly n/2 points on each side. • Conquer: find closest pair in each side recursively. • Conquer: find closest pair in each side recursively. • Combine: • Combine: • Find closest pair with one point in each side. seems like Θ (n 2 ) • Return best of 3 solutions L L 8 21 21 12 12 Thank you to Kevin Wayne for inspiration to slides. Thank you to Kevin Wayne for inspiration to slides.

  3. Find closest pair with point on each side Find closest pair with point on each side • Find closest pair with one point in each side, assuming that distance < δ . • Find closest pair with one point in each side, assuming that distance < δ . • Observation: only need to consider points within δ of line L. L L 21 21 δ = min(12, 21) δ = min(12, 21) 12 12 δ Thank you to Kevin Wayne for inspiration to slides. Thank you to Kevin Wayne for inspiration to slides. Find closest pair with point on each side Find closest pair with point on each side • Find closest pair with one point in each side, assuming that distance < δ . • Find closest pair with one point in each side, assuming that distance < δ . • Observation: only need to consider points within δ of line L. • Observation: only need to consider points within δ of line L. • Sort points in 2 δ -strip by their y coordinate. • Sort points in 2 δ -strip by their y coordinate. • Only check distances between these points within 7 positions in sorted list! L L 7 7 6 6 5 5 21 21 4 4 δ = min(12, 21) δ = min(12, 21) 12 3 12 3 2 2 1 1 δ δ Thank you to Kevin Wayne for inspiration to slides. Thank you to Kevin Wayne for inspiration to slides.

  4. Closest Pair Algorithm 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. Closest-Pair(p 1 , …, p n ) { • Claim. If |i – j| ≥ 8, then the distance between 
 If n < 4 compute closest pair by comparing all pairs. j 39 s i and s j is at least δ . Compute separation line L such that half the points 
 O(n log n) are on one side and half on the other side. • Pf. 31 δ 1 = Closest-Pair(left half) • s j at most δ apart from s i then the di ff erence in 2T(n / 2) δ 2 = Closest-Pair(right half) y-coordinate is at most δ . δ = min( δ 1 , δ 2 ) • No two points lie in same ½ δ -by- ½ δ box: 30 Delete all points further than δ from separation line L O(n) ½ δ 29 δ s✓ 1 ◆ 2 ◆ 2 r ✓ 1 O(n log n) 1 ½ δ Sort remaining points by y-coordinate. 28 2 δ + 2 δ = 2 δ ≈ 0 . 7 δ < δ i 27 Scan points in y-order and compare distance between 
 O(n) each point and next 7 neighbors. If any of these 
 26 • At most 7 points within distance δ (one in each distances is less than δ , update δ . 25 other box). ▪ return δ . } δ δ Thank you to Kevin Wayne for inspiration to slides. Thank you to Kevin Wayne for inspiration to slides. Closest Pair Algorithm Closest Pair Algorithm • Analysis: • T(n) = 2T(n/2) + O(n log n), for n > 4. T(n) = O(1), for n ≤ 4. Presort points into lists X and Y after x- and y-coordinate, respectively. • T(n) = O(n log 2 n). Closest-Pair(X[1…n],Y[1…n]) { If n < 4 compute closest pair by comparing all pairs. • Can improve this by pre-sorting points: Compute separation line L such that half the points 
 O(n) • Start by constructing 2 sorted lists X and Y containing all points sorted are on one side and half on the other side. after x- and y-coordinate, resp. δ 1 = Closest-Pair(left half) 2T(n / 2) • Divide: δ 2 = Closest-Pair(right half) δ = min( δ 1 , δ 2 ) • Split X-array in middle. O(n) Delete all points further than δ from separation line L • Use linear time to split Y-array into 2 (according to x-coordinate). O(n) Sort remaining points by y-coordinate. • Combine: • Prune Y-array (only consider points with x-coordinate within δ of L). 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 δ . }

  5. 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). Closest pair of points • In total O(n log n). A randomized algorithm Randomized algorithm How to check a point • δ current smallest distance. Divide unit square into subsquares with side lengths δ /2. • 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 δ . δ /2 6 12 6 12 13 13 7 7 14 14 8 8 δ /2 3 3 1 1 5 5 15 δ 1 15 δ 2 2 4 4 11 11 9 δ 2 10 10 9

  6. How to check a point How to check a point • δ current smallest distance. Divide unit square into subsquares with side lengths δ /2. • δ 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 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 δ /2 6 12 6 12 13 13 7 7 14 14 8 8 δ /2 δ /2 3 3 1 1 5 5 15 15 2 2 4 4 11 11 10 10 9 9 Closest Pair of Points: Randomized algorithm Closest Pair of Points • Use hashtable to store which square a point is in. Only store points already • Use hashtable to store which square a point is in. Only store points already looked at (red points). looked at (red points). • When starting new round: rehash all points from 1…i. δ 3 6 12 6 12 13 13 7 7 14 14 8 8 3 3 1 1 5 5 15 δ 1 15 2 2 4 4 11 11 9 δ 2 9 δ 2 10 10

  7. Closest Pair of Points Closest Pair of Points: Analysis • Use hashtable to store which square a point is in. Only store points already • Number of lookup operations: looked at (red points). • Number of distance calculations: • Number of MakeDictionary operations: • 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: • 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