WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Dynamic Programming · Guru · question 95 of 100

Solve the problem of finding the Optimal Matrix Parenthesization with a Special Cost Function using dynamic programming.?

📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

The Optimal Matrix Parenthesization (OMP) problem is a classic example of dynamic programming approach. Given a sequence of matrices, the goal is to find the optimal way to parenthesize them so as to minimize the total number of multiplications required.

However, in this variation of the OMP problem, we are given a special cost function instead of a fixed cost for all matrix multiplication. Let the cost function be defined as follows:

- Given two matrices A and B of dimensions (m x n) and (n x p) respectively, the cost of multiplying them is m * n * p

Our task is to come up with a dynamic programming solution for this variation of the OMP problem.

Approach:

Let’s say we are given a sequence of matrices A1, A2, A3,..., An to multiply. Our approach is to recursively solve smaller subproblems and store the results of each subproblem in a table that can be reused later.

Let’s define P(i,j) to be the cost of parenthesizing the matrices from Ai to Aj, both inclusive, using the special cost function defined above.

The base case is when i=j, which means we only have one matrix and therefore no multiplication is needed, so P(i,i)=0.

For the more general case, we can define P(i,j) in terms of smaller subproblems:

P(i,j) = min{P(i,k) + P(k+1,j) + (Ai...Ak)(Ak+1...Aj)}, where i  k < j

In the above formula, we are considering all possible splits of the sequence of matrices from Ai to Aj, and finding the split that leads to the minimum cost.

We can compute the values of P(i,j) using a bottom-up approach, starting with the smallest subproblems and building up to the final solution. The below code shows a sample implementation of this algorithm in Java:

public static int optimalMatrixParenthesis(int[] matrices) {
    int n = matrices.length;
    int[][] dp = new int[n][n];
    
    // Base case - When we have only one matrix, cost is 0.
    for (int i = 0; i < n; i++) {
        dp[i][i] = 0;
    }

    // Build up the solution by considering all possible splits
    for (int len = 2; len <= n; len++) {
        for (int i = 0; i < n - len + 1; i++) {
            int j = i + len - 1;
            dp[i][j] = Integer.MAX_VALUE;

            // Consider all possible splits
            for (int k = i; k < j; k++) {
                int cost = matrices[i] * matrices[k+1] * matrices[j+1];
                int subproblemCost = dp[i][k] + dp[k+1][j];
                int temp = subproblemCost + cost;
                
                // Choose the split that leads to the minimum cost
                if (temp < dp[i][j]) {
                    dp[i][j] = temp;
                }
            }
        }
    }
    return dp[0][n-1];
}

The time complexity of this algorithm is O(n3), which is polynomial in the size of the input. Therefore, it is a very efficient way to solve this problem.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview — then scores it.
📞 Practice Dynamic Programming — free 15 min
📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

All 100 Dynamic Programming questions · All topics