530 likes | 672 Views
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
E N D
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(n)=aT(n /b)+f (n), where a≧1, b>1, and f (n) is a given function.
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.
A brute-force solution • Take time.Can we do better? • A transformation • Maximum subarray
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 = –∞
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)
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)
(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)
Analyzing the divide-and conquer algorithm if n=1 if n>1
4.2 Strassen`s algorithm for matrix multiplication • If A = (aij) and B = (bij) are square n×n matrices. • C = A‧B • Cij =
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
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 。
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)
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.
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.
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.
Strassen`s algorithm(1/7) • if n = 1, if n >1.
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
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
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
Strassen`s algorithm(5/7) • Similarly, we set C12 = P1+P2 • A11.B12-A11.B22 +A11.B22+A12.B22 A11.B12+A12.B22
Strassen`s algorithm(6/7) • Setting C21 = P3+P4 • A21.B11+A22.B11 -A22.B11+A22.B21 A21.B11 +A22.B21
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
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.
Example (We may omit the initial condition later.) Guess Assume
Initial condition However,
Making a good guess We guess Making guess provides loose upper bound and lower bound. Then improve the gap.
Subtleties • Guess • Assume • However, assume
Avoiding pitfalls • Assume • Hence • (WRONG!) You cannot find such a c.
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.
substitution method as long as d c/(lg3 – (2/3)).
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.
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.
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.
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.