Professional Documents
Culture Documents
Batch-11 Daa
Batch-11 Daa
analysis of
algorithm
BATCH-11
211FA04099
211FA04116
211FA04152
211fa04157
T1 QUESTION
T1 QUESTION: Design an algorithm that multiplies two 8 × 8
matrices using Strassen’s Matrix Algorithm. Determine the
following cases:
a) number of addition operations
b) number of Multiplications operations
c) number of Subtractions operations
d) total number of arithmetic operations
e) Analyse your algorithm with normal multiplication for n=8 and
give the results using order Notation
2
DESCRIPTION:
we are designing a code and algorithm for 8*8 Strassen's
matrix and implementing the conditions:
a)number of addition operations
b) number of Multiplications operations
c) number of Subtractions operations
d) total number of arithmetic operations
e) Analyze your algorithm with normal multiplication for
n=8 and give the results using order Notation
algorithm
╸ Divide each matrix into four 4x4 submatrices.
╸ Calculate the following 7 matrices recursively:
╸ a. M1 = (A11 + A22) x (B11 + B22) b. M2 = (A21 + A22) x B11 c.
M3 = A11 x (B12 - B22) d. M4 = A22 x (B21 - B11) e. M5 = (A11
+ A12) x B22 f. M6 = (A21 - A11) x (B11 + B12) g. M7 = (A12 -
A22) x (B21 + B22)
╸ Calculate the following 4 submatrices recursively:
╸ a. C11 = M1 + M4 - M5 + M7 b. C12 = M3 + M5 c. C21 = M2 +
M4 d. C22 = M1 - M2 + M3 + M6
╸ Combine the submatrices into the resulting 8x8 matrix.
╸ Now, let's analyze the complexity of this algorithm.
4
a) Number of addition operations: There are 6 additions for each of the 7 matrices M1 to
M7, for a total of 42 additions. There are 9 additions to calculate each submatrix C11 to
C22, for a total of 36 additions. The total number of addition operations is 78.
5
d) Total number of arithmetic operations: The total number of arithmetic
operations is the sum of additions, multiplications, and subtractions: 78 + 7 +
15 = 100.
6
• return;
Source code • }
• int i, j;
╸ #include <stdio.h> • // allocate memory for submatrices
╸ void add(int A[][8], int B[][8], int n, int C[][8]) • int A11[4][4], A12[4][4], A21[4][4],
╸ { A22[4][4];
╸ int i, j; • int B11[4][4], B12[4][4], B21[4][4],
╸ for (i = 0; i < n; i++) { B22[4][4];
╸ for (j = 0; j < n; j++) { • int C11[4][4], C12[4][4], C21[4][4],
╸ C[i][j] = A[i][j] + B[i][j]; C22[4][4];
╸ } • int M1[4][4], M2[4][4], M3[4][4],
void sub(int A[][8], int B[][8], int n, int C[][8])
M4[4][4], M5[4][4], M6[4][4], M7[4]
╸ {
[4];
╸ int i, j;
• // divide matrices into submatrices
╸ for (i = 0; i < n; i++) {
╸
• for (i = 0; i < 4; i++) {
for (j = 0; j < n; j++) {
╸ C[i][j] = A[i][j] - B[i][j];
• for (j = 0; j < 4; j++) {
╸ void strassen(int A[][8], int B[][8], int C[][8])
• A11[i][j] = A[i][j];
╸ { • A12[i][j] = A[i][j + 4];
╸ if (sizeof(A) / sizeof(*A) <= 2) { • A21[i][j] = A[i + 4][j];
╸ int i, j, k; • A22[i][j] = A[i + 4][j + 4];
╸ for (i = 0; i < 8; i++) { • B11[i][j] = B[i][j];
╸ for (j = 0; j < 8; j++) { • B12[i][j] = B[i][j + 4];
╸ C[i][j] = 0; • B21[i][j] = B[i + 4][j];
╸ for (k = 0; k < 8; k++) { • B22[i][j] = B[i + 4][j + 4];
╸ C[i][j] += A[i][k] * B[k][j]; • }
╸ } • } 7
╸ }
╸ // compute submatrices M1 to M7
• sub(A11, A21, 4, M1);
╸ sub(B12, B22, 4, M1);
╸
• add(B11, B12, 4, M2);
strassen(A11, M1, M2);
╸
• strassen(M1, M2, M3);
add(C11, M2, 4, C11);
╸ add(A11, A12, 4, M1);
• add(C12, M3, 4, C12);
╸ strassen(M1, B22, M2);
• add(A12, A21, 4, M1);
╸ sub(C12, M2, 4, C12);
• strassen(M1, B22, M2);
╸ add(A21, A22, 4, M1); • add(C22, M2, 4, C22);
╸ strassen(M1, B11, M2); • // merge submatrices into
╸ sub(M2, C21, 4, C21); result matrix
╸ sub(B21, B11, 4, M1); • for (i = 0; i < 4; i++) {
╸ strassen(A22, M1, M2); • for (j = 0; j < 4; j++) {
╸ add(C22, M2, 4, C22); • C[i][j] = C11[i][j];
╸ add(A11, A22, 4, M1); • C[i][j + 4] = C12[i][j];
╸ add(B11, B22, 4, M2); • C[i + 4][j] = C21[i][j];
╸ strassen(M1, M2, M3); • C[i + 4][j + 4] =
╸ add(C11, M3, 4, C11); C22[i][j];
╸ sub(A12, A22, 4, M1);
╸ add(B21, B22, 4, M2);
╸ strassen(M1, M2, M3);
╸ add(C21, M3, 4, C21);
8
void print_matrix(int A[][8])
{ for(i=0;i<8;i++)
int i, j; {
for (i = 0; i < 8; i++) { for(j=0;j<8;j++)
for (j = 0; j < 8; j++) { scanf("%d",&B[i][j]);
printf("%d ", A[i][j]); }
} int C[8][8];
printf("\n"); strassen(A, B, C);
}
}
int main()
{
printf("\nMatrix C (A x B):\n");
int A[8][8]; print_matrix(C);
int B[8][8];
int i,j;
for(i=0;i<8;i++) return 0;
{
for(j=0;j<8;j++)
scanf("%d",&A[i][j]); }
}
Test cases:
11