lecture04-4

download lecture04-4

of 15

Transcript of lecture04-4

  • 8/2/2019 lecture04-4

    1/15

    March 25, 2012Design and Analysis of Computer Algorithm1

    Design and Analysis ofComputer AlgorithmLecture 4-4

    Pradondet NilaguptaDepartment of Computer Engineering

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

  • 8/2/2019 lecture04-4

    2/15

    March 25, 2012Design and Analysis of Computer Algorithm2

    Divide and Conquer(Cont.)

  • 8/2/2019 lecture04-4

    3/15

    March 25, 2012Design and Analysis of Computer Algorithm3

    MULTIPLICATION OF TWO n-BITNUMBERS For this problem, the basic operations should be

    bit operations.

    Let xand ybe two n-bit numbers, the traditionalmethod requires O(n2) bit operations, say

  • 8/2/2019 lecture04-4

    4/15

    March 25, 2012Design and Analysis of Computer Algorithm4

    MULTIPLICATION OF TWO n-BITNUMBERS (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, wecan 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 additionsand shifts (multiplications by powers of 2).

  • 8/2/2019 lecture04-4

    5/15

    March 25, 2012Design and Analysis of Computer Algorithm5

    Two n-bits Number Analysis Let T(n) be the number of bit-operations required

    to multiply two n-bit numbers.

  • 8/2/2019 lecture04-4

    6/15

    March 25, 2012Design and Analysis of Computer Algorithm6

    Two n-bits Number Analysis (cont.)

  • 8/2/2019 lecture04-4

    7/15

    March 25, 2012Design and Analysis of Computer Algorithm7

    Two n-bits Number Analysis (cont.)

    Thus T(n) depends very much the number ofsubproblems and also the size of thesubproblems.

    If the number of subproblems were 1, 3, 4, 8,then the algorithms would be of order n, nlog3, n2,n3 respectively.

  • 8/2/2019 lecture04-4

    8/15

    March 25, 2012Design and Analysis of Computer Algorithm8

    A better Algorithm So, the multiplication of two n-bit numbers

    remains O(n2) if the number of half-sizesubproblems remains 4

    In order to reduce the time complexity, weattempt to reduce the number of subproblems

    (i.e., multiplications ) by a trick which uses moreadditions and shifts, specifically,by reducing one

    multiplication with many additions and shifts.

    This trick pays off asymptotically, i.e., when nislarge.

  • 8/2/2019 lecture04-4

    9/15

    March 25, 2012Design and Analysis of Computer Algorithm9

    A better Algorithm (cont.)u= (a+ b)(c+ d)v= a* cw= b* dz= u-v-w= ad+ bc

    So, xycan 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.

  • 8/2/2019 lecture04-4

    10/15

    March 25, 2012Design and Analysis of Computer Algorithm10

    Analysis One can use the multiplication routine recursively to

    evaluate the products u, v, and w. The additions and shiftsrequire O(n) time. Thus the time complexity of multiplyingtwo n-bit numbers is bounded from above by

    where kis a constant reflecting the additions and shifts.

    From the above discussion, the solution to the recurrenceis then O(nlog3) = O(n1.59).

  • 8/2/2019 lecture04-4

    11/15

    March 25, 2012Design and Analysis of Computer Algorithm11

    Example

    This divide and conquer technique for multiplication of two n-

    bit number can also applied to the multiplication problem of

    two n-digit integers and n-degree polynomial.

  • 8/2/2019 lecture04-4

    12/15

    March 25, 2012Design and Analysis of Computer Algorithm12

    MATRIX MULTIPLICATIONThe matrix multiplication can be performed as follows:

    This formulation divides the nx nmatrix into n/2 x n/2matrix, and divide the problem into 8 subproblem of size

    n/2. (Note that nis used as the input size even though n2

    isthe input size ofthe matrix.) This gives the followingrecurrence,

  • 8/2/2019 lecture04-4

    13/15

    March 25, 2012Design and Analysis of Computer Algorithm13

    Recurrence Relation

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

    computed as follows.

  • 8/2/2019 lecture04-4

    14/15

    March 25, 2012Design and Analysis of Computer Algorithm14

    Strassens Matrix Multiplication

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

  • 8/2/2019 lecture04-4

    15/15

    March 25, 2012Design and Analysis of Computer Algorithm15

    Strassens Matrix Multiplication Analysis