You are on page 1of 15

Design and Analysis of Computer Algorithm Lecture 4-4

Pradondet Nilagupta Department of Computer Engineering

This lecture note has been modified from lecture note for 23250 by Prof. Francis Chin

Design and Analysis of Computer Algorithm

March 25, 2012

Divide and Conquer (Cont.)

Design and Analysis of Computer Algorithm

March 25, 2012

MULTIPLICATION OF TWO n-BIT NUMBERS


For this problem, the basic operations should be bit operations. Let x and y be two n-bit numbers, the traditional method requires O(n2) bit operations, say

Design and Analysis of Computer Algorithm

March 25, 2012

MULTIPLICATION OF TWO n-BIT NUMBERS (cont.)

Assume n=2m, we treat x and y as two n-bit strings and partition them into two halves as x = a * b, y= c * d, i.e., concatenation of a, b and c, d. As binary numbers, we can express

xy = (a2n/2 + b) (c2n/2 + d) = ac2n + (ad + bc) 2n/2 + bd ............ (*) The above computation of xy reduces to four multiplications of (n/2)-bit numbers plus some additions and shifts (multiplications by powers of 2).

Design and Analysis of Computer Algorithm

March 25, 2012

Two n-bits Number Analysis

Let T(n) be the number of bit-operations required to multiply two n-bit numbers.

Design and Analysis of Computer Algorithm

March 25, 2012

Two n-bits Number Analysis (cont.)

Design and Analysis of Computer Algorithm

March 25, 2012

Two n-bits Number Analysis (cont.)

Thus T(n) depends very much the number of subproblems and also the size of the subproblems. If the number of subproblems were 1, 3, 4, 8, then the algorithms would be of order n, nlog3, n2, n3 respectively.

Design and Analysis of Computer Algorithm March 25, 2012 7

A better Algorithm
So, the multiplication of two n-bit numbers remains O(n2) if the number of half-size subproblems remains 4 In order to reduce the time complexity, we attempt to reduce the number of subproblems (i.e., multiplications ) by a trick which uses more additions and shifts, specifically,by reducing one multiplication with many additions and shifts. This trick pays off asymptotically, i.e., when n is large.

Design and Analysis of Computer Algorithm March 25, 2012 8

A better Algorithm (cont.)


u = (a + b)(c + d) v=a*c w=b*d z = u - v - w = ad + bc
So, xy can be written as v2n+ z2 n/2 + w (from Eq (*)). This approach requires only three multiplications of two (n/2)-bit numbers, plus some additions and shifts, to multiply two n-bit numbers.

Design and Analysis of Computer Algorithm

March 25, 2012

Analysis

One can use the multiplication routine recursively to evaluate the products u, v, and w. The additions and shifts require O(n) time. Thus the time complexity of multiplying two n-bit numbers is bounded from above by

where k is a constant reflecting the additions and shifts. From the above discussion, the solution to the recurrence is then O(n log3) = O(n 1.59).

Design and Analysis of Computer Algorithm

March 25, 2012

10

Example

This divide and conquer technique for multiplication of two nbit number can also applied to the multiplication problem of two n-digit integers and n-degree polynomial.

Design and Analysis of Computer Algorithm

March 25, 2012

11

MATRIX MULTIPLICATION
The matrix multiplication can be performed as follows:

This formulation divides the n x n matrix into n/2 x n/2 matrix, and divide the problem into 8 subproblem of size n/2. (Note that n is used as the input size even though n2 is the input size ofthe matrix.) This gives the following recurrence,
March 25, 2012 12

Design and Analysis of Computer Algorithm

Recurrence Relation

T(n) remains O(n3). However, the matrix multiplication can also be computed as follows.

Design and Analysis of Computer Algorithm

March 25, 2012

13

Strassens Matrix Multiplication

Consequently, 7 multiplications and 18 additions/subtractions are needed.


Design and Analysis of Computer Algorithm March 25, 2012 14

Strassens Matrix Multiplication Analysis

Design and Analysis of Computer Algorithm

March 25, 2012

15

You might also like