Skip to content

Commit 315e947

Browse files
author
Debasish Biswas
authored
Add more tests (TheAlgorithms#3601)
1 parent 6235fd6 commit 315e947

File tree

4 files changed

+125
-66
lines changed

4 files changed

+125
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
package com.thealgorithms.divideandconquer;
22

3-
public class BinaryExponentiation {
3+
// Java Program to Implement Binary Exponentiation (power in log n)
44

5-
public static void main(String args[]) {
6-
System.out.println(calculatePower(2, 30));
7-
}
5+
/*
6+
* Binary Exponentiation is a method to calculate a to the power of b.
7+
* It is used to calculate a^n in O(log n) time.
8+
*
9+
* Reference:
10+
* https://iq.opengenus.org/binary-exponentiation/
11+
*/
12+
13+
public class BinaryExponentiation {
814

9-
// Function to calculate x^y
10-
// Time Complexity: O(logn)
15+
// recursive function to calculate a to the power of b
1116
public static long calculatePower(long x, long y) {
1217
if (y == 0) {
1318
return 1;
1419
}
1520
long val = calculatePower(x, y / 2);
16-
val *= val;
17-
if (y % 2 == 1) {
18-
val *= x;
21+
if (y % 2 == 0) {
22+
return val * val;
23+
}
24+
return val * val * x;
25+
}
26+
27+
// iterative function to calculate a to the power of b
28+
long power(long N, long M) {
29+
long power = N, sum = 1;
30+
while (M > 0) {
31+
if ((M & 1) == 1) {
32+
sum *= power;
33+
}
34+
power = power * power;
35+
M = M >> 1;
1936
}
20-
return val;
37+
return sum;
2138
}
2239
}

src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java

+17-56
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package com.thealgorithms.divideandconquer;
22

3-
// Java Program to Implement Strassen Algorithm
4-
// Class Strassen matrix multiplication
3+
// Java Program to Implement Strassen Algorithm for Matrix Multiplication
4+
5+
/*
6+
* Uses the divide and conquer approach to multiply two matrices.
7+
* Time Complexity: O(n^2.8074) better than the O(n^3) of the standard matrix multiplication algorithm.
8+
* Space Complexity: O(n^2)
9+
*
10+
* This Matrix multiplication can be performed only on square matrices
11+
* where n is a power of 2. Order of both of the matrices are n × n.
12+
*
13+
* Reference:
14+
* https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_strassens_matrix_multiplication.htm#:~:text=Strassen's%20Matrix%20multiplication%20can%20be,matrices%20are%20n%20%C3%97%20n.
15+
* https://www.geeksforgeeks.org/strassens-matrix-multiplication/
16+
*/
17+
518
public class StrassenMatrixMultiplication {
619

7-
// Method 1
820
// Function to multiply matrices
921
public int[][] multiply(int[][] A, int[][] B) {
1022
int n = A.length;
@@ -80,7 +92,6 @@ public int[][] multiply(int[][] A, int[][] B) {
8092
return R;
8193
}
8294

83-
// Method 2
8495
// Function to subtract two matrices
8596
public int[][] sub(int[][] A, int[][] B) {
8697
int n = A.length;
@@ -96,7 +107,6 @@ public int[][] sub(int[][] A, int[][] B) {
96107
return C;
97108
}
98109

99-
// Method 3
100110
// Function to add two matrices
101111
public int[][] add(int[][] A, int[][] B) {
102112
int n = A.length;
@@ -112,9 +122,7 @@ public int[][] add(int[][] A, int[][] B) {
112122
return C;
113123
}
114124

115-
// Method 4
116-
// Function to split parent matrix
117-
// into child matrices
125+
// Function to split parent matrix into child matrices
118126
public void split(int[][] P, int[][] C, int iB, int jB) {
119127
for (int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++) {
120128
for (int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++) {
@@ -123,9 +131,7 @@ public void split(int[][] P, int[][] C, int iB, int jB) {
123131
}
124132
}
125133

126-
// Method 5
127-
// Function to join child matrices
128-
// into (to) parent matrix
134+
// Function to join child matrices into (to) parent matrix
129135
public void join(int[][] C, int[][] P, int iB, int jB) {
130136
for (int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++) {
131137
for (int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++) {
@@ -134,49 +140,4 @@ public void join(int[][] C, int[][] P, int iB, int jB) {
134140
}
135141
}
136142

137-
// Method 5
138-
// Main driver method
139-
public static void main(String[] args) {
140-
System.out.println(
141-
"Strassen Multiplication Algorithm Implementation For Matrix Multiplication :\n"
142-
);
143-
144-
StrassenMatrixMultiplication s = new StrassenMatrixMultiplication();
145-
146-
// Size of matrix
147-
// Considering size as 4 in order to illustrate
148-
int N = 4;
149-
150-
// Matrix A
151-
// Custom input to matrix
152-
int[][] A = {
153-
{ 1, 2, 5, 4 },
154-
{ 9, 3, 0, 6 },
155-
{ 4, 6, 3, 1 },
156-
{ 0, 2, 0, 6 },
157-
};
158-
159-
// Matrix B
160-
// Custom input to matrix
161-
int[][] B = {
162-
{ 1, 0, 4, 1 },
163-
{ 1, 2, 0, 2 },
164-
{ 0, 3, 1, 3 },
165-
{ 1, 8, 1, 2 },
166-
};
167-
168-
// Matrix C computations
169-
// Matrix C calling method to get Result
170-
int[][] C = s.multiply(A, B);
171-
172-
System.out.println("\nProduct of matrices A and B : ");
173-
174-
// Print the output
175-
for (int i = 0; i < N; i++) {
176-
for (int j = 0; j < N; j++) {
177-
System.out.print(C[i][j] + " ");
178-
}
179-
System.out.println();
180-
}
181-
}
182143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.divideandconquer;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class BinaryExponentiationTest {
8+
9+
@Test
10+
public void testCalculatePower() {
11+
assertEquals(1, BinaryExponentiation.calculatePower(1, 10000000));
12+
assertEquals(1, BinaryExponentiation.calculatePower(1, 100000000));
13+
assertEquals(1, BinaryExponentiation.calculatePower(1, 1000000000));
14+
assertEquals(1, BinaryExponentiation.calculatePower(1, 10000000000L));
15+
assertEquals(1, BinaryExponentiation.calculatePower(1, 100000000000L));
16+
assertEquals(1, BinaryExponentiation.calculatePower(1, 1000000000000L));
17+
assertEquals(1, BinaryExponentiation.calculatePower(1, 10000000000000L));
18+
assertEquals(1, BinaryExponentiation.calculatePower(1, 100000000000000L));
19+
assertEquals(1, BinaryExponentiation.calculatePower(1, 1000000000000000L));
20+
assertEquals(1, BinaryExponentiation.calculatePower(1, 10000000000000000L));
21+
assertEquals(1, BinaryExponentiation.calculatePower(1, 100000000000000000L));
22+
}
23+
24+
@Test
25+
public void testPower() {
26+
assertEquals(1, new BinaryExponentiation().power(1, 10000000));
27+
assertEquals(1, new BinaryExponentiation().power(1, 100000000));
28+
assertEquals(1, new BinaryExponentiation().power(1, 1000000000));
29+
assertEquals(1, new BinaryExponentiation().power(1, 10000000000L));
30+
assertEquals(1, new BinaryExponentiation().power(1, 100000000000L));
31+
assertEquals(1, new BinaryExponentiation().power(1, 1000000000000L));
32+
assertEquals(1, new BinaryExponentiation().power(1, 10000000000000L));
33+
assertEquals(1, new BinaryExponentiation().power(1, 100000000000000L));
34+
assertEquals(1, new BinaryExponentiation().power(1, 1000000000000000L));
35+
assertEquals(1, new BinaryExponentiation().power(1, 10000000000000000L));
36+
assertEquals(1, new BinaryExponentiation().power(1, 100000000000000000L));
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.thealgorithms.divideandconquer;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class StrassenMatrixMultiplicationTest {
8+
9+
StrassenMatrixMultiplication SMM = new StrassenMatrixMultiplication();
10+
11+
// Strassen Matrix Multiplication can only be allplied to matrices of size 2^n
12+
// and has to be a Square Matrix
13+
14+
@Test
15+
public void StrassenMatrixMultiplicationTest2x2() {
16+
int[][] A = { { 1, 2 }, { 3, 4 } };
17+
int[][] B = { { 5, 6 }, { 7, 8 } };
18+
int[][] expResult = { { 19, 22 }, { 43, 50 } };
19+
int[][] actResult = SMM.multiply(A, B);
20+
assertArrayEquals(expResult, actResult);
21+
}
22+
23+
@Test
24+
void StrassenMatrixMultiplicationTest4x4() {
25+
int[][] A = { { 1, 2, 5, 4 }, { 9, 3, 0, 6 }, { 4, 6, 3, 1 }, { 0, 2, 0, 6 } };
26+
int[][] B = { { 1, 0, 4, 1 }, { 1, 2, 0, 2 }, { 0, 3, 1, 3 }, { 1, 8, 1, 2 } };
27+
int[][] expResult = { { 7, 51, 13, 28 }, { 18, 54, 42, 27 }, { 11, 29, 20, 27 }, { 8, 52, 6, 16 } };
28+
int[][] actResult = SMM.multiply(A, B);
29+
assertArrayEquals(expResult, actResult);
30+
}
31+
32+
@Test
33+
void StrassenMatrixMultiplicationTestNegetiveNumber4x4() {
34+
int[][] A = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
35+
int[][] B = { { 1, -2, -3, 4 }, { 4, -3, -2, 1 }, { 5, -6, -7, 8 }, { 8, -7, -6, -5 } };
36+
int[][] expResult = { { 56, -54, -52, 10 }, { 128, -126, -124, 42 }, { 200, -198, -196, 74 },
37+
{ 272, -270, -268, 106 } };
38+
int[][] actResult = SMM.multiply(A, B);
39+
assertArrayEquals(expResult, actResult);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)