Compare commits
13 Commits
aa13f159c2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a7c8efd537 | |||
| 68ad00a450 | |||
| ccf34b5dca | |||
| c96985c0d8 | |||
| 3abf5a0f0b | |||
| c4ea7b971a | |||
| aa95c26224 | |||
| a5ed6d3d07 | |||
| e14250232b | |||
| 995ba47ca2 | |||
| 0a3ff3bbcb | |||
| 1b8064927b | |||
| 2a11fa4237 |
44
notes/midterm.md
Normal file
44
notes/midterm.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Alogorithm Midterm Access
|
||||
|
||||
## Sorting Problem
|
||||
|
||||
Comparison Sorts(Lower bound is $\Omega(n \lg n)$ because decision-tree model)
|
||||
|
||||
* Insertion Sort
|
||||
* Selection Sort
|
||||
* Merge Sort
|
||||
* Quick Sort
|
||||
* Heap Sort
|
||||
|
||||
Sorting In Linear Time
|
||||
|
||||
* Counting Sort
|
||||
* Radix Sort
|
||||
|
||||
## Asymtotic Notation
|
||||
|
||||
* $Theta$-notation
|
||||
* $O$-notation
|
||||
* $\Omega$-notation
|
||||
|
||||
Transitivity, Reflexivity, Symmetry, Transpose Symmetry
|
||||
|
||||
Three methods for solving recurrences:
|
||||
1. Substitution method
|
||||
2. Recursion-tree method
|
||||
3. Master method
|
||||
|
||||
**Getting asymtotic notation by recursion tree is important**
|
||||
|
||||
## Dynamic Programming
|
||||
|
||||
* Assembly-line scheduling
|
||||
* Rod cutting
|
||||
* Longest-Common Subsequence
|
||||
* Matrix-chain Multiplication
|
||||
|
||||
## Greedy Algorithm
|
||||
|
||||
* An Activity Selection Problem
|
||||
* Elements of the greedy strategy
|
||||
* Huffman codes
|
||||
BIN
out/reviews/R7.pdf
(Stored with Git LFS)
BIN
out/reviews/R7.pdf
(Stored with Git LFS)
Binary file not shown.
BIN
out/reviews/R8a.pdf
(Stored with Git LFS)
BIN
out/reviews/R8a.pdf
(Stored with Git LFS)
Binary file not shown.
BIN
out/reviews/R8b.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R8b.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R8c.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R8c.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R8d.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R8d.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R8e.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R8e.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R9a.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R9a.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R9b.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R9b.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/1.Intro.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/1.Intro.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/15.Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/15.Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/16.Greedy Algorithms.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/16.Greedy Algorithms.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/2.Getting Started.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/2.Getting Started.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/3.Growth of Functions.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/3.Growth of Functions.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/4.Divide-and-Conquer.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/4.Divide-and-Conquer.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/6.Heapsort.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/6.Heapsort.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/7.Quicksort.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/7.Quicksort.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/8.Sorting in Linear Time.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/8.Sorting in Linear Time.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 10 Amortized analysis.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 10 Amortized analysis.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 10-2 Amortized analysis.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 10-2 Amortized analysis.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 10-3 Amortized analysis.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 10-3 Amortized analysis.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 12-1 Elementary Graph Algorithms.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 12-1 Elementary Graph Algorithms.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 12-2 Elementary Graph Algorithms.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 12-2 Elementary Graph Algorithms.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 12-3 Elementary Graph Algorithms.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 12-3 Elementary Graph Algorithms.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 12-4 Elementary Graph Algorithms.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 12-4 Elementary Graph Algorithms.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 13 Disjoint Sets.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 13 Disjoint Sets.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 14. Minimum Spanning Trees.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 14. Minimum Spanning Trees.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 15 shortest path.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 15 shortest path.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 8-2 Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 8-2 Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 8-3 Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 8-3 Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 8-4 Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 8-4 Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 8-5 Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 8-5 Dynamic Programming.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 9-1 greedy algorithms.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 9-1 greedy algorithms.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
pdf/Review 9-2 greedy algorithms.pdf
(Stored with Git LFS)
Normal file
BIN
pdf/Review 9-2 greedy algorithms.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
@@ -194,8 +194,5 @@ col4.append(create_radix_table(soup,
|
||||
"TAR",], [0, 1, 2]
|
||||
))
|
||||
|
||||
|
||||
|
||||
|
||||
print(soup)
|
||||
```
|
||||
@@ -1,4 +1,4 @@
|
||||
# Review 7
|
||||
# Review 8-1
|
||||
|
||||
* Hajin Ju, 2024062806
|
||||
|
||||
|
||||
78
reviews/R8b.md
Normal file
78
reviews/R8b.md
Normal 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
reviews/R8c.md
Normal file
94
reviews/R8c.md
Normal 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
reviews/R8d.md
Normal file
43
reviews/R8d.md
Normal 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
reviews/R8e.md
Normal file
105
reviews/R8e.md
Normal 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 ")"
|
||||
```
|
||||
@@ -17,6 +17,9 @@ TH_STYLE_TEMPLATE = "font-weight: normal; border: none; width: 1.2em; height: 0.
|
||||
def td_style_with_gray(style):
|
||||
return style + "background-color: lightgray;"
|
||||
|
||||
def td_style_with_no_left_border(style):
|
||||
return style + "border-left: none;"
|
||||
|
||||
def td_style_with_no_right_border(style):
|
||||
return style + "border-right: none;"
|
||||
|
||||
@@ -68,6 +71,8 @@ def create_radix_table(soup: BeautifulSoup,
|
||||
tr = soup.new_tag("tr")
|
||||
for i, char in enumerate(word):
|
||||
style = TD_STYLE_TEMPLATE
|
||||
if i != 0:
|
||||
style = td_style_with_no_left_border(style)
|
||||
if i != len(word) - 1:
|
||||
style = td_style_with_no_right_border(style)
|
||||
if highlighted and i in highlighted:
|
||||
|
||||
Reference in New Issue
Block a user