COMS 482: unofficial class blog


Lecture 8: Divide and Conquer

Posted in Class Notes by Elliott Back on February 9th, 2005. [Del.icio.us]

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 , , , , , , , , , , , , , , , , , , , . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

Leave a Reply

Your blog, no spam: the Herculean Wordpress Hash-cash.

Please take time to enjoy the archives: May 2005 (1) April 2005 (11) March 2005 (11) February 2005 (15) January 2005 (7)

Fresh, related resources:

Supplied by Google Blog Search
  • MIT?s Introduction to Algorithms, Lecture 6: Order Statistics
    [04:30] Randomized divide and conquer algorithm. [11:20] Example of algorithm run on array (6, 10, 13, 5, 8, 3, 2, 11) to find 7-th smallest element. [15:30] Intuitive analysis of Randomized-Select algorithm. ...
  • MIT?s Introduction to Algorithms, Lectures 4 and 5: Sorting
    Lecture 4: Quicksort. The lecture starts by giving the divide and conquer description of the algorithm:. Divide: Partition the array to be sorted into two subarrays around pivot x, such that elements in the lower subarray
  • MIT?s Introduction to Algorithms, Lectures 4 and 5: Sorting
    The previous post covered a lecture on ?Divide and Conquer? algorithm design technique and its applications. Lecture four is devoted entirely to a single sorting algorithm which uses this technique. The algorithm I am talking about is ...
  • THE VILIFICATION OF ENEMY LEADERSHIP IN WW 2
    In one divide-and-conquer leaflet they tried to convince the Australians that they were being sacrificed by the American President. Roosevelt is shown as a figure of death, wrapped in a black robe with greenish skin. ...
  • New York University Algorithms Course
    It also covers computational techniques that include divide-and-conquer, greedy method, and dynamic programming. It then reviews randomized algorithms, amortization, and an introduction to NP-Completeness Theory. Detailed lecture notes ...