170 likes | 355 Views
The Divide-and-Conquer Strategy. 報告者 : 郭育婷. The 2-Dimesional Maxima Finding Problem. 何謂 Dominates ?. A point (x1, y1) dominates (x2, y2) if x1 > x2 and y1 > y2. Y. A. A dominates B. B. X. The 2-Dimesional Maxima Finding Problem. 何謂 Maxima ?.
E N D
The Divide-and-Conquer Strategy 報告者:郭育婷
The 2-Dimesional Maxima Finding Problem 何謂 Dominates ? • A point (x1, y1) dominates (x2, y2) if x1 > x2 and y1 > y2. Y A A dominates B B X
The 2-Dimesional Maxima Finding Problem 何謂 Maxima ? • A point is called a maxima if no other point dominates it. Y 兩兩比較 Time Complexity:O(n2) X
Maxima Finding Algorithm Input: A set S of n planar points. Output:The maximal points of S. Step 1: If S contains only one point, return it as the maxima. Otherwise, find a line L perpendicular to the X-axis which separatesthe set of points into two subsets SLand SR, each consisting of n/2 points. Step 2: Recursively find the maximal points of SL and SR . • Step 3: Project the maximal points of SLandSRonto L and sort these points according to their y-values. Conduct a linear scan on the projections and discard each of the maximal points of SLif its y-value is less than the y-value of some maximal point of SR.
Y Y X X Maxima Finding Algorithm Illustration Step 1 If only one point else Time Complexity:O(n) L SL SR maxima
Y X Maxima Finding Algorithm Illustration Step 2 Time Complexity:2T(n/2) L L SL SR
Maxima Finding Algorithm Illustration Step 3 Time Complexity:O(nlogn) Y L SL SR X
Maxima Finding Time complexity Time complexity: T(n) Step 1: O(n) Step 2: 2T(n/2) Step 3: O(nlogn) Assume n = 2k T(n)=O(nlogn)+O(nlog2n) =O(nlog2n)
Maxima Finding Time complexity After presorting Time complexity: O(nlogn)+T(n) Step 1: O(n) Step 2: 2T(n/2) Step 3: O(n) Assume n = 2k T(n)=O(nlogn) Time complexity =O(nlogn)+T(n) = O(nlogn)+O(nlogn)= O(nlogn)
Y X X The Closest Pair Problem 何謂 Closest Pair ? • Given a set S of n points, find a pair of points which are closest together(在一個有 n 個點的集合 S 中,找最靠近的兩個點) 1-D version: 2-D version: Time Complexity:O(nlogn)
Closest Pair Algorithm Input: A set S of n planar points. Output: The distance between two closest points. Step 1: Sort points in S according to their y-values and x-values. Step 2: If S contains only one point, return infinity as its distance. Step 3: Find a median line L perpendicular to the X-axis to divide S into SL and SR, with equal sizes. Step 4: Recursively apply Steps 2 and 3 to solve the closest pair problems of SL and SR. Let dL(dR) denote the distance between the closest pair in SL (SR). Let d = min(dL, dR). Step 5: For a point P in the half-slab bounded by L-d and L, let its y-value be denoted as yP . For each such P, find all points in the half-slab bounded by L and L+d whose y-value fall within yP+d and yP-d. If the distance dbetween P and a point in the other half-slab is less than d, let d=d. The final value of d is the answer
Y Y X X Closest Pair Algorithm Illustration Step 1 先針對每個點X軸的值和Y軸的值做排序 Step 3 Step 2 If only one point Time Complexity:O(n) L SL SR d = ∞
Y X Closest Pair Algorithm Illustration Step 4 Time Complexity:2T(n/2) L L SL SR
Y X Closest Pair Algorithm Illustration Step 5 L SL SR dL dR d=min(dL,dR) L-d L+d Brute-force way = n2/4 , not good
2d d Closest Pair Algorithm Illustration Fortunately, the merging step can be accomplished in O(n) time. Y P X
Closest Pair Time complexity Time complexity: Step 1: O(nlogn) Step 2~5: T(n) = 2T(n/2)+S(n)+M(n) T(n)=O(nlogn) Total time-complexity =O(nlogn)+T(n) = O(nlogn)+O(nlogn)= O(nlogn)