This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 2 Divideandconquer algorithms The divideandconquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem 2. Recursively solving these subproblems 3. Appropriately combining their answers The real work is done piecemeal, in three different places: in the partitioning of problems into subproblems; at the very tail end of the recursion, when the subproblems are so small that they are solved outright; and in the gluing together of partial answers. These are held together and coordinated by the algorithms core recursive structure. As an introductory example, well see how this technique yields a new algorithm for multiplying numbers, one that is much more efFcient than the method we all learned in elementary school! 2.1 Multiplication The mathematician Carl riedrich Gauss (17771855) once noticed that although the product of two complex numbers ( a + bi )( c + di ) = acbd + ( bc + ad ) i seems to involve four realnumber multiplications, it can in fact be done with just three : ac , bd , and ( a + b )( c + d ) , since bc + ad = ( a + b )( c + d )acbd. In our bigO way of thinking, reducing the number of multiplications from four to three seems wasted ingenuity. But this modest improvement becomes very signiFcant when applied recursively . 55 56 Algorithms Lets move away from complex numbers and see how this helps with regular multiplication. Suppose x and y are two nbit integers, and assume for convenience that n is a power of 2 (the more general case is hardly any different). As a Frst step toward multiplying x and y , split each of them into their left and right halves, which are n/ 2 bits long: x = x L x R = 2 n/ 2 x L + x R y = y L y R = 2 n/ 2 y L + y R . or instance, if x = 10110110 2 (the subscript 2 means binary) then x L = 1011 2 , x R = 0110 2 , and x = 1011 2 2 4 + 0110 2 . The product of x and y can then be rewritten as xy = (2 n/ 2 x L + x R )(2 n/ 2 y L + y R ) = 2 n x L y L + 2 n/ 2 ( x L y R + x R y L ) + x R y R . We will compute xy via the expression on the right. The additions take linear time, as do the multiplications by powers of 2 (which are merely leftshifts). The signiFcant operations are the four n/ 2bit multiplications, x L y L , x L y R , x R y L , x R y R ; these we can handle by four recursive calls. Thus our method for multiplying nbit numbers starts by making recursive calls to multiply these four pairs of n/ 2bit numbers (four subproblems of half the size), and then evaluates the preceding expression in O ( n ) time. Writing T ( n ) for the overall running time on nbit inputs, we get the recurrence relation T ( n ) = 4 T ( n/ 2) + O ( n ) . We will soon see general strategies for solving such equations. In the meantime, this particular one works out to O ( n 2 ) , the same running time as the traditional gradeschool multiplication technique. So we have a radically new algorithm, but we havent yet made any progress in efFciency. How can our method be sped up?...
View
Full
Document
This note was uploaded on 12/15/2009 for the course CS 473 taught by Professor Viswanathan during the Spring '08 term at University of Illinois at Urbana–Champaign.
 Spring '08
 Viswanathan
 Algorithms

Click to edit the document details