1 / 86

The Divide-and-Conquer Strategy

The Divide-and-Conquer Strategy. Chapter 4. 4-0 The Divide-and-Conquer Strategy. Recall: Binary searching: (Top-Down splitting) E.g. find the maximum of a set S of n numbers (Button-Up merging) => T(n)= 2T(n/2)+1. 4-0 The Divide-and-Conquer Strategy. Time complexity: Assume n = 2 k ,

wallacer
Download Presentation

The Divide-and-Conquer Strategy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Divide-and-Conquer Strategy Chapter 4

  2. 4-0 The Divide-and-Conquer Strategy • Recall: Binary searching: (Top-Down splitting) • E.g. find the maximum of a set S of n numbers (Button-Up merging) => T(n)= 2T(n/2)+1

  3. 4-0 The Divide-and-Conquer Strategy • Time complexity: • Assume n = 2k, T(n) = 2T(n/2)+1 = 2(2T(n/4)+1)+1 = 4T(n/4)+2+1 : =2k-1T(2)+2k-2+…+4+2+1 =2k-1+2k-2+…+4+2+1 =2k-1 = n-1

  4. 4-0 A general divide-and-conquer algorithm • Method : • Divide: Partition the problem into “independent” subproblems recursively until the subproblem’s size is small enough. • Conquer : solve the independent “small” subproblems. • Combine : merge the subsolutions into a solution.

  5. 4-0 A general divide-and-conquer algorithm • General Structure : (Recursive structure) • S(P) : solution of P. • small( P ) : decide P small or not . Algorithm D_and_C( P ) if small(P) then return S(P)(Be careful !!!) else divideP into small subproblem P1, P2, …, Pk , k≧1. D_ and_C( Pi ), for k ≧ i ≧ 1. return Combine ( S( P1 ) , S( P2 ), …, S( Pk ) ) End D_and_C.

  6. 4-0 A general divide-and-conquer algorithm • Time Complexity T(n) : g(n) nc T(n) = T(n1)+T(n2)+…+T(nk)+f(n) , otherwise f(n) : time for partitioning and combining. Usually: T(1) n = 1 T(n) = aT(n/b) +f(n)n > 1 • Small problem : 1 element • Divide into “a” subproblems with size “n/b”.

  7. 4-0 A general divide-and-conquer algorithm • Time complexity: where S(n) : time for splitting/partitioning M(n) : time for merging/Combining b : a constant c : a constant • E.g. quick sort • E.g. merge sort

  8. 4-0 A general divide-and-conquer algorithm • Substitution Method to solve T(n): • Example: T(1) n = 1 T(n) = 2T(n/2) +nn > 1

  9. 4-0 Techniques for Solving Recurrence (Conti.) • Theorem (General substitution method) Assume that a,b,c  0 and k is a constant. b for n=1 T(n) = a T( n/c ) + b * nk for n >1 O (nk ) if a < ck Then, T (n) = O ( nk log n ) if a = ck O ( nlogc a ) if a > ck

  10. General substitution method

  11. General substitution method (cont.)

  12. General substitution method (cont.) • Note: Checking logca is > = < k • O (nk) if a < ck • T (n) = O ( nk logc n ) if a = ck (see equation (1)) • O ( nlogc a ) if a > ck

  13. 4-0 Techniques for Solving Recurrence (Conti.) • Example: T1(n) = 4 T1(n/2)+3n and T1(1) = c T1 = O( n2) • Example: T2(n) = 2 T2(n/4) + 3n and T2 (1) = c • T2 = O( n) • Hanoi Tower problem T(n) = 2 T(n-1) + 1 and T2 (1) = 1 • T(n)= 2n-1=O( 2n)

  14. 4-0 Techniques for Solving Recurrence (Conti.) n =1 Let • Transformation :

  15. 4-0 Techniques for Solving Recurrence (Conti.) n =2 • Transformation :

  16. 4-0 Techniques for Solving Recurrence (Conti.) • Transformation :

  17. 4-0 Techniques for Solving Recurrence (Conti.) • Exercise 4.6 find T(n)

  18. 4-0 Techniques for Solving Recurrence (Conti.)

  19. 4-0 Techniques for Solving Recurrence (Conti.) • Recursion Tree : • Example: T(n)= 1 n=1 2T(n/2)+ n2 n>1 • Cost n2 to divide T(n) into 2 subproblems with size n/2 T(n) T(n/2) T(n/2) T(n/4) T(n/4) T(n/4) T(n/4)

  20. 4-0 Techniques for Solving Recurrence (Conti.) • Recursion Tree (Conti.): • n2 • (n/2)2 (n/2)2 • (n/4)2 (n/4)2 (n/4)2 (n/4)2 n2 1/2 n2 1/4 n2 ……………………………………. …………

  21. 4-0 Techniques for Solving Recurrence (Conti.) • Recursion Tree (Conti.):

  22. 4-1 2-D maxima finding problem • Def : A point (x1, y1) dominates (x2, y2) if x1 > x2 and y1 > y2. A point is called a maximaif no other point dominates it. • Straightforward method : Compare every pair of points • Time complexity: O(n2) • Applications: • Thinking (Image….)

  23. 4-1 2-D maxima finding problem • Divide-and-conquer method: The maximal points of SL and SR

  24. 4-1 Divide-and-conquer approach • 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.

  25. 4-1 Divide-and-conquer approach • Step 2 Recursively find the maximal points of SL and SR. • Step 3 Project the maximal points of SL and SR onto 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 SL if its y-value is less than the y-value of some maximal point of SR. This step (O(nlogn)) can be modified

  26. 4-1 Divide-and-conquer approach • Time complexity: • Assume n = 2k (Try it ) T(n) = O(n log n) + O(n log2n) = O(n log2n) • (Note: log(n/2)=logn-log2)

  27. 4-1 Divide-and-conquer approach • Improvement: • (1) Step 3: Find the largest y-value of SR. Time complexity: T(n) = O(n log n)

  28. 4-1 Divide-and-conquer approach • (2) The sorting of y-values need be done only once (only one presorting). • No sorting is needed in Step3. Time complexity: O(n log n) + T(n) = O(n log n) where

  29. 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 : • Stupid method: compute distances of all pairs, and select the smallest pair. • Time complexity : O(n2 ) • Solved by sorting, then compute the distance between two sequential points. • Time complexity : O(n log n) • 2-D version: This section presents this version. • 3-D version: Thinking

  30. 4-2 The closest pair problem • 2-D version: Be careful ! Why?: at most 6 pointsin A:

  31. 4-2 Divide-and-conquer approach • Input : A set S of n points in the plane. • Output: The distance between two closest points. • Step 1Sortpoints in S according to their y-values and x-values. • Step 2 If S contains only two points, return their distance.

  32. 4-2 Divide-and-conquer approach • Step 3 Find a median line L perpendicular to the X-axis to divide S into two subsets, with equal sizes, SL and SR. Every point in SL lies to the left of L and every point in SR lies to the right of L. • 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).

  33. 4-2 Divide-and-conquer approach • Step 5 Project all points within the slab bounded by L-d and L+d onto the line L. 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. Therefore, the merge step requires only O(n) time.

  34. 4-2 Divide-and-conquer approach • Time complexity: O(n log n) • Step 1: O(n log n): Sorting n points • Steps 2~5: T(n) = O(n log n)

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

  36. 4-3 The convex hull problem • A polygon is defined to be convex if for any two points p1 and p2 inside the polygon, the directed line segment from p1 to p2 (<p1, p2>) is fully contained in the polygon.

  37. 4-3 The convex hull problem • Stupid method-1:Maybe stupid or excellent • 有個很簡單的演算法,就是對於每個點都去檢查是否在某三個點所形成的三角形裡面,若是的話,它就不會是 convex hull 上的點,不然就是 convex hull 上的點。 • 只是每個點都需要檢查 O(n3) 次,共有 n個點,所 complexity 是 O(n4)。 • Stupid method-2: O(n2)。 • 1. Find the lowest point (the base-point). • 2. Compute all angles between two lines which includes • One line: this base-point and the previous point. • One of many lines: this base-point and other points. • 3.Determine the next point (find the smallest value) • 4. Let this point be the base-point and goto Step2.

  38. 4-3 The convex hull problem • The divide-and-conquer strategy to solve the problem:

  39. 4-3 The convex hull problem • 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 (points in SL , first finding lowest point) (2) a, b, c, d (points in SR , finding a and b points) (3) f, e (points in SR , first finding lowest point) • Merge these 3 sequences into 1 sequence: (Merge sort) g, h, a, b, f, c, e, d, i, j, k. (Time complexity O(n)) Note: Step 2 and 3 may be replaced with the following: Compute polar angles of all points in SL and SR and sort them. => (Time complexityO(nlogn))

  40. 4-3 The convex hull problem • Polar angles

  41. 4-3 The convex hull problem • Apply Graham scan to examine the points one by one and eliminate the points which cause reflexive angles. For example: points b and f need to be deleted.

  42. 4-3 The convex hull problem Final result:

  43. 4-3 Divide-and-Conquer approach • 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 . (O(n))

  44. 4-3 Divide-and-Conquer approach • Step 3 Recursively construct convex hulls for SL and SR. Denote these convex hulls by Hull(SL) and Hull(SR) respectively. • Step 4 Find an interior point P of SL. Find the vertices v1 and v2 of Hull(SR) which divide the vertices of Hull(SR) into two sequences of vertices which have increasing polar angles with respect to P. Without loss of generality, let us assume that v1 has greater y-value than v2 . Then form three sequences as follows:

  45. 4-3 Divide-and-Conquer approach • Sequence 1: all of the convex hull vertices of Hull(SL) in counterclockwise direction. • Sequence 2: the convex hull vertices of Hull(SR) from v2 to v1 in counter-clockwise direction. • Sequence 3: the convex hull vertices of Hull(SR) from v2 to v1 in clockwise direction. Note: v1 has greater y-value than v2

  46. 4-3 Divide-and-Conquer approach • Merge these three sequences and conduct the Graham scan. Eliminate the points which are reflexive and the remaining points from the convex hull. • Time complexity: T(n) = 2T(n/2) + O(n) = O(n log n)

  47. 4-4 The Voronoi diagram problem (Option) • Def : Given two points Pi, Pj S, let H(Pi,Pj) denote the half planecontaining Pi. The Voronoi polygon associated with Pi is defined as Pj Pi H(Pj ,Pi) H(Pi , Pj)

  48. 4-4 The Voronoi diagram problem • E.g. The Voronoi diagram for three points V(P1)=H(p1,p2) ∩ H(p1,p3) V(P2)=H(p2,p1) ∩ H(p2,p3) V(P3)=H(p3,p1) ∩ H(p3,p2) Each Lij is perpendicular bisector of the line

  49. 4-4 The Voronoi diagram problem • E.g. A Voronoi diagram of 6 points: • The vertices of the Voronoi diagram are called Voronoi points and its segments are called Voronoi edges. • Applications: (Known Voronoi edges in advance) • Easy to know “Nearest neighbors” of points on a plane

  50. 4-4 The Voronoi diagram problem • E.g. A Delaunay triangulation: Each Lij is perpendicular bisector of the line Remark: Three perpendicular bisectors of a triangle must intersect on a point.

More Related