COMS 482: unofficial class blog

Lecture 8: Divide and Conquer

Posted in Class Notes by Elliott Back on February 9th, 2005.

Multiplication by the usual 3rd grade algorithm, multiplying each digit by each other and carrying the overflows, is O(n2). A more efficient way comes from the following observation:

7052 | 4638 = 7052 * 104 + 4638

So, to multiply two n-digit numbers a * c, divide them in half as follows:

a = [A, B]
c = [C, D]
a * c = (A * 10n/2 + B) * (C * 10n/2 + D)

However, take a look at the runtime analysis:

Multiplication Time Complexity

So T(n) = 4*T(n/2) + cn. And, the sum of the levels = cn2(1 + 1/2 + 1/4 + 1/8 + … + 1/n2) = O(n2). This is just as bad.

Improved multiplication method:

(A + B)(C + D) = AC + BC + AD + BD
AD + BC = (A + B)(C + D) – AC – BD

Now there’s only one multiplication needed in the inner part, rather than two. Now T(n) = 3*T(n/2) + cn, which is O(nlog 3) = O(n1.59). The BEST way to do multiplication is Fast Fourier Transform, which is O(n log n).

Application to matrices:

(n, n) * (n, n) = (n, n) takes O(n3) time naively. But, dividing into quadrants:

Matrix Quadrants

gives the following complexity:

T(n) = 8*T(n/2) + cn2 becomes
T(n) = 7*T(n/2) + cn2
T(n) = O(nlog 7) = O(n2.81)

This is close to the best, which is O(n2.376).

Fibbonacci Example of Dynamic Programming:

This is the “regular” recursive Fibbonacci function:

Function F(n):
    if n < = 1 return 1
    return F(n-1) + F(n-2)
End F

Now, with dynamic programming, you can solve F in O(n) time and space complexity:

Function F(n):
    array fib[0 ... n]
    fib[0] = 1
    fib[1] = 1
    
    for i = 2 to n do
        fib[i] = fib[i-1] + fib[i-2]
    EndFor
    
    return fib[n]
End F

Matrix Chain Multiplication:

Find the best order for multiplying a chain of matrices. The time to multiply two matrices of the following dimensions (m,n) * (n,p) is O(m*n*p) by the standard method:

((4,3)(3,8)) * (8,1) = O(4*3*8) + O(4*8*1) = 96 + 32 = 128
(4,3) * ((3,8)(8,1)) = O(3*8*1) + O(4*3*1) = 24 + 12 = 36

Input is M1, … , Mn and row, column sizes r0, … , rn. We want to find the least number of floating point multiplies for the chain multiplication:

function best(i,j)
    least = infinity
    for k = 1 to j-1 do
        cost = least(i, k) + least(k+1, j) + r[i-1] * r[k] *r[j]
        least = min(least, cost)
    return least
End best

This entry was posted on Wednesday, February 9th, 2005 at 12:26 pm and is tagged with multiplication method, complexity function, nbsp nbsp nbsp nbsp nbsp, fibbonacci, space complexity, runtime analysis, lt 1, nlog, dynamic programming, digit numbers, n2, overflows, cn2, time and space, n1, algorithm, bd, cn, observation, array. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

Leave a Reply