Salta al contenuto
Note per Studenti Matrix Chain Multiplication Report

Matrix Chain Multiplication

In questa pagina 7

Multiplying a chain of rectangular matrices can cost much or little depending on the order of the products, although the result is always the same (the product is associative but not commutative). Choosing the best order is an optimization problem solved by dynamic programming (Dynamic Programming - Paradigm and Memoizationdynamic programming improves on divide and conquer by storing the solutions of repeated subinstances in a table; Fibonacci shows the gap (F_n - 1 additions recursively, n - 1 with a table), memoization is recursion plus a global table, and optimization problems follow a five-step scheme built on optimal substructure and cut-and-paste.Dynamic Programming - Paradigm and Memoization →).

The problem

Two matrices A∈Cp×qA \in \mathbb{C}^{p \times q} and B∈Cq×rB \in \mathbb{C}^{q \times r} are compatible; their product is C=AB∈Cp×rC = AB \in \mathbb{C}^{p \times r} with cij=∑k=1qaikbkjc_{ij} = \sum_{k=1}^{q} a_{ik} b_{kj}.

RECT_MAT(A, B)
  p <- rows(A); q <- columns(A); r <- columns(B)
  for i <- 1 to p do
    for j <- 1 to r do
      c_ij <- a_i1 * b_1j
      for k <- 2 to q do c_ij <- c_ij + a_ik * b_kj
  return C

It performs exactly p⋅q⋅rp \cdot q \cdot r products between scalars (we count only products).

We want the product of a chain ⟨A1,…,An⟩\langle A_1, \dots, A_n \rangle of compatible matrices. Compatibility means n+1n + 1 numbers suffice: the vector p=(p0,p1,…,pn)∈(N+)n+1p = (p_0, p_1, \dots, p_n) \in (\mathbb{N}^+)^{n+1}, with Ai∈Cpi−1×piA_i \in \mathbb{C}^{p_{i-1} \times p_i}.

Parenthesizations

Every execution order is a full parenthesization, represented by a full binary tree: leaves are the matrices, internal nodes are products. A full binary tree with nn leaves has n−1n - 1 internal nodes, so every parenthesization performs exactly n−1n - 1 matrix products. For n=3n = 3 there are two, for n=4n = 4 five:

(((A1 A2) A3) A4)    ((A1 A2) (A3 A4))    (A1 (A2 (A3 A4)))    ((A1 (A2 A3)) A4)    (A1 ((A2 A3) A4))

The number of scalar products depends on the parenthesization.

Example. A1∈C10×100A_1 \in \mathbb{C}^{10 \times 100}, A2∈C100×5A_2 \in \mathbb{C}^{100 \times 5}, A3∈C5×50A_3 \in \mathbb{C}^{5 \times 50}, i.e. p=(10,100,5,50)p = (10, 100, 5, 50).

  • ((A1A2)A3)((A_1 A_2) A_3): A1A2A_1 A_2 costs 10⋅100⋅5=500010 \cdot 100 \cdot 5 = 5000 and gives a 10×510 \times 5 matrix; then 10⋅5⋅50=250010 \cdot 5 \cdot 50 = 2500. Total 75007500.
  • (A1(A2A3))(A_1 (A_2 A_3)): A2A3A_2 A_3 costs 100⋅5⋅50=25 000100 \cdot 5 \cdot 50 = 25\,000 and gives 100×50100 \times 50; then 10⋅100⋅50=50 00010 \cdot 100 \cdot 50 = 50\,000. Total 75 00075\,000.

Definition (MCM). Given p∈(N+)n+1p \in (\mathbb{N}^+)^{n+1}, find the parenthesization of the associated chain requiring the minimum number of scalar products.

The instance is only the vector pp.

How many parenthesizations?

Let P(n)P(n) be the number of parenthesizations of nn matrices. The last product splits the chain into ⟨A1,…,Ak⟩\langle A_1, \dots, A_k \rangle and ⟨Ak+1,…,An⟩\langle A_{k+1}, \dots, A_n \rangle, 1≤k≤n−11 \le k \le n - 1:

P(n)={1n=1∑k=1n−1P(k) P(n−k)n>1P(n) = \begin{cases} 1 & n = 1 \\ \sum_{k=1}^{n-1} P(k)\, P(n-k) & n > 1 \end{cases}

The first values are 1,1,2,5,14,42,132,429,1430,4862,…1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, \dots (Catalan numbers).

Theorem. P(n)≥2n−1nP(n) \ge \dfrac{2^{n-1}}{n} for every n≥1n \ge 1.

Proof. Base: P(1)=1=20/1P(1) = 1 = 2^0 / 1, P(2)=1=21/2P(2) = 1 = 2^1 / 2. Step: let n≥3n \ge 3 and assume the claim for smaller values. Then

P(n)≥∑k=1n−12k−1k⋅2n−k−1n−k=2n−2∑k=1n−11k(n−k)≥2n−2(1n−1+1n−1)=2n−1n−1≥2n−1n.P(n) \ge \sum_{k=1}^{n-1} \frac{2^{k-1}}{k} \cdot \frac{2^{n-k-1}}{n-k} = 2^{n-2} \sum_{k=1}^{n-1} \frac{1}{k(n-k)} \ge 2^{n-2}\left(\frac{1}{n-1} + \frac{1}{n-1}\right) = \frac{2^{n-1}}{n-1} \ge \frac{2^{n-1}}{n}.

The sum was bounded below by the terms k=1k = 1 and k=n−1k = n - 1, distinct because n≥3n \ge 3. □\square

So parenthesizations are exponential and exhaustive search is impractical. Actually P(n)=1n(2(n−1)n−1)=Θ ⁣(4nn3/2)P(n) = \frac{1}{n}\binom{2(n-1)}{n-1} = \Theta\!\left(\frac{4^n}{n^{3/2}}\right) (Catalan formula and Stirling).

Optimal substructure

A parenthesization of ⟨A1,…,An⟩\langle A_1, \dots, A_n \rangle contains parenthesizations of subchains, so the natural subproblem space is the set of non-empty subchains ⟨Ai,…,Aj⟩\langle A_i, \dots, A_j \rangle, 1≤i≤j≤n1 \le i \le j \le n: Θ(n2)\Theta(n^2) of them. Write Ai..j=Ai⋯Aj∈Cpi−1×pjA_{i..j} = A_i \cdots A_j \in \mathbb{C}^{p_{i-1} \times p_j}, Ti..jT_{i..j} for a parenthesization tree, c(Ti..j)c(T_{i..j}) for its cost and Ti..j∗T^*_{i..j} for an optimal one. We look for T1..n∗T^*_{1..n}.

Base cases: a subchain ⟨Ai⟩\langle A_i \rangle has a single one-node tree, of cost 00.

Theorem (optimal substructure). Let i<ji < j and Ti..j∗T^*_{i..j} optimal for ⟨Ai,…,Aj⟩\langle A_i, \dots, A_j \rangle. Then there is k∗k^* with i≤k∗<ji \le k^* < j such that the root has as subtrees two optimal trees Ti..k∗∗T^*_{i..k^*} and Tk∗+1..j∗T^*_{k^*+1..j}, and c(Ti..j∗)=c(Ti..k∗∗)+c(Tk∗+1..j∗)+pi−1 pk∗ pj,c(T^*_{i..j}) = c(T^*_{i..k^*}) + c(T^*_{k^*+1..j}) + p_{i-1}\, p_{k^*}\, p_j, where k∗k^* minimizes c(Ti..k∗)+c(Tk+1..j∗)+pi−1pkpjc(T^*_{i..k}) + c(T^*_{k+1..j}) + p_{i-1} p_k p_j over i≤k<ji \le k < j.

Proof. The tree has at least two leaves, so the root has two subtrees Ti..k∗T_{i..k^*} and Tk∗+1..jT_{k^*+1..j}: the last product is Ai..k∗⋅Ak∗+1..jA_{i..k^*} \cdot A_{k^*+1..j}, of dimensions pi−1×pk∗p_{i-1} \times p_{k^*} and pk∗×pjp_{k^*} \times p_j, costing pi−1pk∗pjp_{i-1} p_{k^*} p_j.

The subtrees are optimal. If one of them (say Ti..k∗T_{i..k^*}) were not optimal, replace it with an optimal Ti..k∗∗T^*_{i..k^*}: the new parenthesization is still feasible (same dimensions) and costs strictly less than Ti..j∗T^*_{i..j}, a contradiction. This is cut-and-paste.

Choice of k∗k^*. For each kk, joining Ti..k∗T^*_{i..k} and Tk+1..j∗T^*_{k+1..j} under a new root is feasible with cost c(Ti..k∗)+c(Tk+1..j∗)+pi−1pkpjc(T^*_{i..k}) + c(T^*_{k+1..j}) + p_{i-1} p_k p_j. If k∗k^* did not minimize it, the minimizer would beat Ti..j∗T^*_{i..j}. □\square

The value k∗k^* is the only information needed to rebuild the optimal tree.

Cost recurrence

With c(i,j)=c(Ti..j∗)c(i, j) = c(T^*_{i..j}):

c(i,j)={0i=jmin⁡i≤k<j{ c(i,k)+c(k+1,j)+pi−1 pk pj }1≤i<j≤nc(i, j) = \begin{cases} 0 & i = j \\ \displaystyle\min_{i \le k < j} \big\{\, c(i, k) + c(k+1, j) + p_{i-1}\, p_k\, p_j \,\big\} & 1 \le i < j \le n \end{cases}

The answer is c(1,n)c(1, n).

The divide and conquer algorithm

R_MCM(p, i, j)
  if i = j then return 0
  m <- +infinity
  for k <- i to j-1 do
    q <- R_MCM(p, i, k) + R_MCM(p, k+1, j) + p_{i-1} * p_k * p_j
    if q < m then m <- q
  return m

Counting the multiplications between dimensions (two per kk), the cost for a chain of nn matrices is

T(n)={0n=1∑k=1n−1(T(k)+T(n−k)+2)=2(n−1)+2∑k=1n−1T(k)n>1T(n) = \begin{cases} 0 & n = 1 \\ \sum_{k=1}^{n-1}\big(T(k) + T(n-k) + 2\big) = 2(n-1) + 2\sum_{k=1}^{n-1} T(k) & n > 1 \end{cases}

Exponential lower bound. T(n)≥2n−2T(n) \ge 2^n - 2 by induction. Base: T(1)=0=2−2T(1) = 0 = 2 - 2, T(2)=2=4−2T(2) = 2 = 4 - 2. For n≥3n \ge 3, keeping only the term k=n−1k = n - 1:

T(n)≥2(n−1)+2 T(n−1)≥4+2(2n−1−2)=2n.T(n) \ge 2(n-1) + 2\,T(n-1) \ge 4 + 2(2^{n-1} - 2) = 2^n.

Exact value: T(n)=3n−1−1T(n) = 3^{n-1} - 1. For n≥3n \ge 3 subtract the recurrences of nn and n−1n - 1:

T(n)−T(n−1)=2+2 T(n−1)⟹T(n)=3 T(n−1)+2,T(n) - T(n-1) = 2 + 2\,T(n-1) \quad \Longrightarrow \quad T(n) = 3\,T(n-1) + 2,

which also holds for n=2n = 2. Adding 11 to both sides, T(n)+1=3 (T(n−1)+1)T(n) + 1 = 3\,(T(n-1) + 1), hence T(n)+1=3n−1T(n) + 1 = 3^{n-1}. For n=4n = 4 this is 2626, for n=10n = 10 it is 19 68219\,682.

Why? Repeated subinstances. For n=4n = 4 there are 27=3n−127 = 3^{n-1} calls to R_MCM(p, i, j) and each subchain appears several times:

subchain (1,1)(1,1) (2,2)(2,2) (3,3)(3,3) (4,4)(4,4) (1,2)(1,2) (2,3)(2,3) (3,4)(3,4) (1,3)(1,3) (2,4)(2,4) (1,4)(1,4)
calls 4 5 5 4 2 2 2 1 1 1

The dynamic programming approach

Two tables m[1..n,1..n]m[1..n, 1..n] and s[1..n,1..n]s[1..n, 1..n], using only the upper triangle (i≤ji \le j): m[i,j]m[i, j] holds c(i,j)c(i, j) and s[i,j]s[i, j] holds k∗k^* (additional information). Base cases m[i,i]=0m[i, i] = 0 lie on the main diagonal.

Fill order. Computing m[i,j]m[i, j] needs m[i,k]m[i, k] (same row, to the left) and m[k+1,j]m[k+1, j] (same column, below), for i≤k<ji \le k < j. Scanning by increasing rows or by increasing columns does not work: in both, m[k+1,j]m[k+1, j] would be computed after m[i,j]m[i, j]. But the needed values lie on diagonals closer to the main one. Diagonal ℓ\ell contains the cells with j−i+1=ℓj - i + 1 = \ell, i.e. the subchains of length ℓ\ell; the cells that (i,j)(i, j) depends on are shorter subchains, on diagonals ℓ′<ℓ\ell' < \ell. A scan by increasing diagonal length respects the dependencies. On diagonal ℓ\ell the row index ii runs from 11 to n−ℓ+1n - \ell + 1 and j=i+ℓ−1j = i + \ell - 1.

MCM(p)
  n <- length(p) - 1
  for i <- 1 to n do m[i, i] <- 0
  for l <- 2 to n do
    for i <- 1 to n - l + 1 do
      j <- i + l - 1
      m[i, j] <- +infinity
      for k <- i to j - 1 do
        q <- m[i, k] + m[k+1, j] + p_{i-1} * p_k * p_j
        if q < m[i, j] then
          m[i, j] <- q
          s[i, j] <- k
  return (m[1, n], s)

Cost (exact sum). With cost 22 for each value of kk:

T(n)=∑ℓ=2n∑i=1n−ℓ+1∑k=ii+ℓ−22=2∑ℓ=2n(ℓ−1)(n−ℓ+1)=2∑ℓ′=1n−1ℓ′ (n−ℓ′)=2n⋅n(n−1)2−2⋅(n−1) n (2n−1)6=n(n−1)(n−2n−13)=n3−n3,\begin{aligned} T(n) &= \sum_{\ell=2}^{n} \sum_{i=1}^{n-\ell+1} \sum_{k=i}^{i+\ell-2} 2 = 2\sum_{\ell=2}^{n} (\ell - 1)(n - \ell + 1) = 2\sum_{\ell'=1}^{n-1} \ell'\,(n - \ell') \\ &= 2n \cdot \frac{n(n-1)}{2} - 2\cdot\frac{(n-1)\,n\,(2n-1)}{6} = n(n-1)\Big(n - \frac{2n-1}{3}\Big) = \frac{n^3 - n}{3}, \end{aligned}

using Gauss' sum and the sum of squares. So T(n)=Θ(n3)T(n) = \Theta(n^3), against 3n−1−13^{n-1} - 1 of D&C (for n=10n = 10: 330330 against 19 68219\,682).

Other valid scans. By rows from row nn down to row 11, each from m[i,i]m[i, i] to m[i,n]m[i, n]; or by columns, each from the bottom up. Both respect the dependency graph.

Worked example

p=(4,10,3,12,20,7)p = (4, 10, 3, 12, 20, 7), i.e. n=5n = 5: A1∈4×10A_1 \in 4 \times 10, A2∈10×3A_2 \in 10 \times 3, A3∈3×12A_3 \in 3 \times 12, A4∈12×20A_4 \in 12 \times 20, A5∈20×7A_5 \in 20 \times 7.

Table m[i,j]m[i, j]:

i\ji \backslash j 1 2 3 4 5
1 0 120 264 1080 1344
2 0 360 1320 1350
3 0 720 1140
4 0 1680
5 0

Table s[i,j]s[i, j]:

i\ji \backslash j 2 3 4 5
1 1 2 2 2
2 2 2 2
3 3 4
4 4

Diagonal by diagonal:

  • ℓ=2\ell = 2: m[i,i+1]=pi−1pipi+1m[i, i+1] = p_{i-1} p_i p_{i+1}: m[1,2]=4⋅10⋅3=120m[1,2] = 4 \cdot 10 \cdot 3 = 120; m[2,3]=360m[2,3] = 360; m[3,4]=720m[3,4] = 720; m[4,5]=1680m[4,5] = 1680.
  • ℓ=3\ell = 3, cell (1,3)(1, 3): k=1k = 1: 0+360+4⋅10⋅12=8400 + 360 + 4 \cdot 10 \cdot 12 = 840; k=2k = 2: 120+0+4⋅3⋅12=264120 + 0 + 4 \cdot 3 \cdot 12 = 264. Minimum 264264 with k=2k = 2. Similarly m[2,4]=min⁡{0+720+600, 360+0+2400}=1320m[2,4] = \min\{0 + 720 + 600,\ 360 + 0 + 2400\} = 1320 and m[3,5]=min⁡{0+1680+252, 720+0+420}=1140m[3,5] = \min\{0 + 1680 + 252,\ 720 + 0 + 420\} = 1140 with k=4k = 4.
  • ℓ=4\ell = 4, cell (1,4)(1, 4): k=1k = 1: 0+1320+800=21200 + 1320 + 800 = 2120; k=2k = 2: 120+720+240=1080120 + 720 + 240 = 1080; k=3k = 3: 264+0+960=1224264 + 0 + 960 = 1224. Minimum 10801080 with k=2k = 2. For (2,5)(2, 5): k=2k = 2: 0+1140+210=13500 + 1140 + 210 = 1350; k=3k = 3: 360+1680+840=2880360 + 1680 + 840 = 2880; k=4k = 4: 1320+0+1400=27201320 + 0 + 1400 = 2720; minimum 13501350.
  • ℓ=5\ell = 5, cell (1,5)(1, 5): k=1k = 1: 0+1350+280=16300 + 1350 + 280 = 1630; k=2k = 2: 120+1140+84=1344120 + 1140 + 84 = 1344; k=3k = 3: 264+1680+336=2280264 + 1680 + 336 = 2280; k=4k = 4: 1080+0+560=16401080 + 0 + 560 = 1640. Minimum 13441344 with k=2k = 2.

Optimal cost m[1,5]=1344m[1, 5] = 1344. Reconstruction: s[1,5]=2s[1,5] = 2 splits into ⟨A1,A2⟩\langle A_1, A_2 \rangle and ⟨A3,A4,A5⟩\langle A_3, A_4, A_5 \rangle; s[3,5]=4s[3,5] = 4 splits the second into ⟨A3,A4⟩\langle A_3, A_4 \rangle and ⟨A5⟩\langle A_5 \rangle. Parenthesization: ((A1A2)((A3A4)A5))((A_1 A_2)((A_3 A_4) A_5)). Check: 120+720+3⋅20⋅7+4⋅3⋅7=120+720+420+84=1344120 + 720 + 3 \cdot 20 \cdot 7 + 4 \cdot 3 \cdot 7 = 120 + 720 + 420 + 84 = 1344. Of the 1414 possible orders this is the only optimal one, and the worst costs 44404440.

Printing the parenthesization

PRINT_MCM(s, i, j)
  if i = j then
    print("A_i")
    return
  print("(")
  PRINT_MCM(s, i, s[i, j])
  PRINT_MCM(s, s[i, j] + 1, j)
  print(")")

The return after the base case is needed (otherwise the code would read the undefined s[i,i]s[i,i]). It prints one parenthesis per internal node (2(n−1)2(n-1)) and one character per leaf (nn): T(n)=3n−2T(n) = 3n - 2 characters.

The memoized approach

Same substructure, with recursion and a default for uncomputed cells. The sentinel is +∞+\infty: it marks cells to compute and correctly initializes the running minimum (base cases are already 00).

INIT_MCM(p)
  n <- length(p) - 1
  if n = 1 then return 0
  for i <- 1 to n do
    m[i, i] <- 0
    for j <- i + 1 to n do m[i, j] <- +infinity
  return REC_MCM(p, 1, n)

REC_MCM(p, i, j)
  if m[i, j] = +infinity then
    for k <- i to j - 1 do
      q <- REC_MCM(p, i, k) + REC_MCM(p, k+1, j) + p_{i-1} * p_k * p_j
      if q < m[i, j] then m[i, j] <- q
  return m[i, j]

Cost. Each non-base subinstance (i,j)(i, j) is an internal node exactly once, with work 2(j−i)2(j - i), and from (1,n)(1, n) every pair 1≤i<j≤n1 \le i < j \le n is reached. Hence

T(n)=∑i=1n−1∑j=i+1n2(j−i)=2∑d=1n−1(n−d) d=n3−n3.T(n) = \sum_{i=1}^{n-1}\sum_{j=i+1}^{n} 2(j - i) = 2\sum_{d=1}^{n-1} (n - d)\, d = \frac{n^3 - n}{3}.

Unlike LCS, memoization never saves anything: all subinstances must be solved anyway and the top-down phase cannot skip any. Its benefit is not having to design the diagonal fill order.

Typical exam questions

  1. For p=(1,9,10,4)p = (1, 9, 10, 4) (three matrices), how many scalar products does the optimal parenthesization need? Answer: 130130, with ((A1A2)A3)((A_1 A_2) A_3) (90+4090 + 40; the other order costs 360+36=396360 + 36 = 396).
  2. How many parenthesizations does a chain of 55 matrices have, and how many scalar products does MCM perform for n=10n = 10? Answer: P(5)=14P(5) = 14; n3−n3=330\frac{n^3 - n}{3} = 330.
  3. Why is D&C exponential and DP cubic? Why does the row scan fail? Answer: repeated subinstances (subchains are Θ(n2)\Theta(n^2) but calls are 3n−13^{n-1}); m[i,j]m[i, j] depends on m[k+1,j]m[k+1, j] in later rows, not ready for increasing rows; ready for diagonals.

Versione ripasso

  • Problem. Chain ⟨A1,…,An⟩\langle A_1, \dots, A_n \rangle, Ai∈Cpi−1×piA_i \in \mathbb{C}^{p_{i-1} \times p_i}, instance p=(p0,…,pn)p = (p_0, \dots, p_n). A p×qp \times q times q×rq \times r product costs pqrpqr scalar products. A parenthesization is a full binary tree, always n−1n - 1 matrix products, variable scalar cost. For p=(10,100,5,50)p = (10, 100, 5, 50): ((A1A2)A3)((A_1A_2)A_3) costs 5000+2500=75005000 + 2500 = 7500, (A1(A2A3))(A_1(A_2A_3)) costs 25 000+50 000=75 00025\,000 + 50\,000 = 75\,000.
  • Parenthesizations: P(1)=1P(1) = 1, P(n)=∑k=1n−1P(k)P(n−k)P(n) = \sum_{k=1}^{n-1} P(k)P(n-k) (Catalan: 1,1,2,5,14,42,…1, 1, 2, 5, 14, 42, \dots), P(n)≥2n−1/nP(n) \ge 2^{n-1}/n by induction (keep terms k=1k = 1, k=n−1k = n-1 of ∑1k(n−k)\sum \frac{1}{k(n-k)}), P(n)=1n(2(n−1)n−1)=Θ(4n/n3/2)P(n) = \frac{1}{n}\binom{2(n-1)}{n-1} = \Theta(4^n / n^{3/2}).
  • Substructure (cut-and-paste): in Ti..j∗T^*_{i..j} the root splits into Ti..k∗∗T^*_{i..k^*} and Tk∗+1..j∗T^*_{k^*+1..j}, both optimal, with c(Ti..j∗)=c(Ti..k∗∗)+c(Tk∗+1..j∗)+pi−1pk∗pjc(T^*_{i..j}) = c(T^*_{i..k^*}) + c(T^*_{k^*+1..j}) + p_{i-1} p_{k^*} p_j; k∗k^* minimizes the sum. Base: one matrix, cost 00.
  • Recurrence: c(i,i)=0c(i,i) = 0; c(i,j)=min⁡i≤k<j{c(i,k)+c(k+1,j)+pi−1pkpj}c(i,j) = \min_{i \le k < j}\{c(i,k) + c(k+1,j) + p_{i-1} p_k p_j\}. Additional information s[i,j]=k∗s[i,j] = k^*.
  • D&C R_MCM: T(n)=∑k=1n−1(T(k)+T(n−k)+2)T(n) = \sum_{k=1}^{n-1}(T(k) + T(n-k) + 2), T(n)≥2n−2T(n) \ge 2^n - 2; exactly T(n)=3T(n−1)+2T(n) = 3T(n-1) + 2, i.e. T(n)=3n−1−1T(n) = 3^{n-1} - 1 (2626 for n=4n = 4). For n=4n = 4 there are 2727 calls and (1,1),(2,2)(1,1), (2,2) etc. repeat 44-55 times.
  • Bottom-up by diagonals ℓ=2..n\ell = 2..n, i=1..n−ℓ+1i = 1..n-\ell+1, j=i+ℓ−1j = i+\ell-1: increasing rows or columns fail (m[k+1,j]m[k+1,j] not ready); rows from nn to 11 or columns bottom-up also work. Cost ∑ℓ=2n2(ℓ−1)(n−ℓ+1)=n3−n3=Θ(n3)\sum_{\ell=2}^{n} 2(\ell-1)(n-\ell+1) = \frac{n^3-n}{3} = \Theta(n^3).
MCM(p): n <- length(p)-1; for i <- 1 to n do m[i,i] <- 0
  for l <- 2 to n do for i <- 1 to n-l+1 do
    j <- i+l-1; m[i,j] <- +inf
    for k <- i to j-1 do
      q <- m[i,k] + m[k+1,j] + p_{i-1} p_k p_j
      if q < m[i,j] then m[i,j] <- q; s[i,j] <- k
  return (m[1,n], s)
PRINT_MCM(s,i,j): if i = j then print A_i, return; print "("; PRINT_MCM(s,i,s[i,j]); PRINT_MCM(s,s[i,j]+1,j); print ")"
  • Example p=(4,10,3,12,20,7)p = (4, 10, 3, 12, 20, 7): m[1,2..5]=120,264,1080,1344m[1,2..5] = 120, 264, 1080, 1344; m[2,3..5]=360,1320,1350m[2,3..5] = 360, 1320, 1350; m[3,4..5]=720,1140m[3,4..5] = 720, 1140; m[4,5]=1680m[4,5] = 1680. s[1,⋅]=1,2,2,2s[1,\cdot] = 1, 2, 2, 2; s[2,⋅]=2,2,2s[2,\cdot] = 2, 2, 2; s[3,4]=3s[3,4] = 3, s[3,5]=4s[3,5] = 4; s[4,5]=4s[4,5] = 4. Cell (1,5)(1,5): k=1..4k = 1..4 give 1630,1344,2280,16401630, 1344, 2280, 1640. Optimum 13441344: ((A1A2)((A3A4)A5))((A_1A_2)((A_3A_4)A_5)) with costs 120+720+420+84120 + 720 + 420 + 84; worst of the 1414 costs 44404440. Printing takes 3n−23n - 2 characters.
  • Memoized: sentinel +∞+\infty; each (i,j)(i,j) is an internal node once with work 2(j−i)2(j-i), all subinstances reached: T(n)=2∑d=1n−1(n−d) d=n3−n3T(n) = 2\sum_{d=1}^{n-1}(n-d)\,d = \frac{n^3-n}{3}, never better than bottom-up (unlike LCS).
REC_MCM(p, i, j)                    { m[i,i] = 0, other cells = +inf }
  if m[i,j] = +inf then
    for k <- i to j-1 do
      q <- REC_MCM(p,i,k) + REC_MCM(p,k+1,j) + p_{i-1} p_k p_j
      if q < m[i,j] then m[i,j] <- q
  return m[i,j]
  • Typical questions: p=(1,9,10,4)p = (1, 9, 10, 4): ((A1A2)A3)((A_1A_2)A_3) costs 90+40=13090 + 40 = 130, the other 360+36=396360 + 36 = 396; P(5)=14P(5) = 14; products of MCM for n=10n = 10: 330330; D&C against DP for n=10n = 10: 19 68219\,682 against 330330.
  • Typical mistakes: scanning by increasing rows or columns; writing pipkpjp_i p_k p_j for pi−1pkpjp_{i-1} p_k p_j; counting also additions; forgetting return in PRINT_MCM; believing memoization reduces the work (here it does not).

Teoria collegata