Sie sind auf Seite 1von 4

Program 6

Aim:-Wap to multiply two matrix using Strassen's algorithm Theory:Strassens Algorithm, which allows us to multiply two n-by-n matrices with a number of multiplications that is a small multiple of n
k (ln 7)/(ln 2)

, when n is of the form


2.8

2 . This means we will be able to multiply matrices using about n instead of n .


3

multiplications

Algorithm:Strassen's algorithm To calculate the matrix product C = AB, Strassen's algorithm partitions the data to reduce the number of multiplications performed. This algorithm requires M, N and P to be powers of 2. The algorithm is described below. 1. Partition A, B and and C into 4 equal parts: A= B= C= A11 A12 A21 A22 B11 B12 B21 B22 C11 C12 C21 C22

2. Evaluate the intermediate matrices: P1 = (A11 + A22) (B11 + B22) P2 = (A21 + A22) B11 P3 = A11 (B12 B22) P4 = A22 (B21 B11) P5 = (A11 + A12) B22 P6 = (A21 A11) (B11 + B12) P7 = (A12 A22) (B21 + B22) 3. Construct C using the intermediate matrices: C11 = P1 + P4 P5 + P7 C12 = P3 + P5 C21 = P2 + P4 C22 = P1 P2 + P3 + P6

1. Partition A and B into quarter matrices as described above. 2. Compute the intermediate matrices: 1. If the sizes of the matrices are greater than a threshold value, multiply them recursively using Strassen's algorithm. 2. Else use the traditional matrix multiplication algorithm. 3. Construct C using the intermediate matrices.

COMPLEXITY:This alogorithm calculates 7 multiplications and 18 add/subs. T(n)=7T(n/2)+(n2) nlogba =nlog27 =n2.81 As 2.81 is smaller than 3 of ordinary method so Strassens algorithm is better than Ordinary algorithm

Program
#include<stdio.h> #include<conio.h> void main() { clrscr(); int a[2][2],b[2][2],c[2][2],i,j; int p1,p2,p3,p4,p5,p6,p7; printf("Enter the 4 elements of first matrix: "); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]); printf("Enter the 4 elements of second matrix: "); for(i=0;i<2;i++) for(j=0;j<2;j++)

scanf("%d",&b[i][j]); printf("\nThe first matrix is\n"); for(i=0;i<2;i++){ printf("\n"); for(j=0;j<2;j++) printf("%d\t",a[i][j]); } printf("\nThe second matrix is\n"); for(i=0;i<2;i++){ printf("\n"); for(j=0;j<2;j++) printf("%d\t",b[i][j]); } p1= (a[0][0]+a[1][1])*(b[0][0]+b[1][1]); p2= (a[1][0]+a[1][1])*b[0][0]; p3= a[0][0]*(b[0][1]-b[1][1]); p4= a[1][1]*(b[1][0]-b[0][0]); p5= (a[0][0]+a[0][1])*b[1][1]; p6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]); p7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

c[0][0]=p1+p4-p5+p7; c[0][1]=p3+p5; c[1][0]=p2+p4; c[1][1]=p1-p2+p3+p6; printf("\nAfter multiplication using \n"); for(i=0;i<2;i++){

printf("\n"); for(j=0;j<2;j++) printf("%d\t",c[i][j]); } getch(); } OUTPUT

Das könnte Ihnen auch gefallen