# Review 8-5 * Hajin Ju, 2024062806 ## Problem 1 The minimum number of scalar multiplications for computing $A_iA_{i+1}\dots A_j$, denoted by $m[i, j]$, is as follows. Fill in the blanks. ### Solution 1 $$m[i,j] = \begin{cases} 0 & \text{if}\; i = j\\ \min_{i\leq k < j}{(m[i][k] + m[k+1][j] + p_{i-1}p_{k}p_j)} & \text{if}\;{i < j} \end{cases}$$ ## Problem 2 Compute (a)$m [2, 5]$ and (b)$s[2, 5]$ in the following example and parenthesize (c) the prodct $A_1A_2A_3A_4A_5A_6$ fully to minimize the number of scalar multiplications. | $m$ | 1 | 2 | 3 | 4 | 5 | 6 | | :---: | ---: | ----: | ---: | ---: | ----: | ----: | | 1 | 0 | 15750 | 7875 | 9375 | 11875 | 15125 | | 2 | | 0 | 2625 | 4375 | (a) | 10500 | | 3 | | | 0 | 750 | 2500 | 5375 | | 4 | | | | 0 | 1000 | 3500 | | 5 | | | | | 0 | 5000 | | 6 | | | | | | 0 | | $s$ | 2 | 3 | 4 | 5 | 6 | | :---: | --- | --- | --- | --- | --- | | 1 | 1 | 1 | 3 | 3 | 3 | | 2 | | 2 | 3 | (b) | 3 | | 3 | | | 3 | 3 | 3 | | 4 | | | | 4 | 5 | | 5 | | | | | 5 | | 6 | | | | | | | name | matrix dimension | | :-----: | ---------------- | | $A_1$ | $30\times 35$ | | $A_2$ | $35\times 15$ | | $A_3$ | $15\times 5$ | | $A_4$ | $5\times 10$ | | $A_5$ | $10\times 20$ | | $A_6$ | $20\times 25$ | ### Solution 2 | index | p | | ----- | --- | | 0 | 30 | | 1 | 35 | | 2 | 15 | | 3 | 5 | | 4 | 10 | | 5 | 20 | | 6 | 25 | (a). $$m[2, 5] = \left(\min\begin{cases}m[2][2] + m[3][5] + p[1][2][3] &= 13000\\ m[2][3] + m[4][5] + p[1][3][5] &= 7125\\ m[2][4] + m[5][5] + p[1][4][5] &= 11375\\ \end{cases}\right)=7125$$ (b). therefore $s[2, 5] = 3$ (c). $$(A_1 (A_2A_3))((A_4A_5)A_6)$$ ## Problem 3 Fill in the blanks in the following pseudocode for `MATRIX-CHAIN-ORDER`. ### Solution 3 ```text MATRIX-CHAIN-ORDER (p) let m[1..n, 1..n] and s[1..(n-1), 2..n] be new tables for i = 1 to n m[i, i] = 0 for l = 2 to n for i = 1 to n - l + 1 j = i + l - 1 m[i, j] = inf for k = i to j - 1 q = m[i][k] + m[k + 1][j] + p[i-1] * p[k] * p[j] if q < m[i, j] m[i, j] = q s[i, j] = k return m and s ``` ## Problem 4 Fill in the blanks in the following pseudocode for `PRINT-OPTIMAL-PARENS`. ### Solution 4 ```text PRINT-OPTIMAL-PARENS (s, i, j) if i == j print "A_i" else print "(" PRINT-OPTIMAL-PARENS(s, i, s[i, j]) PRINT-OPTIMAL-PARENS(s, s[i, j] + 1, j) print ")" ```