 # The Divide-and-Conquer Strategy

## Presentation on theme: "The Divide-and-Conquer Strategy"— Presentation transcript:

The Divide-and-Conquer Strategy
The 2-Dimensional maxima finding problem The Closest pair problem The Convex hull problem Speaker:董家禎

Introduction The 2-Dimensional maxima finding problem The Closest pair problem The Convex hull problem

Introduction divide-and-conquer(作法)
Split a problem into two smaller sub-problems, except its input size is small sub-problems that themselves can be solved by the divide-and-conquer (recursively) Both sub-problems are then solved and merged into the final solution

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 sizes Step2. Recursively solve these two sub-problems by applying the divide-and-conquer algorithm to these sub-problems Step3. Merge the solutions of the two sub-problems into a solution of the original problem

Introduction (cont.) divide-and-conquer (time complexity)
where S (n) : time for splitting M (n) : time for merging b : a constant c : a constant

Introduction (cont.) Example: Finding the maximum

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

The 2-Dimensional maxima finding problem
Definition: A point (x1, y1) dominates (x2, y2) if x1 > x2 and y1 > y2 A point is called a maxima if no other point dominates it Maximal points

The 2-Dimensional maxima finding problem (cont.)
The Maximal of SL and SR

The 2-Dimensional maxima finding problem (cont.)
Input: A set 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 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

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 = 2k T(n) = O(n log n) + O (nlog2n) = O (nlog2n) After presorting: T(n) = O (nlogn) + O (nlogn) = O (nlogn)

The 2-Dimensional maxima finding problem (cont.)
Straightforward method : Compare every pair of points Time complexity: O(n2) Divide-and-conquer method: Time complexity: O(nlogn)

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 sorting Time complexity : O(nlogn) 2-D version: X

The closest pair problem (2-D version) (2/5)
At most 6 points in area A: Examine Yp+d and Yp-d

The closest pair problem (2-D version) (3/5)
Algorithm Input: A set 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 two points, return infinity as their distance Step 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

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.

The closest pair problem (2-D version) (5/5)
Time complexity: O(nlogn) Step 1: O(nlogn) Steps 2~5: T(n) = O(nlogn)

The convex hull problem

The convex hull problem (cont.)
a set of planar points is the smallest convex polygon containing all of the points.

The convex hull problem
The divide-and-conquer strategy to solve the problem (Use the Graham scan) i j k g h a b c d e f p L

The convex hull problem (cont.)
The process of solving convex hull problem V1 and V2 divide the vertices of Hull(SR) Three sequences are formed Find the greatest polar angle <π/2 and the least polar angle >3π/2 Find a interior point P of SL Contrust a horizontal line through P Merge these three sequences Conduct the Graham scan L d V1 j i e k p c 順時針 f h 逆時針 g b SL a V2 SR

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, e Merge 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.

Divide-and-conquer for convex hull
Input : A set S of planar points Output : A convex hull for S Step 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.

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)

The End