Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer

2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Definition –Recursion lends itself to a general problem-solving technique (algorithm design) called divide & conquer Divide the problem into 1 or more similar sub-problems Conquer each sub-problem, usually using a recursive call Combine the results from each sub-problem to form a solution to the original problem –Algorithmic Pattern: DC( problem ) solution =  if ( problem is small enough ) solution = problem.solve() else children = problem.divide() for each c in children solution = solution + c.solve() return solution Divide Conquer Combine

3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Applicability –Use the divide-and-conquer algorithmic pattern when ALL of the following are true: The problem lends itself to division into sub-problems of the same type The sub-problems are relatively independent of one another (ie, no overlap in effort) An acceptable solution to the problem can be constructed from acceptable solutions to sub-problems

4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Well-Known Uses –Searching Binary search –Sorting Merge Sort Quick Sort –Mathematics Polynomial and matrix multiplication Exponentiation Large integer manipulation –Points Closest Pair Merge Hull Quick Hull

5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (1) –Example: Binary Search Find the location of an element in a sorted collection of n items (assuming it exists) binarySearch( A, low, high, target ) mid = (high – low) / 2 if ( A[mid] == target ) return mid else if ( A[mid] < target ) return binarySearch(A, mid+1, high, target) else return binarySearch(A, low, mid-1, target)

6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (2) –Example: MergeSort Sort a collection of n items into increasing order mergeSort( A, low, high ) if ( low < high ) mid = (high – low) / 2 mergeSort(A, low, mid) mergeSort(A, mid+1, high) merge(A, low, mid, mid+1, high)

7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 38541762 3854 3458 12345678 38541762 38541762 34581267 12345678

8 38541762 38541762 38175462 38541672

9 How it actually runs The recursion of the right list doesn’t actually occur until the recursion on the left list returns This leads to a left to right processing of the recursion “tree”

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

11 38541762 38541762

12 38541762 38541762 3854

13 38541762 38541762 3854 38

14 38541762 38541762 3854 38

15 38541762 38541762 3854

16 38541762 38541762 3854 54

17 38541762 38541762 3854 54

18 38541762 38541762 3845

19 38541762 38541762 3845

20 38541762 34581762

21 38541762 34581762 1762

22 38541762 34581762 1762 17

23 38541762 34581762 1762 17

24 38541762 34581762 1762

25 38541762 34581762 1762 62

26 38541762 34581762 1762 62

27 38541762 34581762 1726

28 38541762 34581762 1726

29 38541762 34581267

30 38541762 34581267

31 12345678

32 Algorithmics: Divide & Conquer (3) –Example: QuickSort Sort a collection of n items into increasing order –Algorithm steps: Break the list into 2 pieces based on a pivot –The pivot is usually the first item in the list All items smaller than the pivot go in the left and all items larger go in the right Sort each piece (recursion again) Combine the results together

33 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 38542761 21385476 12345678 12345678

34 Algorithmics: Divide & Conquer (4) –Example: Exponentiation Calculate a b for integer b  0 exp( a, b ) if ( b == 0 ) return 1 else if ( b is even ) return exp(a, b / 2) 2 else return a * exp(a, b – 1)

35 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (5) –Example: Closest-Pair Problem (1) Find the closest pair of points in a collection of n points in a given plane (use Euclidean distance) Assume n = 2 k If n = 2, answer is distance between these two points Otherwise, –sort points by x-coordinate –split sorted points into two piles of equal size (i.e., divide by vertical line l) Three possibilities: closest pair is –in Left side –in Right side –between Left and Right side Application: Closest pair of airplanes at a given altitude.

36 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (5) –Example: Closest-Pair Problem (2) Let d = min( d L, d R ) –only need to see if there are points in Left and Right that are closer than d –only need to check points within 2d of l –sort points by y-coordinate –can only be eight points in the 2d x d slice distance between closest points in left and right sides

37 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (1) –Definitions: A convex hull of a set of 2D points is the shape taken by a rubber band stretched around nails pounded into the plane at each point. A convex hull of a set S of points is the smallest polygon P for which each point of S is either on the boundary or in the interior of P. A convex hull of a set S of points is the smallest set   S | if x 1, x 2   and   [0,1] then x =  x 1 + (1 -  )x 2   Applications: collision avoidance in robotics; mapping the surface of an object (like an asteroid); analyzing images of soil particles; estimating percentage of lung volume occupied by pneumonia

38 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (2) –QuickHull Algorithm (1): This is actually a half-space hull finder –You start with the leftmost (A) and rightmost (B) points and all the points above them –It finds the half hull to the top side One can then find the entire hull by running the same algorithm on the bottom points

39 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (3) –QuickHull Algorithm (2): Pick the point C that is furthest from the line AB Form two lines – AC and CB and place all points above AC into one list and all points above CB into another list. Ignore the inner points

40 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (4) –QuickHull Algorithm (3): Recursively conquer each list of points. Return the line AB for each eventual empty list of points Combine resulting AB lines into polygon

41 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (5) –MergeHull Algorithm (1): This convex hull algorithm is similar to Merge Sort (obviously given the name!) To make the algorithm as fast as possible we first need to sort the points from left to right (based on x-coordinate) After that we apply a divide-and-conquer technique

42 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (6) –MergeHull Algorithm (2): Split the points into two equal halves Conquer each half to get convex hull for that half (stop when you reach a single point)

43 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Algorithmics: Divide & Conquer (6) –Example: Convex Hull (7) –MergeHull Algorithm (3): Combine the 2 small hulls together into 1 large hull –First find the top and bottom tangent lines to the hulls »A special “walking” algorithm is used to find the tangent lines in linear time –Then use these tangent lines to create the full hull


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Divide and Conquer."

Similar presentations


Ads by Google