Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic: Divide and Conquer

Similar presentations


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

1 Topic: Divide and Conquer
General idea: Divide a problem into subprograms of the same kind; solve subprograms using the same approach, and combine partial solution (if necessary). Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

2 Topic: Divide and Conquer
Find the maximum and minimum The problem: Given a list of unordered n elements, find max and min The straightforward algorithm: max ← min ← A (1); for i ← 2 to n do if A (i) > max, max ← A (i); if A (i) < min, min ← A (i); Key comparisons: 2(n – 1) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

3 Topic: Divide and Conquer
List List 1 List 2 n elements n/2 elements n/2 elements min, max min1, max1 min2, max2 min = MIN ( min1, min2 ) max = MAX ( max1, max2) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

4 Topic: Divide and Conquer
The Divide-and-Conquer algorithm: procedure Rmaxmin (i, j, fmax, fmin); // i, j are index #, fmax, begin // fmin are output parameters case: i = j: fmax ← fmin ← A (i); i = j –1: if A (i) < A (j) then fmax ← A (j); fmin ← A (i); else fmax ← A (i); fmin ← A (j); else: mid ← ; call Rmaxmin (i, mid, gmax, gmin); call Rmaxmin (mid+1, j, hmax, hmin); fmax ← MAX (gmax, hmax); fmin ← MIN (gmin, hmin); end end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

5 Topic: Divide and Conquer
Example: find max and min in the array: 22, 13, -5, -8, 15, 60, 17, 31, 47 ( n = 9 ) Index: Array: Rmaxmin(1, 9, 60, -8) 1, 5, 22, , 9, 60, 17 1, 3, 22, , 5, 15, ,7, 60, , 9, 47, 31 1,2, 22, , 3,-5, -5 (1) (6) (7) (2) (5) (8) (3) (4) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

6 Topic: Divide and Conquer
Analysis: For algorithm containing recursive calls, we can use recurrence relation to find its complexity T(n) - # of comparisons needed for Rmaxmin Recurrence relation: Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

7 Topic: Divide and Conquer
Assume n = 2k for some integer k Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

8 Topic: Divide and Conquer
Theorem: Claim: They don’t have to be the same constant. We make them the same here for simplicity. It will not affect the overall result. where are a, b, c constants Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

9 Topic: Divide and Conquer
Proof: Assume n = ck Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

10 Topic: Divide and Conquer
If a < c, is a constant T(n) = bn · a constant  T(n) = O(n) if a = c, Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

11 Topic: Divide and Conquer
If a > c, Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

12 Topic: Divide and Conquer
2. Matrix multiplication The problem: Multiply two matrices A and B, each of size Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

13 Topic: Divide and Conquer
The traditional way: use three for-loop Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

14 Topic: Divide and Conquer
The Divide-and-Conquer way: transform the problem of multiplying A and B, each of size [n×n] into 8 subproblems, each of size Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

15 Topic: Divide and Conquer
which an2 is for addition so, it is no improvement compared with the traditional way Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

16 Topic: Divide and Conquer
Example: use Divide-and-Conquer way to solve it as following: Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

17 Topic: Divide and Conquer
Strassen’s matrix multiplication: · Discover a way to compute the Cij’s using 7 multiplications and 18 additions or subtractions Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

18 Topic: Divide and Conquer
Algorithm : procedure Strassen (n, A, B, C) // n is size, A,B the input matrices, C output matrix begin if n = 2, else (cont.) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

19 Topic: Divide and Conquer
else Partition A into 4 submatrices: ; Partition B into 4 submatrices: ; call Strassen ( ; call Strassen ( call Strassen ( ; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

20 Topic: Divide and Conquer
call Strassen ( ; end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

21 Topic: Divide and Conquer
Analysis: Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

22 Topic: Divide and Conquer
Assume n = 2k for some integer k Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

23 Topic: Divide and Conquer
3. Integer multiplication The problem: Multiply two large integers (n digits) The traditional way: Use two loops, it takes O(n2) operations Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

24 Topic: Divide and Conquer
The Divide-and-Conquer way: Suppose x and y are large integers, divide x into two parts (a and b), and divide y into c and d. x: y: a b c d So, transform the problem of multiplying two n-digit integers into four subproblems of multiplying two -digit integers. Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

25 Topic: Divide and Conquer
Worst-Case is: However, it is same as the traditional way. Instead, we write: Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

26 Topic: Divide and Conquer
The algorithm by Divide-and-Conquer: function multiplication (x,y) begin n = MAX ( # of digits in x, # of digits in y); if (x = 0) or (y = 0), return 0; else if (n = 1), return x*y in the usual way; else m = ; a = x divide 10m; b = x rem 10m; c = y divide 10m; d = y rem 10m; p1= MULTIPLICATION (a, c); p2= MULTIPLICATION (b, d); p3 = MULTIPLICATION (a+b, c+d); return ; end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer


Download ppt "Topic: Divide and Conquer"

Similar presentations


Ads by Google