Presentation is loading. Please wait.

Presentation is loading. Please wait.

Divide and Conquer.

Similar presentations


Presentation on theme: "Divide and Conquer."— Presentation transcript:

1 Divide and Conquer

2 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 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 Well-Known Uses Searching Sorting Mathematics Points Binary search
Merge Sort Quick Sort Mathematics Polynomial and matrix multiplication Exponentiation Large integer manipulation Points Closest Pair Merge Hull Quick Hull

5 MergeSort Sort a collection of n items into increasing order
mergeSort(A) { if(A.size() <= 1) return A; else { left = A.subList(0, A.size()/2); right = A.subList(A.size()/2, A.size()); sLeft = mergeSort(left); sRight = mergeSort(right); newA = merge(sLeft, sRight); return newA; }

6 MergeSort 3 8 5 4 1 7 6 2 3 8 5 4 1 7 6 2 3 8 5 4 3 8 5 4 1 7 6 2 3 4 5 8 3 4 5 8 1 2 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

7 Sort a collection of n items into increasing order
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

8 QuickSort 3 8 5 4 2 7 6 1 2 1 3 8 5 4 7 6 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

9 To Develop a Divide & Conquer Algorithm
Determine how to obtain the solution to an instance from the solution to one or more smaller instances Determine the terminal conditions that the smaller instances are approaching Determine the solution in the case of the terminal conditions

10 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)

11 Matrix Multiplication
col 2 row 1, col 2 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 row 1 30 36 42 66 81 96 x = 2x3 3x3 2x3 1x2 + 2x5 + 3x8 = = 36 rows from the first matrix columns from the second matrix

12 Matrix Multiplication
“inner” dimensions must match result is “outer” dimension Examples: 2  3 X 3  3 = 2x3 3  4 X 4  5 = 3x5 2  3 X 4  3 = cannot multiply

13 Matrix Multiplication - Iterative
public static Matrix mult(Matrix m1, Matrix m2) { Matrix result = new Matrix(); for (int i=0; i<m1.numRow(); i++) { for (int j=0; j<m2.numCol(); j++) { double total = 0.0; for (int k=0; k<m1.numCol(); k++) { total += (m1.m[i][k]*m2.m[k][j]); } result.m[i][j] = total; return result;

14 Matrix Multiplication
Divide & Conquer: Easy to break into subproblems Multiplication can be performed blockwise X × Y = Divide & Conquer steps… Divide each matrix into 4 blocks Recursively compute each multiplication Combine multiplications with a few additions and assemble final matrix Run-time? A B × E F = AE + BG AF + BH C D G H CE + DG DF + DH

15 Matrix Multiplication
Algebra Tricks: X × Y = P1 = A(F – H) P2 = (A + B)H P3 = (C + D)E P4 = D(G – E) P5 = (A + D)(E + H) P6 = (B – D)(G + H) P7 = (A – C)(E + F) A B × E F C D G H X × Y = P5 + P4 – P2 + P6 P1 + P2 P3 + P4 P1 + P5 – P3 – P7

16 Closest-Pair Problem Find the closest pair of points in a collection of n points in a given plane (use Euclidean distance) Assume n = 2k 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.

17 Closest-Pair Problem Closest pair is between Left and Right?
Let d = min(LeftMin, RightMin) 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

18 Convex Hull 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 x1, x2   and   [0,1] then x = x1 + (1 - )x2   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

19 Convex Hull QuickHull Algorithm:
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

20 Convex Hull QuickHull Algorithm: Ignore the inner points
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

21 Convex Hull QuickHull Algorithm:
Recursively conquer each list of points. Return the line AB for each eventual empty list of points Combine resulting AB lines into polygon

22 Convex Hull MergeHull Algorithm:
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

23 Convex Hull MergeHull Algorithm Split the points into two equal halves
Conquer each half to get convex hull for that half (stop when you reach a single point)

24 Convex Hull MergeHull Algorithm:
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

25 When to avoid Divide-and-Conquer?
An instance of size n is divided into 2 or more instances each almost of size n An instance of size n is divided into almost n instance of size n/c, where c is a constant When smaller instances are related

26 Practice Problems Foundations of Algorithms
Chapter 2: 1, 6, 7, 8, 10, 19, 24, 39, 41, 42


Download ppt "Divide and Conquer."

Similar presentations


Ads by Google