Divide-Conquer-Glue Algorithms 5. D IVIDE AND C ONQUER Closest Pair ‣ mergesort Tyler Moore ‣ counting inversions ‣ closest pair of points CSE 3353, SMU, Dallas, TX ‣ randomized quicksort Lecture 12 ‣ median and selection � ������� ��� Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos . Some code reused from Python Algorithms by Magnus Lie Hetland. 2 / 19 ���������������������� ���������������������� Closest pair problem. Given � points in the plane, find a pair of points Closest pair problem. Given � points in the plane, find a pair of points with the smallest Euclidean distance between them. with the smallest Euclidean distance between them. Fundamental geometric primitive. Brute force. Check all pairs with Θ � � � � distance calculations. � Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. 1d version. Easy � � � ����� � � algorithm if points are on a line. � Special case of nearest neighbor, Euclidean MST , Voronoi. Nondegeneracy assumption. No two points have the same � -coordinate. fast closest pair inspired fast algorithms for these problems 21 22 3 / 19 4 / 19
Closest-pair problem in one dimension Closest-pair problem has divide-and-conquer solution A divide-and-conquer algorithm works as follows. In the closest-pair problem, you are to select the pair of points 1 Base Case: If the list contains two points, then they must be the ( p 1 , p 2 ) from a set S that are closest to each other. closest pair. 2 Divide: Divide the set into two halves (e.g., S l and S r in the fi gure S l = { l 1 , l 2 , l 3 } S r = { r 1 , r 2 , r 3 } above). Put all points less than the midpoint m in S l and all points greater than or equal to the midpoint in S r . l 1 l 2 l 3 r 3 r 1 r 2 3 Conquer: Find the closest-pair for each half (( l 1 , l 2 ) for S l and ( r 1 , r 2 ) for S r ). d min d min d min 4 Glue: To fi nd the closest pair in the entire set, select from 3 options: m closest pair in the left half (( l 1 , l 2 )); 1 closest pair in the right half (( r 1 , r 2 )); 2 a pair with one point each from S l and S r . 3 5 / 19 6 / 19 1D Glue procedure ������������������������������������� Sorting solution. � Sort by � -coordinate and consider nearby points. A divide-and-conquer algorithm works as follows. � Sort by � -coordinate and consider nearby points. Glue: To fi nd the closest pair in the entire set, select from 3 options: closest pair in the left half (( l 1 , l 2 )); 1 closest pair in the right half (( r 1 , r 2 )); 2 a pair with one point each from S l and S r . 3 For the closest pair to take a point from both sets, each point must be within distance d min of the midpoint m between the two sets (here d min = min( distance ( l 1 , l 2 ) , distance ( r 1 , r 2 ))). Only the largest point in the left set l max and the smallest point in the right set r min could be closer than d min . Compute distance( l max , r min ) and update the closest pair if less than d min . 23 7 / 19 8 / 19
������������������������������������� ��������������������������������������� Sorting solution. Divide. Subdivide region into 4 quadrants. � Sort by � -coordinate and consider nearby points. � Sort by � -coordinate and consider nearby points. 8 24 25 9 / 19 10 / 19 ��������������������������������������� ����������������������������������������������������� � Divide: draw vertical line � so that � � � � � points on each side. Divide. Subdivide region into 4 quadrants. Obstacle. Impossible to ensure � � � � � points in each piece. � Conquer: find closest pair in each side recursively. � Combine: find closest pair with one point in each side. � Return best of 3 solutions. seems like Θ (N 2 ) L 8 21 12 26 27 11 / 19 12 / 19
Recommend
More recommend