Lecture 4 0 0 1 Divide Conquer Method We now take up the concept of divide and conquer We give several examples in what follows The basic idea is to divide up the problem into smaller problems and recursively solve each and combine the solutions to make up the solution of the larger problem Some times this reduces the computational e ort and it is these cases that we are interested in The first few examples from the first lecture that you have most likely seen before this class were i Binary Search ii Merge Sort and iii The Tiling Problem from lecture 1 Now we do a few more Example 1 Given an array of numbers A 1 2 n find the maximum and the minimum of the set of the given numbers using only pairwise comparisons We assume that n is a power of 2 Algorithm 2 A Starting with both M ax and M in set to A 1 we compare each element in the array to the current values of these and if A i M ax change M ax to A i if A i M in change M in to A i We need to do the second comparison only if the first does not change the value of M ax Algorithm 3 A First find M ax and this takes n 1 comparisons Now find M in among the remaining elements and this takes n 2 comparisons for a total of 2n 3 comaprisons What is the worst case number of comparisons in Algorithm A It should be clear that any algorithm for finding the M ax is n Thus the above algorithms are optimal as regards the order But may be there is another algorithm that is also O n but the constant is lower This is indeed the case as we shall see below Algorithm 4 B Consider the relation jnk jnk M ax A 1 2 n max M ax A 1 2 M ax A 1 n j n2k j n2k M in A 1 n M in A 1 2 n min M in A 1 2 2 2 So if we know the results for the two half arrays by doing two more comparisons we get the results for the full array This gives the following recursion 0 if n 1 1 if n 2 t n n t 2 t n2 2 if n 3 1 We use the iteration method to solve this recursion Why not use the Master Theorem Since we have assumed that n is a power of 2 let n 2k for some k Then n t n 2 2t 2 n 2 2 2 2t 4 2 4 8 16 k 1 X 2i 2k 1 t 2 i 1 k 1 X 2i 2 4 8 2k 1 i 1 2 1 2 4 2k 2 2 2k 1 1 2k 2 Therefore t n 2k 2k 1 2 n n 2 2 32 n 2 2n 3 5 Show by substitution method that for general values of n t n Exercise 3 n 2 2 Algorithm B does less work than Algorithms A The savings are only about 25 and this may be o set by the di culty in implementing algorithm B There is a much simpler algorithm that achieves the same bound as algorithm B Algorithm 6 C Define two arrays B 1 2 n2 and C 1 2 n2 of half the length of the array A as follows B i max A 2i 1 A 2i C i min A 2i 1 A 2i 1 i n2 Find the maximum of the numbers in the array B and the minimum of the numbers in the array C The total e ort here is also 32 n 2 but the algorithms is much simpler But we are really looking for much bigger savings Let s move on to other examples Example 7 Fast Integer Multiplication Let X x1 x2 x3 xn and Y y1 y2 y3 yn be two n digit numbers that we wish to multiply The ordinary method of computing the product X Y involves n2 multiplication of single digit numbers yi 1 i n with xj 1 j n Let us consider divide and conquer to develop a faster algorithm Divide both X and Y into two numbers with half the number of digits in the original numbers So we get X1 X2 Y1 Y2 x1 x2 x3 x n2 x n2 1 x n2 2 xn y1 y2 y3 y n2 y n2 1 y n2 2 yn 2 We assume that n is a power of two for the sake of convenience Thus X Y n X1 10 2 X2 n Y1 10 2 Y2 and n X Y X1 Y1 10n X1 Y2 X2 Y1 10 2 X2 Y2 The last expression involves four products of integers of half the size compared to the original problem The number of steps for adding these terms together and for doing the left shifting to get X1 Y 1 10n from X1 Y 1 is cn for some positive c Hence we obtain the following recurrence n t n 4t cn 2 If we use the master theorem to solve this recurrence a 4 b 2 logb a 2 f n n and so we are in Case 1 Hence t n n2 This is no improvement If you look carefully we need only to compute three terms X1 Y1 X1 Y2 X2 Y1 and X2 Y2 How we do it is up to us Notice X1 Y2 X2 Y1 X1 X2 Y1 Y2 X1 Y1 X2 Y2 Hence if we compute the three products X1 Y1 X1 X2 Y1 Y2 and X2 Y2 and two additions subtractions actually above we get all the required terms in X Y With this our recurrence becomes n t n 3t cn 2 Now we apply the master theorem again Case 1 we get t n nlg 3 which is better than before We have indeed conquered In order to see whether we can do even better for example can we split the numbers into three equal parts and do the same thing we need to understand why this worked For this purpose let X t X1 t X2 Y t Y1 t Y2 Z t X t Y t X1 Y1 t2 X1 Y2 X2 Y1 t X2 Y2 at2 bt c Thus Z t is a second degree polynomial in t and has three coe cients a b and c If we know the values of Z t for three di erent values of t then we can calculate the coe cients a b and c The three values of t are normally chosen as 1 0 and 1 We use La Grange s formula 3 Z t 1 X 1 Y i 1 j6 i j 1 t j Z i i j t t 1 t 1 t 1 t 1 t Z 1 Z 0 Z 1 1 1 1 1 1 2 1 2 t t t2 t Z 1 …
View Full Document
Unlocking...