Skip to content

Commit a86335a

Browse files
More PPs and notes etc
1 parent 7d3181b commit a86335a

24 files changed

Lines changed: 269 additions & 54 deletions

OMSCS/Courses/GA/07.3 - LP - Geometry.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ The Optimum of LP is achieved at a vertex of the feasible region except for 2 ca
2121
- $3x+2y \ge 6$
2222
- $x,y \ge 0$
2323
- The objective function has nothing to do with it. There are no points which satisfy all constraints.
24-
25-
![[Pasted image 20260401145018.png]]
26-
24+
25+
![[Pasted image 20260401145018.png]]
26+
2727
### Unbounded
2828
- This means that the feasible region is arbitrarily large.
2929
- Consider $\max \space x+y$

OMSCS/Courses/GA/08.2 - NP - Knapsack.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,53 @@ tags:
44
- Algorithms
55
---
66
# 08.2 - NP - Knapsack
7+
- NP-Completeness
8+
- All of NP can be reduced to SAT
9+
- SAT can be reduced to 3SAT
10+
- 3SAT can be reduced to IS
11+
- IS can be reduced to Clique and Vertex Cover
12+
- **3SAT can be reduced to Subset-Sum**
13+
- **Subset-Sum can be reduced to Knapsack**
14+
15+
Recall that Knapsack's DP solution requires $O(nB)$ time, which is not polynomial in the size of the input. It would need to be $O(n^p \cdot log(B))$ instead of $O(n^p \cdot B)$.
16+
17+
## Subset Sum Problem
18+
- **Input:** positive integers: $a_1,...,a_n$ and $t$
19+
- **Output:** subset $S$ of $\{1,...,n\}$ where
20+
- $\sum_{i \in S} a_i = t$ (if such a sum exists)
21+
- No otherwise
22+
- You can use Dynamic Programming to find an $O(nt)$ time algorithm, this is not a P-time algorithm, since it's order $O(n^p \cdot t)$ not $O(n^p \cdot log(t))$.
23+
24+
### Theorem: Subset-Sum is NP-Complete
25+
- It's in NP, because we can validate the solution on $O(n \space log \space t)$ time.
26+
- We can reduce 3SAT to Subset-Sum.
27+
- Input to subset-sum: $2n+2m+1$ numbers.
28+
- $v_i,v_i'$ will correspond to $x_i,\overline{x_i}$
29+
- $S_j,\overline{S_j}$ will correspond to $C_j,\overline{C_j}$
30+
- $t$
31+
- All are $\le n+m$ digits long in base 10
32+
- $t \approx 10^{n+m}$
33+
- How to set the variables? Carefully.
34+
35+
![[Pasted image 20260407142432.png]]
36+
![[Pasted image 20260407142908.png]]
37+
38+
### Correctness
39+
![[Pasted image 20260407143023.png]]
40+
41+
![[Pasted image 20260407143055.png]]
42+
43+
![[Pasted image 20260407143137.png]]
44+
45+
46+
### Example
47+
![[Pasted image 20260407142655.png]]
48+
49+
![[Pasted image 20260407142728.png]]
50+
51+
![[Pasted image 20260407142801.png]]
52+
53+
54+
## Ungraded Homework
55+
Prove that Knapsack is NP-Complete: [[8.99.1 - Knapsack is NP-Complete (TODO)]]
56+

OMSCS/Courses/GA/Practice Problems/7.11 - Dual to the Example (TODO).md

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
---
7+
# 7.11 - Dual to the Example
8+
9+
Write the dual to the following linear program.
10+
11+
- $\max x+y$
12+
- $2x+y \le 3$
13+
- $x+3y \le 5$
14+
- $x,y \ge 0$
15+
16+
Find the optimal solutions to both primal and dual LPs.
17+
18+
**Dual LP**
19+
- $\min \space 3y_1 + 5y_2$
20+
- s.t.
21+
- $y_1,y_2 \ge 0$
22+
- $2y_1 + y_2 \ge 1$
23+
- $y_1 + 3y_2 \ge 1$
24+
25+
This one got a little ugly, but it's the same process from [[7.12 - Prove Optimality of Point]].
26+
27+
![[Scanned_20260408-1637.jpg]]

OMSCS/Courses/GA/Practice Problems/7.12 - Prove Optimality of Point (TODO).md

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
---
7+
# 7.12 - Prove Optimality of Point
8+
9+
For the linear program
10+
11+
- $\max x_1-2x_3$
12+
- $x_1-x_2 \le 1$
13+
- $2x_2 - x_3 \le 1$
14+
- $x_1,x_2,x_3 \ge 0$
15+
16+
prove that the solution $(x_1, x_2, x_3) = (3/2, 1/2, 0)$ is optimal.
17+
18+
## Work
19+
Okay, so there's 5 half-spaces being defined here. Each half-space is defined by a plane. Each pair of planes defines a line. Each trio of planes define a point. Since there are 5 spaces, there are $5 \choose 3$ points.
20+
21+
$${5 \choose 3} = \frac{5!}{(3!)(2!)}=\frac{(5)(4)}{(2)(1)}=10$$
22+
23+
What are all of these points? How can we find them?
24+
25+
We can write all of the constraint inequalities as equalities.
26+
27+
1. $x_1=0$
28+
2. $x_2=0$
29+
3. $x_3=0$
30+
4. $x_1-x_2=1$
31+
5. $2x_2-x_3=1$
32+
33+
Now we can perform the 5 nCr 3 "joins" of these equations to find the vertices formed by each equation. If there is no solution to a triplet of equations, then there does not exist a singular point from the 3 equations, meaning they do not form a vertex of the feasible region. If they do form a vertex, we need to check whether that point lies on the boundary of the feasible region. This basically means we need to check to make sure it doesn't violate any of the constraints. The most common constraint violation will likely be $x_1,x_2,x_3 \ge 0$. Then we can evaluate the objective function at each of the feasible points, and take the max over those points.
34+
35+
Here's that work. There's apparently only 4 vertices in the feasible region.
36+
37+
| Equations | X | Feasible? | Obj: $x_1-2x_3$ |
38+
| --------- | ------------ | -------------------- | --------------- |
39+
| 1,2,3 | (0,0,0) | Yes | 0 |
40+
| 1,2,4 | No solution | n/a | n/a |
41+
| 1,2,5 | (0,0,-1) | no ($x_3 \not\ge 0$) | n/a |
42+
| 1,3,4 | (0,-1,0) | no ($x_2 \not\ge 0$) | n/a |
43+
| 1,3,5 | (0,1/2,0) | yes | 0 |
44+
| 1,4,5 | (0,-1,...) | no ($x_2 \not\ge 0$) | n/a |
45+
| 2,3,4 | (1,0,0) | yes | 1 |
46+
| 2,3,5 | No solution. | n/a | n/a |
47+
| 2,4,5 | (1,0,-1) | no ($x_3 \not\ge 0$) | n/a |
48+
| 3,4,5 | (3/2,1/2,0) | yes | **1.5** |
49+
50+
Among the vertices of the feasible region, $(3/2,1/2,0)$ maximizes the objective function, with value $3/2$. Looking at the geometric view, this LP appears to be potentially unbounded.
51+
52+
We need to make sure that the Dual LP is also feasible in order to show that the Primal LP is bounded. Minimizing the objective function for the Dual LP also gives us an upper bound on the maximum value for the Primal LP's objective function.
53+
54+
**Primal**
55+
- $\max \space x_1+0x_2-2x_3$
56+
- s.t. $1x_1-1x_2+0x_3 \le 1$
57+
- s.t. $0x_1 + 2x_2 - 1x_3 \le 1$
58+
- s.t. $x_1,x_2,x_3 \ge 0$
59+
60+
**Dual**
61+
- $\min\space y_1 + y_2$
62+
- s.t. $1y_1 + 0y_2 \ge 1$
63+
- s.t. $-1y_1 + 2y_2 \ge 0$
64+
- s.t. $0y_1 - 1y_2 \ge -2$
65+
- s.t. $y_1,y_2 \ge 0$
66+
67+
We now need to check all of the points of intersection of the corresponding equalities, like we did with the primal. In this case, we have 2 unknowns, so we need to take these 5 equations and choose 2, resulting in ${5 \choose 2} = 10$ possible feasible region vertices.
68+
69+
1. $y_1=0$
70+
2. $y_2=0$
71+
3. $y_1=1$
72+
4. $-y_1+2y_2 = 0$
73+
5. $-y_2=-2$
74+
75+
| Equations | Y | Feasible? | Obj: $y_1+y_2$ |
76+
| --------- | ----------- | ------------------------------- | -------------- |
77+
| 1,2 | (0,0) | No ($y_1 \not\ge 1$) | n/a |
78+
| 1,3 | No solution | n/a | n/a |
79+
| 1,4 | (0,...) | No ($y_1 \not\ge 1$) | n/a |
80+
| 1,5 | (0,...) | No ($y_1 \not\ge 1$) | n/a |
81+
| 2,3 | (1,0) | No ($-y_1+2y_2 = -1 \not\ge 0$) | n/a |
82+
| 2,4 | (0,0) | No ($y_1 \not\ge 1$) | n/a |
83+
| 2,5 | No solution | n/a | n/a |
84+
| 3,4 | (1, 1/2) | Yes | **3/2** |
85+
| 3,5 | (1,2) | Yes | 3 |
86+
| 4,5 | (4,2) | Yes | 6 |
87+
Here we find that the feasible region of the Dual LP has 3 vertices, of which $(1, 1/2)$ minimizes the objective function with value 3/2. Therefore, we've found an upper bound on the possible value for the Primal LP's objective function.
88+
89+
Point $(3/2,1/2,0)$ maximizes the Primal LP's objective function, with value $3/2$, which is equal to the minimum value of the Dual LP's objective function.

OMSCS/Courses/GA/Practice Problems/7.2 - Duckwheat (TODO).md

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
---
7+
# 7.2 - Duckwheat
8+
> Duckwheat is produced in Kansas and Mexico and consumed in New York and California. Kansas produces 15 shnupells of duckwheat and Mexico 8. Meanwhile, New York consumes 10 shnupells and California 13. The transportation costs per shnupell are $4 from Mexico to New York, $1 from Mexico to California, $2 from Kansas to New York, and $3 and from Kansas to California.
9+
>
10+
> Write a linear program that decides the amounts of duckwheat (in shnupells and fractions of a shnupell) to be transported from each producer to each consumer, so as to minimize the overall transportation cost.
11+
12+
They couldn't have worded this dumber.
13+
14+
## Work
15+
- Variables
16+
- $kc$ - Units from Kansas to California
17+
- $mc$ - Units from Mexico to California
18+
- $ky$ - Units from Kansas to New (Y)ork
19+
- $my$ - Units from Mexico to New (Y)ork
20+
- Objective:
21+
- $\min \space 4my+1mc+2ky+3kc$ (as stated)
22+
- $\max \space -4my-1mc-2ky-3kc$ (normalized)
23+
- Constraints
24+
- $kc + ky = 15$
25+
- $kc+ky \le 15$
26+
- $kc+ky \ge 15 \implies -ky-ky \le 15$
27+
- $mc+my=8$
28+
- $mc + my \le 8$
29+
- $-mc-my \le 8$
30+
- $ky+my=10$
31+
- $ky+my \le 10$
32+
- $-ky-my \le 10$
33+
- $kc+mc=13$
34+
- $kc+mc \le 13$
35+
- $-kc-mc \le 13$
36+
- $kc,mc,ky,my \ge 0$
37+
38+
## Normal Form
39+
- **Objective:** $\max \space -4my-mc-2ky-3kc$
40+
- **Constraints:**
41+
- $kc+ky \le 15$
42+
- $-ky-ky \le 15$
43+
- $mc + my \le 8$
44+
- $-mc-my \le 8$
45+
- $ky+my \le 10$
46+
- $-ky-my \le 10$
47+
- $kc+mc \le 13$
48+
- $-kc-mc \le 13$
49+
- $kc,mc,ky,my \ge 0$

OMSCS/Courses/GA/Practice Problems/7.7 - Necessary and Sufficient Conditions (TODO).md

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
- Practice
6+
---
7+
# 7.7 - Necessary and Sufficient Conditions
8+
Find necessary and sufficient conditions on the reals $a$ and $b$ under which the linear program
9+
10+
- $\max \space x+y$
11+
- $ax+by \le 1$
12+
- $x,y \ge 0$
13+
14+
Is (a) infeasible, (b) unbounded, and (c) has a unique optimal solution.
15+
16+
## Infeasible
17+
We need to find $a$ and $b$ such that the feasible region is empty. This never occurs. $(x=0,y=0)$ always satisfies the constraints, regardless of the values of $a$ and $b$.
18+
19+
## Unbounded
20+
The LP becomes unbounded whenever $a \le 0$ or $b \le 0$.
21+
22+
- When $a$ is 0, the half-plane $ax+by \le 1$ is defined by a line which is parallel with the x axis.
23+
- When $b$ is 0, the half-plane $ax+by \le 1$ is defined by a line which is parallel with the y axis.
24+
- When $b \gt 0$ and $a \lt 0$, the half-plane $ax+by \le 1$ is defined by a line with a positive slope, and the feasible region contains all points below that line. (0,0) is always contained within that region.
25+
- When $a \gt 0$ and $b \lt 0$, the half-plane $ax+by \le 1$ is defined by a line with a positive slope, and the feasible region contains all points above that line. (0,0) is always contained within that region.
26+
- When both $a,b < 0$, the half-plane $ax+by \le 1$ is defined by a line with negative slope, and the feasible region contains all points above that line. (0,0) is always contained within that region.
27+
28+
On the corollary, if both $a,b > 0$, then the half-plane $ax+by \le 1$ is defined by a line with negative slope, and the feasible region contains all points below that line. (0,0) is always contained within that region. The difference with this case is that the half plane is also bounded by the x and y axes, constraining the region to a triangular shape.
29+
30+
## Unique Optimal Solution
31+
This LP has a unique optimal solution whenever $a,b>0$ and $a \ne b$. This causes the feasible region to be bounded, forming a right triangle where the hypotenuse is not parallel with $x+y$. This causes the objective function to become maximized at one of the two points.

0 commit comments

Comments
 (0)