VOOZH about

URL: https://www.geeksforgeeks.org/dsa/printing-brackets-matrix-chain-multiplication-problem/

⇱ Printing brackets in Matrix Chain Multiplication Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Printing brackets in Matrix Chain Multiplication Problem

Last Updated : 6 Dec, 2025

Given an array arr[] which represents the chain of matrices such that the dimensions of the ith matrix are arr[i-1] x arr[i]. The task is to find the correct parenthesis of the matrices such that when we multiply all the matrices together, the cost or total number of element multiplications is minimal.

Examples:

Input: arr[] = [10, 20, 30]
Output: (AB)
Explanation: There is only one way to multiply two matrices:
(AB): The cost for the multiplication will be 6000.

Input: arr[] = [10, 20, 30, 40]
Output: ((AB)C)
Explanation: There are three possible ways to multiply four matrices:

  • ((AB)C): The cost for the multiplication will be 18,000
  • (A(BC)): The cost for the multiplication will be 32,000
  • (AB)(CD): The cost for the multiplication will be 51,000

So, the minimum cost will be achieved in the arrangement ((AB)C).

Input: arr[] = [40, 20, 30, 10, 30]
Output: ((A(BC))D)
Explanation: There are five possible ways to multiply four matrices:

  • (((AB)C)D)): The cost for the multiplication will be 48,000
  • ((A(BC))D): The cost for the multiplication will be 26,000
  • (A((BC)D)): The cost for the multiplication will be 36,000
  • ((AB)(CD)): The cost for the multiplication will be 69,000
  • (A(B(CD))): The cost for the multiplication will be 51,000

So, the minimum costs will be occurred in the arrangement ((A(BC))D).

Using Recursion - O(2^n) Time and O(n) Space

The approach is similar to recursive approach of matrix chain multiplication, with the key difference is, instead of returning optimal cost for each subproblem (i, j), we are also returning the matrix multiplication order in form of string, along with the corresponding optimal cost.


Output
((A(BC))D)

Using Top-Down DP (Memoization) - O(n^3) Time and O(n^2) Space

Let’s suppose we have four matrices (M1, M2, M3, M4). Based on the recursive approach described above, we can construct a recursion tree.

πŸ‘ Lightbox

If we observe carefully, we can see that recursive solution has these two properties of Dynamic Programming:

1) Optimal Substructure: We are breaking the bigger groups into smaller subgroups and solving them to finally find the minimum number of multiplications. Therefore, it can be said that the problem has optimal substructure property.

2) Overlapping Subproblems: We can see in the recursion tree that the same subproblems are called again and again and this problem has the Overlapping Subproblems property.

So, we create a 2D memo[][] array of size n*n, where memo[i][j] stores a pair consisting of a string(representing matrix multiplication order) and an integer(optimal cost) for multiplying matrices from i to j.


Output
((A(BC))D)

Using Bottom-Up DP(Tabulation) - O(n^3) Time and O(n^2) Space

The approach is similar to the previous one; just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner. We maintain a 2d dp[][] table, such that dp[i][j], stores the pair of matrix multiplication order and optimal cost for subproblem (i, j).


Output
((A(BC))D)
Comment