0% found this document useful (0 votes)
46 views3 pages

Ex - 2

DAA lab

Uploaded by

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

Ex - 2

DAA lab

Uploaded by

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

EX – 2

Divide and Conquer - Strassen’s Matrix Multiplication


AIM:
To write a python code to implement Strassen’s Matrix Multiplication using
Divide and Conquer using Numpy array.
ALGORITHM:
STEP – 1: Start the program
STEP – 2 : Matrix Splitting:

o Each matrix X is split into four submatrices: A,B,C,D for matrix X


and E,F,G,H for matrix Y.
o These submatrices are obtained by dividing the original matrices into
quarters.

STEP – 3 : Recursive Multiplication:

o Strassen's algorithm computes seven new matrices (instead of the


usual eight multiplications needed for a standard method):

P1 = A * (F - H)
P2 = (A + B) * H
P3 = (C + D) * E
P4 = D * (G - E)
P5 = (A + D) * (E + H)
P6 = (B - D) * (G + H)
P7 = (A - C) * (E + F)

o These products are then used to compute the four submatrices of the
result matrix CCC:

C11 = P5 + P4 - P2+ P6
C12 = P1 + P2
C21 = P3 + P4
C22 = P1 + P5 - P3 - P7

STEP : 4 : Combining Results:


o The four resulting submatrices C11,C12,C21 and C22 are combined
to form the final result matrix C.

STEP – 5 : Base Case:

o The recursion stops when the matrices are reduced to a size of 1×1. At
this point, a simple multiplication is performed.

STEP – 6 : Stop the program

PSEUDOCODE :
Function split(matrix):
row, col = dimensions of matrix
row2, col2 = row // 2, col // 2
Return matrix[:row2, :col2], matrix[:row2, col2:],
matrix[row2:, :col2], matrix[row2:, col2:]

Function strassen(x, y):


If size of matrix x is 1:
Return x * y
a, b, c, d = split(x)
e, f, g, h = split(y)

p1 = strassen(a, f - h)
p2 = strassen(a + b, h)
p3 = strassen(c + d, e)
p4 = strassen(d, g - e)
p5 = strassen(a + d, e + h)
p6 = strassen(b - d, g + h)
p7 = strassen(a - c, e + f)

c11 = p5 + p4 - p2 + p6
c12 = p1 + p2
c21 = p3 + p4
c22 = p1 + p5 - p3 - p7

top = concatenate horizontally (c11, c12)


bottom = concatenate horizontally (c21, c22)
c = concatenate vertically (top, bottom)

Return c

Matrix A = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14,
15, 16]]
Matrix B = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14,
15, 16]]
Result Matrix C = strassen(A, B)
Print C
Print execution time

You might also like