Strassen’s Matrix Multiplication
meghav@kannuruniv.ac.in 1
Presented By
Megha V
Research Scholar
Dept. of IT
Kannur University
Strassen’s Matrix Multiplication
› Belongs to Divide and Conquer Paradigm
› How Matrix Multiplication done Normally
› How divide and Conquer strategy applied for matrix
multiplication
› What Strassen had done to improve matrix multiplication
using divide and conquer strategy
meghav@kannuruniv.ac.in 2
Matrix Multiplication
A=
𝑎11 𝑎12
𝑎21 𝑎22
× B
𝑏11 𝑏12
𝑏21 𝑏22
2× 2 2× 2
= C =
𝑐11 𝑐12
𝑐21 𝑐22
2 × 2
Cij = 𝑘=1
𝑛
𝐴𝑖𝑘 × 𝐵𝑖𝑘
meghav@kannuruniv.ac.in 3
Matrix Multiplication (contd.)
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
C[i,j]=0;
C[i,j] = A[i,k] × B[k,j];
}
}
Time Complexity of Matrix multiplication is O(n3)
meghav@kannuruniv.ac.in 4
Divide and Conquer strategy for matrix
multiplication
A=
𝑎11 𝑎12
𝑎21 𝑎22
× B
𝑏11 𝑏12
𝑏21 𝑏22
2× 2 2× 2
= C =
𝑐11 𝑐12
𝑐21 𝑐22
2 × 2
› Multiplying a 2 × 2 matrix is a small problem
› 𝑐11 = 𝑎11 × 𝑏11+𝑎12 × 𝑏21
› 𝑐12 = 𝑎11 × 𝑏12+𝑎12 × 𝑏22
› 𝑐21 = 𝑎21 × 𝑏11+𝑎22 × 𝑏21
› 𝑐22 = 𝑎21 × 𝑏12+𝑎22 × 𝑏22
meghav@kannuruniv.ac.in 5
Divide and Conquer strategy for matrix
multiplication (contd.)
› If each addition term takes one unit of time, then total time is
4 unit, it’s a constant
› If there are 8 multiplications and take 8 unit of time, it is also
a constant.
› These 4 formulas take constant time.
› If we want to reduce the problem into 1x1 matrix
A = [a11], B = [b11]
A x B =C
C= [a11* b11]
meghav@kannuruniv.ac.in 6
Divide and Conquer strategy for matrix
multiplication (contd.)
› If the row/column size is grater than 2, we need to divide
the problem and solve each single problem
› We take the problem or assume the problem as power of
2.
› If the matrix is not power of 2 then we make it as power
of 2 by adding 0’s
meghav@kannuruniv.ac.in 7
Divide and Conquer strategy for matrix
multiplication (contd.)
A11 A12 B11 B12
A=
𝑎11 𝑎12 𝑎13 𝑎14
𝑎21 𝑎22 𝑎23 𝑎24
𝑎31 𝑎32 𝑎33 𝑎34
𝑎41 𝑎42 𝑎43 𝑎44
× B
𝑏11 𝑏12 𝑏13 𝑏14
𝑏21 𝑏22 𝑏23 𝑏24
𝑏31 𝑏32 𝑏33 𝑏34
𝑏41 𝑏42 𝑏43 𝑏44
A21 A22 4/2 × 4/2 B21 B22 4/2 × 4/2
A11 A12
= C =
𝑐11 𝑐12 𝑐13 𝑐14
𝑐21 𝑐22 𝑐23 𝑐24
𝑐31 𝑐32 𝑐33 𝑐34
𝑐41 𝑐42 𝑐43 𝑐44
A11 A12 4/2 × 4/2
Here A and B are divided into four 2x2 matrices
meghav@kannuruniv.ac.in 8
Divide and Conquer strategy for matrix
multiplication (contd.)
› Algorithm of divide and conquer matrix multiplication
Algorithm MM(A,B,n)
{
if(n≤2)
{
𝑐11 = 𝑎11 × 𝑏11+𝑎12 × 𝑏21
𝑐12 = 𝑎11 × 𝑏12+𝑎12 × 𝑏22
𝑐21 = 𝑎21 × 𝑏11+𝑎22 × 𝑏21
𝑐22 = 𝑎21 × 𝑏12+𝑎22 × 𝑏22
}
else
{
MM(A11,B11,n/2)+ MM(A12,B21,n/2)
MM(A11,B12,n/2)+ MM(A12,B22,n/2)
MM(A21,B11,n/2)+ MM(A22,B21,n/2)
MM(A21,B12,n/2)+ MM(A22,B22,n/2)
}
}
meghav@kannuruniv.ac.in 9
Divide and Conquer strategy for matrix
multiplication (contd.)
› Time Complexity
8 times recursive call is required.
T(n)=
1 n≤2
8𝑇
𝑛
2
+ n2 n>2
Time complexity is θ(n3)
› If we reduce the number of multiplication we can make the
algorithm faster
meghav@kannuruniv.ac.in 10
Strassen’s Matrix Multiplication
› Reduce 8 to 7 multiplication
› Different equations are applied on the four formulas
› No. of multiplication is reduced but addition and subtraction increased.
P= (A11+A22)(B11+B22)
Q=(A21+A22)
R= A11(B12-B22)
S=A22(B21-B11)
T=(A11+A12)B22
U=(A21–A11)(B11+B12)
V=(A12-A22)(B21+B22)
meghav@kannuruniv.ac.in
11
Strassen’s Matrix Multiplication(contd)
C11 = P+S-T+V
C12 = R+T
C21 = Q+S
C22 = P+R-Q+U
Recurrence relation
T(n)=
1 n≤2
8𝑇
𝑛
2
+ n2 n>2
Time complexity is O(n2.81)
meghav@kannuruniv.ac.in 12

Strassen's matrix multiplication

  • 1.
    Strassen’s Matrix Multiplication meghav@kannuruniv.ac.in1 Presented By Megha V Research Scholar Dept. of IT Kannur University
  • 2.
    Strassen’s Matrix Multiplication ›Belongs to Divide and Conquer Paradigm › How Matrix Multiplication done Normally › How divide and Conquer strategy applied for matrix multiplication › What Strassen had done to improve matrix multiplication using divide and conquer strategy meghav@kannuruniv.ac.in 2
  • 3.
    Matrix Multiplication A= 𝑎11 𝑎12 𝑎21𝑎22 × B 𝑏11 𝑏12 𝑏21 𝑏22 2× 2 2× 2 = C = 𝑐11 𝑐12 𝑐21 𝑐22 2 × 2 Cij = 𝑘=1 𝑛 𝐴𝑖𝑘 × 𝐵𝑖𝑘 meghav@kannuruniv.ac.in 3
  • 4.
    Matrix Multiplication (contd.) for(i=0;i<n;i++) { for(j=0;j<n;j++) { C[i,j]=0; C[i,j]= A[i,k] × B[k,j]; } } Time Complexity of Matrix multiplication is O(n3) meghav@kannuruniv.ac.in 4
  • 5.
    Divide and Conquerstrategy for matrix multiplication A= 𝑎11 𝑎12 𝑎21 𝑎22 × B 𝑏11 𝑏12 𝑏21 𝑏22 2× 2 2× 2 = C = 𝑐11 𝑐12 𝑐21 𝑐22 2 × 2 › Multiplying a 2 × 2 matrix is a small problem › 𝑐11 = 𝑎11 × 𝑏11+𝑎12 × 𝑏21 › 𝑐12 = 𝑎11 × 𝑏12+𝑎12 × 𝑏22 › 𝑐21 = 𝑎21 × 𝑏11+𝑎22 × 𝑏21 › 𝑐22 = 𝑎21 × 𝑏12+𝑎22 × 𝑏22 meghav@kannuruniv.ac.in 5
  • 6.
    Divide and Conquerstrategy for matrix multiplication (contd.) › If each addition term takes one unit of time, then total time is 4 unit, it’s a constant › If there are 8 multiplications and take 8 unit of time, it is also a constant. › These 4 formulas take constant time. › If we want to reduce the problem into 1x1 matrix A = [a11], B = [b11] A x B =C C= [a11* b11] meghav@kannuruniv.ac.in 6
  • 7.
    Divide and Conquerstrategy for matrix multiplication (contd.) › If the row/column size is grater than 2, we need to divide the problem and solve each single problem › We take the problem or assume the problem as power of 2. › If the matrix is not power of 2 then we make it as power of 2 by adding 0’s meghav@kannuruniv.ac.in 7
  • 8.
    Divide and Conquerstrategy for matrix multiplication (contd.) A11 A12 B11 B12 A= 𝑎11 𝑎12 𝑎13 𝑎14 𝑎21 𝑎22 𝑎23 𝑎24 𝑎31 𝑎32 𝑎33 𝑎34 𝑎41 𝑎42 𝑎43 𝑎44 × B 𝑏11 𝑏12 𝑏13 𝑏14 𝑏21 𝑏22 𝑏23 𝑏24 𝑏31 𝑏32 𝑏33 𝑏34 𝑏41 𝑏42 𝑏43 𝑏44 A21 A22 4/2 × 4/2 B21 B22 4/2 × 4/2 A11 A12 = C = 𝑐11 𝑐12 𝑐13 𝑐14 𝑐21 𝑐22 𝑐23 𝑐24 𝑐31 𝑐32 𝑐33 𝑐34 𝑐41 𝑐42 𝑐43 𝑐44 A11 A12 4/2 × 4/2 Here A and B are divided into four 2x2 matrices meghav@kannuruniv.ac.in 8
  • 9.
    Divide and Conquerstrategy for matrix multiplication (contd.) › Algorithm of divide and conquer matrix multiplication Algorithm MM(A,B,n) { if(n≤2) { 𝑐11 = 𝑎11 × 𝑏11+𝑎12 × 𝑏21 𝑐12 = 𝑎11 × 𝑏12+𝑎12 × 𝑏22 𝑐21 = 𝑎21 × 𝑏11+𝑎22 × 𝑏21 𝑐22 = 𝑎21 × 𝑏12+𝑎22 × 𝑏22 } else { MM(A11,B11,n/2)+ MM(A12,B21,n/2) MM(A11,B12,n/2)+ MM(A12,B22,n/2) MM(A21,B11,n/2)+ MM(A22,B21,n/2) MM(A21,B12,n/2)+ MM(A22,B22,n/2) } } meghav@kannuruniv.ac.in 9
  • 10.
    Divide and Conquerstrategy for matrix multiplication (contd.) › Time Complexity 8 times recursive call is required. T(n)= 1 n≤2 8𝑇 𝑛 2 + n2 n>2 Time complexity is θ(n3) › If we reduce the number of multiplication we can make the algorithm faster meghav@kannuruniv.ac.in 10
  • 11.
    Strassen’s Matrix Multiplication ›Reduce 8 to 7 multiplication › Different equations are applied on the four formulas › No. of multiplication is reduced but addition and subtraction increased. P= (A11+A22)(B11+B22) Q=(A21+A22) R= A11(B12-B22) S=A22(B21-B11) T=(A11+A12)B22 U=(A21–A11)(B11+B12) V=(A12-A22)(B21+B22) meghav@kannuruniv.ac.in 11
  • 12.
    Strassen’s Matrix Multiplication(contd) C11= P+S-T+V C12 = R+T C21 = Q+S C22 = P+R-Q+U Recurrence relation T(n)= 1 n≤2 8𝑇 𝑛 2 + n2 n>2 Time complexity is O(n2.81) meghav@kannuruniv.ac.in 12