You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/courses/CSCI-UA-310/08-09-hashing.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ The table size $m$ is much smaller than $|U|$.
51
51
52
52
**Collision**: Two keys $k_1 \neq k_2$ with $h(k_1) = h(k_2)$ is a **collision**. Collisions are unavoidable if $n > m$.
53
53
54
-
### Hash Function Design
54
+
### 3.1 Hash Function Design
55
55
56
56
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.
57
57
@@ -67,7 +67,7 @@ A good hash function satisfies the **simple uniform hashing assumption**: each k
67
67
68
68
* Slot $j$ contains a pointer to the head of the list of all elements with $h(k) = j$.
69
69
70
-
### Pseudocode
70
+
### 4.1 Pseudocode
71
71
72
72
```text
73
73
CHAINED-HASH-INSERT(T, x)
@@ -131,7 +131,7 @@ HASH-SEARCH(T, k)
131
131
132
132
**Deletion** is tricky: cannot just set to `NIL` (would break search). Use a special `DELETED` sentinel.
**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.
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.
291
291
292
-
### Recurrence
292
+
### 8.3 Recurrence
293
293
294
294
Define $m[i,j]$ = minimum number of multiplications to compute $A_i \cdots A_j$.
Copy file name to clipboardExpand all lines: notes/courses/CSCI-UA-310/15-16-greedy.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,13 +33,13 @@ A greedy algorithm is correct when the **greedy choice property** holds: a globa
33
33
34
34
## 2. Interval Scheduling (Activity Selection)
35
35
36
-
### Problem
36
+
### 2.1 Problem
37
37
38
38
**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).
39
39
40
40
**Output**: A maximum-size set of mutually compatible activities.
41
41
42
-
### Greedy Algorithm
42
+
### 2.2 Greedy Algorithm
43
43
44
44
**Greedy rule**: Always select the compatible activity with the **earliest finish time**.
45
45
@@ -57,7 +57,7 @@ GREEDY-ACTIVITY-SELECTOR(s, f)
57
57
58
58
**Running time**: $O(n \log n)$ for sorting; $O(n)$ for selection. Total: $O(n \log n)$.
59
59
60
-
### Correctness (Exchange Argument)
60
+
### 2.3 Correctness (Exchange Argument)
61
61
62
62
**Theorem**: `GREEDY-ACTIVITY-SELECTOR` produces an optimal solution.
63
63
@@ -75,15 +75,15 @@ Since the greedy finishes no later than optimal at each step, if $O$ has $m$ act
75
75
76
76
## 3. Fractional Knapsack
77
77
78
-
### Problem
78
+
### 3.1 Problem
79
79
80
80
Same as 0/1 Knapsack but items can be split: take a **fraction** $x_i \in [0,1]$ of item $i$.
81
81
82
82
**Input**: $n$ items with weights $w_i$ and values $v_i$; capacity $W$.
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).
208
208
209
209
**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.
210
210
211
-
### Lemma 2 (Optimal Substructure)
211
+
### 8.2 Lemma 2 (Optimal Substructure)
212
212
213
213
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\}$.
0 commit comments