CS Lunch Mary Allen Wilkes Wednesday 12:15 Kendade 307 2 Divide - - PowerPoint PPT Presentation
CS Lunch Mary Allen Wilkes Wednesday 12:15 Kendade 307 2 Divide - - PowerPoint PPT Presentation
1 CS Lunch Mary Allen Wilkes Wednesday 12:15 Kendade 307 2 Divide and Conquer Divide-and-conquer. Break up problem into several parts. Solve each part recursively. Combine solutions to sub-problems into overall solution. Most common usage.
Closest Pair of Points
1-dimensional version 4
Closest Pair of Points
1-D version. Sort points For each point, find the distance between a point and the point that follows it. Remember the smallest.
5-1
Closest Pair of Points
1-D version. Sort points For each point, find the distance between a point and the point that follows it. Remember the smallest.
Cost
5-2 Slides14 - ClosestPairPoints.key - March 20, 2019
Closest Pair of Points
1-D version. Sort points For each point, find the distance between a point and the point that follows it. Remember the smallest.
Cost O(n log n)
5-3
Closest Pair of Points
1-D version. Sort points For each point, find the distance between a point and the point that follows it. Remember the smallest.
Cost O(n log n) O(n)
5-4
Closest Pair of Points
1-D version. Sort points For each point, find the distance between a point and the point that follows it. Remember the smallest.
Cost O(n log n) O(n) Total is O(n log n)
5-5 Slides14 - ClosestPairPoints.key - March 20, 2019
Closest Pair of Points
Divide: draw vertical line L so that n/2 points on each side.
L
6-1
Closest Pair of Points
Divide: draw vertical line L so that n/2 points on each side.
L
6-2
Closest Pair of Points
Solve: find closest pair in each side recursively.
L
7-1 Slides14 - ClosestPairPoints.key - March 20, 2019
Closest Pair of Points
Solve: find closest pair in each side recursively.
12 L
7-2
Closest Pair of Points
Solve: find closest pair in each side recursively.
12 21 L
7-3
Closest Pair of Points
Combine: find closest pair with one point in each side. Return best of 3 solutions. How do we do this without comparing each point on left with each point on right???
12 21 8 L
8 Slides14 - ClosestPairPoints.key - March 20, 2019
Closest Pair of Points
Let δ be the minimum between pair on left and pair on right If there exists a pair with one point in each side and whose distance < δ, find that pair.
12 21 δ = min(12, 21) L
9
Closest Pair of Points
Observation: only need to consider points within δ of line L.
12 21 δ L δ = min(12, 21)
10
12 21
1 2 3 4 5 6 7
Closest Pair of Points
Sort points in 2δ-strip by their y coordinate.
L δ = min(12, 21) δ
11 Slides14 - ClosestPairPoints.key - March 20, 2019
12 21
1 2 3 4 5 6 7
Closest Pair of Points
Only need to check distances of those within 15 positions in sorted list!!!!
L δ = min(12, 21) δ
12
Closest Pair of Points
Let si be the point in the 2δ-strip, with the ith smallest y-coordinate.
- Claim. If |i – j| ≥ 16, then the distance
between si and sj is at least δ. Proof: No two points lie in same ½δ-by-½δ box. Two points separated by at least 3 rows have distance ≥ 3(½δ).
δ
27 29 30 31 28 26 25
δ
½δ
3 rows
½δ ½δ
39
i j
13
Closest Pair of Points
Let si be the point in the 2δ-strip, with the ith smallest y-coordinate.
- Claim. If |i – j| ≥ 16, then the distance
between si and sj is at least δ. Proof: No two points lie in same ½δ-by-½δ box. Two points separated by at least 3 rows have distance ≥ 3(½δ). If a point is within δ of point 27, it must be in one of the blue boxes. There are only 15 blue boxes!
δ
27 29 30 31 28 26 25
δ
½δ
2 rows
½δ ½δ
39
i j
14 Slides14 - ClosestPairPoints.key - March 20, 2019
Closest Pair Algorithm
Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and calculate distance to all points with a y value within δ. If any of these distances is less than δ, update δ. return δ. }
15-1
Closest Pair Algorithm
Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and calculate distance to all points with a y value within δ. If any of these distances is less than δ, update δ. return δ. }
Cost
15-2
Closest Pair Algorithm
Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and calculate distance to all points with a y value within δ. If any of these distances is less than δ, update δ. return δ. }
O(n log n)
Cost
15-3 Slides14 - ClosestPairPoints.key - March 20, 2019
Closest Pair Algorithm
Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and calculate distance to all points with a y value within δ. If any of these distances is less than δ, update δ. return δ. }
O(n log n) 2T(n / 2)
Cost
15-4
Closest Pair Algorithm
Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and calculate distance to all points with a y value within δ. If any of these distances is less than δ, update δ. return δ. }
O(n log n) 2T(n / 2) O(n)
Cost
15-5
Closest Pair Algorithm
Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and calculate distance to all points with a y value within δ. If any of these distances is less than δ, update δ. return δ. }
O(n log n) 2T(n / 2) O(n) O(n log n)
Cost
15-6 Slides14 - ClosestPairPoints.key - March 20, 2019
Closest Pair Algorithm
Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and calculate distance to all points with a y value within δ. If any of these distances is less than δ, update δ. return δ. }
O(n log n) 2T(n / 2) O(n) O(n log n) O(n)
Cost
15-7
Closest Pair Algorithm
Closest-Pair(p1, …, pn) { Compute separation line L such that half the points are on one side and half on the other side. δ1 = Closest-Pair(left half) δ2 = Closest-Pair(right half) δ = min(δ1, δ2) Delete all points further than δ from separation line L Sort remaining points by y-coordinate. Scan points in y-order and calculate distance to all points with a y value within δ. If any of these distances is less than δ, update δ. return δ. }
O(n log n) 2T(n / 2) O(n) O(n log n) O(n)