Skip to content

Create Matrix_chain_multiplication_alogorithm_problem_DP.java #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Matrix_chain_multiplication_alogorithm_problem_DP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Main
{
// Function to find the most efficient way to multiply
// a given sequence of matrices
public static int matrixChainMultiplication(int[] dims, int i, int j)
{
// base case: one matrix
if (j <= i + 1) {
return 0;
}

// stores the minimum number of scalar multiplications (i.e., cost)
// needed to compute matrix `M[i+1] … M[j] = M[i…j]`
int min = Integer.MAX_VALUE;

// take the minimum over each possible position at which the
// sequence of matrices can be split

/*
(M[i+1]) × (M[i+2]………………M[j])
(M[i+1]M[i+2]) × (M[i+3…………M[j])
(M[i+1]M[i+2]…………M[j-1]) × (M[j])
*/

for (int k = i + 1; k <= j - 1; k++)
{
// recur for `M[i+1]…M[k]` to get an `i × k` matrix
int cost = matrixChainMultiplication(dims, i, k);

// recur for `M[k+1]…M[j]` to get an `k × j` matrix
cost += matrixChainMultiplication(dims, k, j);

// cost to multiply two `i × k` and `k × j` matrix
cost += dims[i] * dims[k] * dims[j];

if (cost < min) {
min = cost;
}
}

// return the minimum cost to multiply `M[j+1]…M[j]`
return min;
}

// Matrix Chain Multiplication Problem
public static void main(String[] args)
{
// Matrix `M[i]` has dimension `dims[i-1] × dims[i]` for `i=1…n`
// input is 10 × 30 matrix, 30 × 5 matrix, 5 × 60 matrix
int[] dims = { 10, 30, 5, 60 };

System.out.print("The minimum cost is " +
matrixChainMultiplication(dims, 0, dims.length - 1));
}
}