Download presentation
Presentation is loading. Please wait.
1
Lecture 6 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng
2
Divide and Conquer Divide the problem into a number of sub-problems (similar to the original problem but smaller); Conquer the sub-problems by solving them recursively (if a sub-problem is small enough, just solve it in a straightforward manner. Combine the solutions to the sub-problems into the solution for the original problem
3
Merge Sort Divide the n-element sequence to be sorted into two subsequences of n/2 element each Conquer: Sort the two subsequences recursively using merge sort Combine: merge the two sorted subsequences to produce the sorted answer Note: during the recursion, if the subsequence has only one element, then do nothing.
4
Algorithm Design Paradigm I Solve smaller problems, and use solutions to the smaller problems to solve larger ones –Divide and Conquer Correctness: mathematical induction
5
Recurrence of T(n) T(1) = 1 for n > 1, we have if n = 1 if n > 1
6
Solution of Recurrence of T(n) T(n) = 4 nlog n + n = O(nlog n) Picture Proof by Recursion Tree
7
Two Dimensional Divide and Conquer Can we extend the divide and conquer idea to 2 dimensions? We will consider a slightly simpler problem (handout #33, Chapter 33.4)
8
Closest Pair Problems Input: –A set of points P = {p 1,…, p n } in two dimensions Output: –The pair of points p i, p j that minimize the Euclidean distance between them.
9
Closest Pair Problem
11
Divide and Conquer O(n 2 ) time algorithm is easy Assumptions: –No two points have the same x-coordinates –No two points have the same y-coordinates How do we solve this problem in 1 dimensions? –Sort the number and walk from left to right to find minimum gap
12
Divide and Conquer Divide and conquer has a chance to do better than O(n 2 ). Assume that we can find the median in O(n) time!!! We can first sort the point by their x- coordinates
13
Closest Pair Problem
14
Divide and Conquer for the Closest Pair Problem Divide by x-median
15
Divide Divide by x-median L R
16
Conquer Conquer: Recursively solve L and R L R
17
Combination I Takes the smaller one of 1, 2 : = min( 1, 2 ) L R 22
18
Combination II Is there a point in L and a point in R whose distance is smaller than ? Takes the smaller one of 1, 2 : = min( 1, 2 ) L R
19
Combination II If the answer is “no” then we are done!!! If the answer is “yes” then the closest such pair forms the closest pair for the entire set Why???? How do we determine this?
20
Combination II Is there a point in L and a point in R whose distance is smaller than ? Takes the smaller one of 1, 2 : = min( 1, 2 ) L R
21
Combination II Is there a point in L and a point in R whose distance is smaller than ? Need only to consider the narrow band O(n) time L R
22
Combination II Is there a point in L and a point in R whose distance is smaller than ? Denote this set by S, assume S y is sorted list of S by y-coordinate. L R
23
Combination II There exists a point in L and a point in R whose distance is less than if and only if there exist two points in S whose distance is less than . If S is the whole thing, did we gain any thing? If s and t in S has the property that ||s-t|| < , then s and t are within 30 position of each other in the sorted list S y.
24
Combination II Is there a point in L and a point in R whose distance is smaller than ? L R There are at most one point in each box
25
Closest-Pair Closest-pair(P) –Preprocessing: Construct P x and P y as sorted-list by x- and y-coordinates –Divide Construct L, L x, L y and R, R x, R y –Conquer Let 1 = Closest-Pair(L, L x, L y ) Let 2 = Closest-Pair(R, R x, R y ) –Combination Let = min( 1, 2 ) Construct S and S y For each point in S y, check each of its next 30 points down the list If the distance is less than , update the as this smaller distance
26
Complexity Analysis Preprocessing takes O(n lg n) time Divide takes O(n) time Conquer takes 2 T(n/2) time Combination takes O(n) time So totally takes O(n lg n) time
27
Guarding Art Gallery
28
Visibility Problem
29
Art Gallery Problem To learn more about this problem, you can google “Art Gallery Problem” or google “Art Gallery Problems”
30
Visibility Problems: Intersection of Ray with Line or Plane How to describe a line passing a point along a direction? How to describe a line and a plane? How to find their intersection?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.