Skip to content

Commit 2fccf02

Browse files
committed
310 updates
1 parent 66d1a58 commit 2fccf02

11 files changed

Lines changed: 74 additions & 74 deletions

notes/courses/CSCI-UA-310/08-09-hashing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ The table size $m$ is much smaller than $|U|$.
5151

5252
**Collision**: Two keys $k_1 \neq k_2$ with $h(k_1) = h(k_2)$ is a **collision**. Collisions are unavoidable if $n > m$.
5353

54-
### Hash Function Design
54+
### 3.1 Hash Function Design
5555

5656
A good hash function satisfies the **simple uniform hashing assumption**: each key is equally likely to hash to any of the $m$ slots, independently of all other keys.
5757

@@ -67,7 +67,7 @@ A good hash function satisfies the **simple uniform hashing assumption**: each k
6767

6868
* Slot $j$ contains a pointer to the head of the list of all elements with $h(k) = j$.
6969

70-
### Pseudocode
70+
### 4.1 Pseudocode
7171

7272
```text
7373
CHAINED-HASH-INSERT(T, x)
@@ -131,7 +131,7 @@ HASH-SEARCH(T, k)
131131

132132
**Deletion** is tricky: cannot just set to `NIL` (would break search). Use a special `DELETED` sentinel.
133133

134-
### Probing Strategies
134+
### 6.1 Probing Strategies
135135

136136
**Linear Probing**: $h(k, i) = (h'(k) + i) \bmod m$.
137137

notes/courses/CSCI-UA-310/10-binary-search-trees.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Similarly, **pre-order** (root, left, right) and **post-order** (left, right, ro
4848

4949
## 3. Searching
5050

51-
### SEARCH
51+
### 3.1 SEARCH
5252

5353
```text
5454
TREE-SEARCH(x, k)
@@ -61,7 +61,7 @@ TREE-SEARCH(x, k)
6161

6262
Running time: $O(h)$.
6363

64-
### MINIMUM and MAXIMUM
64+
### 3.2 MINIMUM and MAXIMUM
6565

6666
```text
6767
TREE-MINIMUM(x)
@@ -79,7 +79,7 @@ TREE-MAXIMUM(x)
7979

8080
Both run in $O(h)$.
8181

82-
### SUCCESSOR
82+
### 3.3 SUCCESSOR
8383

8484
The **successor** of node $x$ is the node with the smallest key greater than $x.\text{key}$.
8585

@@ -100,7 +100,7 @@ Running time: $O(h)$.
100100

101101
## 4. Modification
102102

103-
### INSERT
103+
### 4.1 INSERT
104104

105105
```text
106106
TREE-INSERT(T, z)
@@ -121,7 +121,7 @@ TREE-INSERT(T, z)
121121

122122
Running time: $O(h)$.
123123

124-
### DELETE
124+
### 4.2 DELETE
125125

126126
Three cases when deleting node $z$:
127127

notes/courses/CSCI-UA-310/11-red-black-trees.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The **black-height** $\text{bh}(x)$ of a node $x$ is the number of black nodes o
4747

4848
**Rotations** are local restructuring operations that preserve the BST property and run in $O(1)$ time.
4949

50-
### Left Rotation on node $x$
50+
### 3.1 Left Rotation on node $x$
5151

5252
```text
5353
LEFT-ROTATE(T, x)
@@ -84,7 +84,7 @@ RB-INSERT(T, z)
8484
2. RB-INSERT-FIXUP(T, z)
8585
```
8686

87-
### Fixup Cases
87+
### 4.1 Fixup Cases
8888

8989
Let $z$ be the newly inserted red node, $z.\text{parent}$ be red (violation of Property 4), and $y$ be $z$'s **uncle** (sibling of $z$'s parent).
9090

notes/courses/CSCI-UA-310/12-13-14-dynamic-programming.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ date: 2026-03-02/04/09
5353
|---|---|---|---|---|---|---|---|---|---|---|
5454
| Price $p[i]$ | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 | 24 | 30 |
5555

56-
### Naive Recursive Solution
56+
### 3.1 Naive Recursive Solution
5757

5858
$$r_n = \max_{1 \leq i \leq n}(p[i] + r_{n-i})$$
5959

@@ -71,7 +71,7 @@ CUT-ROD(p, n)
7171

7272
**Running time**: $T(n) = 1 + \sum_{j=0}^{n-1} T(j)$, giving $T(n) = 2^n$ — exponential due to redundant subproblems.
7373

74-
### Bottom-Up Dynamic Programming
74+
### 3.2 Bottom-Up Dynamic Programming
7575

7676
Solve subproblems in order of increasing size, filling a table $r[0 \dots n]$.
7777

@@ -89,7 +89,7 @@ BOTTOM-UP-CUT-ROD(p, n)
8989

9090
**Running time**: $\Theta(n^2)$ — two nested loops.
9191

92-
### Top-Down Memoization
92+
### 3.3 Top-Down Memoization
9393

9494
```text
9595
MEMOIZED-CUT-ROD(p, n)
@@ -141,7 +141,7 @@ PRINT-CUT-ROD-SOLUTION(p, n)
141141

142142
## 5. Longest Common Subsequence
143143

144-
### Problem
144+
### 5.1 Problem
145145

146146
A **subsequence** of a sequence is the sequence with zero or more elements removed (not necessarily contiguous).
147147

@@ -152,21 +152,21 @@ A **subsequence** of a sequence is the sequence with zero or more elements remov
152152
**Example**: $X = \langle A, B, C, B, D, A, B \rangle$, $Y = \langle B, D, C, A, B, A \rangle$.
153153
LCS: $\langle B, C, B, A \rangle$ (length 4) or $\langle B, D, A, B \rangle$ (length 4).
154154

155-
### Optimal Substructure
155+
### 5.2 Optimal Substructure
156156

157157
**Theorem**: Let $Z = \langle z_1, \dots, z_k \rangle$ be any LCS of $X$ and $Y$.
158158

159159
1. If $x_m = y_n$, then $z_k = x_m = y_n$ and $Z_{k-1}$ is an LCS of $X_{m-1}$ and $Y_{n-1}$.
160160
2. If $x_m \neq y_n$ and $z_k \neq x_m$, then $Z$ is an LCS of $X_{m-1}$ and $Y$.
161161
3. If $x_m \neq y_n$ and $z_k \neq y_n$, then $Z$ is an LCS of $X$ and $Y_{n-1}$.
162162

163-
### Recurrence
163+
### 5.3 Recurrence
164164

165165
Define $c[i,j]$ = length of LCS of $X_i = \langle x_1,\dots,x_i \rangle$ and $Y_j = \langle y_1,\dots,y_j \rangle$.
166166

167167
$$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-1,j],\; c[i,j-1]) & \text{if } i,j > 0 \text{ and } x_i \neq y_j \end{cases}$$
168168

169-
### Algorithm
169+
### 5.4 Algorithm
170170

171171
```text
172172
LCS-LENGTH(X, Y)
@@ -196,28 +196,28 @@ LCS-LENGTH(X, Y)
196196

197197
## 6. 0/1 Knapsack
198198

199-
### Problem
199+
### 6.1 Problem
200200

201201
**Input**: $n$ items where item $i$ has weight $w_i \in \mathbb{Z}^+$ and value $v_i \geq 0$; knapsack capacity $W \in \mathbb{Z}^+$.
202202

203203
**Output**: Choose a subset $S \subseteq \{1, \dots, n\}$ to maximize $\sum_{i \in S} v_i$ subject to $\sum_{i \in S} w_i \leq W$.
204204

205205
The **0/1** constraint means each item is either taken or not (no fractions).
206206

207-
### Optimal Substructure
207+
### 6.2 Optimal Substructure
208208

209209
For item $n$:
210210

211211
* **Skip item $n$**: optimal value from items $\{1,\dots,n-1\}$ with capacity $W$.
212212
* **Take item $n$** (if $w_n \leq W$): $v_n$ plus optimal value from items $\{1,\dots,n-1\}$ with capacity $W - w_n$.
213213

214-
### Recurrence
214+
### 6.3 Recurrence
215215

216216
Define $\text{OPT}(i, w)$ = maximum value using items $\{1,\dots,i\}$ with capacity $w$.
217217

218218
$$\text{OPT}(i, w) = \begin{cases} 0 & \text{if } i = 0 \\ \text{OPT}(i-1, w) & \text{if } w_i > w \\ \max\bigl(\text{OPT}(i-1, w),\; v_i + \text{OPT}(i-1, w - w_i)\bigr) & \text{otherwise} \end{cases}$$
219219

220-
### Algorithm
220+
### 6.4 Algorithm
221221

222222
```text
223223
KNAPSACK(n, W, w[1..n], v[1..n])
@@ -234,7 +234,7 @@ KNAPSACK(n, W, w[1..n], v[1..n])
234234

235235
**Note**: $O(nW)$ is **pseudopolynomial** — it is polynomial in $n$ and $W$, but $W$ can be exponential in the number of bits required to represent it. The 0/1 Knapsack problem is NP-hard.
236236

237-
### Worked Example
237+
### 6.5 Worked Example
238238

239239
Items: $(w_1, v_1) = (2, 6)$, $(w_2, v_2) = (2, 10)$, $(w_3, v_3) = (3, 12)$. Capacity $W = 5$.
240240

@@ -272,7 +272,7 @@ FIBONACCI-DP(n)
272272

273273
## 8. Matrix Chain Multiplication
274274

275-
### Problem
275+
### 8.1 Problem
276276

277277
**Input**: A chain of $n$ matrices $A_1, A_2, \dots, A_n$ where $A_i$ has dimensions $p_{i-1} \times p_i$.
278278

@@ -285,19 +285,19 @@ FIBONACCI-DP(n)
285285
* $((A_1 A_2) A_3) A_4$: $30 \cdot 35 \cdot 15 + 30 \cdot 15 \cdot 5 + 30 \cdot 5 \cdot 10 = 18{,}000$ multiplications.
286286
* $(A_1 (A_2 (A_3 A_4)))$: $15 \cdot 5 \cdot 10 + 35 \cdot 15 \cdot 10 + 30 \cdot 35 \cdot 10 = 16{,}250$ multiplications.
287287

288-
### Optimal Substructure
288+
### 8.2 Optimal Substructure
289289

290290
Any optimal parenthesization of $A_i A_{i+1} \cdots A_j$ must split at some $k$ ($i \leq k < j$) into $(A_i \cdots A_k)(A_{k+1} \cdots A_j)$. Each part must also be optimally parenthesized.
291291

292-
### Recurrence
292+
### 8.3 Recurrence
293293

294294
Define $m[i,j]$ = minimum number of multiplications to compute $A_i \cdots A_j$.
295295

296296
$$m[i,j] = \begin{cases} 0 & \text{if } i = j \\ \min_{i \leq k < j} \bigl( m[i,k] + m[k+1,j] + p_{i-1} \cdot p_k \cdot p_j \bigr) & \text{if } i < j \end{cases}$$
297297

298298
**Guess**: the split position $k$.
299299

300-
### Algorithm
300+
### 8.4 Algorithm
301301

302302
Solve by increasing **chain length** $\ell = j - i$.
303303

@@ -325,21 +325,21 @@ MATRIX-CHAIN-ORDER(p)
325325

326326
## 9. Subset Sum
327327

328-
### Problem
328+
### 9.1 Problem
329329

330330
**Input**: A set $S = \{a_1, a_2, \dots, a_n\}$ of positive integers and a target $W$.
331331

332332
**Output**: Does there exist a subset $T \subseteq S$ such that $\sum_{a \in T} a = W$?
333333

334-
### Recurrence
334+
### 9.2 Recurrence
335335

336336
Define $\text{DP}[i][w] = \mathtt{true}$ if some subset of $\{a_1, \dots, a_i\}$ sums to exactly $w$.
337337

338338
$$\text{DP}[i][w] = \text{DP}[i-1][w] \;\lor\; \bigl(w \geq a_i \;\land\; \text{DP}[i-1][w - a_i]\bigr)$$
339339

340340
Base cases: $\text{DP}[i][0] = \mathtt{true}$ for all $i$; $\text{DP}[0][w] = \mathtt{false}$ for $w > 0$.
341341

342-
### Algorithm
342+
### 9.3 Algorithm
343343

344344
```text
345345
SUBSET-SUM(a[1..n], W)

notes/courses/CSCI-UA-310/15-16-greedy.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ A greedy algorithm is correct when the **greedy choice property** holds: a globa
3333

3434
## 2. Interval Scheduling (Activity Selection)
3535

36-
### Problem
36+
### 2.1 Problem
3737

3838
**Input**: $n$ activities, each with start time $s_i$ and finish time $f_i$. Two activities $i$ and $j$ are **compatible** if they do not overlap (i.e., $[s_i, f_i)$ and $[s_j, f_j)$ are disjoint).
3939

4040
**Output**: A maximum-size set of mutually compatible activities.
4141

42-
### Greedy Algorithm
42+
### 2.2 Greedy Algorithm
4343

4444
**Greedy rule**: Always select the compatible activity with the **earliest finish time**.
4545

@@ -57,7 +57,7 @@ GREEDY-ACTIVITY-SELECTOR(s, f)
5757

5858
**Running time**: $O(n \log n)$ for sorting; $O(n)$ for selection. Total: $O(n \log n)$.
5959

60-
### Correctness (Exchange Argument)
60+
### 2.3 Correctness (Exchange Argument)
6161

6262
**Theorem**: `GREEDY-ACTIVITY-SELECTOR` produces an optimal solution.
6363

@@ -75,15 +75,15 @@ Since the greedy finishes no later than optimal at each step, if $O$ has $m$ act
7575

7676
## 3. Fractional Knapsack
7777

78-
### Problem
78+
### 3.1 Problem
7979

8080
Same as 0/1 Knapsack but items can be split: take a **fraction** $x_i \in [0,1]$ of item $i$.
8181

8282
**Input**: $n$ items with weights $w_i$ and values $v_i$; capacity $W$.
8383

8484
**Output**: Fractions $x_1, \dots, x_n \in [0,1]$ maximizing $\sum_i x_i v_i$ subject to $\sum_i x_i w_i \leq W$.
8585

86-
### Greedy Algorithm
86+
### 3.2 Greedy Algorithm
8787

8888
**Key insight**: Take as much as possible of the item with the highest **value density** $v_i / w_i$.
8989

@@ -123,7 +123,7 @@ The exchange argument is the canonical way to prove greedy correctness:
123123

124124
## 5. Interval Partitioning (Scheduling on Multiple Machines)
125125

126-
### Problem
126+
### 5.1 Problem
127127

128128
**Input**: $n$ jobs with start times $s_j$ and finish times $f_j$.
129129

@@ -133,7 +133,7 @@ The exchange argument is the canonical way to prove greedy correctness:
133133

134134
**Lower bound**: We need at least depth($\mathcal{I}$) machines.
135135

136-
### Greedy Algorithm
136+
### 5.2 Greedy Algorithm
137137

138138
Sort jobs by start time. Maintain a set of machines. For each job, assign it to any machine that is free; if none is free, open a new machine.
139139

@@ -145,7 +145,7 @@ Sort jobs by start time. Maintain a set of machines. For each job, assign it to
145145

146146
## 6. Huffman Coding
147147

148-
### Problem
148+
### 6.1 Problem
149149

150150
**Input**: An alphabet $C = \{c_1, \dots, c_n\}$ where character $c_i$ has frequency $f_i$.
151151

@@ -182,7 +182,7 @@ HUFFMAN(C)
182182

183183
**Running time**: $O(n \log n)$ using a binary min-heap.
184184

185-
### Worked Example
185+
### 7.1 Worked Example
186186

187187
Frequencies: $a:45, b:13, c:12, d:16, e:9, f:5$.
188188

@@ -202,13 +202,13 @@ $B(T) = 45 \cdot 1 + 13 \cdot 3 + 12 \cdot 3 + 16 \cdot 3 + 9 \cdot 4 + 5 \cdot
202202

203203
## 8. Correctness Proof
204204

205-
### Lemma 1 (Greedy Choice Property)
205+
### 8.1 Lemma 1 (Greedy Choice Property)
206206

207207
Let $x$ and $y$ be the two characters with the smallest frequencies. There exists an optimal prefix-free code in which $x$ and $y$ have codewords of the same length that differ only in the last bit (i.e., they are siblings in the code tree).
208208

209209
**Proof**: Take any optimal tree $T$. Let $b$ and $c$ be siblings at maximum depth. WLOG $f_b \leq f_c$ and $f_x \leq f_y$. Swapping $x \leftrightarrow b$ and $y \leftrightarrow c$ does not increase $B(T)$ because $x, y$ have the smallest frequencies and move to the deepest positions.
210210

211-
### Lemma 2 (Optimal Substructure)
211+
### 8.2 Lemma 2 (Optimal Substructure)
212212

213213
Let $T$ be an optimal code tree for $C$, and suppose $x$ and $y$ are sibling leaves. Let $T'$ be $T$ with $x$ and $y$ replaced by their parent $z$ with $f_z = f_x + f_y$. Then $T'$ is optimal for $C' = C \setminus \{x, y\} \cup \{z\}$.
214214

notes/courses/CSCI-UA-310/17-graphs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ A **graph** $G = (V, E)$ consists of a set of **vertices** $V$ and a set of **ed
3737

3838
## 2. Representations
3939

40-
### Adjacency List
40+
### 2.1 Adjacency List
4141

4242
An array $\text{Adj}[1 \dots n]$ where $\text{Adj}[u]$ is a linked list of all vertices $v$ such that $(u, v) \in E$.
4343

@@ -46,7 +46,7 @@ An array $\text{Adj}[1 \dots n]$ where $\text{Adj}[u]$ is a linked list of all v
4646
* **Time to iterate** over all neighbors of $u$: $\Theta(\deg(u))$.
4747
* **Preferred** for sparse graphs.
4848

49-
### Adjacency Matrix
49+
### 2.2 Adjacency Matrix
5050

5151
An $n \times n$ matrix $A$ where $A[u][v] = 1$ if $(u,v) \in E$, else $0$.
5252

0 commit comments

Comments
 (0)