# 4 -1 Chapter 4 The Divide-and-Conquer Strategy. 4 -2 Outlines 4-1 The 2-Dimensional Maxima Finding Problem 4-2 The Closest Pair Problem 4-3 The Convex.

## Presentation on theme: "4 -1 Chapter 4 The Divide-and-Conquer Strategy. 4 -2 Outlines 4-1 The 2-Dimensional Maxima Finding Problem 4-2 The Closest Pair Problem 4-3 The Convex."— Presentation transcript:

4 -1 Chapter 4 The Divide-and-Conquer Strategy

4 -2 Outlines 4-1 The 2-Dimensional Maxima Finding Problem 4-2 The Closest Pair Problem 4-3 The Convex Hull Problem 4-4 The Voronoi Diagrams Constructed by the Divide-and-Conquer Strategy 4-5 Applications of the Voronoi Diagrams 4-6 The Fast Fourier Transform 4-7 The Experimental Results

4 -3 Introduction divide-and-conquer strategy first divides a problem into two smaller sub-problems and each sub-problem is identical to its original problem, except its input size is smaller. Both sub-problems are then solved and the sub- solutions are finally merged into the final solution. These two sub-problems themselves can be solved by the divide-and-conquer strategy again. Or, to put it in another way, these two sub- problems are solved recursively.

4 -4 A simple example Finding the maximum of a set S of n numbers. Dividing the input into two sets, each set consisting of n/2 numbers. Let us call these two sets S 1 and S 2. Find the maximums of S 1 and S 2 respectively. Let the maximum of S i be denoted as X i, i =1, 2. Then the maximum of S can be found by comparing X 1 and X 2. Whichever is the larger is the maximum of S.

4 -5 A simple example finding the maximum of a set S of n numbers

4 -6 Time Complexity In general, the complexity T(n) of a divide-and- conquer algorithm is determined by the following formulas: where S(n) denotes the time steps needed to split the problem into two sub-problems, M(n) denotes the time steps needed to merge two sub-solutions and b is a constant.

4 -7 Time complexity: Calculation of T(n): Assume n = 2 k, T(n)= 2T(n/2)+1 = 2(2T(n/4)+1)+1 = 4T(n/4)+2+1 : =2 k-1 T(2)+2 k-2 + … +4+2+1 =2 k-1 +2 k-2 + … +4+2+1 =2 k -1 = n-1 Time complexity

4 -8 A general divide-and-conquer algorithm Step 1: If the problem size is small, solve this problem directly; otherwise, split the original problem into 2 sub-problems with equal sizes. Step 2: Recursively solve these 2 sub-problems by applying this algorithm. Step 3: Merge the solutions of the 2 sub- problems into a solution of the original problem.

4 -9 Time complexity of the general algorithm Time complexity: where S(n) : time for splitting M(n) : time for merging b : a constant c : a constant e.g. Binary search e.g. quick sort e.g. merge sort

4 -10 4.1 2-D maxima finding problem Def : A point (x 1, y 1 ) dominates (x 2, y 2 ) if x 1 > x 2 and y 1 > y 2. A point is called a maxima if no other point dominates it Maxima finding problem: find the maximal points among these n points. Straightforward method : Compare every pair of points. Time complexity: O(n 2 )

4 -11 Divide-and-conquer for maxima finding The maximal points of S L and S R Perpendicular line Median point

4 -12 Merge Step The merging process is rather simple. Since the x-value of a point in S R is always larger than the x-value of every point in S L. A point in S L is a maxima if and only if its y-value is not less than the y-value of a maxima of S R.

4 4 -13 The algorithm: 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 S L and S R, each of which consisting of n/2 points. Step 2: Recursively find the maximal points of S L and S R. Step 3: Find the largest y-value of S R. Project the maximal points of S L onto L. Discard each of the maximal points of S L if its y-value is less than the largest y-value of S R.

4 4 -14 Time complexity: T(n) Step 1: O(n) median finding Step 2: 2T(n/2) Step 3: O(nlogn) :sorting n points according to their y-value. Assume n = 2 k T(n) = O(n log n) +O(nlog 2 n)= O(nlog 2 n)

4 4 -15 Improvement We note that our divide-and-conquer strategy is dominated by sorting in the merging steps. sorting should be done once and for all. Somehow we are not doing a very efficient job because sorting should be done once and for all. That is, we should conduct a presorting. If this is done, the merging complexity is O(n) and the total number of time steps needed is O(nlogn) + T(n) where and T(n) can be easily found to be O(nlogn). Thus the total time-complexity of using the divide-and-conquer strategy to find maximal points with presorting is O(nlogn).

4 4 -16 Merge Sort Rewrite: Becomes A Recursion Tree

4 4 -17 Merge Sort (Recursion Tree)

4 4 -18 Merge Sort (Recursion Tree)

4 4 -19 Merge Sort (Recursion Tree)

4 4 -20 Merge Sort (Recursion Tree) when

4 4 -21 Merge Sort (Recursion Tree)

4 4 -22 Recurrence is a recurrence. Recurrence: an equation that describes a function in terms of its value on smaller functions

4 4 -23 Recurrence (Examples)

4 4 -24 Solving Recurrences l Substitution method l Iteration Method l Recursion-tree method l Master method

4 4 -25 Substitution Method Guess the form of the solution Use mathematical induction to find the constants and show that the solution works n Examples: u T(n) = 2T(n / 2) +  (n)  T(n) =  (n lg n) We already know

4 4 -26 Substitution Method Guess the form of the solution Use mathematical induction to find the constants and show that the solution works n Examples: u T(n) = 2T(n / 2) +  (n)  T(n) =  (n lg n) u T(n) = 2T(  n / 2  ) + n  ???

4 4 -27 Substitution Method Guess the form of the solution Use mathematical induction to find the constants and show that the solution works n Examples: u T(n) = 2T(n / 2) +  (n)  T(n) =  (n lg n) u T(n) = 2T(  n / 2  ) + n   T(n) =  (n lg n) Need to Prove by Induction

4 4 -28 Changing Variables Sometimes, changing variables may help n Examples: u T(n) = 2T(n / 2) + n  T(n) = O(n lg n) We already know

4 4 -29 Changing Variables Sometimes, changing variables may help n Examples: u T(n) = 2T(n / 2) + n  T(n) = O(n lg n)

4 4 -30 Changing Variables Sometimes, changing variables may help n Examples: u T(n) = 2T(n / 2) + n  T(n) = O(n lg n) Let

4 4 -31 Changing Variables Sometimes, changing variables may help n Examples: u T(n) = 2T(n / 2) + n  T(n) = O(n lg n) Let

4 4 -32 Changing Variables Sometimes, changing variables may help n Examples: u T(n) = 2T(n / 2) + n  T(n) = O(n lg n) Let T(n) = S(m) = O(m lg m) = O(lg n lg lg n)

4 4 -33 Iteration Method Expand the recurrence Work some algebra to express as a summation Evaluate the summation

4 4 -34 Iteration Method (Example)

4 4 -35 Iteration Method (Example) T(n) = c + T(n-1)

4 4 -36 Iteration Method (Example) T(n) = c + T(n-1) = c + c + T(n-2)

4 4 -37 Iteration Method (Example) T(n) = c + T(n-1) = c + c + T(n-2) = 2c + T(n-2)

4 4 -38 Iteration Method (Example) T(n) = c + T(n-1) = c + c + T(n-2) = 2c + T(n-2) = 2c + c + T(n-3)

4 4 -39 Iteration Method (Example) T(n) = c + T(n-1) = c + c + T(n-2) = 2c + T(n-2) = 2c + c + T(n-3) = 3c + T(n-3)

4 4 -40 Iteration Method (Example) T(n) = c + T(n-1) = c + c + T(n-2) = 2c + T(n-2) = 2c + c + T(n-3) = 3c + T(n-3) =...

4 4 -41 Iteration Method (Example) T(n) = c + T(n-1) = c + c + T(n-2) = 2c + T(n-2) = 2c + c + T(n-3) = 3c + T(n-3) = … = kc + T(n-k) Set k = ?

4 4 -42 Iteration Method (Example) T(n) = c + T(n-1) = c + c + T(n-2) = 2c + T(n-2) = 2c + c + T(n-3) = 3c + T(n-3) = … = kc + T(n-k) Set k = n

4 4 -43 Iteration Method (Example) T(n) = c + T(n-1) = c + c + T(n-2) = 2c + T(n-2) = 2c + c + T(n-3) = 3c + T(n-3) = … = kc + T(n-k) = nc + T(0) = nc T(n) =  (n) Set k = n

4 4 -44 Iteration Method (Example)

4 4 -45 Iteration Method (Example) T(n) = n + T(n-1)

4 4 -46 Iteration Method (Example) T(n) = n + T(n-1) = n + n-1 + T(n-2)

4 4 -47 Iteration Method (Example) T(n) = n + T(n-1) = n + n-1 + T(n-2) = n + n-1 + n-2 + T(n-3)

4 4 -48 Iteration Method (Example) T(n) = n + T(n-1) = n + n-1 + T(n-2) = n + n-1 + n-2 + T(n-3) = …

4 4 -49 Iteration Method (Example) T(n) = n + T(n-1) = n + n-1 + T(n-2) = n + n-1 + n-2 + T(n-3) = … = n + n-1 + n-2 + … + n-(k-1) + T(n-k)

4 4 -50 Iteration Method (Example) T(n) = n + T(n-1) = n + n-1 + T(n-2) = n + n-1 + n-2 + T(n-3) = … = n + n-1 + n-2 + … + n-(k-1) + T(n-k) Set k = n

4 4 -51 Iteration Method (Example) T(n) = n + T(n-1) = n + n-1 + T(n-2) = n + n-1 + n-2 + T(n-3) = … = n + n-1 + n-2 + … + n-(k-1) + T(n-k) = n + n-1 + n-2 + … + 1 + T(0) Set k = n

4 4 -52 T(n) = n + T(n-1) = n + n-1 + T(n-2) = n + n-1 + n-2 + T(n-3) = … = n + n-1 + n-2 + … + n-(k-1) + T(n-k) = n + n-1 + n-2 + … + 1 + T(0) = Iteration Method (Example) T(n) =  (n 2 )

4 4 -53 Recursion-Tree Method Expand the recurrence Construct a recursion-tree Sum the costs

4 4 -54 Recursion-Tree Method (Example)

4 4 -55 Recursion-Tree Method (Example)

4 4 -56 Recursion-Tree Method (Example)

4 4 -57 Recursion-Tree Method (Example)

4 4 -58 Recursion-Tree Method (Example)

4 4 -59 Recursion-Tree Method (Example)

4 4 -60 Recursion-Tree Method (Example)

4 4 -61 Recursion-Tree Method (Example)

4 4 -62 Recursion-Tree Method (Example)

4 4 -63 Recursion-Tree Method (Example) Recall

4 4 -64 Recursion-Tree Method (Example)

4 4 -65 Recursion-Tree Method (Example)

4 4 -66 Recursion-Tree Method (Example) T(n) = O(n 2 )

4 4 -67 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

4 68 Algorithms Analysis Master Theorem

4 4 -69 Master Theorem*** Provide a “cookbook” method for solving recurrences Divide-and-conquer algorithm An algorithm that divides the problem of size n into a subproblems, each of size n / b

4 4 -70 Master Theorem 1. 2. 3.

4 4 -71 Notes on Master Theorem Some technicalities: In case 1, f (n) must be polynomially smaller than by a factor of In case 3, f (n) must be polynomially larger than by a factor of The three cases doesn’t cover all possibilities of f (n). Can’t use Master Theorem

4 4 -72 Using the Master Method

4 4 -73 Using the Master Method

4 4 -74 Using the Master Method

4 4 -75 Using the Master Method

4 4 -76 Using the Master Method Use Case 1:

4 4 -77 Using the Master Method Use Case 1:

4 4 -78 Using the Master Method

4 4 -79 Using the Master Method

4 4 -80 Using the Master Method

4 4 -81 Using the Master Method

4 4 -82 Using the Master Method Use Case 2:

4 4 -83 Using the Master Method Use Case 2:

4 4 -84 Using the Master Method

4 4 -85 Using the Master Method

4 4 -86 Using the Master Method

4 4 -87 Using the Master Method

4 4 -88 Using the Master Method

4 4 -89 Using the Master Method Use Case 3:

4 4 -90 Using the Master Method Use Case 3:

4 4 -91 Using the Master Method

4 4 -92 Using the Master Method

4 4 -93 Using the Master Method But is not polynomially larger than is asymptotically less than Master Method doesn’t Apply

4 4 -94 Extended Master Method

4 4 -95 Extended Master Method Back to the previous recurrence:

4 4 -96 Extended Master Method Back to the previous recurrence:

4 4 -97 Proof of the Master Theorem Lemma 4.2 If Then

4 4 -98 Proof of Master Theorem

4 4 -99 Proof of Master Theorem

4 4 -100 Proof of Master Theorem

4 4 -101 Proof of Master Theorem

4 4 -102 Proof of Master Theorem

4 4 -103 Proof of Master Theorem

4 4 -104 Proof of Master Theorem Lemma 4.2 proved

4 4 -105 Proof of Master Theorem (Lemma 4.2)

4 4 -106 Proof of the Master Theorem Lemma 4.3 1. 2. 3.

4 4 -107 Proof of the Master Theorem For case 1:

4 4 -108 Proof of the Master Theorem For case 1:

4 4 -109 Proof of the Master Theorem For case 1: Since

4 4 -110 Proof of the Master Theorem For case 1: Case 1 Proved

4 4 -111 Proof of the Master Theorem For case 2: We have

4 4 -112 Proof of the Master Theorem For case 2: We have

4 4 -113 Proof of the Master Theorem For case 2: We have

4 4 -114 Proof of the Master Theorem For case 2: We have Case 2 Proved

4 4 -115 Proof of the Master Theorem For case 3:

4 4 -116 Proof of the Master Theorem For case 3: Repeat j time:

4 4 -117 Proof of the Master Theorem For case 3: Repeat j time: Therefore

4 4 -118 Proof of the Master Theorem For case 3: Repeat j time: Therefore

4 4 -119 Proof of the Master Theorem For case 3: Repeat j time: Therefore Case 3 Proved Lemma 4.3 Proved

4 4 -120 Proof of the Master Theorem Lemma 4.4 If 1. 2. 3.

4 4 -121 Proof of the Master Theorem By combing Lemma 4.2 & 4.3 For Case 1: For Case 2: For Case 3: because Lemma 4.4 Proved

4 4 -122 Proof of the Master Theorem Lemma 4.2, 4.3, and 4.4 proved the Master Theorem We skip the proof when floors and ceilings are used in the Master Theorem

4 4 -123 Master Theorem

4 4 -124

4 4 -125

4 4 -126

4 4 -127

4 4 -128 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

4 4 -129 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

4 4 -130 4.2 The closest pair problem 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(n log n)  2-D version

4 4 -131 D&C We first partition the set S into S L and S R such that every point in S L lies to the left of every point in S R and the number of points in S L is equal to that in S R. Find a vertical line L perpendicular to the x-axis such that S is cut into two equal sized subsets. Solving the closest pair problems in S L and S R respectively, we shall obtain d L and d R where d L and d R denote the distances of the closest pairs in S L and S R respectively. Let d = min(d L, d R ). If the closest pair (P a „ P b ) of S consists of a point in S L and a point in S R, then P a and P b must lie within a slab centered at line L and bounded by lines L-d and L+d. Merge Step: examine points in slab.

4 4 -132 Points in Slab During the merging step, we may examine only points in the slab. Although in average, the number of points within the slab may not be too large, in the worst case, there can be as many as n points within the slab. Thus the brute-force way to find the closest pair in the slab needs calculating n 2 /4 distances and comparisons. This kind of merging step will not be good for our divide-and-conquer algorithm. Fortunately, as will be shown in the following, the merging step can be accomplished in 0(n) time. (how?)

4 4 -133 Merge Step If a point P in S L and a point Q in S R constitute a closest pair, the distance between P and Q must be less than d. Hence we do not have to consider a point too far away from P. Consider Figure 4-5. We only have to examine the shaded area in Figure 4-5. If P is exactly on line L, this shaded area will be the largest. Even in this case, this shaded area will be contained in the d  2d rectangle A as shown in Figure 5-6. Thus we only have to examine points within this rectangle A.

4 4 -134 Merge Step For each point P in the slab, we only have to examine limited number of points in the other half of the slab. Without losing generality, we may assume that P is within the left-half of the slab. Let the y-value of P be denoted as y p. For P, we only have to examine points in the other half of the slab whose y-values are within y p +d and y p -d. There will be at most six such points as discussed above (why?). O(n) Step

4 4 -135 at most 6 points in area A: d/2 One box contains one point. If s,s’  S have the property that d(s,s’)<d, then s and s’ are within 15 positions of each other in the sorted list Sy. Sort points by x-values and sort points by y-values. L Box

4 4 -136 The 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 one points, return infinity( ∞) as their distance. Step 3: Find a median line L perpendicular to the X- axis to divide S into two subsets, with equal sizes, S L and S R. Step 4: Recursively apply Step 2 and Step 3 to solve the closest pair problems of S L and S R. Let d L (d R ) denote the distance between the closest pair in S L (S R ). Let d = min(d L, d R ).

4 4 -137 Step 5: For a point P in the half-slab bounded by L-d and L, let its y-value by denoted as y P. For each such P, find all points in the half-slab bounded by L and L+d whose y-value fall within y P +d and y P - 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. Time complexity: O(n log n) Step 1: O(n log n) Steps 2~5:  T(n) = O(n log n)

4 4 -138 4.3 The convex hull problem The convex hull of a set of planar points is the smallest convex polygon containing all of the points. concave polygon:convex polygon:

Convex Hull 139 Convex Polygon A convex polygon is a nonintersecting polygon whose internal angles are all convex (i.e., less than  ) In a convex polygon, a segment joining two vertices of the polygon lies entirely inside the polygon The convex hull of a set of points is the smallest convex polygon containing the points Think of a rubber band snapping around the points convexnonconvex

Convex Hull 140 Special Cases The convex hull is a segment Two points All the points are collinear The convex hull is a point there is one point All the points are coincident

Convex Hull 141 Applications Motion planning Find an optimal route that avoids obstacles for a robot Geometric algorithms Convex hull is like a two-dimensional sorting obstacle start end

Convex Hull 142 Computing the Convex Hull The following method computes the convex hull of a set of points Phase 1: Find the lowest point (anchor point) Phase 2: Form a nonintersecting polygon by sorting the points counterclockwise around the anchor point Phase 3: While the polygon has a nonconvex vertex, remove it

The orientation of a triplet (p 1, p 2, p 3 ) of points in the plane is counterclockwise, clockwise, or collinear, depending on whether,  (p 1,p 2,p 3 ) is positive, negative, or zero, respectively. 4 -143

Convex Hull 144 Orientation The orientation of three points in the plane is clockwise, counterclockwise, or collinear orientation(a, b, c) clockwise (CW, right turn) counterclockwise (CCW, left turn) collinear (COLL, no turn) The orientation of three points is characterized by the sign of the determinant  (a, b, c), whose absolute value is twice the area of the triangle with vertices a, b and c a b c a c b c b a CW CCW COLL

4 4 -145

Convex Hull 146 Sorting by Angle Computing angles from coordinates is complex and leads to numerical inaccuracy We can sort a set of points by angle with respect to the anchor point a using a comparator based on the orientation function b  c  orientation(a, b, c)  CCW b  c  orientation(a, b, c)  COLL b  c  orientation(a, b, c)  CW a b c CCW a c b CW a b c COLL

Convex Hull 147 Removing Nonconvex Vertices Testing whether a vertex is convex can be done using the orientation function Let p, q and r be three consecutive vertices of a polygon, in counterclockwise order q convex  orientation(p, q, r)  CCW q nonconvex  orientation(p, q, r)  CW or COLL p q r q r p

Gift Wrapping Algorithm We can identify a particular point, say one with minimum y-coordinate, that provides an initial starting configuration for an algorithm that computes the convex hull. The gift wrapping algorithm for computing the convex hull of a set of points in the plane is based on just such a starting point. 4 -148

Gift Wrapping 1.View the points as pegs implanted in a level field, and imagine that we tie a rope to the peg corresponding to the point a with minimum y-coordinate (and minimum x- coordinate if there are ties). Call a the anchor point, and note that a is a vertex of the convex hull. 2. Pull the rope to the right of the anchor point and rotate it counterclockwise until it touches another peg, which corresponds to the next vertex of the convex hull. 3. Continue rotating the rope counterclockwise, identifying a new vertex of the convex hull at each step, until the rope gets back to the anchor point. 4 4 -149

4 4 -150

Graham Scan Algorithm 4 4 -151

Graham Scan 4 4 -152

4 4 -153

4 4 -154

Convex Hull 155 Graham Scan The Graham scan is a systematic procedure for removing nonconvex vertices from a polygon The polygon is traversed counterclockwise and a sequence H of vertices is maintained for each vertex r of the polygon Let q and p be the last and second last vertex of H while orientation(p, q, r)  CW or COLL remove q from H q  p p  vertex preceding p in H Add r to the end of H p q r H p q r H p q r H

Convex Hull 156 Analysis Computing the convex hull of a set of points takes O(n log n) time Finding the anchor point takes O(n) time Sorting the points counterclockwise around the anchor point takes O(n log n) time Use the orientation comparator and any sorting algorithm that runs in O(n log n) time (e.g., heap-sort or merge-sort) The Graham scan takes O(n) time Each point is inserted once in sequence H Each vertex is removed at most once from sequence H

4 4 -157 The divide-and-conquer strategy to solve the problem:

4 4 -158 convex hull problem To find a convex hull, we may use the divide-and- conquer. The set of planar points hav into two subsets S L and S R by a line perpendicular to the x-axis. Convex hulls for S L and S R are now constructed and they are denoted as Hull(S l ),Hull(S r ) respectively. To combine Hull(SL) and Hull(SR) into one convey use the Graham scan.

4 4 -159 Graham scan An interior point of Hull(S l ) is selected. Consider the point as the origin. Then each other point forms a polar angle with interior point. All of the points are now sorted with respect to these polar angle. The Graham scan examines the points one by one and eliminates the points which cause reflexive angles, as illustrated in Figure 4-10.

4 4 -160 Reflexive angle

4 4 -161 e.g. points b and f need to be deleted. Final result: reflexive angles

4 4 -162 The merging procedure: 1.Select an interior point p. 2.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 3.Merge these 3 sequences into 1 sequence: g, h, a, b, f, c, e, d, i, j, k. 4.Apply Graham scan to examine the points one by one and eliminate the points which cause reflexive angles.

4 4 -163 The divide-and-conquer strategy to solve the problem:

4 4 -164 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 S L and S R ; S L lies to the left of S R. Step 3: Recursively construct convex hulls for S L and S R. Denote these convex hulls by Hull(S L ) and Hull(S R ) respectively.

4 4 -165 Step 4: Apply the merging procedure to merge Hull(S L ) and Hull(S R ) together to form a convex hull. Time complexity: T(n) = 2T(n/2) + O(n) = O(n log n)

4 4 -166 4.4 The Voronoi diagram problem e.g. The Voronoi diagram for two & three points Each L ij is the perpendicular bisector of the line.

4 4 -167 Definition of Voronoi diagrams Def : Given two points P i, P j  S, let H(P i,P j ) denote the half plane containing P i. The Voronoi polygon associated with P i is defined as pipi

4 4 -168 Given a set of n points, the Voronoi diagram consists of all the Voronoi polygons of these points. The vertices of the Voronoi diagram are called Voronoi points and its segments are called Voronoi edges.

4 4 -169 Delaunay triangulation The straight line dual of a Voronoi diagram is called the Delaunay triangulation, in honor of a famous French mathematician. There is a line segment connecting P i and P j in a Delaunay triangulation if and only if the Voronoi polygons of P i and P j share the same edge.

4 4 -170 Application of Voronoi Diagram Voronoi diagrams are very useful for many purposes: We can solve the so called all closest pairs problem by extracting information from the Voronoi diagram. A minimal spanning tree can also be found from the Voronoi diagram.

4 4 -171 Example for constructing Voronoi diagrams Divide the points into two parts.

4 4 -172 Merging two Voronoi diagrams Merging along the piecewise linear hyperplane HP

4 4 -173 Property of HP If a point P is within the left(right) side of HP, the nearest neighbor of P must be a point in S L (S R ). After discarding all of VD(SL) to the right of HP and all of VD(SR) to the left of HP, we obtain the resulting Voronoi diagram as shown in Figure 5-19.

4 4 -174 After merging The final Voronoi diagram

4 4 -175 Divide-and-conquer for Voronoi diagram Input: A set S of n planar points. Output: The Voronoi diagram of S. Step 1: If S contains only one point, return. Step 2: Find a median line L perpendicular to the X-axis which divides S into S L and S R such that S L (S R ) lies to the left(right) of L and the sizes of S L and S R are equal.

4 4 -176 Step 3: Construct Voronoi diagrams of S L and S R recursively. Denote these Voronoi diagrams by VD(S L ) and VD(S R ). Step 4: Construct a dividing piece-wise linear hyperplane HP which is the locus of points simultaneously closest to a point in S L and a point in S R. Discard all segments of VD(S L ) which lie to the right of HP and all segments of VD(S R ) that lie to the left of HP. The resulting graph is the Voronoi diagram of S. (See details on the next page.)

4 4 -177 Mergeing Two Voronoi Diagrams into One Voronoi Diagram Input: (a) S L and S R where S L and S R are divided by a perpendicular line L. (b) VD(S L ) and VD(S R ). Output: VD(S) where S = S L ∩ S R Find the convex hulls of S L and S R Step 1: Find the convex hulls of S L and S R, denoted as Hull(S L ) and Hull(S R ), respectively. (A special algorithm for finding a convex hull in this case will by given later.)

4 4 -178 Step 2: Find segments and which join HULL(S L ) and HULL(S R ) into a convex hull (P a and P c belong to S L and P b and P d belong to S R ) Assume that lies above. Let x = a, y = b, SG= and HP = . Step 3: Find the perpendicular bisector of SG. Denote it by BS. Let HP = HP ∪ {BS}. If SG =, go to Step 5; otherwise, go to Step 4.

4 4 -179 Step 4: The ray from VD(S L ) and VD(S R ) which BS first intersects with must be a perpendicular bisector of either or for some z. If this ray is the perpendicular bisector of, then let SG = ; otherwise, let SG =. Go to Step 3. Step 5: Discard the edges of VD(S L ) which extend to the right of HP and discard the edges of VD(S R ) which extend to the left of HP. The resulting graph is the Voronoi diagram of S = S L ∪ S R.

4 4 -180 Properties of Voronoi Diagrams Def : Given a point P and a set S of points, the distance between P and S is the distance between P and P i which is the nearest neighbor of P in S. The HP obtained from the above algorithm is the locus of points which keep equal distances to S L and S R. The HP is monotonic in y.

4 4 -181 The relationship between a horizontal line H and S L and S R. Each horizontal line H intersects with HP at one and only on point.

4 4 -182 The HP is monotonic in y. horizontal line (1) a, c belong to S L and b belong to S R (2) a, c belong to S R and b belong to S L

4 4 -183 # of edges of a Voronoi diagram  3n - 6, where n is # of points. Reasoning: i.# of edges of a planar graph with n vertices  3n - 6. ii.A Delaunay triangulation is a planar graph. iii.Edges in Delaunay triangulation edges in Voronoi diagram. # of Voronoi edges Voronoi vertices Voronoi edge Corollary: If G is a connected planar simple graph with E edges and V vertices where V ≧ 3, then E ≦ 3V-6.

4 4 -184 # of Voronoi vertices # of Voronoi vertices  2n – 4 (upper bound). Reasoning : i.Let F, E and V denote # of face(region), edges and vertices in a planar graph. Euler ’ s relation: F = E - V + 2. ii.In a Delaunay triangulation, V = n, E  3n – 6  F = E - V + 2  3n - 6 - n + 2 = 2n - 4. Voronoi vertices Reference: Rosen pp. 606~607.

4 4 -185 Construct a convex hull from a Voronoi diagram After a Voronoi diagram is constructed, a convex hull can by found in O(n) time. infinite rays Connecting the points associated with the infinite rays.

4 4 -186 Construct Convex Hull from Voronoi diagram Step 1: Find an infinite ray by examining all Voronoi edges. O(n) Step 2: Let P i be the point to the left of the infinite ray. P i is a convex hull vertex. Examine the Voronoi polygon of P i to find the next infinite ray. Step 3: Repeat Step 2 until we return to the Starting ray.

4 4 -187 Time complexity Time complexity for merging 2 Voronoi diagrams: Total: O(n) Step 1: O(n) Step 2: O(n) Step 3 ~ Step 5: O(n) (at most 3n - 6 edges in VD(S L ) and VD(S R ) and at most n segments in HP) Time complexity for constructing a Voronoi diagram: O(n log n) because T(n) = 2T(n/2) + O(n)=O(n log n)

4 4 -188 Finding lower bound by problem transformation Problem A reduces to problem B (A  B) iff A can be solved by using any algorithm which solves B. If A  B, B is more difficult. Note: T(tr1) + T(tr2) < T(B) T(A)  T(tr1) + T(tr2) + T(B)  O(T(B))

4 4 -189 Lower bound of the Voronoi diagram Let us consider a set of points on a straight line. The Voronoi diagram of such a set of points consists of a set of bisecting lines as shown in Figure 5-26. After these lines have been constructed, a linear scanning of these Voronoi edges will accomplish the function of sorting. In other words, the Voronoi diagram problem can not be easier than the sorting problem. A lower bound of the Voronoi diagram problem is therefore Q(nlogn) and the algorithm is consequently optimal.

4 4 -190 Lower bound The lower bound of the Voronoi diagram problem is  (n log n). sorting  Voronoi diagram problem The Voronoi diagram for a set of points on a straight line

4 4 -191 5.5 Applications of Voronoi diagrams The Euclidean nearest neighbor searching problem. The Euclidean all nearest neighbor problem.

4 4 -192 The Euclidean nearest neighbor searching problem. The Euclidean nearest neighbor searching problem is defined as follows: We are given a set of n planar points: P 1, P 2, …,P n, and a testing point P. Our problem is to find a nearest neighbor of P among P i ’s and the distance used is the Euclidean distance. A straightforward method is to conduct an exhaustive search. This algorithm would be an O(n) algorithm. Using the Voronoi diagram, we can reduce the searching time to O(logn) with preprocessing time O(nlogn).

4 4 -193 Note that the Voronoi diagram divides the entire plane into regions R 1, R 2 R n. Within each region Ri, there is a point P i. If a testing point falls within region R i, then its nearest neighbor, among all points, is Pi. Therefore, we may avoid an exhaustive search by simply transforming the problem into a region location problem. That is, if we can determine which region R i a testing point is located, we can determine a nearest neighbor of this testing point.

4 4 -194 A Voronoi diagram is a planar graph. Our first step is to sort these Voronoi vertices according to their y-values. The Voronoi vertices are labeled V 1, V 2,…,V 6 according to their decreasing y-values. For each Voronoi vertex, a horizontal line is drawn passing this vertex. These horizontal lines divide the entire space into slabs.

4 4 -195 slab region R2R2 R3R3 R4R4 R6R6 R1R1

4 4 -196 Euclidean nearest neighbor searching algorithm (1) Conduct a binary search to determine which slab this testing point is located. Since there are at most 0(n) Voronoi vertices, this can be done in O(logn) time. (2) Within each slab, conduct a binary search to determine which region this point is located in. Since there are at most 0(n) Voronoi edges, this can be done in O(logn) time. The total searching time is O(logn). It is easy to see that the preprocessing time is O(nlogn), essentially the time needed to construct the Voronoi diagram.

4 4 -197 The Euclidean all nearest neighbor problem. We are given a set of n planar points P 1, P 2,..., P n. The Euclidean closest pair problem is to find a nearest neighbor of every P i. Properties: If P j is a nearest neighbor of P i, then P i and P j share the same Voronoi edge. Moreover, the midpoint of segment P i P j is located exactly on this commonly shared Voronoi edge.

4 4 -198 Proof We shall show this property by contradiction. Suppose that P i and P j do not share the same Voronoi edge. By the definition of Voronoi polygons, the perpendicular bisector of P i P j must be outside of the Voronoi polygon associated with P i. Let P,Pi intersect the bisector at M and some Voronoi edge at N, as illustrated in Figure 5-28.

4 4 -199 Euclidean all nearest neighbor problem Given the above property, the Euclidean all nearest neighbor problem can be solved by examining every Voronoi edge of each Voronoi polygon. Since each Voronoi edge is shared by exactly two Voronoi polygons, no Voronoi edge is examined more than twice. That is, this Euclidean all nearest neighbor problem can be solved in linear time after the Voronoi diagram is constructed. Thus this problem can be solved in O(nlogn) time.

4 4 -200

4 4 -201 Fast Fourier transform (FFT) Fourier transform A(f) = a(t)e 2  ift dt Inverse Fourier transform a(t) = A(f)e -2  ift df Discrete Fourier transform(DFT) Given a 0, a 1, …, a n-1 A j = a k e 2  ijk/n,0  j  n-1 Inverse DFT a k = A j e -2  ijk/n,0  k  n-1

4 4 -202 DFT and waveform Any periodic waveform can be decomposed into the linear sum of sinusoid functions (sine or cosine). See the left part:

4 4 -203 An application of the FFT  polynomial multiplication Polynomial multiplication: The straightforward product requires O(n 2 ) time. DFT notations:

4 4 -204 Fast polynomial multiplication Step 1: Let N be the smallest integer that N=2 q and N  2n-1. Step 2: Compute FFT of Step 3: Compute FFT of Step 4: Step 5: Time complexity: O(NlogN)=O(nlogn), N<4n.

4 4 -205 FFT algorithm A straightforward method to calculate DFT requires O(n 2 ) time. DFT can be solved by the divide-and-conquer strategy (FFT). Let w=e 2  i/n. i.e. w n =1, w n/2 =-1. A j = a k e 2  ijk/n, 0  j  n-1 = a k w jk e.g. n = 8, let e 2  i/8 = w= e  i/4

4 4 -206 FFT algorithm when n=4 n =4, let e 2  i/4 =w = e  i/2 (w 4 = 1, w 2 = -1) A 0 = a 0 + a 1 + a 2 + a 3 A 1 = a 0 + a 1 w + a 2 w 2 + a 3 w 3 A 2 = a 0 + a 1 w 2 + a 2 w (2)(2) + a 3 w (2)(3) = a 0 + a 1 w 2 + a 2 w 4 + a 3 w 6 A 3 = a 0 + a 1 w 3 + a 2 w (3)(2) + a 3 w (3)(3) = a 0 + a 1 w 3 + a 2 w 6 + a 3 w 9

4 4 -207 Rewrite as: A 0 = a 0 + a 2 + (a 1 + a 3 ) A 2 = a 0 + a 2 w 4 + (a 1 w 2 + a 3 w 6 ) = a 0 + a 2 - (a 1 + a 3 ) When we calculate A 0, we shall calculate (a 0 + a 2 ) and (a 1 + a 3 ). Later, A 2 can be easily found. Similarly, A 1 = (a 0 + a 2 w 2 ) + a 1 w + a 3 w 3 A 3 = (a 0 + a 2 w 6 ) + (a 1 w 3 + a 3 w 9 ) = (a 0 + a 2 w 2 ) - (a 1 w + a 3 w 3 )

4 4 -208 n = 8, let e 2  i/8 = w = e  i/4 (w 8 = 1, w 4 = -1) A 0 = a 0 + a 1 + a 2 + a 3 + a 4 + a 5 + a 6 + a 7 A 1 = a 0 +a 1 w+a 2 w 2 +a 3 w 3 +a 4 w 4 +a 5 w 5 +a 6 w 6 +a 7 w 7 A 2 = a 0 +a 1 w 2 +a 2 w 4 +a 3 w 6 +a 4 w 8 +a 5 w 10 +a 6 w 12 +a 7 w 14 A 3 = a 0 +a 1 w 3 +a 2 w 6 +a 3 w 9 +a 4 w 12 +a 5 w 15 +a 6 w 18 +a 7 w 21 A 4 = a 0 +a 1 w 4 +a 2 w 8 +a 3 w 12 +a 4 w 16 +a 5 w 20 +a 6 w 24 +a 7 w 28 A 5 = a 0 +a 1 w 5 +a 2 w 10 +a 3 w 15 +a 4 w 20 +a 5 w 25 +a 6 w 30 +a 7 w 35 A 6 = a 0 +a 1 w 6 +a 2 w 12 +a 3 w 18 +a 4 w 24 +a 5 w 30 +a 6 w 36 +a 7 w 42 A 7 = a 0 +a 1 w 7 +a 2 w 14 +a 3 w 21 +a 4 w 28 +a 5 w 35 +a 6 w 42 +a 7 w 49 FFT algorithm when n=8

4 4 -209 After reordering, we have A 0 =(a 0 +a 2 +a 4 +a 6 )+(a 1 +a 3 +a 5 +a 7 ) A 4 =(a 0 +a 2 +a 4 +a 6 )-(a 1 +a 3 +a 5 +a 7 ) A 1 =(a 0 +a 2 w 2 +a 4 w 4 +a 6 w 6 )+w(a 1 +a 3 w 2 +a 5 w 4 +a 7 w 6 ) A 5 =(a 0 +a 2 w 2 +a 4 w 4 +a 6 w 6 )-w(a 1 +a 3 w 2 +a 5 w 4 +a 7 w 6 ) A 2 =(a 0 +a 2 w 4 +a 4 w 8 +a 6 w 12 )+w 2 (a 1 +a 3 w 4 +a 5 w 8 +a 7 w 12 ) A 6 =(a 0 +a 2 w 4 +a 4 w 8 +a 6 w 12 )-w 2 (a 1 +a 3 w 4 +a 5 w 8 +a 7 w 12 ) A 3 =(a 0 +a 2 w 6 +a 4 w 12 +a 6 w 18 )+w 3 (a 1 +a 3 w 6 +a 5 w 9 +a 7 w 18 ) A 7 =(a 0 +a 2 w 6 +a 4 w 12 +a 6 w 18 )-w 3 (a 1 +a 3 w 6 +a 5 w 9 +a 7 w 18 ) Rewrite as: A 0 = B 0 + C 0 A 4 = B 0 － C 0 A 1 = B 1 + C 1 A 5 = B 1 － C 1 A 2 = B 2 + C 2 A 6 = B 2 － C 2 A 3 = B 3 + C 3 A 7 = B 3 － C 3

4 4 -210 let x=w 2 =e 2  i/4 = e  i/2 (x 4 = 1, x 2 = -1) We can recursively apply the same method to calculate B i ’ s and C i ’ s. B 0 = a 0 +a 2 +a 4 +a 6 B 1 = a 0 +a 2 w 2 +a 4 w 4 +a 6 w 6 = a 0 +a 2 x+a 4 x 2 +a 6 x 3 B 2 = a 0 +a 2 w 4 +a 4 w 8 +a 6 w 12 = a 0 +a 2 x 2 +a 4 x 4 +a 6 x 6 B 3 = a 0 +a 2 w 6 +a 4 w 12 +a 6 w 18 = a 0 +a 2 x 3 +a 4 x 6 +a 6 x 9 Thus, {B 0, B 1, B 2, B 3 } is the DFT of {a 0, a 1, a 2, a 3 }.

4 4 -211 General FFT In general, let w = e 2  i/n (assume n is even) (w n = 1, w n/2 = -1) A j = a 0 +a 1 w j +a 2 w 2j + … +a n-1 w (n-1)j ={a 0 +a 2 w 2j +a 4 w 4j + … +a n-2 w (n-2)j } + {a 1 w j +a 3 w 3j + … +a n-1 w (n-1)j } = B j + C j A j+n/2 = a 0 +a 1 w j+n/2 +a 2 w 2j+n +a 3 w 3j+3n/2 + … + a n-1 w (n-1)j+(n(n-1)/2) =a 0 -a 1 w j +a 2 w 2j -a 3 w 3j + … +a n-2 w (n-2)j -a n-1 w (n-1)j = B j - C j

4 4 -212 Divide-and-conquer (FFT) Input : a 0, a 1, …, a n-1, n = 2 k Output : A j, j=0, 1, 2, …, n-1, where A j =  0  k  n-1 a k e 2  ijk/n Step 1: If n=2, compute A 0 = a 0 + a 1, A 1 = a 0 - a 1, and return. Step 2: Divide each a j, 0  j  n/2 - 1 into two sequences: O j and E j, where O j (E j ), consists of odd-numbered (even-numbered) terms of A j.

4 4 -213 Step 3: Recursively calculate the sums of terms in O j and E j. Denote the sum of terms of O j and E j by B j and C j, respectively. Step 4: Compute A j by the following formual : A j = B j + C j for 0  j  n/2 - 1 A j +n/2 = B j - C j for 0  j  n/2 - 1. Time complexity : T(n) = 2T(n/2) + O(n) = O(n log n)

4 4 -214 5.8 Matrix multiplication Let A, B and C be n  n matrices C = AB C(i, j) = A(i, k)B(k, j) The straightforward method to perform a matrix multiplication requires O(n 3 ) time.

4 4 -215 Divide-and-conquer approach C = AB C 11 = A 11 B 11 + A 12 B 21 C 12 = A 11 B 12 + A 12 B 22 C 21 = A 21 B 11 + A 22 B 21 C 22 = A 21 B 12 + A 22 B 22 Time complexity: (# of additions : n 2 ) We get T(n) = O(n 3 )

4 4 -216 P = (A 11 + A 22 )(B 11 + B 22 ) Q = (A 21 + A 22 )B 11 R = A 11 (B 12 - B 22 ) S = A 22 (B 21 - B 11 ) T = (A 11 + A 12 )B 22 U = (A 21 - A 11 )(B 11 + B 12 ) V = (A 12 - A 22 )(B 21 + B 22 ). C 11 = P + S - T + V C 12 = R + T C 21 = Q + S C 22 = P + R - Q + U Strassen’s matrix multiplicaiton

4 4 -217 Time complexity 7 multiplications and 18 additions or subtractions Time complexity: T(n) = an 2 + 7T(n/2) = an 2 + 7(a(n/2) 2 + 7T(n/4)) = an 2 + (7/4)an 2 + 7 2 T(n/4) = … : = an 2 (1 + 7/4 + (7/4) 2 +…+(7/4) k-1 +7 k T(1))  cn 2 (7/4) log2n +7 log2n, c is a constant = cn log24+log27-log24 +n log27 = O(n log27 )  O(n 2.81 )

Download ppt "4 -1 Chapter 4 The Divide-and-Conquer Strategy. 4 -2 Outlines 4-1 The 2-Dimensional Maxima Finding Problem 4-2 The Closest Pair Problem 4-3 The Convex."

Similar presentations