Skip to content

Commit 138793d

Browse files
authored
Improve docs, remove main, add tests for `MatrixChainRecursiveTopDo… (#5659)
1 parent eba6823 commit 138793d

File tree

3 files changed

+102
-13
lines changed

3 files changed

+102
-13
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@
836836
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
837837
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
838838
* [MatrixChainMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplicationTest.java)
839+
* [MatrixChainRecursiveTopDownMemoisationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisationTest.java)
839840
* [MaximumSumOfNonAdjacentElementsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElementsTest.java)
840841
* [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java)
841842
* [MinimumSumPartitionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java)

src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java

+33-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
// Matrix-chain Multiplication
4-
// Problem Statement
5-
// we have given a chain A1,A2,...,Ani of n matrices, where for i = 1,2,...,n,
6-
// matrix Ai has dimension pi−1 ×pi
7-
// , fully parenthesize the product A1A2 ···An in a way that
8-
// minimizes the number of scalar multiplications.
3+
/**
4+
* The MatrixChainRecursiveTopDownMemoisation class implements the matrix-chain
5+
* multiplication problem using a top-down recursive approach with memoization.
6+
*
7+
* <p>Given a chain of matrices A1, A2, ..., An, where matrix Ai has dimensions
8+
* pi-1 × pi, this algorithm finds the optimal way to fully parenthesize the
9+
* product A1A2...An in a way that minimizes the total number of scalar
10+
* multiplications required.</p>
11+
*
12+
* <p>This implementation uses a memoization technique to store the results of
13+
* subproblems, which significantly reduces the number of recursive calls and
14+
* improves performance compared to a naive recursive approach.</p>
15+
*/
916
public final class MatrixChainRecursiveTopDownMemoisation {
1017
private MatrixChainRecursiveTopDownMemoisation() {
1118
}
1219

20+
/**
21+
* Calculates the minimum number of scalar multiplications needed to multiply
22+
* a chain of matrices.
23+
*
24+
* @param p an array of integers representing the dimensions of the matrices.
25+
* The length of the array is n + 1, where n is the number of matrices.
26+
* @return the minimum number of multiplications required to multiply the chain
27+
* of matrices.
28+
*/
1329
static int memoizedMatrixChain(int[] p) {
1430
int n = p.length;
1531
int[][] m = new int[n][n];
@@ -21,6 +37,17 @@ static int memoizedMatrixChain(int[] p) {
2137
return lookupChain(m, p, 1, n - 1);
2238
}
2339

40+
/**
41+
* A recursive helper method to lookup the minimum number of multiplications
42+
* for multiplying matrices from index i to index j.
43+
*
44+
* @param m the memoization table storing the results of subproblems.
45+
* @param p an array of integers representing the dimensions of the matrices.
46+
* @param i the starting index of the matrix chain.
47+
* @param j the ending index of the matrix chain.
48+
* @return the minimum number of multiplications needed to multiply matrices
49+
* from i to j.
50+
*/
2451
static int lookupChain(int[][] m, int[] p, int i, int j) {
2552
if (i == j) {
2653
m[i][j] = 0;
@@ -38,11 +65,4 @@ static int lookupChain(int[][] m, int[] p, int i, int j) {
3865
}
3966
return m[i][j];
4067
}
41-
42-
// in this code we are taking the example of 4 matrixes whose orders are 1x2,2x3,3x4,4x5
43-
// respectively output should be Minimum number of multiplications is 38
44-
public static void main(String[] args) {
45-
int[] arr = {1, 2, 3, 4, 5};
46-
System.out.println("Minimum number of multiplications is " + memoizedMatrixChain(arr));
47-
}
4868
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class MatrixChainRecursiveTopDownMemoisationTest {
8+
9+
/**
10+
* Test case for four matrices with dimensions 1x2, 2x3, 3x4, and 4x5.
11+
* The expected minimum number of multiplications is 38.
12+
*/
13+
@Test
14+
void testFourMatrices() {
15+
int[] dimensions = {1, 2, 3, 4, 5};
16+
int expected = 38;
17+
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
18+
assertEquals(expected, actual, "The minimum number of multiplications should be 38.");
19+
}
20+
21+
/**
22+
* Test case for three matrices with dimensions 10x20, 20x30, and 30x40.
23+
* The expected minimum number of multiplications is 6000.
24+
*/
25+
@Test
26+
void testThreeMatrices() {
27+
int[] dimensions = {10, 20, 30, 40};
28+
int expected = 18000;
29+
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
30+
assertEquals(expected, actual, "The minimum number of multiplications should be 18000.");
31+
}
32+
33+
/**
34+
* Test case for two matrices with dimensions 5x10 and 10x20.
35+
* The expected minimum number of multiplications is 1000.
36+
*/
37+
@Test
38+
void testTwoMatrices() {
39+
int[] dimensions = {5, 10, 20};
40+
int expected = 1000;
41+
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
42+
assertEquals(expected, actual, "The minimum number of multiplications should be 1000.");
43+
}
44+
45+
/**
46+
* Test case for a single matrix.
47+
* The expected minimum number of multiplications is 0, as there are no multiplications needed.
48+
*/
49+
@Test
50+
void testSingleMatrix() {
51+
int[] dimensions = {10, 20}; // Single matrix dimensions
52+
int expected = 0;
53+
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
54+
assertEquals(expected, actual, "The minimum number of multiplications should be 0.");
55+
}
56+
57+
/**
58+
* Test case for matrices with varying dimensions.
59+
* The expected minimum number of multiplications is calculated based on the dimensions provided.
60+
*/
61+
@Test
62+
void testVaryingDimensions() {
63+
int[] dimensions = {2, 3, 4, 5, 6}; // Dimensions for 4 matrices
64+
int expected = 124; // Expected value needs to be calculated based on the problem
65+
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
66+
assertEquals(expected, actual, "The minimum number of multiplications should be 124.");
67+
}
68+
}

0 commit comments

Comments
 (0)