fix R8a and add R8*.md and build them

This commit is contained in:
yenru0 committed 2025-10-12 19:27:11 +09:00
1 parent 0a3ff3bbcb
commit 995ba47ca2
10 files changed
+335 -3

No files matched your search

BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -1,4 +1,4 @@
# Review 7
# Review 8-1
* Hajin Ju, 2024062806
+78
View File
@@ -0,0 +1,78 @@
# Review 8-2
* Hajin Ju, 2024062806
## Problem 1
Fill in the blanks in the table below.
* $p[i]$: the price for a rod of length i
* $r[i]$: the maximum revenue for a rod of length i
* $s[i]$: the length of the leftmost piece when the revenue is maximum
### Solution 1
| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| ------ | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| $p[i]$ | 0 | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 | 24 | 30 |
| $r[i]$ | 0 | 1 | 5 | 8 | 10 | 13 | 17 | 18 | 22 | 25 | 30 |
| $s[i]$ | 0 | 1 | 2 | 3 | 2 | 2 | 6 | 1 | 2 | 3 | 10 |
## Problem 2
Fill in the blanks in the following pseudocode for `EXTENDED-BOTTOM-UP-CUT-ROD`.
### Solution 2
```text
EXTENDED-BOTTOM-UP-CUT-ROD (p, n)
let r[0..n] and s[0..n] be new arrays
r[0] = 0
for j = 1 to n
r[j] = -inf
for i = 1 to j
if r[j] < p[i] + r[j-i]
r[j] = p[i] + r[j-i]
s[j] = i
return r, s
```
## Problem 3
Fill in the blanks in the following pseudocode for `PRINT-CUT-ROD-SOLUTION`.
### Solution 3
```text
PRINT-CUT-ROD-SOLUTION (p, n)
(r, s) = EXTENDED-BOTTOM-UP-CUT-ROD(p, n)
while n > 0
print s[n]
n = n - s[n]
```
## Problem 4
Fill in the blanks in the following pseudocode for `M-CUT-ROD`.
### Solution 4
```text
M-CUT-ROD (p, n)
let r[0..n] be a new array
for i = 0 to n
r[i] = -inf
return M-CUT-ROD-A(p, n, r)
M-CUT-ROD-A (p, n, r)
if r[n] >= 0
return r[n]
if n == 0
return 0
else q = -inf
for i = 1 to n
q = max(q, p[i] + M-CUT-ROD-A(p, n-i, r))
r[n] = q
return q
```
+94
View File
@@ -0,0 +1,94 @@
# Review 8-3
* Hajin Ju, 2024062806
## Problem 1
Fill in the blanks in the following LCS computation
### Solution 1
$$c[i][j] = \text{The length of an LCS of the subsequences} \,X_i\, \text{and}\, Y_j.$$
$$c[i][j] = \begin{cases}
0 & \text{if}\, i = 0 \,\text{or}\, j = 0\\
c[i-1][j-1] + 1 &\text{if}\, i,j > 0 \,\text{and}\, x_i = y_j\\
max(c[i][j-1], c[i-1][j]) &\text{if}\, i, j > 0 \,\text{and}\, x_i \neq y_j
\end{cases}$$
| * | $y_j$ | $B$ | $D$ | $C$ | $A$ | $B$ | $A$ |
| ----- | ----- | ------------ | ------------- | ------------- | -------------- | -------------- | -------------- |
| $x_i$ | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| $A$ | 0 | $\uparrow 0$ | $\uparrow 0$ | $\uparrow 0$ | $\nwarrow 1$ | $\leftarrow 1$ | $\nwarrow 1$ |
| $B$ | 0 | $\nwarrow1$ | $\leftarrow1$ | $\leftarrow1$ | $\uparrow 1$ | $\nwarrow 2$ | $\leftarrow 2$ |
| $C$ | 0 | $\uparrow 1$ | $\uparrow 1$ | $\nwarrow 2$ | $\leftarrow 2$ | $\uparrow 2$ | $\uparrow 2$ |
| $B$ | 0 | $\nwarrow 1$ | $\uparrow 1$ | $\uparrow 2$ | $\uparrow2$ | $\nwarrow3$ | $\leftarrow3$ |
| $D$ | 0 | $\uparrow1$ | $\nwarrow2$ | $\uparrow2$ | $\uparrow2$ | $\uparrow3$ | $\uparrow3$ |
| $A$ | 0 | $\uparrow1$ | $\uparrow2$ | $\uparrow2$ | $\nwarrow 3$ | $\uparrow 3$ | $\nwarrow4$ |
| $B$ | 0 | $\nwarrow1$ | $\uparrow2$ | $\uparrow2$ | $\uparrow3$ | $\nwarrow4$ | $\uparrow4$ |
## Problem 2
Fill in the blanks in the following multiple LCS computation.
### Solution 2
| * | $y_j$ | $B$ | $D$ | $C$ | $A$ | $B$ | $A$ |
| ----- | ----- | ---------------------- | ---------------------- | ---------------------- | ---------------------- | ---------------------- | ---------------------- |
| $x_i$ | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| $A$ | 0 | $\leftarrow\uparrow 0$ | $\leftarrow\uparrow 0$ | $\leftarrow\uparrow 0$ | $\nwarrow 1$ | $\leftarrow 1$ | $\nwarrow 1$ |
| $B$ | 0 | $\nwarrow1$ | $\leftarrow1$ | $\leftarrow1$ | $\leftarrow\uparrow 1$ | $\nwarrow 2$ | $\leftarrow 2$ |
| $C$ | 0 | $\uparrow 1$ | $\leftarrow\uparrow 1$ | $\nwarrow 2$ | $\leftarrow 2$ | $\leftarrow\uparrow 2$ | $\leftarrow\uparrow 2$ |
| $B$ | 0 | $\nwarrow 1$ | $\leftarrow\uparrow 1$ | $\uparrow 2$ | $\uparrow2$ | $\nwarrow3$ | $\leftarrow3$ |
| $D$ | 0 | $\uparrow1$ | $\nwarrow2$ | $\leftarrow\uparrow2$ | $\uparrow2$ | $\uparrow3$ | $\uparrow3$ |
| $A$ | 0 | $\uparrow1$ | $\uparrow2$ | $\uparrow2$ | $\nwarrow 3$ | $\leftarrow\uparrow 3$ | $\nwarrow4$ |
| $B$ | 0 | $\nwarrow1$ | $\uparrow2$ | $\leftarrow\uparrow2$ | $\uparrow3$ | $\nwarrow4$ | $\leftarrow\uparrow4$ |
## Problem 3
Fill in the blanks in the following pseudocode for `LCS-LENGTH`.
### Solution 3
```text
LCS-LENGTH(X, Y)
m = X.length
n = Y.length
let b[1..m, 1..n] and c[1..m, 1..n] be new tables
for i = 1 to m
c[i][0] = 0
for j = 1 to n
c[0][j] = 0
for i = 1 to m
for j = 1 to n
if X[i] = Y[j]
c[i][j] = c[i - 1][j - 1] + 1
b[i][j] = \nwarrow
else if c[i-1][j] >= c[i][j-1]
c[i][j] = c[i-1][j]
b[i][j] = \uparrow
else
c[i][j] = c[i][j-1]
b[i][j] = \leftarrow
return c, b
```
## Problem 4
Fill in the blanks in the following pseudocode for `PRINT-LCS`.
### Solution 4
```text
PRINT-LCS(b, X, i, j)
if i = 0 or j = 0
return
if b[i][j] == \nwarrow
PRINT-LCS(b, X, i-1, j)
print X[i]
else if b[i][j] == \uparrow
PRINT-LCS(b, X, i-1, j)
else
PRINT-LCS(b, X, i, j-1)
```
+43
View File
@@ -0,0 +1,43 @@
# Review 8-4
* Hajin Ju, 2024062806
## Problem 1
What is the dimension of the matrix product $AB$ if $A$ is a $p\times q$ matrix and $B$ is a $q\times r$ matrix?
### Solution 1
$p\times r$
## Problem 2
Count the number of scalar multiplications to multiply $A$ and $B$ where $A$ is a $p\times q$ matrix and $B$ is a $q\times r$ matrix.
### Solution 2
$pqr$
## Problem 3
Count the number of scalar multiplications where the dimensions of $A_1$, $A_2$, and $A_3$$ are $10\times 100$, $100\times 5$ and $5\times 50$, respectively.
1. $(A_1A_2)A_3$
2. $A_1(A_2A_3)$
### Solution 3
1. $5000 + 2500 = 7500$
2. $25000 + 50000 = 75000$
## Problem 4
Fully parenthesize the product $A_1A_2A_3A_4$. (There are five distinct ways.)
### Solution 4
1. $(A_1A_2)(A_3A_4)$
2. $(A_1(A_2A_3))A_4$
3. $A_1(A_2(A_3A_4))$
4. $A_1((A_2A_3)A_4)$
5. $((A_1A_2)A_3)A_4$
+105
View File
@@ -0,0 +1,105 @@
# 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 ")"
```