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 i Gørtz 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. Fundamental geometric primitive. Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. Special case of nearest neighbor, Euclidean MST, Voronoi diagrams. Brute force. Compare all pairs => O(n ) time. -D version. Sort and scan => O(n log n) time. Closest pair of points A divide-and-conquer algorithm Simplifying assumption. No two points coincide (for a simpler presentation). 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: Closest pair: Divide-and-Conquer Divide: draw vertical line so that roughly n/ points on each side. Conquer: Closest pair: Divide-and-Conquer Divide: draw vertical line so that roughly n/ points on each side. Conquer: find closest pair in each side recursively. Closest pair: Divide-and-Conquer Divide: draw vertical line so that roughly n/ points on each side. Conquer: find closest pair in each side recursively. Find closest pair with one point in each side. seems like Θ(n ) Return best of solutions
Find closest pair with point on each side Find closest pair with one point in each side, assuming that distance <. 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. = min(, ) = min(, ) 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. Sort points in -strip by their y coordinate. 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. Sort points in -strip by their y coordinate. Only check distances between these points within positions in sorted list! = min(, ) = min(, )
Find closest pair with point on each side Closest Pair Algorithm Def. et s i be the point in the -strip, with the i th smallest y-coordinate. Closest-Pair(p,, p n ) { Claim. If i j, then the distance between s i and s j is at least. Pf. 9 j If n < compute closest pair by comparing all pairs. Compute separation line such that half the points are on one side and half on the other side. O(n log n) s j at most apart from s i then the difference in y-coordinate is at most. = Closest-Pair(left half) = Closest-Pair(right half) = min(, ) T(n / ) No two points lie in same ½-by-½ box: s r + = 0. < At most points within distance (one in each other box). i 9 0 ½ ½ } Delete all points further than from separation line Sort remaining points by y-coordinate. Scan points in y-order and compare distance between each point and next neighbors. If any of these distances is less than, update. return. O(n log n) Closest Pair Algorithm Closest Pair Algorithm Analysis: T(n) = T(n/) + O(n log n), for n >. T(n) = O(), for n. T(n) = O(n log n). Can improve this by pre-sorting points: Start by constructing 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 (according to x-coordinate). Prune Y-array (only consider points with x-coordinate within of ). Presort points into lists X and Y after x- and y-coordinate, respectively. Closest-Pair(X[ n],y[ n]) { If n < compute closest pair by comparing all pairs. Compute separation line such that half the points are on one side and half on the other side. = Closest-Pair(left half) = Closest-Pair(right half) = min(, ) Delete all points further than from separation line Sort remaining points by y-coordinate. Scan points in y-order and compare distance between each point and next neighbors. If any of these distances is less than, update. T(n / ) } return.
Closest Pair Algorithm Analysis: Total time: T(n) + O(n log n). T(n) = T(n/) +, n>. T(n) = O(), n. 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. et = d(p,p). Check for each point pi (in order) if there exists a point pj, j<i, such that d(pi,pj) <. If such a point found. Update. How to check a point current smallest distance. Divide unit square into subsquares with side lengths /. / / 9 0 9 0
How to check a point current smallest distance. Divide unit square into subsquares with side lengths /. If two points i and j are in the same subsquare then d(i,j) <. How to check a point current smallest distance. Divide unit square into subsquares with side lengths /. If two points i and j are in the same subsquare then d(i,j) <. If d(i,j) < then j is in the x grid of subsquares around i. / / / / 9 0 9 0 Closest Pair of Points: Randomized algorithm Use hashtable to store which square a point is in. Only store points already looked at (red points). 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 i. 9 0 9 0
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 i. Closest Pair of Points: Analysis Number of lookup operations: Number of distance calculations: Number of MakeDictionary operations: 9 0 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 Pr[Xi = ] /i i= X i = ( i causes to change 0 otherwise Expected number of insertions: nx nx nx E[X] =n + i E[X i ]=n + i Pr[X i ] apple n + i /i = n +n =n. i= i= Use hashtable as dictionary: time in total.