Skip to content

Commit 85c5470

Browse files
authored
Merge pull request dubesar#520 from abhinavtiwary04/Updated-Strassen-Algorithm
Strassen Algorithm
2 parents 9ff3712 + 5cc7175 commit 85c5470

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

Algorithms/Strassen Algorithm.java

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import java.util.Scanner;
2+
3+
4+
public class Strassen
5+
{
6+
// Function to multiply matrices
7+
public int[][] multiply(int[][] A, int[][] B)
8+
{
9+
int n = A.length;
10+
int[][] R = new int[n][n];
11+
12+
/** base case **/
13+
if (n == 1)
14+
R[0][0] = A[0][0] * B[0][0];
15+
else
16+
{
17+
int[][] A11 = new int[n/2][n/2];
18+
int[][] A12 = new int[n/2][n/2];
19+
int[][] A21 = new int[n/2][n/2];
20+
int[][] A22 = new int[n/2][n/2];
21+
int[][] B11 = new int[n/2][n/2];
22+
int[][] B12 = new int[n/2][n/2];
23+
int[][] B21 = new int[n/2][n/2];
24+
int[][] B22 = new int[n/2][n/2];
25+
26+
/** Dividing matrix A into 4 halves **/
27+
split(A, A11, 0 , 0);
28+
split(A, A12, 0 , n/2);
29+
split(A, A21, n/2, 0);
30+
split(A, A22, n/2, n/2);
31+
32+
33+
/** Dividing matrix B into 4 halves **/
34+
split(B, B11, 0 , 0);
35+
split(B, B12, 0 , n/2);
36+
split(B, B21, n/2, 0);
37+
split(B, B22, n/2, n/2);
38+
39+
/**
40+
M1 = (A11 + A22)(B11 + B22)
41+
M2 = (A21 + A22) B11
42+
M3 = A11 (B12 - B22)
43+
M4 = A22 (B21 - B11)
44+
M5 = (A11 + A12) B22
45+
M6 = (A21 - A11) (B11 + B12)
46+
M7 = (A12 - A22) (B21 + B22)
47+
**/
48+
49+
int [][] M1 = multiply(add(A11, A22), add(B11, B22));
50+
int [][] M2 = multiply(add(A21, A22), B11);
51+
int [][] M3 = multiply(A11, sub(B12, B22));
52+
int [][] M4 = multiply(A22, sub(B21, B11));
53+
int [][] M5 = multiply(add(A11, A12), B22);
54+
int [][] M6 = multiply(sub(A21, A11), add(B11, B12));
55+
int [][] M7 = multiply(sub(A12, A22), add(B21, B22));
56+
57+
/**
58+
C11 = M1 + M4 - M5 + M7
59+
C12 = M3 + M5
60+
C21 = M2 + M4
61+
C22 = M1 - M2 + M3 + M6
62+
**/
63+
64+
int [][] C11 = add(sub(add(M1, M4), M5), M7);
65+
int [][] C12 = add(M3, M5);
66+
int [][] C21 = add(M2, M4);
67+
int [][] C22 = add(sub(add(M1, M3), M2), M6);
68+
69+
/** Joining 4 halves into one result matrix **/
70+
join(C11, R, 0 , 0);
71+
join(C12, R, 0 , n/2);
72+
join(C21, R, n/2, 0);
73+
join(C22, R, n/2, n/2);
74+
}
75+
76+
/** Returning the resultant matrix **/
77+
return R;
78+
}
79+
80+
/** Funtion to add two matrices **/
81+
public int[][] add(int[][] A, int[][] B)
82+
{
83+
int n = A.length;
84+
int[][] C = new int[n][n];
85+
for (int i = 0; i < n; i++)
86+
for (int j = 0; j < n; j++)
87+
C[i][j] = A[i][j] + B[i][j];
88+
return C;
89+
}
90+
91+
/** Funtion to subtract two matrices **/
92+
public int[][] sub(int[][] A, int[][] B)
93+
{
94+
int n = A.length;
95+
int[][] C = new int[n][n];
96+
for (int i = 0; i < n; i++)
97+
for (int j = 0; j < n; j++)
98+
C[i][j] = A[i][j] - B[i][j];
99+
return C;
100+
}
101+
102+
/** Funtion to split parent matrix into child matrices **/
103+
public void split(int[][] P, int[][] C, int iB, int jB)
104+
{
105+
for(int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++)
106+
for(int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++)
107+
C[i1][j1] = P[i2][j2];
108+
}
109+
110+
/** Funtion to join child matrices intp parent matrix **/
111+
public void join(int[][] C, int[][] P, int iB, int jB)
112+
{
113+
for(int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++)
114+
for(int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++)
115+
P[i2][j2] = C[i1][j1];
116+
}
117+
118+
/** Main function **/
119+
public static void main (String[] args)
120+
{
121+
Scanner sc = new Scanner(System.in);
122+
123+
/** Making an object of Strassen class **/
124+
Strassen s = new Strassen();
125+
126+
System.out.println("Enter the order of matrices (n) :");
127+
int N = sc.nextInt();
128+
129+
/** Accepting two 2d matrices **/
130+
System.out.println("Enter N*N order matrix 1\n");
131+
int[][] A = new int[N][N];
132+
for (int i = 0; i < N; i++)
133+
for (int j = 0; j < N; j++)
134+
A[i][j] = sc.nextInt();
135+
136+
System.out.println("Enter N*N order matrix 2\n");
137+
int[][] B = new int[N][N];
138+
for (int i = 0; i < N; i++)
139+
for (int j = 0; j < N; j++)
140+
B[i][j] = sc.nextInt();
141+
142+
int[][] C = s.multiply(A, B);
143+
144+
System.out.println("\nProduct of matrices 1 and 2 is : ");
145+
for (int i = 0; i < N; i++)
146+
{
147+
for (int j = 0; j < N; j++)
148+
System.out.print(C[i][j] +" ");
149+
System.out.println();
150+
}
151+
152+
}
153+
}

0 commit comments

Comments
 (0)