• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Solve the Matrix Chain Multiplication Problem

laptop infographic online business 6087062

Introduction

Matrix Chain Multiplication is a classic optimization problem in dynamic programming. It focuses on determining the most efficient way to multiply a given sequence of matrices.The crucial insight is that changing the order of matrix multiplication can greatly impact the total number of scalar operations needed. This problem is not about actually performing the multiplication but finding the optimal parenthesization of the matrices.

Problem Statement

Given a sequence of matrices, the goal is to find the minimum number of scalar multiplications needed to compute the product. A matrix A of size p × q multiplied by matrix B of size q × r takes p * q * r scalar multiplications. The challenge is that matrix multiplication is associative, so we can parenthesize the product in many ways, but the number of operations can vary dramatically depending on that order.

Approach

To solve this problem efficiently, we use dynamic programming. The idea is to break the problem into smaller subproblems, solve them once, and store their results to avoid recomputation. We define a 2D table dp where dp[i][j] holds the minimum number of multiplications needed to multiply the chain of matrices from index i to j.

Algorithm

  1. Let n be the number of matrices, where matrix Ai has dimension p[i-1] x p[i].
  2. Create a 2D array dp[n][n] initialized with 0 on the diagonal and infinity elsewhere.
  3. Iterate over chain lengths from 2 to n.
  4. For each possible starting index i, compute the corresponding end index j = i + chainLen - 1.
  5. Try every possible position k to split the product, and update dp[i][j] with the minimum cost:
    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j]).

Java Example

public class MatrixChainMultiplication {

    public static int matrixChainOrder(int[] dims) {
        int n = dims.length - 1; // Number of matrices
        int[][] dp = new int[n][n];

        for (int len = 2; len <= n; len++) {
            for (int i = 0; i <= n - len; i++) {
                int j = i + len - 1;
                dp[i][j] = Integer.MAX_VALUE;

                for (int k = i; k < j; k++) {
                    int cost = dp[i][k] + dp[k + 1][j] + dims[i] * dims[k + 1] * dims[j + 1];
                    if (cost < dp[i][j]) {
                        dp[i][j] = cost;
                    }
                }
            }
        }

        return dp[0][n - 1];
    }

    public static void main(String[] args) {
        int[] dimensions = {40, 20, 30, 10, 30}; // 4 matrices: 40x20, 20x30, 30x10, 10x30
        int minCost = matrixChainOrder(dimensions);
        System.out.println("Minimum number of multiplications: " + minCost);
    }
}

Example Explanation

In the example above, the matrix dimensions array {40, 20, 30, 10, 30} represents four matrices:

  • A1: 40 x 20
  • A2: 20 x 30
  • A3: 30 x 10
  • A4: 10 x 30

The program calculates the most efficient order to multiply them. It outputs the minimum number of scalar multiplications required to multiply the chain of matrices, which is 26000 for this input.

Conclusion

Matrix Chain Multiplication is a fundamental example of dynamic programming in action. Instead of solving the problem with brute force, which has exponential complexity, the dynamic approach brings it down to polynomial time. Understanding this technique is essential for mastering optimization problems and efficient computation strategies in computer science.

You can find the example code on Github.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions