0% found this document useful (0 votes)
179 views24 pages

Strassen Matrix

The document discusses Strassen's matrix multiplication algorithm, which improves upon the naive O(n^3) matrix multiplication algorithm by recursively breaking the matrices into smaller subproblems and using a divide and conquer approach. It explains how Strassen was able to perform 2x2 matrix multiplication using only 7 multiplications instead of 8, and how this idea can be generalized to multiply larger n×n matrices in O(n^2.807) time by recursively applying the same approach.

Uploaded by

BushraS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views24 pages

Strassen Matrix

The document discusses Strassen's matrix multiplication algorithm, which improves upon the naive O(n^3) matrix multiplication algorithm by recursively breaking the matrices into smaller subproblems and using a divide and conquer approach. It explains how Strassen was able to perform 2x2 matrix multiplication using only 7 multiplications instead of 8, and how this idea can be generalized to multiply larger n×n matrices in O(n^2.807) time by recursively applying the same approach.

Uploaded by

BushraS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Strassen’s Matrix

Multiplication
Algorithm using
Divide and
Conquer
Divide and Conquer
► Three steps are involved:
► Divide the problem into several subproblems, perhaps of equal size
► Subproblems are solved, typically recursively
► The solutions to the subproblems are combined to get a solution to the
original problem

Real work is done in 3 different places: in partitioning; at the very tail end
of the recursion, when subproblems are so small that they are solved directly;
and in gluing together of partial solutions
Divide and Conquer (contd.)
Problem of size n

Subproblem 1 Subproblem 2
of size n/2 of size n/2
Don’t assume
always breaks up
into 2, could be > 2
subproblems
Solution to Solution to
subproblem 1 subproblem 2

Solution to the
original probelm
void multiply(int A[][N], int B[][N], int C[][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
C[i][j] = 0;
for (int k = 0; k < N; k++)
{
C[i][j] += A[i][k]*B[k][j];
}
}
}
} There are three for loops in this algorithm and one is nested in other. So,
the time complexity of the naive algorithm is O(n³). Hence, the algorithm
takes O(n3) time to execute.
There are three for loops in this algorithm and one is nested in
other. So, the time complexity of the naive algorithm is O(n³).
Hence, the algorithm takes O(n3) time to execute.

In the above method, we do 8 multiplications for matrices of size


N/2 x N/2 and 4 additions. Addition of two matrices takes O(N2)
time. So the time complexity can be written as
T(N) = 8T(N/2) + O(N2)
Div. & Conq. : Strassen’s Matrix Multiplication

► How do we multiply two 2×2 matrices ?


1 2 3 5 5 13 How many multiplications
=
and additions did we need?
3 4 1 4 13 31
8 mults and 4 adds
It isn’t clear how Strassen came up with these formulas but
we can verify that it works. Also, there are conditions for
Strassen’s algorithm to work.
1. Both input matrices should be of dimensions n x n.
2. n should be a power of 2.

Now let’s calculate the time complexity of the algorithm


using master method. The algorithm makes seven recursive
calls while calculating P1 to P7, so a=7. The input matrix
size is divided by 2 in each recursive call, so b=2. The work
done outside recursive call and merging the solutions is
adding, subtracting and copying values to C which is O(n²),
so d=2.

So, the master’s equation is T(n) = 7T(n/2) + O(n²)


Div. & Conq. : Strassen’s Matrix Multiplication

► How do we multiply two 2×2 matrices ?


1 2 3 5 5 13 How many multiplications
=
and additions did we need?
3 4 1 4 13 31
8 mults and 4 adds
V. Strassen in 1969 found out, he can
do the above multiplication in the following
way:
m1+m4-m5+m7 m3+m5
a00 a01 b00 b01 c00 c01
= =
m2+m4 m1+m3-m2+m6
a10 a11 b10 b11 c10 c11

m1 = (a00+a11)*(b00+b11) m2 = (a10+a11)*b00 m3 = a00*(b01-b11)


m4 = a11*(b10-b00) m5 = (a00+a01)*b11 7 mults
18 adds/subs
m6 = (a10-a00)*(b00+b01) m7 = (a01-a11)*(b10+b11)
Div. & Conq. : Strassen’s Matrix Multiplication

► Let us see how we can apply Strassen’s idea for


multiplying two n×n matrices
Let A and B be two n×n matrices where n is a power of 2

A00 A01 B00 B01 C00 C01


=
A10 A11 B10 B11 C10 C11

Each block is (n/2)×(n/2)


E.g., In Strassen’s
method,
You can treat blocks
M1 = (A00+A11)*(B00+B11)
as if they were numbers
M2 = (A10+A11)*B00
to get the C = A*B
etc.
Div. & Conq. : Strassen’s Matrix Multiplication
ALGORITHM Strassen(A, B, n)
//Input: A and B are n×n matrices Recurrence for
//where n is a power of two # of multiplications is
//Output: C = A*B
if n = 1 M(n) = 7M(n/2) for n > 1
return C = A*B M(1) = ?
else For n = 2m,
A00 A01 B00 B01
M(n) = 7M(n/2) = 72M(n/22)
Partition A = and B =
A10 A11 B10 B11 M(n) = 7m M(n/2m)
where the blocks Aij and Bij are (n/2)-by-(n/2) = 7m = 7lgn
= nlg7 ≈ n2.807
M1 <- Strassen(A00+A11, B00+B11, n/2)
M2 <- Strassen(A10+A11, B00, n/2)
M3 <- Strassen(A00, B01-B11, n/2) For # of adds/subs,
M4 <- Strassen(A11, B10-B00, n/2) A(n) = 7A(n/2)+18(n/2)2 for n > 1
M5 <- Strassen(A00+A01, B11, n/2) A(1) = 0
M6 <- Strassen(A10-A00, B00+B01, n/2)
M7 <- Strassen(A01-A11, B10+B11, n/2) Using Master thm,
C00 <- M1+M4-M5+M7 A(n) є Θ(nlg7) better
C01 <- M3+M5
C10 <- M2+M4 than brute-force’s Θ(n3)
C11 <- M1+M3-M2+M6

return C = C00 C01


DONE WITH STRASSEN!
C10 C11
Example:
Example:

=1*(1-1)=0

=1*(1+2)=3 C11=p5+p4-p2+p6 C12=p1+p2


= 16-3-3-3 = 0+3
=3*(2+3)=15 =7 =3
=3*(2-3)= -3
C21=p3+p4 C22=p1+p5-p3-p7
=(1+3)*(3+1)=16 = 15-3 = 0+16-15+4
=12 =5
=(2-3)*(2+1)= -3

=(1-2)*(3+1)= -4
Thank you…

You might also like