1 / 20

Divide and Conquer

Divide and Conquer. Mergesort Quicksort Binary Search Selection Matrix Multiplication Convex Hull. Selection. Find the kth smallest (largest) item in a list. Easy if it is the smallest item or largest. Hardest if towards the middle - median (?)

dperryman
Télécharger la présentation

Divide and Conquer

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. Divide and Conquer • Mergesort • Quicksort • Binary Search • Selection • Matrix Multiplication • Convex Hull

  2. Selection • Find the kth smallest (largest) item in a list. • Easy if it is the smallest item or largest. • Hardest if towards the middle - median (?) • How might we approach? Sort the list and find the kth element (complexity O(nlogn)) • Or … better complexity with a similar method to quicksort.

  3. Quicksort Partition • Choose a pivot element. • Move all items less of the pivot to the left. • Move all items greater than the pivot to the right. • Put the pivot in place, say position p. Quicksort: recursive call with left and right. Selection: recursive call with _______?

  4. Selection(Find kth) Strategy • Partition: places pivot element in position p in O(n) time. 1. Suppose k = p. Return pivot element. 2. Suppose k < p. Only left side to consider. 3. Suppose k > p. Only right side to consider.

  5. Selection (A, left, right,k) 1. Pivot = ChoosePivot( A, left, right); 2. PivotPos = Partition (A, left, right, Pivot); 3. If (PivotPos == k) return A[k]; Else If (PivotPos > k) Selection (A, left, PivotPos-1,k); Else Selection (A,PivotPos+1,right,k);

  6. Complexity of Selection • Divide • ChoosePivot = O(1). • Partition = O(n) • Combine = O(1) - no recombining. • Number of Subproblems:1 • Size of Subproblem  n/2 (hopefully). • T(n) = n + T(n/2) = __________ (pg. 187)

  7. Matrix Multiplication 1 2 3 -1 1 5 -2 4 10 0 1 2 X -2 0 1 = -2 2 3 1 4 -1 1 1 1 -10 0 8 for I = 1 to n for J = 1 to n for K = 1 to n C[I][J] += A[I][K] * B[K][J]

  8. Divide and Conquer - #1 Divide Each Matrix Into 4 Matrices A12 B12 C12 A11 B11 C11 A21 A22 B21 B22 C21 C22 C11 = A11*B11 + A12*B21 ...

  9. Matrix Mult: Divide and Conquer. • C11 = A11 * B11 + A12 * B21 • C12 = A12 * B12 + A12 * B22 • C21 = A21 * B11 + A22 * B21 • C22 = A21 * B12 + A22 * B22 Cost of Dividing = O(1) Cost of Combining = 4 Matrix Adds: O(n^2) Number of Subproblems = 8 Size(n/2)

  10. Complexity of D-and-C(1) • T(n) = 8T(n/2) + O(n^2). • Complexity = _______________ RATS

  11. Strassen Matrix Multiply • P= (A11+A22) (B11 + B22 ) • Q= (A21+A22) B11 • R = A11 (B12 - B22) • S = A22 ( B21 - B11) • T = (A11 + A12) B22 • U = (A21 - A11) (B11 + B12) • V = (A12 - A22) (B21 - B22)

  12. Strassen- cont. • C11 = P + S - T + V • C12 = R + T • C21 = Q + S • C22 = P + R - Q + U Divide = O(1), Combine = 18 add/sub O(n^2) Subproblems = 7 multiplies. Size = n/2 T(n) = 7 T (n/2) + 18n^2. ___________

  13. Convex Hull A convex polygon containing all the points.

  14. Algorithm #1 • The points on the hull are the points that are not inside any triangle. • For point p = 1 to n • Try every group of 3 points and see if p is inside of the triangle. • /* testing to see if a point is inside of a triangle can be done in constant time */ Complexity: O(n^4)

  15. Quickhull Algorithm 1. Choose the points with smallest and largest x coordinates. (Points: p1, p2) 2. Divide the rest of the points into those in the upper part of the hull and those in the lower part. 3. Recursively do the following for each part:

  16. Convex Hull (cont) 1. Find a point p3, that maximizes the triangular area (p1,p2,p3). It will be on the hull. 2. For each other point, determine if it is A) inside the triangle (disregard it). B) outside the left C) outside the right 3. Call recursively with left and right points

  17. Convex Hull p3 p2 p1

  18. Complex. Convex Hull • Divide Part: (Given points p1, p2) • for each other point p: find tri. area p1,p2,p p3 = maximum area point • for each other point p: is p inside the triangle p1, p2, p3, or to the left or to the right. COMPLEXITY OF DIVIDE = O(n)

  19. Convex Hull Complexity • Divide Cost: O(n) • Combine cost: O(1) • Number of Subproblems: 2 • Size of Subproblems: ??? Maybe n/2 • Anyway: complexity similar to quicksort. • O(nlogn) average case, probably.

  20. Homework for Sept. 16 • Implement: Finding the closest pair of points divide&conquer algorithm. (p1040-1044 3rd ed,p.957-961 2nd ed). • Study the big picture of the algorithm. Understand the complexity of the algorithm. • Be very careful to maintain the big oh in your implementation. Sloppy implementation can add more than a constant to the code.

More Related