divide conquer glue algorithms
play

Divide-Conquer-Glue Algorithms 5. D IVIDE AND C ONQUER Closest Pair - PowerPoint PPT Presentation

Divide-Conquer-Glue Algorithms 5. D IVIDE AND C ONQUER Closest Pair mergesort Tyler Moore counting inversions closest pair of points CS 2123, The University of Tulsa randomized quicksort median and selection Some slides


  1. Divide-Conquer-Glue Algorithms 5. D IVIDE AND C ONQUER Closest Pair ‣ mergesort Tyler Moore ‣ counting inversions ‣ closest pair of points CS 2123, The University of Tulsa ‣ randomized quicksort ‣ median and selection Some slides created by or adapted from Dr. Kevin Wayne. For more information see S ECTION 5.4 http://www.cs.princeton.edu/~wayne/kleinberg-tardos . Some code reused from Python Algorithms by Magnus Lie Hetland. 2 / 32 Closest pair of points Closest pair of points Closest pair problem. Given n points in the plane, find a pair of points Closest pair problem. Given n 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 Θ ( n 2 ) distance calculations. ・ Graphics, computer vision, geographic information systems, molecular modeling, air traffic control. 1d version. Easy O ( n log n ) algorithm if points are on a line. ・ Special case of nearest neighbor, Euclidean MST , Voronoi. Nondegeneracy assumption. No two points have the same x -coordinate. fast closest pair inspired fast algorithms for these problems 21 22 3 / 32 4 / 32

  2. 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 figure 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 find 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 / 32 6 / 32 1D Glue procedure Closest pair of points: first attempt Sorting solution. ・ Sort by x -coordinate and consider nearby points. A divide-and-conquer algorithm works as follows. ・ Sort by y -coordinate and consider nearby points. Glue: To find 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 / 32 8 / 32

  3. Closest pair of points: first attempt Closest pair of points: second attempt Sorting solution. Divide. Subdivide region into 4 quadrants. ・ Sort by x -coordinate and consider nearby points. ・ Sort by y -coordinate and consider nearby points. 8 24 25 9 / 32 10 / 32 Closest pair of points: second attempt Closest pair of points: divide-and-conquer algorithm ・ Divide: draw vertical line L so that n / 2 points on each side. Divide. Subdivide region into 4 quadrants. Obstacle. Impossible to ensure n / 4 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 / 32 12 / 32

  4. How to find closest pair with one point in each side? How to find closest pair with one point in 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. ・ Only check distances of those within 11 positions in sorted list! why 11? L L 7 6 5 21 21 4 δ = min(12, 21) δ = min(12, 21) 12 12 3 2 1 δ 28 δ 29 13 / 32 14 / 32 How to find closest pair with one point in each side? Closest pair of points: divide-and-conquer algorithm Def. Let s i be the point in the 2 δ -strip, with the i th smallest y -coordinate. C LOSEST -P AIR ( p 1 , p 2 , …, p n ) ⋮ Claim. If | i – j | ≥ 12 , then the distance _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ between s i and s j is at least δ . Compute separation line L such that half the points O ( n log n ) j 39 are on each side of the line. 31 Pf. δ 1 ← C LOSEST -P AIR (points in left half) . 2 T ( n / 2) ・ No two points lie in same ½ δ -by- ½ δ box. δ 2 ← C LOSEST -P AIR (points in right half) . ½ δ ・ Two points at least 2 rows apart δ ← min { δ 1 , δ 2 }. have distance ≥ 2 ( ½ δ ) . ▪ 2 rows Delete all points further than δ from line L . 30 ½ δ O ( n ) 29 Sort remaining points by y -coordinate. O ( n log n ) ½ δ 28 27 i Scan points in y -order and compare distance between Fact. Claim remains true if we replace 12 with 7 . each point and next 11 neighbors. If any of these O ( n ) distances is less than δ , update δ . 26 R ETURN δ . 25 _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ ⋮ δ δ 30 31 15 / 32 16 / 32

  5. Closest pair of points: analysis Improved closest pair algorithm Theorem. The divide-and-conquer algorithm for finding the closest pair of Q. How to improve to O ( n log n ) ? points in the plane can be implemented in O ( n log 2 n ) time. A. Yes. Don't sort points in strip from scratch each time. ・ Each recursive returns two lists: all points sorted by x -coordinate, Θ (1) if n = 1 and all points sorted by y -coordinate. T ( n ) = T ( ⎡ n / 2 ⎤ ) + T ( ⎣ n / 2 ⎦ ) + O ( n log n ) otherwise ・ Sort by merging two pre-sorted lists. Theorem. [Shamos 1975] The divide-and-conquer algorithm for finding the ( x 1 - x 2 ) 2 + ( y 1 - y 2 ) 2 closest pair of points in the plane can be implemented in O ( n log n ) time. Lower bound. In quadratic decision tree model, any algorithm Θ (1) if n = 1 for closest pair (even in 1D) requires Ω ( n log n ) quadratic tests. Pf. T ( n ) = T ( ⎡ n / 2 ⎤ ) + T ( ⎣ n / 2 ⎦ ) + Θ ( n ) otherwise Note. See S ECTION 13.7 for a randomized O ( n ) time algorithm. not subject to lower bound since it uses the floor function 32 33 17 / 32 18 / 32 2D Divide-Conquer-Glue 2D Divide-Conquer-Glue Step 0: Get Sorted Lists Divide (2 , 6) (2 , 6) (5 , 5) (6 , 5) (5 , 5) (6 , 5) (9 , 4) (9 , 4) (3 , 2) (3 , 2) (1 , 1) (6 , 1) (1 , 1) (6 , 1) m = 5 seq = [(5,5),(1,1),(6,5),(3,2),(6,1),(9,4),(2,6)] lftx = [(1,1),(2,6),(3,2),(5,5)], rgtx=[(6,1),(6,5),(9,4)] seqx = [(1,1),(2,6),(3,2),(5,5),(6,1),(6,5),(9,4)] lfty = [(1,1),(3,2),(5,5),(2,6)], rgty=[(6,1),(9,4),(6,5)] seqy = [(1,1),(6,1),(3,2),(9,4),(6,5),(5,5),(2,6)] Conquer : invoke cpp helper recursively with left and right halves 19 / 32 19 / 32

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend