rmykkänen

notes

Reducing NP-hard graph problems

date

Independent set

Given as input a graph G=(V,E)G = (V, E) and an integer goal gg, output whether there exists a subset of the vertices SS such that ∣S∣≥g|S| \geq g and that ∀(va,vb)\forall (v_a, v_b) pairs ∈V,∄(va,vb)∈E\in V, \nexists (v_a, v_b) \in E.

Independent Set is in NP\mathsf{NP}, since we can check that ∣S∣≥g|S| \geq g and iterate through the edges in GG and confirm that the source and destination of the edge are not both in SS in O(∣V∣+∣E∣)O(|V| + |E|) time.

Now let's reduce 3SAT to Independent Set to show that it is NP-hard. Given as input a set of clauses C1..CmC_1 .. C_m to 3SAT, build a graph GG as follows:

  1. Represent each literal in the function as a vertex in GG.
  2. For each clause CiC_i, add edges between each pair of literals in the clause, forming triangles.
  3. Add edges between each literal and its negation, if it exists in the function.

We take this graph as input into Independent Set, with goal g=mg = m. Our function ff converts input II of 3SAT into the graph f(I)f(I) for Independent Set. This takes O(3m)=O(m)O(3m) = O(m) time because we have at most three literals per clause to include as vertices into our graph. Our function is polynomial relative to II.

Next, let's show that x∈A  ⟹  f(x)∈Bx \in A \implies f(x) \in B. A valid assignment of 3SAT implies that at least one literal evaluates to True in each clause. Therefore, in our graph f(I)f(I), there must be at least one vertex from each clause that is not connected to the others. This is why we assign g=mg = m; there must be at least mm independent literals that evaluate to True.

Now let's show x∉A  ⟹  f(x)∉Bx \notin A \implies f(x) \notin B. If II is not a valid 3SAT assignment, then there exists a clause of 3 False literals. This means that ∣S∣≤g−1|S| \leq g - 1 because the triangle representing the clause mim_i is not contributing a vertex to SS.

We've shown that Independent Set is in NP, and reduced its NP-hardness from 3SAT; Independent Set is NP-complete.

Clique

Given as input a graph G=(V,E)G = (V, E) and an integer goal gg, output whether there exists a subset of the vertices SS such that ∣S∣≥g|S| \geq g and that ∀(vi,vj)∈S,∃(vi,vj)∈E\forall (v_i, v_j) \in S, \exists (v_i, v_j) \in E.

Clique is in NP\mathsf{NP}, since we can check that ∣S∣≥g|S| \geq g and iterate through SS to ensure that each (vi,vj)∈E(v_i, v_j) \in E in O(∣V∣+∣E∣)O(|V| + |E|) time.

Let's reduce Independent Set to Clique to show that it is NP-hard. Given as input a graph GG and goal gg to Independent Set, convert this graph to G′=(V,Eˉ)G' = (V, \bar{E}) where Eˉ={(x,y)∣(x,y)∉E}\bar{E} = \{(x,y)|(x,y) \notin E\}. This function ff takes O(∣E∣)O(|E|) time.

Let's show x∈A  ⟹  f(x)∈Bx \in A \implies f(x) \in B. If SS is a valid solution for II, then all v∈Sv \in S have no edges connecting them. By the definition of G′,g=f(I)G', g = f(I), each pair of those vertices must have edges connecting them. Therefore, this subset must be a valid solution of Clique with goal gg.

Next, let's do x∈B  ⟹  f(x)∈Ax \in B \implies f(x) \in A. By the same logic as above, if G′G' is a valid clique, and we perform h(G′)=Gh(G') = G, the inverse of f(x)f(x), ∄(vi,vj)∈V∋(vi,vj)∈E\nexists (v_i, v_j) \in V \ni (v_i, v_j) \in E.

We've shown that Clique is in NP, and reduced its NP-hardness from Independent Set; Clique is NP-complete.

Vertex cover

Given a graph G=(V,E)G = (V, E) and a goal gg, output whether ∃S⊆V∋∣S∣≤g\exists S \subseteq V \ni |S| \leq g and every edge in EE has at least one endpoint in SS.

Vertex Cover is in NP\mathsf{NP} since we can confirm that ∣S∣≤g|S| \leq g in O(∣V∣)O(|V|) time and then iterate through EE and ensure that each edge has ≥1\geq 1 endpoint in SS in O(∣V∣∣E∣)O(|V||E|) time.

Let's reduce Independent Set to Vertex cover to show that it is NP-hard. Given as input a graph GG and goal gg to Independent Set, f(I)=(G,∣V∣−g)f(I) = (G, |V| - g). Given a solution SS for Independent Set, we can return S′=V∖SS' = V \setminus S.

Let's show x∈A  ⟹  f(x)∈Bx \in A \implies f(x) \in B. By the definition of Independent Set, our intermediate result SS is a set of gg vertices for which there exists no edge connecting them. This implies that V∖SV \setminus S contains ∣V∣−g|V| - g vertices that must share edges that connect the gg vertices in SS. This is the definition of a Vertex Cover.

Next, let's do f(x)∈B  ⟹  x∈Af(x) \in B \implies x \in A. If there are ∣V∣−g|V| - g vertices which cover all other vertices, then the gg vertices not in SS must not be connected to each other by an edge, as it is required that all edges in EE have at least one endpoint in SS. There must be an Independent Set of size gg.

We've shown that Vertex Cover is in NP\mathsf{NP}, and reduced its NP-hardness from Independent Set; Vertex Cover is NP-complete.

Subset sum

Given a set SS of integers and a target tt, output whether there exists a subset S′⊆SS' \subseteq S such that the sum of the elements of S′S' is exactly tt.

Subset Sum is in NP\mathsf{NP} since we can easily verify whether the sum of the elements of S′=tS' = t in polynomial time.

Let's reduce 3SAT to Subset Sum to show that it is NP-hard. Given as input a boolean expression of nn variables and mm clauses to 3SAT, we will construct a set SS of 2(m+n)2(m + n) elements where each variable and clause will have two elements in the set SS; S={v1,¬v1,..,vn,¬vn,z1,¬z1,..,zm,¬zm}S = \{v_1, \neg v_1, .., v_n, \neg v_n, z_1, \neg z_1, .., z_m, \neg z_m\}. For the reduction, if xix_i is true, we include viv_i in the sum, and never ¬vi\neg v_i, and vice versa. We can set each of the values for the elements as n+mn+m digit numbers. We're essentially coding the evaluation of 3SAT variables and clauses within a derived tt. This tt will take a form similar to 111333. In the first nn columns, column ii has a 1 in both viv_i and ¬vi\neg v_i. In the last mm columns, column n+jn + j has a 1 in each literal that appears in CjC_j. We also have mm buffer variables, z1..zmz_1 .. z_m. If the possible sum can be less than 3, we add 1s in these buffer positions such that we reach 3. Our tt often takes the form 111333 because we must choose either viv_i or ¬vi\neg v_i for each of the nn 1s and then the mm 3s ensure that the clauses are satisfied because if it is not possible to make the sum exactly 3, then it's not possible to satisfy the original boolean expression.

3-coloring

Given a graph G(V,E)G(V, E), can we color the graph such that no two adjacent vertices share the same color?

3-Coloring is in NP\mathsf{NP} since we can verify that a coloring assignment CC is valid if CC contains 3 colors total and that each vertex's neighboring vertices have a different color in O(∣V∣2)O(|V|^2) time.

Let's reduce 3SAT to 3-Coloring to show that it is NP-hard. Consider the 3SAT input of nn variables and mm clauses. We construct a graph G=f(I)G = f(I) as follows:

  1. Create a triangle of nodes from each CiC_i.
  2. For each variable xix_i, construct nodes labeled xix_i and xˉi\bar{x}_i connected in a triangle with the node Base. When GG is three-colored, either xix_i or xˉi\bar{x}_i will get a True coloring, which determines the assignment.
  3. For each Ci=(xi∨xj∨xk)∈IC_i = (x_i \lor x_j \lor x_k) \in I, add an OR-gadget graph with input nodes xi,xj,xkx_i, x_j, x_k and connect the output node of this gadget to the False node and Base. This ensures that if xi,xj,xkx_i, x_j, x_k are False, then the output node of the OR-gadget has to be False, and that if at least one of the inputs is True, then the OR-gadget is True.

Let's show that x∈A  ⟹  f(x)∈Bx \in A \implies f(x) \in B. If xix_i is True, then we can color xix_i True and xˉi\bar{x}_i False in GG, and for each CiC_i, at least one of the nodes xi,xj,xkx_i, x_j, x_k will be True. The OR-gadget for CiC_i can be 3-colored such that the output is True based on the other properties of the things it connects to.

Let's do the inverse, that f(x)∈B  ⟹  x∈Af(x) \in B \implies x \in A. We can determine the truth assignments of xix_i and xˉi\bar{x}_i from the colorings because it cannot be that any OR-gadget has all inputs False. Assume the contrary that all inputs in the OR-gadget are false, then the output of the gadget would be False, but since it's connected to both the False and Base nodes, this would disallow a three-coloring.

We've shown that 3-Coloring is in NP, and reduced its NP-hardness from 3SAT; 3-Coloring is NP-complete.