Presentation is loading. Please wait.

Presentation is loading. Please wait.

4.Divide-and-Conquer Hsu, Lih-Hsing. Computer Theory Lab. Chapter 4P.2 Instruction Divide Conquer Combine.

Similar presentations


Presentation on theme: "4.Divide-and-Conquer Hsu, Lih-Hsing. Computer Theory Lab. Chapter 4P.2 Instruction Divide Conquer Combine."— Presentation transcript:

1 4.Divide-and-Conquer Hsu, Lih-Hsing

2 Computer Theory Lab. Chapter 4P.2 Instruction Divide Conquer Combine

3 Computer Theory Lab. Chapter 4P.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 Computer Theory Lab. Chapter 4P.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 Computer Theory Lab. Chapter 4P.5 4.1 The maximum-subarray problem

6 Computer Theory Lab. Chapter 4P.6

7 Computer Theory Lab. Chapter 4P.7 A brute-force solution Take time.Can we do better? A transformation Maximum subarray

8 Computer Theory Lab. Chapter 4P.8

9 Computer Theory Lab. Chapter 4P.9 FIND-MAX-CROSSING- SUBARRAY(A,low,mid,high) 1 left-sun = –∞ 2 sum = 0 3 for I = mid downto low 4 sum = sum + A[i] 5 if sum > left-sum 6 left-sum = sum 7 max-left = I 8 right-sun = –∞

10 Computer Theory Lab. Chapter 4P.10 9 sum = 0 10 for j = mid + 1 to high 11 sum = sum + A[j] 12 if sum > right-sum 13 right-sum = sum 14 max-right = j 15 return(max-left,max-right,left-sum + right-sum)

11 Computer Theory Lab. Chapter 4P.11 FIND-MAXIMUM- SUBARRAY(A,low,high) 1 if high == low //base case:only one element 2 return(low,high,A[low]) 3 else mid = 4 (left-low,left-high,left-sum) = FIND- MAXIMUM-SUBBARY(A,low,mid) 5 (right-low,right-high,right-sum)= FIND- MAXIMUM-SUBBARY (A,mid+1,high)

12 Computer Theory Lab. Chapter 4P.12 6 (cross-low,cross-high,cross-sum) = FIND- MAX-CROSSING-SUBARRAY (A,low,mid,high) 7 if left-sum ≧ right-sum and left-sum ≧ cross- sum 8 return(left-low,left-high,left-sum) 9 elseif right-sum ≧ left-sum and right-sum ≧ cross-sum 10 return(right-low,right-high,right-sum) 11 else return(cross-low,cross-high,cross-sum)

13 Computer Theory Lab. Chapter 4P.13 Analyzing the divide-and conquer algorithm if n=1 if n > 1

14 Computer Theory Lab. Chapter 4P.14 4.2 Strassen`s algorithm for matrix multiplication If A = (a ij ) and B = (b ij ) are square n×n matrices. C = A ‧ B C ij =

15 Computer Theory Lab. Chapter 4P.15 SQUARE-MATRIX-MULTIPLY(A, B) 1 n = A.rows 2 let C be a new n×n matrix 3 for i = 1 to n 4 for j = 1 to n 5 c ij =0 6 for k = 1 to n 7 c ij = c ij + a ik ‧ b kj 8 return C

16 Computer Theory Lab. Chapter 4P.16 A simple divide-and-conquer algorithm,, = ‧ C 11 = A 11 ‧ B 11 + A 12 ‧ B 21 , C 12 = A 11 ‧ B 12 + A 12 ‧ B 22 , C 21 = A 21 ‧ B 11 + A 22 ‧ B 21 , C 22 = A 21 ‧ B 12 + A 22 ‧ B 22 。

17 Computer Theory Lab. Chapter 4P.17 SQUARE-MATRIX-MULTIPLY- RECURSIVE(A, B) 1 n=A.rows 2 let C be a new n×n matrix 3 if n == 1 4 c 11 =a 11 ‧ b 11 5 else partition A, B, and C as in equations 6 c 11 =SQUARE-MATRIX-MULTIPLY-RECURSIVE (A 11, b 11 ) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A 12, B 21 ) 7 c 12 =SQUARE-MATRIX-MULTIPLY-RECURSIVE (A 11, b 12 ) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A 12, B 22 )

18 Computer Theory Lab. Chapter 4P.18 8 c 21 =SQUARE-MATRIX-MULTIPLY-RECURSIVE (A 21, b 11 ) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A 22, B 21 ) 9 c 22 =SQUARE-MATRIX-MULTIPLY-RECURSIVE (A 21, b 12 ) + SQUARE-MATRIX-MULTIPLY- RECURSIVE (A 22, B 22 ) 10 return C if n = 1, if n > 1.

19 Computer Theory Lab. Chapter 4P.19 Strassen`s method(1/2) 1 Divide the input matrices A and B and output matrix C into n/2×n/2 submatrices. This step takes time by index calculation. 2 Creat 10 matrices S 1, S 2, …,S 10, 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.

20 Computer Theory Lab. Chapter 4P.20 Strassen`s method(2/2) 3 Using the submatrices created in step 1 and the 10 matrices created in step 2, recursively compute seven matrix products P 1,P 2, …,P 7. Each matrix P i is n/2 × n/2. 4 Compute the desired submatrices C 11,C 12,C 21,C 22 of the result matrix C by adding and subtracting various combinations of the P i matrices. We can compute all four submatrices in time.

21 Computer Theory Lab. Chapter 4P.21 Strassen`s algorithm(1/7) if n = 1, if n > 1.

22 Computer Theory Lab. Chapter 4P.22 Strassen`s algorithm(2/7) S 1 = B 12 - B 22 S 2 = A 11 + A 12 S 3 = A 21 + A 22 S 4 = B 21 - B 11 S 5 = A 11 + A 22 S 6 = B 11 + B 22 S 7 = A 12 - A 22 S 8 = B 21 + B 22 S 9 = A 11 - A 21 S 10 = B 11 + B 12

23 Computer Theory Lab. Chapter 4P.23 Strassen`s algorithm(3/7) P 1 = A 11 . S 1 = A 11 . B 12 - A 11 . B 22 , P 2 = S 2 . B 22 = A 11 . B 22 - A 12 . B 22 , P 3 = S 3 . B 11 = A 21 . B 11 - A 22 . B 11 , P 4 = A 22 . S 4 = A 22 . B 21 - A 22 . B 11 , P 5 =S 5 . S 6 =A 11 . B 11 + A 11 . B 22 + A 22 . B 11 + A 22 . B 22 P 6 =S 7 . S 8 =A 12 . B 21 + A 12 . B 22 - A 22 . B 21 - A 22 . B 22 P 7 =S 9 . S 10 =A 11 . B 11 + A 11 . B 12 - A 21 . B 11 - A 21 . B 12

24 Computer Theory Lab. Chapter 4P.24 Strassen`s algorithm(4/7) We start with C 11 = P 5 + P 4 - P 2 + P 6 A 11 . B 11 + A 11 . B 22 + A 22 . B 11 + A 22 . B 22 - A 22 . B 11 + A 22 . B 21 - A 11 . B 22 - A 12 . B 22 - A 22 . B 22 - A 22 . B 21 + A 12 . B 22 + A 12 . B 21 ___________________________________________________________________________________________ A 11 . B 11 + A 12 . B 21

25 Computer Theory Lab. Chapter 4P.25 Strassen`s algorithm(5/7) Similarly, we set C 12 = P 1 + P 2 A 11 . B 12 - A 11 . B 22 + A 11 . B 22 + A 12 . B 22 A 11 . B 12 + A 12 . B 22

26 Computer Theory Lab. Chapter 4P.26 Strassen`s algorithm(6/7) Setting C 21 = P 3 + P 4 A 21 . B 11 + A 22 . B 11 - A 22 . B 11 + A 22 . B 21 A 21 . B 11 + A 22 . B 21

27 Computer Theory Lab. Chapter 4P.27 Strassen`s algorithm(7/7) Finally, we set C 22 = P 5 + P 1 - P 3 - P 73 A 11 . B 11 + A 11 . B 22 + A 22 . B 11 + A 22 . B 22 - A 11 . B 22 + A 11 . B 12 - A 22 . B 11 - A 21 . B 11 - A 11 . B 11 - A 11 . B 12 + A 21 . B 11 + A 21 . B 12 A 22 . B 22 + A 21 . B 12

28 Computer Theory Lab. Chapter 4P.28 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.

29 Computer Theory Lab. Chapter 4P.29 Example (We may omit the initial condition later.) Guess Assume

30 Computer Theory Lab. Chapter 4P.30 Initial condition However,

31 Computer Theory Lab. Chapter 4P.31 Making a good guess We guess Making guess provides loose upper bound and lower bound. Then improve the gap.

32 Computer Theory Lab. Chapter 4P.32 Subtleties Guess Assume However, assume

33 Computer Theory Lab. Chapter 4P.33

34 Computer Theory Lab. Chapter 4P.34

35 Computer Theory Lab. Chapter 4P.35 Avoiding pitfalls Assume Hence (WRONG!) You cannot find such a c.

36 Computer Theory Lab. Chapter 4P.36 Changing variables

37 Computer Theory Lab. Chapter 4P.37 4.4 the Recursion-tree method

38 Computer Theory Lab. Chapter 4P.38

39 Computer Theory Lab. Chapter 4P.39 The cost of the entire tree

40 Computer Theory Lab. Chapter 4P.40

41 Computer Theory Lab. Chapter 4P.41 substitution method We want to Show that T(n) ≤ dn 2 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.

42 Computer Theory Lab. Chapter 4P.42

43 Computer Theory Lab. Chapter 4P.43 substitution method as long as d  c/(lg3 – (2/3)).

44 Computer Theory Lab. Chapter 4P.44 4.5 The master method

45 Computer Theory Lab. Chapter 4P.45

46 Computer Theory Lab. Chapter 4P.46

47 Computer Theory Lab. Chapter 4P.47 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.

48 Computer Theory Lab. Chapter 4P.48 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.

49 Computer Theory Lab. Chapter 4P.49 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.

50 Computer Theory Lab. Chapter 4P.50 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)+n 2.


Download ppt "4.Divide-and-Conquer Hsu, Lih-Hsing. Computer Theory Lab. Chapter 4P.2 Instruction Divide Conquer Combine."

Similar presentations


Ads by Google