1 / 50

4.Divide-and-Conquer

4.Divide-and-Conquer. Hsu, Lih-Hsing. Instruction. Divide Conquer Combine. Recurrences --. if n=1 , if n > 1. Substitution method Recursion-tree method Master method

Télécharger la présentation

4.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. 4.Divide-and-Conquer Hsu, Lih-Hsing

  2. Instruction • Divide • Conquer • Combine

  3. Recurrences -- if n=1 , if n>1. • Substitution method • Recursion-tree method • Master method • T(n)=aT(n /b)+f (n), where a≧1, b>1, and f (n) is a given function.

  4. Technicalities in recurrences • Boundary conditions represent another class of details that we typically ignore. • When we state and solve recurrences, we often omit floors, ceilings, and boundary conditions.

  5. 4.1 The maximum-subarray problem

  6. A brute-force solution • Take time.Can we do better? • A transformation • Maximum subarray

  7. FIND-MAX-CROSSING-SUBARRAY(A,low,mid,high) • left-sun = –∞ • sum = 0 • for I = mid downto low • sum = sum+A[i] • if sum>left-sum • left-sum = sum • max-left = I • right-sun = –∞

  8. sum = 0 • for j = mid+1 to high • sum = sum+A[j] • if sum>right-sum • right-sum = sum • max-right = j • return(max-left,max-right,left-sum+right-sum)

  9. FIND-MAXIMUM-SUBARRAY(A,low,high) • if high == low //base case:only one element • return(low,high,A[low]) • else mid = • (left-low,left-high,left-sum) = FIND-MAXIMUM-SUBBARY(A,low,mid) • (right-low,right-high,right-sum)= FIND-MAXIMUM-SUBBARY (A,mid+1,high)

  10. (cross-low,cross-high,cross-sum) = FIND-MAX-CROSSING-SUBARRAY (A,low,mid,high) • if left-sum≧right-sum and left-sum ≧ cross-sum • return(left-low,left-high,left-sum) • elseif right-sum≧left-sum and right-sum ≧ cross-sum • return(right-low,right-high,right-sum) • else return(cross-low,cross-high,cross-sum)

  11. Analyzing the divide-and conquer algorithm if n=1 if n>1

  12. 4.2 Strassen`s algorithm for matrix multiplication • If A = (aij) and B = (bij) are square n×n matrices. • C = A‧B • Cij =

  13. SQUARE-MATRIX-MULTIPLY(A, B) • n = A.rows • let C be a new n×n matrix • fori = 1 to n • for j = 1 to n • cij=0 • for k = 1 to n • cij = cij + aik‧bkj • return C

  14. A simple divide-and-conquer algorithm • , , • = ‧ • C11 = A11‧B11+A12‧B21 , C12 = A11‧B12+A12‧B22 , C21 = A21‧B11+A22‧B21 , C22 = A21‧B12+A22‧B22 。

  15. SQUARE-MATRIX-MULTIPLY-RECURSIVE(A, B) • n=A.rows • let C be a new n×n matrix • if n == 1 • c11=a11‧b11 • else partition A, B, and C as in equations • c11=SQUARE-MATRIX-MULTIPLY-RECURSIVE (A11, b11) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A12, B21) • c12=SQUARE-MATRIX-MULTIPLY-RECURSIVE (A11, b12) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A12, B22)

  16. c21=SQUARE-MATRIX-MULTIPLY-RECURSIVE (A21, b11) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A22, B21) • c22=SQUARE-MATRIX-MULTIPLY-RECURSIVE (A21, b12) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A22, B22) • return C • if n = 1, if n>1.

  17. Strassen`s method(1/2) • Divide the input matrices A and B and output matrix C into n/2×n/2 submatrices. This step takes time by index calculation. • Creat 10 matrices S1, S2,…,S10, each of which is n/2×n/2 and is the sum or difference of two matrices created step 1. We can creat all 10 matrices in time.

  18. Strassen`s method(2/2) • Using the submatrices created in step 1 and the 10 matrices created in step 2, recursively compute seven matrix products P1,P2,…,P7. Each matrix Pi is n/2 × n/2. • Compute the desired submatrices C11,C12,C21,C22 of the result matrix C by adding and subtracting various combinations of the Pi matrices. We can compute all four submatrices in time.

  19. Strassen`s algorithm(1/7) • if n = 1, if n >1.

  20. Strassen`s algorithm(2/7) • S1 = B12-B22 • S2 = A11+A12 • S3 = A21+A22 • S4 = B21-B11 • S5 = A11+A22 • S6 = B11+B22 • S7 = A12-A22 • S8 = B21+B22 • S9 = A11-A21 • S10 = B11+B12

  21. Strassen`s algorithm(3/7) • P1 = A11.S1 = A11.B12-A11.B22 , P2 = S2.B22 = A11.B22-A12.B22 , P3 = S3.B11 = A21.B11-A22.B11 , P4 = A22.S4 = A22.B21-A22.B11 , P5=S5.S6=A11.B11+A11.B22+A22.B11+ A22.B22 P6=S7.S8=A12.B21+A12.B22-A22.B21-A22.B22 P7=S9.S10=A11.B11+A11.B12-A21.B11-A21.B12

  22. Strassen`s algorithm(4/7) • We start with C11 = P5+P4-P2+P6 • A11.B11+A11.B22+A22.B11+A22.B22 -A22.B11 +A22.B21 -A11.B22 -A12.B22 -A22.B22-A22.B21+A12.B22+A12.B21 ___________________________________________________________________________________________ A11.B11+A12.B21

  23. Strassen`s algorithm(5/7) • Similarly, we set C12 = P1+P2 • A11.B12-A11.B22 +A11.B22+A12.B22 A11.B12+A12.B22

  24. Strassen`s algorithm(6/7) • Setting C21 = P3+P4 • A21.B11+A22.B11 -A22.B11+A22.B21 A21.B11 +A22.B21

  25. Strassen`s algorithm(7/7) • Finally, we set C22 = P5+P1-P3-P73 • A11.B11+A11.B22+A22.B11+A22.B22 -A11.B22 +A11.B12 -A22.B11 -A21.B11 -A11.B11 -A11.B12+A21.B11+A21.B12 A22.B22 +A21.B12

  26. 4.3 The substitution method: Mathematical induction • The substitution method for solving recurrence entails two steps: 1. Guess the form of the solution. 2. Use mathematical induction to find the constants and show that the solution works.

  27. Example (We may omit the initial condition later.) Guess Assume

  28. Initial condition However,

  29. Making a good guess We guess Making guess provides loose upper bound and lower bound. Then improve the gap.

  30. Subtleties • Guess • Assume • However, assume

  31. Avoiding pitfalls • Assume • Hence • (WRONG!) You cannot find such a c.

  32. Changing variables

  33. 4.4 the Recursion-tree method

  34. The cost of the entire tree

  35. substitution method We want to Show that T(n) ≤ dn2 for some constant d > 0. using the same constant c > 0 as before, we have Where the last step holds as long as d(16/13)c.

  36. substitution method as long as d  c/(lg3 – (2/3)).

  37. 4.5 The master method

  38. Note that the three cases do not cover all the possibilities for f(n). There is a gap between cases 1 and 2 when f(n) is smaller than but not polynomially smaller. Similarly, there is a gap between cases 2 and 3 when f(n) is larger than but not polynomially larger.

  39. The master method does not apply to the recurrence even though it has the proper form: a = 2, b=2, f(n)= n lgn, and It might seem that case 3 should apply, since f(n)= n lgn is asymptotically larger than • The problem is that it is not polynomially larger.

  40. Exercise 4.3-9 Solve the recurrence T(n)=3T( )+logn by making a change of variables. Your solution should be asymptotically tight. Do not worry about whether values are integral.

  41. Exercise 4.5-1 Use the master method to give tight asympttotic bounds for the following recurrences. a. T(n) = 2T(n/4)+1. b. T(n) = 2T(n/4)+ . c. T(n) = 2T(n/4)+n. d. T(n) = 2T(n/4)+n2.

More Related