Lecture 8: Divide and Conquer
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:

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:

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.
One Response to 'Lecture 8: Divide and Conquer'
Leave a Reply
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:
- You and I?
I more recently saw Paul Huey at a small lecture, on the early Dutch fort settlement of Albany excavated between highways and overpasses, in Yonkers, NY in the vicinity of another project we worked on, near the historic Otis Elevator ... - More Toe Nail Trimming . . . Feminist Edition
It?sa divide and conquer thing. I agree with that, mostly. But I have no patience for men who defend the Patriarchy so why should I give more leeway to women who defend the Patriarchy? I know, I know . . . divide and conquer. ... - The RBG SSTT-Aset University Black / New Afrikan Creed...more
So pieces like this I try to avoid because so much of "the man's" tactic of confuse, divide and conquer is to take us (Black Folk) off the offense and put us on the defense. If you spend all your time having to defend what it is that is ... - Mark Twain
A lecture by Samuel L. Clemens (Mark Twain) in Convention hall is among the early possibilities, the receipts from which if given will be devoted to the purchase of a menagerie which has been offered to the Zoological Society to be . ... - David Icke
In Tales From The Time Loop and other works, Icke states that most organized religions, especially Judaism, Christianity, and Islam, are Illuminati creations designed to divide and conquer the human race through endless conflicts. ...
on March 22nd, 2008 at 12:42 pm
Percocet….
Percocet. Percocet withdrawal….