MST Practical
MST Practical
2. Task to be done:
Write and execute a program to Implementation of Strassen's matrix multiplication algorithm.
// Base case
// If there is only single element
if (n == 1)
// Matrix
else {
// Step 1: Dividing Matrix into parts
// by storing sub-parts to variables
int[][] A11 = new int[n / 2][n / 2];
int[][] A12 = new int[n / 2][n / 2];
int[][] A21 = new int[n / 2][n / 2];
int[][] A22 = new int[n / 2][n / 2];
int[][] B11 = new int[n / 2][n / 2];
int[][] B12 = new int[n / 2][n / 2];
int[][] B21 = new int[n / 2][n / 2];
int[][] B22 = new int[n / 2][n / 2];
// Step 2: Dividing matrix A into 4 halves
split(A, A11, 0, 0);
split(A, A12, 0, n / 2);
split(A, A21, n / 2, 0);
split(A, A22, n / 2, n / 2);
// M1:=(A1+A3)×(B1+B2)
int[][] M1
= multiply(add(A11, A22), add(B11, B22));
// M2:=(A2+A4)×(B3+B4)
int[][] M2 = multiply(add(A21, A22), B11);
// M3:=(A1−A4)×(B1+A4)
int[][] M3 = multiply(A11, sub(B12, B22));
// M4:=A1×(B2−B4)
int[][] M4 = multiply(A22, sub(B21, B11));
// M5:=(A3+A4)×(B1)
int[][] M5 = multiply(add(A11, A12), B22);
// M6:=(A1+A2)×(B4)
int[][] M6
= multiply(sub(A21, A11), add(B11, B12));
// M7:=A4×(B3−B1)
int[][] M7
= multiply(sub(A12, A22), add(B21, B22));
// P:=M2+M3−M6−M7
int[][] C11 = add(sub(add(M1, M4), M5), M7);
// Q:=M4+M6
int[][] C12 = add(M3, M5);
// R:=M5+M7
int[][] C21 = add(M2, M4);
// S:=M1−M3−M4−M5
int[][] C22 = add(sub(add(M1, M3), M2), M6);
// Method 2
// Function to subtract two matrices
public int[][] sub(int[][] A, int[][] B)
{
//
int n = A.length;
//
int[][] C = new int[n][n];
// Method 3
// Function to add two matrices
public int[][] add(int[][] A, int[][] B)
{
//
int n = A.length;
// Method 4
// Function to split parent matrix
// into child matrices
public void split(int[][] P, int[][] C, int iB, int jB)
{
// Iterating over elements of 2D matrix
// using nested for loops
C[i1][j1] = P[i2][j2];
}
// Method 5
// Function to join child matrices
// into (to) parent matrix
public void join(int[][] C, int[][] P, int iB, int jB)
{
// Iterating over elements of 2D matrix
// using nested for loops
P[i2][j2] = C[i1][j1];
}
// Method 5
// Main driver method
public static void main(String[] args)
{
// Display message
System.out.println(
"Strassen Multiplication Algorithm Implementation For Matrix
Multiplication :\n");
// Size of matrix
// Considering size as 4 in order to illustrate
int N = 4;
// Matrix A
// Custom input to matrix
int[][] A = { { 1, 2, 3, 4 },
{ 4, 3, 0, 1 },
{ 5, 6, 1, 1 },
{ 0, 2, 5, 6 } };
// Matrix B
// Custom input to matrix
int[][] B = { { 1, 0, 5, 1 },
{ 1, 2, 0, 2 },
{ 0, 3, 2, 3 },
{ 1, 2, 1, 2 } };
// Matrix C computations
// Display message
System.out.println(
"\nProduct of matrices A and B : ");
7 21 15 22
8 8 21 12
12 17 28 22
8 31 16 31
[Set, Example, Geeks, For]
[Set, Example, Geeks, For]
Rknxjsrrtard6\[:S}{<et, Example, Geeks, For]
[Set, Example, Geeks, For]
11. Graphs (If Any): Image /Soft copy of graph paper to be attached here
1.
2.
3.
4.
5.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):