{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# chap2 - Chapter 2 Divide-and-conquer algorithms The...

This preview shows pages 1–3. Sign up to view the full content.

Chapter 2 Divide-and-conquer algorithms The divide-and-conquer 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 algorithm’s core recursive structure. As an introductory example, we’ll see how this technique yields a new algorithm for multi- plying numbers, one that is much more efficient than the method we all learned in elementary school! 2.1 Multiplication The mathematician Carl Friedrich Gauss (1777–1855) once noticed that although the product of two complex numbers ( a + bi )( c + di ) = ac - bd + ( bc + ad ) i seems to involve four real-number multiplications, it can in fact be done with just three : ac , bd , and ( a + b )( c + d ) , since bc + ad = ( a + b )( c + d ) - ac - bd. In our big- O way of thinking, reducing the number of multiplications from four to three seems wasted ingenuity. But this modest improvement becomes very significant when applied recur- sively . 55

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
56 Algorithms Let’s move away from complex numbers and see how this helps with regular multiplica- tion. Suppose x and y are two n -bit integers, and assume for convenience that n is a power of 2 (the more general case is hardly any different). As a first 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 . For 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 left-shifts). The significant operations are the four n/ 2 -bit 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 n -bit numbers starts by making recursive calls to multiply these four pairs of n/ 2 -bit 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 n -bit 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 particu- lar one works out to O ( n 2 ) , the same running time as the traditional grade-school multiplica- tion technique. So we have a radically new algorithm, but we haven’t yet made any progress in efficiency. How can our method be sped up?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 36

chap2 - Chapter 2 Divide-and-conquer algorithms The...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online