Skip to content

Commit fef29b7

Browse files
More notes and practice problems on NP-Completeness
1 parent d24b05c commit fef29b7

23 files changed

Lines changed: 658 additions & 49 deletions

OMSCS/Courses/GA/06.2 - NP - Definitions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,5 @@ See [[06.1 - NP - Theory Guidance]] for more info on what's required here.
243243
- For a given problem B that _might_ be NP-complete, we simply need to show that there exists an NP-complete problem that can be reduced to B. SAT is the common one since it's so flexible, but there may be a different NP-C problem which is easier to reduce.
244244

245245
## Practice Problems
246-
- [[8.1 - TSP optimization versus search (TODO)]]
247-
- [[8.2 - Search vs Decision (TODO)]]
246+
- [[8.1 - TSP optimization versus search]]
247+
- [[8.2 - Search vs Decision]]

OMSCS/Courses/GA/06.3 - NP - 3SAT.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,6 @@ Given satisfying assignment $\sigma'$ for $f'$, how do we produce satisfying ass
142142
$f$ has $n$ variables and $m$ clauses. $f'$ has $O(nm)$ variables and $O(nm)$ clauses. These are polynomial in the size of $f$.
143143

144144
## Practice Problems
145-
- [[8.3 - Stingy SAT (TODO)]]
146-
- [[8.8 - Exact 4-SAT (TODO)]]
145+
- [[8.3 - Stingy SAT]]
146+
- [[8.8 - Exact 4SAT]]
147147

Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
---
2+
tags:
3+
- OMSCS
4+
- Algorithms
5+
---
6+
# 06.5 - NP - Graph Problems
7+
- 3SAT is NP-complete
8+
- Independent Sets
9+
- Clique
10+
- Vertex Cover
11+
12+
## Independent Set
13+
- For undirected $G=(V,E)$
14+
- A subset $S \subset V$ is an independent set if no edges are contained in $S$.
15+
- $\forall_{x,y \in S} \space (x,y) \notin E$
16+
- **Input:** Undirected $G=(V,E)$
17+
- **Output:** Independent set $S$ of maximum size
18+
19+
This problem is not in NP, because there's no way to verify that $S$ is of maximum size without running the algorithm again. If we introduce a goal, we can produce an algorithm which is in NP.
20+
21+
## Independent Set (Search)
22+
- **Input:**
23+
- Undirected $G=(V,E)$
24+
- Goal $g$
25+
- **Output:**
26+
- Independent set $S$ where $|S| \ge g$ (if one exists), "No" otherwise
27+
28+
This version of the problem lies in NP, because we can easily validate it.
29+
- $\forall_{x,y \in S} \space (x,y) \notin E$ runs in $O(n^2)$ time
30+
- Checking $|S| \ge g$ runs in $O(n)$ time
31+
32+
Can we reduce from a known NP-complete problem to the independent-set problem?
33+
34+
## 3SAT to IS reduction
35+
- Consider 3SAT input $f$ with variables $x_1,...,x_n$ and clauses $C_1, ..., C_m$
36+
- Each clause has size $|C_i| \le 3$
37+
- We can define a graph $G$ based on $f$, and set the goal to m ($g=m$).
38+
- Idea: For each clause $C_i$, we'll create $|C_i|$ vertices.
39+
- There will be at most $3m$ vertices in $G$
40+
- Different types of edges, the first type being **Clause Edges**
41+
42+
### Clause Edges
43+
![[Pasted image 20260326141036.png]]
44+
45+
- Independent set $S$ has $\le 1$ vertex per clause.
46+
- Since $g=m$, solution has $=1$ vertex per clause.
47+
- Each vertex $v \in S$ defines the "satisfied literal" in a given clause.
48+
- The independent set $S$ must correspond to a valid assignment. In the example above, we can't use $S=\{x_1, x_5, \overline{x_1}\}$, since this sets $(x_1=T) \wedge (\overline{x_1}=T)$
49+
- To account for this, we introduce **Variable Edges**
50+
51+
52+
### Variable Edges
53+
![[Pasted image 20260326141628.png]]
54+
55+
- For each variable $x_i$, we add edges between all literals $x_i$ and all $\overline{x_i}$ in $G$
56+
57+
### Example
58+
- Variables $x,y,w,z$
59+
- $f=(\overline{x} \vee y \vee \overline{z}) \wedge (x \vee \overline{y} \vee w) \wedge (\overline{x} \vee \overline{w}) \wedge (\overline{y} \vee z \vee w)$
60+
- We'll start by encoding the clauses
61+
- Add a vertex for each literal in each clause
62+
- Connect all literals in the same clause to each other
63+
- Then we add edges between all literals which are opposite each other.
64+
- The graph below shows the resulting network.
65+
- The clauses are separated into boxes for visual clarity.
66+
- These clausal structures aren't apparent in the graph.
67+
- Each vertex has a unique label, based on clause and literal
68+
69+
```mermaid
70+
graph LR
71+
72+
subgraph "C1"
73+
c1nx((~x))
74+
c1y((y))
75+
c1nz((~z))
76+
c1nx <--> c1y
77+
c1nx <--> c1nz
78+
c1nz <--> c1y
79+
end
80+
81+
subgraph "C2"
82+
c2x((x))
83+
c2ny((~y))
84+
c2w((w))
85+
c2x <--> c2ny
86+
c2x <--> c2w
87+
c2w <--> c2ny
88+
end
89+
90+
subgraph "C3"
91+
c3nx((~x))
92+
c3nw((~w))
93+
c3nx <--> c3nw
94+
end
95+
96+
subgraph "C4"
97+
c4ny((~y))
98+
c4z((z))
99+
c4w((w))
100+
c4ny <--> c4z
101+
c4z <--> c4w
102+
c4w <--> c4ny
103+
end
104+
105+
c1y <--> c2ny
106+
c1y <--> c4ny
107+
c1nx <--> c2x
108+
c2x <--> c3nx
109+
c2w <--> c3nw
110+
c3nw <--> c4w
111+
c1nz <--> c4z
112+
113+
```
114+
115+
We can then find an independent set in this network. Here's one such IS. This corresponding to assignment:
116+
- $x=F, y=F, w=F, z=?$
117+
- Note that $z$ is floating, and can be T or F
118+
119+
```mermaid
120+
graph LR
121+
122+
subgraph "C1"
123+
c1nx(((~x)))
124+
c1y((y))
125+
c1nz((~z))
126+
c1nx <--> c1y
127+
c1nx <--> c1nz
128+
c1nz <--> c1y
129+
end
130+
131+
subgraph "C2"
132+
c2x((x))
133+
c2ny(((~y)))
134+
c2w((w))
135+
c2x <--> c2ny
136+
c2x <--> c2w
137+
c2w <--> c2ny
138+
end
139+
140+
subgraph "C3"
141+
c3nx((~x))
142+
c3nw(((~w)))
143+
c3nx <--> c3nw
144+
end
145+
146+
subgraph "C4"
147+
c4ny(((~y)))
148+
c4z((z))
149+
c4w((w))
150+
c4ny <--> c4z
151+
c4z <--> c4w
152+
c4w <--> c4ny
153+
end
154+
155+
c1y <--> c2ny
156+
c1y <--> c4ny
157+
c1nx <--> c2x
158+
c2x <--> c3nx
159+
c2w <--> c3nw
160+
c3nw <--> c4w
161+
c1nz <--> c4z
162+
163+
```
164+
165+
### Correctness
166+
> $f$ has a satisfying assignment $\iff$ $G$ has an independent set of size $\ge g$
167+
### Forward Implication
168+
> $f$ has a satisfying assignment $\rightarrow$ $G$ has an independent set $S$ of size $|S| \ge g$
169+
170+
- Consider a satisfying assignment for $f$
171+
- For each clause $C$, take 1 of the satisfied literals, and add the corresponding vertex to $S$
172+
- $|S|=m=g$
173+
- $S$ has $=1$ vertex per clause, and not both $x_i$ and $\overline{x_i}$.
174+
- "$S$ has $=1$ vertex per clause". This means there are no clause edges connecting the vertices in $S$
175+
- "not both $x_i$ and $\overline{x_i}$". This means there are no variable edges connecting vertices in $S$.
176+
- Therefore, $S$ is an independent set in $G$
177+
### Reverse Implication
178+
> $f$ has a satisfying assignment $\leftarrow$ $G$ has an independent set $S$ of size $|S| \ge g$
179+
180+
- Consider independent set $S$ of size $\ge g$
181+
- This has $=1$ vertex per clause
182+
- We set the corresponding literal to True
183+
- Therefore every clause is satisfied
184+
- $S$ encodes no contradictory literals, due to the **variable edges**
185+
- Therefore, $S$ defines a valid assignment for $f$
186+
- Note that $S$ doesn't need coverage over the variables that constitute $f$ in order for $S$ to define a **satisfying** assignment for $f$. $f$ may have many different satisfying assignments.
187+
188+
## NP-Hard
189+
- Let's go back and look at the optimization version of IS
190+
- **Input:** Undirected $G=(V,E)$
191+
- **Output:** Independent set $S$ of maximum size
192+
- For the IS-Search problem, it's possible to validate a solution in polynomial time
193+
- It's possible to reduce IS-Search to Max-IS
194+
- If you find the max IS, that either gives you an $|S| \ge g$ or no solution
195+
- Therefore there exists a reduction from every problem in NP to Max-IS
196+
- There exists a reduction from every problem in NP to 3SAT
197+
- There exists a reduction from 3SAT to IS
198+
- There exists a reduction from IS to Max-IS
199+
- **Theorem:** Max-Independent-Set problem in **NP-Hard**.
200+
- **NP** problems are hard problems which have P-time solutions.
201+
- **NP-Complete** problems are the hardest problems in NP.
202+
- **NP-Hard** means it's at least as hard as every problem in NP.
203+
204+
## Clique Problem
205+
- A clique is a fully-connected subgraph.
206+
- For $G=(V,E)$, $S \subset V$ if for all $\forall_{x,y \in S}\space (x,y) \in E$
207+
- This is the opposite of IS?
208+
- We want **big cliques.**
209+
- A single vertex forms a clique.
210+
- 2 connected vertices form a clique.
211+
- Small cliques are easy, big cliques are the problem
212+
- The highlighted vertices below form a clique
213+
214+
![[Pasted image 20260326151527.png]]
215+
216+
### Clique Problem Formulated
217+
- **Input:**
218+
- $G=(V,E)$
219+
- goal $g$
220+
- **Output:**
221+
- $S \subset V$ where $S$ is a clique of size $|S| \ge g$, if one exists, and "NO" otherwise.
222+
223+
### Clique Proof
224+
> Clique is NP-Complete
225+
226+
We can validate a not-NO solution to this problem in $O(n^3)$ time by checking the definition of a clique: $\forall_{x,y \in S}\space (x,y) \in E$. Better algorithms exist and we could get this down to $O(n^2)$, but the point is we have a "bad" validation algorithm which is polynomial. We can also validate $|S| \ge g$ in $O(n)$ time.
227+
228+
**Therefore Clique is in NP.**
229+
230+
To prove that Clique is NP-Complete, we have to find a reduction from a known NP-Complete problem to Clique. We already have a known graph problem which is NP-Complete within this very document. Can we modify it?
231+
232+
### IS $\rightarrow$ Clique Reduction
233+
- **Key Idea:**
234+
- Cliques are fully connected in S.
235+
- IS has no edges within S.
236+
- Clique is the **opposite** of IS
237+
- For $G=(V,E)$, we assume that we have an algorithm for the clique problem.
238+
- Therefore, to solve the "opposite" problem, we take the opposite of the graph.
239+
- $\overline{G}=(V,\overline{E})$ where $\overline{E}=\{(x,y) : x,y \in V \text{ and } (x,y) \notin E\}$
240+
- $(x,y) \in E \iff (x,y) \notin \overline{E}$
241+
- $S$ is a clique in $\overline{G}$ $\iff$ $S$ is an independent set in $G$
242+
- If $S$ is an independent set in $G$, then in $\overline{G}$, all of the vertices in $S$ are fully connected
243+
- If $S$ is fully connected in $G$, then in $\overline{G}$, all of the vertices in $S$ have no edges directly between them.
244+
- Remember that currently, "Clique" is the **unknown** problem, and IS is the **known hard** problem. We need to translate the inputs to IS to the input for Clique, then transform the output of Clique to the output of IS.
245+
- Given input $G=(V,E)$ and goal $g$ for the IS problem.
246+
- Let $\overline{G}$ and $g$ be the input to the clique problem
247+
- We get a set $S$ which is a Clique in $\overline{G}$
248+
- Returning $S$ gives us an IS in $G$, the solution to the IS problem
249+
- If we get NO, we return NO.
250+
251+
## Vertex Cover
252+
- For undirected $G=(V,E)$, subset $S \subset V$ is a vertex cover if it "covers every edge".
253+
- $S$ "covers" every edge if $\forall_{a,b \in E} \space (a \in S) \vee (b \in S)$
254+
- $S=V$ is always a valid vertex cover, but not an interesting one.
255+
- The NP-Hard variant of this problem is finding the minimum size $S$.
256+
257+
![[Pasted image 20260326170445.png]]
258+
259+
### Search Version
260+
- **Input**
261+
- $G=(V,E)$
262+
- budget $b$
263+
- **Output**
264+
- vertex cover $S$ of size $|S| \le b$ if one exists
265+
- No otherwise
266+
- Vertex Cover is in NP
267+
- Proposed solution: $S$
268+
- Iterate over all edges: $O(n+m)$
269+
- See if either its vertices are in $S$
270+
- Check $|S| \le b$: $O(n)$
271+
- This is poly-time.
272+
- Is Vertex Cover in NP-Complete? Need to reduce from another problem to Vertex Cover
273+
- Candidate Problems
274+
- SAT
275+
- 3SAT
276+
- IS
277+
- Clique
278+
- Most natural to take a graph problem, so IS or Clique. Let's try IS
279+
280+
### IS $\rightarrow$ VC
281+
- **Idea:** Model the edges as vertices and the vertices as edges?
282+
- **Claim:** $S$ is a vertex cover $\iff$ $\overline{S}$ is an independent set
283+
284+
The graph below is a minimum vertex cover. The shaded vertices are $S$. $\overline{S}$ is an independent set. How does this work?
285+
286+
![[Pasted image 20260326202008.png]]
287+
288+
Suppose that $S$ is a vertex cover. That means that all of the edges in $G$ touch at least one covered vertex. Now suppose that there exists a vertex $a$ which is connected to a vertex $b$, where $a \notin S$ and $b \notin S$. This would mean that $(a,b)$ is not covered by $S$, making $S$ not a vertex cover. This presents a contradiction. Therefore, if $S$ is a vertex cover, there cannot exist any connected pairs of vertices which are not in $S$. This means $\overline{S}$ must be an independent set.
289+
290+
### Forward Implication
291+
> $S$ is a vertex cover $\implies$ $\overline{S}$ is an independent set
292+
293+
- Take vertex cover $S$.
294+
- For edge $(x,y) \in E$
295+
- $\ge 1$ of x or y are in $S$
296+
- $\le 1$ of x or y in $\overline{S}$
297+
- This implies that no edge is contained in $\overline{S}$
298+
- Thus, $\overline{S}$ is an independent set.
299+
300+
### Reverse Implication
301+
> $\overline{S}$ is an independent set $\implies$ $S$ is a vertex cover
302+
303+
- Take independent set $\overline{S}$
304+
- For every $(x,y) \in E$
305+
- $\le 1$ of x or y in $\overline{S}$
306+
- $\ge 1$ of x or y in $S$
307+
- Therefore, $S$ covers every edge in the graph.
308+
309+
### Reduction
310+
$IS \rightarrow VC$
311+
312+
- For input $G=(V,E)$ and $g$ for independent set
313+
- let $b = n - g$
314+
- Run vertex cover on $G,b$
315+
- $G$ has a vertex cover of size $\le n-g$ $\iff$ $G$ has an independent set of size $\ge g$
316+
- $n-g$ because $|V|=|S|+|\overline{S}|=n=g+(n-g)$
317+
- Given solution $S$ for VC, return $\overline{S}$ as solution to IS problem
318+
- If no solution for VC, then there's no solution for the IS problem
319+
320+
## Practice Problems
321+
- [[8.4 - Clique 3]]
322+
- [[8.10 - NP-Completeness by Generalization]]
323+
- [[8.14 - Clique + IS (TODO)]]
324+
- [[8.19 - Kite (TODO)]]
325+
326+
Remember for all of these practice problems, and for future homeworks in this course.
327+
- Follow this general procedure
328+
- Take a known NP-Complete problem, and an unknown problem.
329+
- Validate that the unknown problem is in NP.
330+
- You might need to modify the problem to become a "search" problem by introducing a goal or budget.
331+
- Then, suppose that an algorithm exists to solve instances of that unknown problem.
332+
- Use the algorithm for the unknown problem to solve instances of the known NP-Complete problem.
333+
- Translate instances of the known NP-Complete problem into instances of the unknown NP problem.
334+
- Translate solutions to the unknown NP problem back into instances of the known NP-Complete problem.
335+
- How to pick which algorithm? Try to pick one which is similar in structure.
336+
- SAT-like problem? Use SAT or 3SAT
337+
- Graph problem? IS, Cliques, or Vertex Cover
338+
- Roughly 2 flavors of NP-Completeness reduction
339+
- Generalization
340+
- Show that the new problem is more general than the NP-Complete problem.
341+
- Generally speaking, NP-Complete -> NP reduction is always demonstrating a generalization
342+
- In this strategy, we're setting the parameters of the unknown problem to handle the parameters of the known problem.
343+
- Gadget
344+
- 3SAT proof.
345+
- [[3SAT is NP.pdf]]
346+
- [[06.3 - NP - 3SAT]]
347+
- Take the formula and modify it in some way.

OMSCS/Courses/GA/Practice Problems/8.1 - TSP optimization versus search (TODO).md

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)