Compare commits
15 Commits
3b64e288ab
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a7c8efd537 | |||
| 68ad00a450 | |||
| ccf34b5dca | |||
| c96985c0d8 | |||
| 3abf5a0f0b | |||
| c4ea7b971a | |||
| aa95c26224 | |||
| a5ed6d3d07 | |||
| e14250232b | |||
| 995ba47ca2 | |||
| 0a3ff3bbcb | |||
| 1b8064927b | |||
| 2a11fa4237 | |||
| aa13f159c2 | |||
| 158facd519 |
BIN
assets/R8-1_1.png
Normal file
BIN
assets/R8-1_1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 123 KiB |
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/R4-1.pdf
(Stored with Git LFS)
BIN
out/reviews/R4-1.pdf
(Stored with Git LFS)
Binary file not shown.
BIN
out/reviews/R4-2.pdf
(Stored with Git LFS)
BIN
out/reviews/R4-2.pdf
(Stored with Git LFS)
Binary file not shown.
BIN
out/reviews/R4a.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R4a.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R4b.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R4b.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R7.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R7.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R7X.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R7X.pdf
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
out/reviews/R8a.pdf
(Stored with Git LFS)
Normal file
BIN
out/reviews/R8a.pdf
(Stored with Git LFS)
Normal file
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.
198
reviews/R7.md
Normal file
198
reviews/R7.md
Normal file
@@ -0,0 +1,198 @@
|
||||
# Review 7
|
||||
|
||||
* Hajin Ju, 2024062806
|
||||
|
||||
## Problem 1
|
||||
|
||||
Illustrate the operation of `COUNTING-SORT` on the array $A= \set{6,2,1,3,1,3,2}$
|
||||
|
||||
### Solution 1
|
||||
|
||||
```python {cmd output=html hide}
|
||||
from __pyfunc__.r7 import *
|
||||
from bs4 import BeautifulSoup as bs
|
||||
|
||||
soup = bs("", "lxml")
|
||||
soup.append(create_array_block(soup, "A", None, [6, 2, 1, 3, 1, 3, 2]))
|
||||
print(soup)
|
||||
|
||||
|
||||
```
|
||||
|
||||
```python {cmd output=html hide}
|
||||
|
||||
from __pyfunc__.r7 import *
|
||||
from bs4 import BeautifulSoup as bs
|
||||
|
||||
soup = bs("", "lxml")
|
||||
main = soup.append(create_main(soup))
|
||||
col1 = main.append(create_col(soup))
|
||||
col2 = main.append(create_col(soup))
|
||||
col1.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
list(range(1,6+1)), [2, 2, 2, 0, 0, 1]))
|
||||
col2.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
[1, 2, 3, 4, 5, 6],
|
||||
[2, 4, 6, 6, 6, 7]))
|
||||
col1.append(
|
||||
create_array_block(
|
||||
soup, "B",
|
||||
[1, 2, 3, 4, 5, 6, 7],
|
||||
["", "", "", 2, "", "", ""]))
|
||||
col2.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
[1, 2, 3, 4, 5, 6],
|
||||
[2, 3, 6, 6, 6, 7]))
|
||||
col1.append(
|
||||
create_array_block(
|
||||
soup, "B",
|
||||
[1, 2, 3, 4, 5, 6, 7],
|
||||
["", "", "", 2, "", 3, ""]))
|
||||
col2.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
[1, 2, 3, 4, 5, 6],
|
||||
[2, 3, 5, 6, 6, 7]))
|
||||
col1.append(
|
||||
create_array_block(
|
||||
soup, "B",
|
||||
[1, 2, 3, 4, 5, 6, 7],
|
||||
["", 1, "", 2, "", 3, ""]))
|
||||
col2.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
[1, 2, 3, 4, 5, 6],
|
||||
[1, 3, 5, 6, 6, 7]))
|
||||
col1.append(
|
||||
create_array_block(
|
||||
soup, "B",
|
||||
[1, 2, 3, 4, 5, 6, 7],
|
||||
["", 1, "", 2, 3, 3, ""]))
|
||||
col2.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
[1, 2, 3, 4, 5, 6],
|
||||
[1, 3, 4, 6, 6, 7]))
|
||||
col1.append(
|
||||
create_array_block(
|
||||
soup, "B",
|
||||
[1, 2, 3, 4, 5, 6, 7],
|
||||
[1, 1, "", 2, 3, 3, ""]))
|
||||
col2.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
[1, 2, 3, 4, 5, 6],
|
||||
[0, 3, 4, 6, 6, 7]))
|
||||
col1.append(
|
||||
create_array_block(
|
||||
soup, "B",
|
||||
[1, 2, 3, 4, 5, 6, 7],
|
||||
[1, 1, 2, 2, 3, 3, ""]))
|
||||
col2.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
[1, 2, 3, 4, 5, 6],
|
||||
[0, 2, 4, 6, 6, 7]))
|
||||
col1.append(
|
||||
create_array_block(
|
||||
soup, "B",
|
||||
[1, 2, 3, 4, 5, 6, 7],
|
||||
[1, 1, 2, 2, 3, 3, 6]))
|
||||
col2.append(
|
||||
create_array_block(
|
||||
soup, "C",
|
||||
[1, 2, 3, 4, 5, 6],
|
||||
[0, 2, 4, 6, 6, 6]))
|
||||
|
||||
|
||||
|
||||
print(soup)
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Problem 2
|
||||
|
||||
Fill in the following `RADIX-SORT` example.
|
||||
|
||||
### Solution 2
|
||||
|
||||
```python {cmd output=html hide}
|
||||
from __pyfunc__.r7 import *
|
||||
from bs4 import BeautifulSoup as bs
|
||||
|
||||
soup = bs("", "lxml")
|
||||
|
||||
main = soup.append(create_main(soup))
|
||||
col1 = main.append(create_col(soup))
|
||||
col2 = main.append(create_col(soup))
|
||||
col3 = main.append(create_col(soup))
|
||||
col4 = main.append(create_col(soup))
|
||||
|
||||
|
||||
col1.append(create_radix_table(soup,
|
||||
[
|
||||
"COW",
|
||||
"DOG",
|
||||
"SEA",
|
||||
"RUG",
|
||||
"ROW",
|
||||
"MOB",
|
||||
"BOX",
|
||||
"TAB",
|
||||
"BAR",
|
||||
"EAR",
|
||||
"TAR"]
|
||||
))
|
||||
|
||||
col2.append(create_radix_table(soup,
|
||||
[
|
||||
"SEA",
|
||||
"MOB",
|
||||
"TAB",
|
||||
"DOG",
|
||||
"RUG",
|
||||
"BAR",
|
||||
"EAR",
|
||||
"TAR",
|
||||
"COW",
|
||||
"ROW",
|
||||
"BOX"], [2]
|
||||
))
|
||||
|
||||
col3.append(create_radix_table(soup,
|
||||
[
|
||||
"TAB",
|
||||
"BAR",
|
||||
"EAR",
|
||||
"TAR",
|
||||
"SEA",
|
||||
"MOB",
|
||||
"DOG",
|
||||
"COW",
|
||||
"ROW",
|
||||
"BOX",
|
||||
"RUG",], [1, 2]
|
||||
))
|
||||
|
||||
col4.append(create_radix_table(soup,
|
||||
[
|
||||
"BAR",
|
||||
"BOX",
|
||||
"COW",
|
||||
"DOG",
|
||||
"EAR",
|
||||
"MOB",
|
||||
"ROW",
|
||||
"RUG",
|
||||
"SEA",
|
||||
"TAB",
|
||||
"TAR",], [0, 1, 2]
|
||||
))
|
||||
|
||||
print(soup)
|
||||
```
|
||||
11
reviews/R7X.md
Normal file
11
reviews/R7X.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Review DataStructure
|
||||
|
||||
* Hajin Ju, 2024062806
|
||||
|
||||
| * | Arrays<br>(not sorted) | Arrays<br>(sorted) | LinkedLists<br>(not sorted) | LinkedList<br>(sorted) | Binary ST<br>(avg) | Balanced ST | HashTables<br>(avg) |
|
||||
| --------------------------- | ---------------------- | ------------------ | --------------------------- | ---------------------- | ------------------ | ----------- | ------------------- |
|
||||
| Search($x$) | $O(n)$ | $O(\lg n)$ | $O(n)$ | $O(n)$ | $O(\lg n)$ | $O(\lg n)$ | $O(1)$ |
|
||||
| Insert($x$) | $O(1)$ | $O(n)$ | $O(1)$ | $O(n)$ | $O(\lg n)$ | $O(\lg n)$ | $O(1)$ |
|
||||
| Insert($x$)<br>(dup search) | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(\lg n)$ | $O(\lg n)$ | $O(1)$ |
|
||||
| Delete($i$) | $O(1)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(\lg n)$ | $O(\lg n)$ | $O(1)$ |
|
||||
| Delete($x$) | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(\lg n)$ | $O(\lg n)$ | $O(1)$ |
|
||||
58
reviews/R8a.md
Normal file
58
reviews/R8a.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# Review 8-1
|
||||
|
||||
* Hajin Ju, 2024062806
|
||||
|
||||
## Problem 1
|
||||
|
||||
Fill in the blanks in the following assembly-line scheduling example.
|
||||
|
||||
### Solution 1
|
||||
|
||||

|
||||
|
||||
|
||||
**table $s$**
|
||||
|
||||
| $s$ | 1 | 2 | 3 | 4 | 5 | 6 |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| 1 | 9 | 18 | 20 | 24 | 32 | 35 |
|
||||
| 2 | 12 | 16 | 22 | 25 | 30 | 37 |
|
||||
|
||||
**table $l$**
|
||||
|
||||
| $l$ | 1 | 2 | 3 | 4 | 5 | 6 |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| 1 | - | 1 | 2 | 1 | 1 | 2 |
|
||||
| 2 | - | 1 | 2 | 1 | 2 | 2 |
|
||||
|
||||
$$\begin{align*}s^* &= 38\\l^* &= 1\end{align*}$$
|
||||
|
||||
## Problem 2
|
||||
|
||||
Fill in the blanks in the following pseudocode for assembly-line scheduling
|
||||
|
||||
### Solution 2
|
||||
|
||||
```python
|
||||
FASTEST-WAY(a, t, e, x, n)
|
||||
s[1][1] = e[1] + a[1][1]
|
||||
s[2][1] = e[2] + a[2][1]
|
||||
for j = 2 to n
|
||||
do
|
||||
if s[1][j-1] + a[1][j] <= s[2][j-1] + t[2][j-1] + a[1][j]
|
||||
then s[1][j] = s[1][j-1] + a[1][j]
|
||||
l[1][j] = 1
|
||||
else s[1][j] = s[2][j-1] + t[2][j-1] + a[1][j]
|
||||
l[1][j] = 2
|
||||
|
||||
if s[2][j-1] <= s[1][j-1] + t[1][j-1]
|
||||
then s[2][j] = s[2][j-1] + a[2][j]
|
||||
l[2][j] = 2
|
||||
else s[2][j] = s[1][j-1] + t[1][j-1] + a[2][j]
|
||||
l[2][j] = 1
|
||||
if s[1][n] + x[1] <= s[2][n] + x[2]
|
||||
then s* = s[1][n] + x[1]
|
||||
l* = 1
|
||||
else s* = s[2][n] + x[2]
|
||||
l* = 2
|
||||
```
|
||||
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 ")"
|
||||
```
|
||||
86
reviews/__pyfunc__/r7.py
Normal file
86
reviews/__pyfunc__/r7.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
MAIN_STYLE = "display: flex; gap: 50px;"
|
||||
|
||||
COLUMN_STYLE = "display: flex; flex-direction: column; gap: 12px;"
|
||||
|
||||
ARRAY_BLOCK_STYLE = "display: flex; align-items: center; gap: 8px;"
|
||||
|
||||
ARRAY_TABLE_TEMPLATE = "border-collapse: collapse; "
|
||||
|
||||
LABEL_STYLE = "font-size: 1.2em; font-weight: bold; width: 20px; text-align: center;"
|
||||
|
||||
TD_STYLE_TEMPLATE = "border: 1px solid black; width: 1.2em; height: 1.2rem; text-align: center; font-size: 0.8em;"
|
||||
|
||||
TH_STYLE_TEMPLATE = "font-weight: normal; border: none; width: 1.2em; height: 0.3rem; text-align: center; font-size: 0.8em;"
|
||||
|
||||
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;"
|
||||
|
||||
def create_array_block(soup: BeautifulSoup, label: str, head: list[int] | None, data: list[int]):
|
||||
block = soup.new_tag("div", attrs = {'style': ARRAY_BLOCK_STYLE})
|
||||
|
||||
if label:
|
||||
block.append(
|
||||
soup.new_tag("span", string=label, attrs={"style": LABEL_STYLE})
|
||||
)
|
||||
|
||||
table = soup.new_tag("table",
|
||||
attrs = {"style": ARRAY_TABLE_TEMPLATE}
|
||||
)
|
||||
if head:
|
||||
upper = soup.new_tag("thead")
|
||||
tr = soup.new_tag("tr")
|
||||
for h in head:
|
||||
tr.append(soup.new_tag("th", string=str(h), attrs={"style": TH_STYLE_TEMPLATE}))
|
||||
upper.append(tr)
|
||||
table.append(upper)
|
||||
body = soup.new_tag("tbody");
|
||||
tr = soup.new_tag("tr")
|
||||
for d in data:
|
||||
tr.append(soup.new_tag("td", string=str(d), attrs={"style": TD_STYLE_TEMPLATE}))
|
||||
body.append(tr)
|
||||
table.append(body)
|
||||
block.append(table)
|
||||
|
||||
return block
|
||||
|
||||
def create_main(soup: BeautifulSoup):
|
||||
main = soup.new_tag("div", attrs={"style": MAIN_STYLE})
|
||||
return main
|
||||
|
||||
def create_col(soup: BeautifulSoup):
|
||||
col = soup.new_tag("div", attrs={"style": COLUMN_STYLE})
|
||||
return col
|
||||
|
||||
|
||||
|
||||
def create_radix_table(soup: BeautifulSoup,
|
||||
data = list[str],
|
||||
highlighted: None | list[int] = None):
|
||||
table = soup.new_tag("table")
|
||||
tbody = soup.new_tag("tbody")
|
||||
|
||||
for word in data:
|
||||
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:
|
||||
style = td_style_with_gray(style)
|
||||
td = soup.new_tag("td", string=char, attrs= {"style": style})
|
||||
tr.append(td)
|
||||
tbody.append(tr)
|
||||
table.append(tbody)
|
||||
|
||||
return table
|
||||
|
||||
Reference in New Issue
Block a user