Skip to content

Commit 91a194c

Browse files
committed
formatting fixes
1 parent 025bbb9 commit 91a194c

12 files changed

Lines changed: 963 additions & 13 deletions

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,15 @@ HASH-SEARCH(T, k)
134134
### Probing Strategies
135135

136136
**Linear Probing**: $h(k, i) = (h'(k) + i) \bmod m$.
137+
137138
* Simple but causes **primary clustering**: long runs of occupied slots form and grow.
138139

139140
**Quadratic Probing**: $h(k, i) = (h'(k) + c_1 i + c_2 i^2) \bmod m$.
141+
140142
* Reduces primary clustering but causes **secondary clustering**: two keys with the same $h'(k)$ have identical probe sequences.
141143

142144
**Double Hashing**: $h(k, i) = (h_1(k) + i \cdot h_2(k)) \bmod m$.
145+
143146
* Uses two independent hash functions.
144147
* Gives $\Theta(m^2)$ distinct probe sequences; approximates uniform hashing well.
145148
* Requirement: $h_2(k)$ must be coprime to $m$ for all $k$ (e.g., choose $m$ prime).

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ The key insight is that a "double-black" violation is introduced when a black no
137137

138138
## Summary
139139

140-
| Operation | Time |
141-
|---|---|
142-
| SEARCH | $O(\log n)$ |
143-
| INSERT | $O(\log n)$ |
144-
| DELETE | $O(\log n)$ |
145-
| MINIMUM/MAXIMUM | $O(\log n)$ |
140+
| Operation | Time |
141+
|-----------------------|-------------|
142+
| SEARCH | $O(\log n)$ |
143+
| INSERT | $O(\log n)$ |
144+
| DELETE | $O(\log n)$ |
145+
| MINIMUM/MAXIMUM | $O(\log n)$ |
146146
| SUCCESSOR/PREDECESSOR | $O(\log n)$ |
147147

148148
---

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ date: 2026-03-02/04/09
2626
**Dynamic programming** solves each subproblem exactly once and stores the result in a table.
2727

2828
**Two key properties for DP to apply**:
29+
2930
1. **Optimal substructure**: An optimal solution to the problem contains optimal solutions to subproblems.
3031
2. **Overlapping subproblems**: The recursive solution revisits the same subproblems repeatedly.
3132

@@ -206,6 +207,7 @@ The **0/1** constraint means each item is either taken or not (no fractions).
206207
### Optimal Substructure
207208

208209
For item $n$:
210+
209211
* **Skip item $n$**: optimal value from items $\{1,\dots,n-1\}$ with capacity $W$.
210212
* **Take item $n$** (if $w_n \leq W$): $v_n$ plus optimal value from items $\{1,\dots,n-1\}$ with capacity $W - w_n$.
211213

@@ -279,6 +281,7 @@ FIBONACCI-DP(n)
279281
**Key fact**: Multiplying a $p \times q$ matrix by a $q \times r$ matrix costs $p \cdot q \cdot r$ scalar multiplications.
280282

281283
**Example**: For $A_1 (30 \times 35)$, $A_2 (35 \times 15)$, $A_3 (15 \times 5)$, $A_4 (5 \times 10)$:
284+
282285
* $((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.
283286
* $(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.
284287

@@ -354,6 +357,7 @@ SUBSET-SUM(a[1..n], W)
354357
**Running time**: $O(nW)$ β€” pseudopolynomial (Subset Sum is NP-complete).
355358

356359
**Worked Example**: $S = \{3, 5, 1\}$, $W = 9$.
360+
357361
* Subset $\{3, 5, 1\}$: sum $= 9$. Answer: **yes**.
358362

359363
---

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ GREEDY-ACTIVITY-SELECTOR(s, f)
6565
Let $A = \{a_1, a_2, \dots, a_k\}$ (greedy solution, sorted by finish time) and $O = \{o_1, o_2, \dots, o_m\}$ (any optimal solution, sorted by finish time). We show $|A| = |O|$, i.e., $k = m$.
6666

6767
**Key Lemma**: For each $i = 1, \dots, k$: $f(a_i) \leq f(o_i)$.
68+
6869
* Base: Greedy picks the activity with the smallest finish time overall, so $f(a_1) \leq f(o_1)$.
6970
* Inductive step: Since $f(a_i) \leq f(o_i) \leq s(o_{i+1})$, activity $o_{i+1}$ is also compatible with $a_i$. The greedy algorithm could have picked something with finish time $\leq f(o_{i+1})$, so $f(a_{i+1}) \leq f(o_{i+1})$.
7071

@@ -149,6 +150,7 @@ Sort jobs by start time. Maintain a set of machines. For each job, assign it to
149150
**Input**: An alphabet $C = \{c_1, \dots, c_n\}$ where character $c_i$ has frequency $f_i$.
150151

151152
**Goal**: Assign a binary codeword to each character such that:
153+
152154
* The code is **prefix-free** (no codeword is a prefix of another).
153155
* The **total encoding length** $\sum_{c \in C} f_c \cdot d_T(c)$ is minimized, where $d_T(c)$ is the depth of $c$ in the code tree $T$.
154156

@@ -185,6 +187,7 @@ HUFFMAN(C)
185187
Frequencies: $a:45, b:13, c:12, d:16, e:9, f:5$.
186188

187189
Step-by-step merges:
190+
188191
1. Merge $f(5)$ and $e(9)$ β†’ node $fe(14)$.
189192
2. Merge $c(12)$ and $b(13)$ β†’ node $cb(25)$.
190193
3. Merge $fe(14)$ and $d(16)$ β†’ node $fed(30)$.

β€Žnotes/courses/CSCI-UA-310/17-graphs.mdβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ A **graph** $G = (V, E)$ consists of a set of **vertices** $V$ and a set of **ed
2525
**Weighted graph**: Each edge $(u, v)$ has a weight $w(u, v) \in \mathbb{R}$.
2626

2727
**Key quantities**:
28+
2829
* $n = |V|$ (number of vertices), $m = |E|$ (number of edges).
2930
* For undirected graphs: $m \leq \binom{n}{2} = O(n^2)$.
3031
* For directed graphs: $m \leq n(n-1) = O(n^2)$.
@@ -78,7 +79,8 @@ An $n \times n$ matrix $A$ where $A[u][v] = 1$ if $(u,v) \in E$, else $0$.
7879

7980
**Weakly connected** (directed): The underlying undirected graph is connected.
8081

81-
**Degree**:
82+
**Degree**:
83+
8284
* Undirected: $\deg(v)$ = number of incident edges.
8385
* Directed: $\text{in-deg}(v)$ = edges entering, $\text{out-deg}(v)$ = edges leaving.
8486
* **Handshake lemma**: $\sum_{v \in V} \deg(v) = 2|E|$.

β€Žnotes/courses/CSCI-UA-310/18-graph-traversal.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ DFS-VISIT(G, u)
8383
### Parenthesis Theorem
8484

8585
For any two vertices $u$ and $v$, exactly one of the following holds:
86+
8687
1. $[u.d, u.f]$ and $[v.d, v.f]$ are **disjoint** ($u$ and $v$ are in different DFS trees).
8788
2. $[u.d, u.f] \subset [v.d, v.f]$ ($u$ is a **descendant** of $v$).
8889
3. $[v.d, v.f] \subset [u.d, u.f]$ ($v$ is a **descendant** of $u$).

β€Žnotes/courses/CSCI-UA-310/19-20-shortest-paths.mdβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ $$\delta(s, v) = \min \{ w(p) : s \xrightarrow{p} v \}$$
2727
and $\delta(s, v) = \infty$ if no path exists.
2828

2929
**Shortest-path properties**:
30+
3031
* **Optimal substructure**: Any subpath of a shortest path is itself a shortest path.
3132
* **Triangle inequality**: $\delta(s, v) \leq \delta(s, u) + w(u, v)$ for all edges $(u,v)$.
3233
* **No negative cycles**: If a negative cycle is reachable from $s$, shortest paths are undefined (can be made arbitrarily small).
@@ -38,6 +39,7 @@ and $\delta(s, v) = \infty$ if no path exists.
3839
All SSSP algorithms maintain estimates $v.d \geq \delta(s,v)$ and predecessors $v.\pi$.
3940

4041
**Initialization**:
42+
4143
```text
4244
INITIALIZE-SINGLE-SOURCE(G, s)
4345
1. for each v in V
@@ -47,6 +49,7 @@ INITIALIZE-SINGLE-SOURCE(G, s)
4749
```
4850

4951
**Relaxation** of edge $(u, v)$:
52+
5053
```text
5154
RELAX(u, v, w)
5255
1. if v.d > u.d + w(u, v)
@@ -55,6 +58,7 @@ RELAX(u, v, w)
5558
```
5659

5760
**Key properties**:
61+
5862
* After `RELAX(u, v, w)`: $v.d \leq u.d + w(u,v)$.
5963
* If $u.d = \delta(s, u)$ at the time of relaxation, then afterwards $v.d \leq \delta(s, v)$.
6064
* $v.d$ never increases.
@@ -150,6 +154,7 @@ BELLMAN-FORD(G, w, s)
150154
**Theorem**: After $k$ iterations of the outer loop, $v.d \leq$ the weight of the shortest path from $s$ to $v$ using **at most $k$ edges**.
151155

152156
**Proof by induction**:
157+
153158
* After 0 iterations: $s.d = 0 = \delta_0(s,s)$ and $v.d = \infty$ for $v \neq s$. Correct (no 0-edge paths to others).
154159
* After $k$ iterations: Assume $v.d \leq \delta_{k-1}(s,v)$ for all $v$ after $k-1$ iterations. When we relax edge $(u,v)$: $v.d \leq u.d + w(u,v) \leq \delta_{k-1}(s,u) + w(u,v) = \delta_k(s,v)$.
155160

β€Žnotes/courses/CSCI-UA-310/21-22-min-spanning-tree.mdβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ MST-KRUSKAL(G, w)
8383
## 4. Union-Find (Disjoint Set Union)
8484

8585
Kruskal's algorithm needs efficient support for:
86+
8687
* `MAKE-SET(x)`: Create a singleton set $\{x\}$.
8788
* `FIND-SET(x)`: Return the representative of $x$'s set.
8889
* `UNION(x, y)`: Merge the sets containing $x$ and $y$.
@@ -133,6 +134,7 @@ since $|E| \leq |V|^2$ so $\log E = O(\log V)$.
133134
**Why safe**: At each step, the cut $(S, V \setminus S)$ where $S$ is the current tree respects $A$. The lightest crossing edge is safe by the safe-edge theorem.
134135

135136
Each vertex $v \notin S$ maintains:
137+
136138
* `v.key`: minimum weight of any edge connecting $v$ to a vertex in $S$ (or $\infty$ if none).
137139
* `v.Ο€`: the vertex in $S$ that achieves `v.key`.
138140

@@ -164,6 +166,7 @@ When $u$ is extracted, `u.key` = $w(u, u.\pi)$ is the minimum-weight edge from $
164166
Graph: $V = \{a,b,c,d,e,f,g,h,i\}$, with edges of various weights.
165167

166168
Starting from vertex $a$:
169+
167170
* Extract $a$ (key=0). Update neighbors: $b.key=4, h.key=8$.
168171
* Extract $b$ (key=4). Update: $c.key=8, h.key=11$ (no improvement for $h$).
169172
* Extract $c$ (key=8). Update: $d.key=7, f.key=4, i.key=2$.

β€Žnotes/courses/CSCI-UA-310/23-apsp-floyd-warshall.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Better, but Floyd-Warshall achieves $O(|V|^3)$ with a different subproblem defin
7070
**Subproblem**: $D[u, v, i]$ = shortest path from $u$ to $v$ using only vertices from $\{v_1, \dots, v_i\}$ as intermediate vertices.
7171

7272
**Key observation**: For the shortest $u \to v$ path using only $\{v_1,\dots,v_i\}$:
73+
7374
* **Case 1**: $v_i$ is **not** on the path. Then $D[u,v,i] = D[u,v,i-1]$.
7475
* **Case 2**: $v_i$ **is** on the path. Then the sub-paths $u \to v_i$ and $v_i \to v$ both use only $\{v_1,\dots,v_{i-1}\}$. So $D[u,v,i] = D[u,v_i,i-1] + D[v_i,v,i-1]$.
7576

@@ -103,6 +104,7 @@ FLOYD-WARSHALL(W)
103104
Graph with $V = \{v_1, v_2, v_3, v_4, v_5, v_6\}$:
104105

105106
Computing $D[v_3, v_5, i]$ through successive iterations:
107+
106108
* $D[v_3, v_5, 0] = \infty$ (no direct edge)
107109
* $D[v_3, v_5, 1] = \infty$ (no path through $v_1$ only)
108110
* $D[v_3, v_5, 2] = 5$ (path $v_3 \to v_2 \to v_5$)

β€Žnotes/courses/CSCI-UA-310/25-26-27-complexity.mdβ€Ž

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ These lectures ask two foundational questions: **which problems can computers so
2929
We focus on **decision problems**: problems whose answer is "yes" or "no."
3030

3131
**Examples**:
32+
3233
* **MST**: Given graph $G$, weights $w$, and integer $k$: is the MST of $G$ of weight $\leq k$?
3334
* **Halting**: Given program $P$ and input $x$: does $P(x)$ terminate?
3435
* **Wang Tiling**: Given a set of colored tile types, can they tile the infinite 2D plane such that adjacent tiles share the same color on touching sides?
@@ -38,10 +39,12 @@ We focus on **decision problems**: problems whose answer is "yes" or "no."
3839
## 2. Decidability
3940

4041
**Notation**:
42+
4143
* $\langle P \rangle$ = the source code of program $P$ (in some fixed language).
4244
* $P(x)$ = running program $P$ on input $x$.
4345

4446
**Definition**: An algorithm $A$ **solves** problem $\Pi$ if for all inputs:
47+
4548
* If the answer is "yes," $A$ outputs "yes" and terminates.
4649
* If the answer is "no," $A$ outputs "no" and terminates.
4750

@@ -93,6 +96,7 @@ Therefore $A^{\text{Halt}}$ cannot exist. $\square$
9396
## 4. Reduction: Program Equality is Undecidable
9497

9598
**Program Equality**:
99+
96100
* **Input**: Two programs $\langle A \rangle$ and $\langle B \rangle$.
97101
* **Goal**: Decide if $A(x) = B(x)$ for all strings $x$.
98102

@@ -127,6 +131,7 @@ So $H$ solves Halting β€” contradicting Turing's theorem. Hence $E$ cannot exist
127131
More precisely: any semantic property of programs (one that cannot be determined from syntax alone) is undecidable.
128132

129133
**Examples of undecidable problems**:
134+
130135
* Does program $P$ output "yes" on input $x$?
131136
* Does program $P$ halt on all inputs?
132137
* Do programs $A$ and $B$ compute the same function? (Program Equality above)
@@ -207,6 +212,7 @@ Thus: if there's a poly-time algorithm for VC, there's one for IS (and vice vers
207212
## 9. Class NP
208213

209214
**Formal Definition**: $L \in$ **NP** if there exists a polynomial-time **verifier** $V$ such that:
215+
210216
* For every $x \in L$: there exists a **witness** $w$ with $V(x, w) = \text{"yes"}$.
211217
* For every $x \notin L$: for all $w$: $V(x, w) = \text{"no"}$.
212218
* $V$ runs in polynomial time in $|x|$.
@@ -216,6 +222,7 @@ Thus: if there's a poly-time algorithm for VC, there's one for IS (and vice vers
216222
**Why "NP"**: Non-deterministic Polynomial time.
217223

218224
**Examples in NP**:
225+
219226
* VC: witness = the $k$ vertices.
220227
* IS: witness = the $k$ independent vertices.
221228
* Clique: witness = the $k$-clique vertices.
@@ -244,12 +251,12 @@ This is the foundational result: 3SAT is the "hardest" problem in NP. Proved by
244251
Computable
245252
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
246253
β”‚ NP β”‚
247-
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
248-
β”‚ β”‚ P β”‚ β”‚ NP-complete β”‚ β”‚
249-
β”‚ β”‚ β€’ MST β”‚ β”‚ β€’ 3SAT β”‚ β”‚
250-
β”‚ β”‚ β€’ APSP β”‚ β”‚ β€’ VC β”‚ β”‚
251-
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
252-
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β€’ Halting
254+
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
255+
β”‚ β”‚ P β”‚ β”‚ NP-complete β”‚ β”‚
256+
β”‚ β”‚ β€’ MST β”‚ β”‚ β€’ 3SAT β”‚ β”‚
257+
β”‚ β”‚ β€’ APSP β”‚ β”‚ β€’ VC β”‚ β”‚
258+
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
259+
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β€’ Halting
253260
β€’ Wang Tiling
254261
```
255262

@@ -260,10 +267,12 @@ Computable
260267
**The central open question**: Is P = NP?
261268

262269
**If P $\neq$ NP** (widely believed):
270+
263271
* There is no efficient algorithm for any NP-complete problem (3SAT, VC, IS, ...).
264272
* Cryptography (RSA, etc.) is secure.
265273

266274
**If P $=$ NP**:
275+
267276
* We can solve almost everything in polynomial time.
268277
* "Can verify $\Rightarrow$ can solve."
269278
* Modern cryptography would break down.
@@ -332,6 +341,7 @@ Better hardware improves constants but does not change the asymptotic complexity
332341
### Strategy 2: Simplify the Problem
333342

334343
Identify special cases that are in P:
344+
335345
* 2SAT (2 literals per clause) $\in$ P.
336346
* Vertex Cover on trees $\in$ P.
337347
* Knapsack with small $W$: pseudopolynomial $O(nW)$ DP.

0 commit comments

Comments
Β (0)