Presentation on theme: "The Divide-and-Conquer Strategy"— Presentation transcript:
1 The Divide-and-Conquer Strategy The 2-Dimensional maxima finding problemThe Closest pair problemThe Convex hull problemSpeaker:董家禎
2 IntroductionThe 2-Dimensional maxima finding problemThe Closest pair problemThe Convex hull problem
3 Introduction divide-and-conquer(作法) Split a problem into two smaller sub-problems, except its input size is smallsub-problems that themselves can be solved by the divide-and-conquer (recursively)Both sub-problems are then solved and merged into the final solution
4 Introduction (cont.) divide-and-conquer (algorithm) Step1. If the problem size is small, solve this problem by some direct method; otherwise split the original problem into two sub-problems, preferably in equal sizesStep2. Recursively solve these two sub-problems by applying the divide-and-conquer algorithm to these sub-problemsStep3. Merge the solutions of the two sub-problems into a solution of the original problem
5 Introduction (cont.) divide-and-conquer (time complexity) where S (n) : time for splittingM (n) : time for mergingb : a constantc : a constant
6 Introduction (cont.)Example: Finding the maximum
7 Introduction (cont.) Example: Finding the maximum (cont.) Time complexity:Calculation of T(n):Assume n = 2k,T(n) = 2T(n/2)+1= 2(2T(n/4)+1) //T(n/2) = 2T[(n/2)/2]+1= 4T(n/4)+2+1:=2k-1T(2)+2k-2+… //T( n=2k/2k-1 = 2)=2k-1+2k-2+…+4+2+1=2k-1 = n-1
8 The 2-Dimensional maxima finding problem Definition:A point (x1, y1) dominates (x2, y2) if x1 > x2 and y1 > y2A point is called a maxima if no other point dominates itMaximal points
9 The 2-Dimensional maxima finding problem (cont.) The Maximal of SL and SR
10 The 2-Dimensional maxima finding problem (cont.) Input: A set of n planar pointsOutput: The maximal points of SStep 1: If S contains only one point, return it as the maxima. Otherwise, find a line L perpendicular to the X-axis which separates the set of points into two subsets SL and SR , each of which consisting of n/2 points.Step 2: Recursively find the maximal points of SL and SR .Step 3: Find the largest y-value of SR. Project the maximal points of SL onto L. Discard each of the maximal points of SL if its y-value is less than the largest y-value of SR
11 The 2-Dimensional maxima finding problem (cont.) Time complexity: T(n)Step 1: O(n)Step 2: 2T(n/2)Step 3: O(nlogn)Assume n = 2kT(n) = O(n log n) + O (nlog2n) = O (nlog2n)After presorting:T(n) = O (nlogn) + O (nlogn) = O (nlogn)
12 The 2-Dimensional maxima finding problem (cont.) Straightforward method :Compare every pair of pointsTime complexity: O(n2)Divide-and-conquer method:Time complexity: O(nlogn)
13 The closest pair problem (1/5) Given a set S of n points, find a pair of points which are closest together.1-D version:Solved by sortingTime complexity : O(nlogn)2-D version:X
14 The closest pair problem (2-D version) (2/5) At most 6 points in area A:Examine Yp+d and Yp-d
15 The closest pair problem (2-D version) (3/5) AlgorithmInput: A set of n planar pointsOutput: The distance between two closest points.Step 1: Sort points in S according to their y-values and x-valuesStep 2: If S contains only two points, return infinity as their distanceStep 3: If not, Find a median line L perpendicular to the X-axis to divide S into two subsets, with equal sizes, SL and SR
16 The closest pair problem (2-D version) (4/5) Step 4: Recursively apply Step 2 and Step 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 by 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 d between P and a point in the other half-slab is less than d, let d=d . The final value of d is the answer.
17 The closest pair problem (2-D version) (5/5) Time complexity: O(nlogn)Step 1: O(nlogn)Steps 2~5:T(n) = O(nlogn)
18 The convex hull problem 何謂凹多邊形和凸多邊形？concave polygon: convex polygon:
19 The convex hull problem (cont.) a set of planar points is the smallest convex polygon containing all of the points.
20 The convex hull problem The divide-and-conquer strategy to solve the problem (Use the Graham scan)ijkghabcdefpL
21 The convex hull problem (cont.) The process of solving convex hull problemV1 and V2 divide the vertices of Hull(SR)Three sequences are formedFind the greatest polar angle <π/2 and the least polar angle >3π/2Find a interior point P of SLContrust a horizontal line through PMerge these three sequencesConduct the Graham scanLdV1jiekpc順時針fh逆時針gbSLaV2SR
22 The convex hull problem (cont.) The merging procedure:Select an interior point p.There are 3 sequences of points which have increasing polar angles with respect to p.(1) g, h, i, j, k(2) a, b, c, d(3) f, eMerge these 3 sequences into 1 sequence:g, h, a, b, f, c, e, d, i, j, k.Apply Graham scan to examine the points one by one and eliminate the points which cause reflexive angles.
23 Divide-and-conquer for convex hull Input : A set S of planar pointsOutput : A convex hull for SStep 1: If S contains no more than five points, use exhaustive searching to find the convex hull and return.Step 2: Find a median line perpendicular to the x-axis which divides S into SL and SR ; SL lies to the left of SR .Step 3: Recursively construct convex hulls for SL and SR. Denote these convex hulls by Hull(SL) and Hull(SR) respectively.
24 Divide-and-conquer for convex hull Step 4: Find an interior point P of SL. Find v1 and v2 of Hull(SR) which divide the vertices of Hull(SR) into two sequence which have increasing polar angles with respect to P, v1 > v2 in y-value. Apply the merging procedure to merge Hull(SL) and Hull(SR) together to form a convex hull.Time complexity:T(n) = 2T(n/2) + O(n)= O(n log n)