mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Improve docs, remove main
, add tests for `MatrixChainRecursiveTopDo… (#5659)
This commit is contained in:
@ -836,6 +836,7 @@
|
||||
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
|
||||
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
|
||||
* [MatrixChainMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplicationTest.java)
|
||||
* [MatrixChainRecursiveTopDownMemoisationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisationTest.java)
|
||||
* [MaximumSumOfNonAdjacentElementsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElementsTest.java)
|
||||
* [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java)
|
||||
* [MinimumSumPartitionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java)
|
||||
|
@ -1,15 +1,31 @@
|
||||
package com.thealgorithms.dynamicprogramming;
|
||||
|
||||
// Matrix-chain Multiplication
|
||||
// Problem Statement
|
||||
// we have given a chain A1,A2,...,Ani of n matrices, where for i = 1,2,...,n,
|
||||
// matrix Ai has dimension pi−1 ×pi
|
||||
// , fully parenthesize the product A1A2 ···An in a way that
|
||||
// minimizes the number of scalar multiplications.
|
||||
/**
|
||||
* The MatrixChainRecursiveTopDownMemoisation class implements the matrix-chain
|
||||
* multiplication problem using a top-down recursive approach with memoization.
|
||||
*
|
||||
* <p>Given a chain of matrices A1, A2, ..., An, where matrix Ai has dimensions
|
||||
* pi-1 × pi, this algorithm finds the optimal way to fully parenthesize the
|
||||
* product A1A2...An in a way that minimizes the total number of scalar
|
||||
* multiplications required.</p>
|
||||
*
|
||||
* <p>This implementation uses a memoization technique to store the results of
|
||||
* subproblems, which significantly reduces the number of recursive calls and
|
||||
* improves performance compared to a naive recursive approach.</p>
|
||||
*/
|
||||
public final class MatrixChainRecursiveTopDownMemoisation {
|
||||
private MatrixChainRecursiveTopDownMemoisation() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the minimum number of scalar multiplications needed to multiply
|
||||
* a chain of matrices.
|
||||
*
|
||||
* @param p an array of integers representing the dimensions of the matrices.
|
||||
* The length of the array is n + 1, where n is the number of matrices.
|
||||
* @return the minimum number of multiplications required to multiply the chain
|
||||
* of matrices.
|
||||
*/
|
||||
static int memoizedMatrixChain(int[] p) {
|
||||
int n = p.length;
|
||||
int[][] m = new int[n][n];
|
||||
@ -21,6 +37,17 @@ public final class MatrixChainRecursiveTopDownMemoisation {
|
||||
return lookupChain(m, p, 1, n - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* A recursive helper method to lookup the minimum number of multiplications
|
||||
* for multiplying matrices from index i to index j.
|
||||
*
|
||||
* @param m the memoization table storing the results of subproblems.
|
||||
* @param p an array of integers representing the dimensions of the matrices.
|
||||
* @param i the starting index of the matrix chain.
|
||||
* @param j the ending index of the matrix chain.
|
||||
* @return the minimum number of multiplications needed to multiply matrices
|
||||
* from i to j.
|
||||
*/
|
||||
static int lookupChain(int[][] m, int[] p, int i, int j) {
|
||||
if (i == j) {
|
||||
m[i][j] = 0;
|
||||
@ -38,11 +65,4 @@ public final class MatrixChainRecursiveTopDownMemoisation {
|
||||
}
|
||||
return m[i][j];
|
||||
}
|
||||
|
||||
// in this code we are taking the example of 4 matrixes whose orders are 1x2,2x3,3x4,4x5
|
||||
// respectively output should be Minimum number of multiplications is 38
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {1, 2, 3, 4, 5};
|
||||
System.out.println("Minimum number of multiplications is " + memoizedMatrixChain(arr));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,68 @@
|
||||
package com.thealgorithms.dynamicprogramming;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MatrixChainRecursiveTopDownMemoisationTest {
|
||||
|
||||
/**
|
||||
* Test case for four matrices with dimensions 1x2, 2x3, 3x4, and 4x5.
|
||||
* The expected minimum number of multiplications is 38.
|
||||
*/
|
||||
@Test
|
||||
void testFourMatrices() {
|
||||
int[] dimensions = {1, 2, 3, 4, 5};
|
||||
int expected = 38;
|
||||
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
|
||||
assertEquals(expected, actual, "The minimum number of multiplications should be 38.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for three matrices with dimensions 10x20, 20x30, and 30x40.
|
||||
* The expected minimum number of multiplications is 6000.
|
||||
*/
|
||||
@Test
|
||||
void testThreeMatrices() {
|
||||
int[] dimensions = {10, 20, 30, 40};
|
||||
int expected = 18000;
|
||||
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
|
||||
assertEquals(expected, actual, "The minimum number of multiplications should be 18000.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for two matrices with dimensions 5x10 and 10x20.
|
||||
* The expected minimum number of multiplications is 1000.
|
||||
*/
|
||||
@Test
|
||||
void testTwoMatrices() {
|
||||
int[] dimensions = {5, 10, 20};
|
||||
int expected = 1000;
|
||||
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
|
||||
assertEquals(expected, actual, "The minimum number of multiplications should be 1000.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for a single matrix.
|
||||
* The expected minimum number of multiplications is 0, as there are no multiplications needed.
|
||||
*/
|
||||
@Test
|
||||
void testSingleMatrix() {
|
||||
int[] dimensions = {10, 20}; // Single matrix dimensions
|
||||
int expected = 0;
|
||||
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
|
||||
assertEquals(expected, actual, "The minimum number of multiplications should be 0.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for matrices with varying dimensions.
|
||||
* The expected minimum number of multiplications is calculated based on the dimensions provided.
|
||||
*/
|
||||
@Test
|
||||
void testVaryingDimensions() {
|
||||
int[] dimensions = {2, 3, 4, 5, 6}; // Dimensions for 4 matrices
|
||||
int expected = 124; // Expected value needs to be calculated based on the problem
|
||||
int actual = MatrixChainRecursiveTopDownMemoisation.memoizedMatrixChain(dimensions);
|
||||
assertEquals(expected, actual, "The minimum number of multiplications should be 124.");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user