The user wants me to create a dynamic programming algorithm for optimal matrix chain multiplication, which includes an explanation and implementation, likely in pseudocode and maybe Python. I need to present this concisely but with enough detail to avoid being overly simplistic.
I'll start with an overview using n matrices A1..An and their respective dimensions. Then, I’ll outline the dynamic programming solution, detailing the recurrence relation, the DP table, implementation, and the time complexity.Implementing matrix chain multiplication
I'm developing a dynamic programming solution for matrix chain multiplication. I'll outline the algorithm, focusing on chain lengths from L = 2 to n. The main loop sets initial costs and calculates the minimum number of multiplications using a nested loop for split points.
I'll return both the cost and the optimal split indices for reconstructing the parenthesization. For implementation, I'll provide a Python function matrix_chain_order(p) that will return the cost and split indices, ensuring the code is well-commented, and under about 120 lines.Finalizing computation for matrix multiplication
I'm computing the minimum cost by iterating over chain lengths from L = 2 to n. Using 0-based indexing, I set indices i and j, and compute the cost by iterating through k. It's important to remember that A_i corresponds to dimensions based on p values.
I'll return the final cost and the function to build the optimal parenthesization. Now, I'm preparing the final answer, making sure it's clear and well-organized for the user! Here is a standard dynamic programming implementation of optimal matrix chain multiplication, with both the cost and the optimal parenthesization.
The goal is to find the parenthesization of (A_1 A_2 \cdots A_n) that minimizes the total number of scalar multiplications.
Idea of the DP algorithm
Define:
m[i][j] = minimum number of scalar multiplications needed to compute the product (A_i \dots A_j)
s[i][j] = index k where the optimal split between (A_i \dots A_j) occurs:
[
(A_i \dots A_k)(A_{k+1} \dots A_j)
]
Recurrence (1-based in math, but 0-based in code):
Base case: m[i][i] = 0 (single matrix, no multiplication)
For chain length L = 2..n:
[
m[i][j] = \min_{i \le k < j} \left( m[i][k] + m[k+1][j] + p_{i-1} \cdot p_k \cdot p_j \right)
]
Python implementation
python
from math import inf
def matrix_chain_order(p):
"""
Given dimensions p[0..n] where matrix A_i has size p[i-1] x p[i],
compute the DP tables for optimal matrix-chain multiplication.
Returns:
m: 2D list, m[i][j] is minimal scalar multiplications for A_i..A_j
s: 2D list, s[i][j] is index k where optimal split occurs
"""
n = len(p) - 1 # number of matrices A1..An
# m[i][j]: minimum scalar multiplications for Ai..Aj (0-based: A0..A{n-1})
m = [[0] * n for _ in range(n)]
# s[i][j]: split point k for optimal cost
s = [[0] * n for _ in range(n)]
# L is the chain length
for L in range(2, n + 1): # L = 2..n
for i in range(0, n - L + 1):
j = i + L - 1
m[i][j] = inf
# try all possible split points k
for k in range(i, j):
# cost = cost of left + cost of right + cost of multiplying the two results
cost = (
m[i][k]
+ m[k + 1][j]
+ p[i] * p[k + 1] * p[j + 1] # dimensions: p[i] x p[k+1] x p[j+1]
)
if cost < m[i][j]:
m[i][j] = cost
s[i][j] = k
return m, s
def build_optimal_parens(s, i, j):
"""
Reconstruct optimal parenthesization using the split table s.
i, j are 0-based indices of matrices A_i..A_j.
"""
if i == j:
return f"A{i+1}" # name matrices A1, A2, ...
k = s[i][j]
left = build_optimal_parens(s, i, k)
right = build_optimal_parens(s, k + 1, j)
return f"({left} x {right})"
def optimal_matrix_chain(p):
"""
Convenience wrapper: given dimension array p, return
(min_cost, parenthesization_string)
"""
m, s = matrix_chain_order(p)
n = len(p) - 1
parenthesization = build_optimal_parens(s, 0, n - 1)
return m[0][n - 1], parenthesization
# Example usage:
if __name__ == "__main__":
# A1: 30x35, A2: 35x15, A3: 15x5, A4: 5x10, A5: 10x20, A6: 20x25
p = [30, 35, 15, 5, 10, 20, 25]
cost, parens = optimal_matrix_chain(p)
print("Minimum number of multiplications:", cost)
print("Optimal parenthesization:", parens)
Complexity
Time complexity: (O(n^3)) for (n) matrices
Space complexity: (O(n^2)) for the DP tables m and s
If you want this in a different language (C/C++/Java/etc.), I can translate the same algorithm.