Divide-And-Conquer (CLRS 4.2) : Matrix Multiplication
Divide-And-Conquer (CLRS 4.2) : Matrix Multiplication
(CLRS 4.2)
To Solve P:
Matrix Multiplication
Y be two n × n matrices
Let X and
x11 x12 · · · x1n
x21 x22 · · · x1n
X= x31 x32 · · · x1n
··· ··· ··· ···
xn1 xn2 · · · xnn
We want to compute Z = X · Y , where zij = nk=1 Xik · Ykj
P
Problem: Given two matrices of size n by n, come up with an algorithm to compute the product.
• Can we do better? That is, is it possible to multiply two matrices faster than Θ(n3 )?
• This was an open problem for a long time... until Strassen came up with an algorithm in
1969. The idea is to use divide-and-conquer.
1
• Then we see that their product X · Y can be written as:
( ) ( ) ( )
A B E F (A · E + B · G) (A · F + B · H)
· =
C D G H (C · E + D · G) (C · F + D · H)
• ANALYSIS: Running time of algorithm is given by T (n) = 8T (n/2) + Θ(n2 ) ⇒ T (n) = Θ(n3 )
• Cool idea, but not so cool result......since we already discussed a (simpler/naive) O(n3 ) algo-
rithm!
• Can we do better?
Strassen’s divide-and-conquer
• Strassen’s algorithm is based on the following observation:
The recurrence
T (n) = 8T (n/2) + Θ(n2 ) ⇒ T (n) = Θ(n3 )
S1 = (B − D) · (G + H)
S2 = (A + D) · (E + H)
S3 = (A − C) · (E + F )
S4 = (A + B) · H
S5 = A · (F − H)
S6 = D · (G − E)
S7 = (C + D) · E
2
• For e.g. let’s test that S6 + S7 is really C · E + D · G
S6 + S7 = D · (G − E) + (C + D) · E
= DG − DE + CE + DE
= DG + CE
T (n) = 7T (n/2) + n2
n n
= n2 + 7(7T ( 2 ) + ( )2 )
2 2
2 7 2 2 n
= n + ( 2 )n + 7 T ( 2 )
2 2
7 2 n n
= n + ( 2 )n + 7 (7T ( 3 ) + ( 2 )2 )
2 2
2 2 2
7 7 n
= n2 + ( 2 )n2 + ( 2 )2 · n2 + 73 T ( 3 )
2 2 2
7 2 7 2 2 7 3 2 7
= n + ( 2 )n + ( 2 ) n + ( 2 ) n .... + ( 2 )log n−1 n2 + 7log n
2
2 2 2 2
logX
n−1
7 i 2
= ( ) n + 7log n
i=0
22
7 log n−1
= n2 · Θ(( ) ) + 7log n
22
7log n
= n2 · Θ( 2 log n ) + 7log n
(2 )
7log n
= n2 · Θ( 2 ) + 7log n
n
log n
= Θ(7 )
3
= n(1/ log7 2)
log2 7
= n log2 2
= nlog 7
• Note: