Independent set

Given as input a graph and an integer goal , output whether there exists a subset of the vertices such that and that pairs .

Independent Set is in , since we can check that and iterate through the edges in and confirm that the source and destination of the edge are both not in time.

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

  1. Represent each literal in the function as a vertex in .
  2. For each clause , 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 . Our function converts input of 3SAT into the graph for Independent Set. This takes time because we have at most three literals per clause to include as vertices into our graph. Our function is polynomial relative to .

Next, let’s show that . A valid assignment of 3SAT implies that at least one literal evaluates to True in each clause. Therefore, in our graph , there must be at least one vertex from each clause that is not connected to the others. This is why we assign ; there must be at least independent literals that evaluate to True.

Now let’s show . If is not a valid 3SAT assignment, then there exists a clause of 3 False literals. This means that because the triangle representing the clause is not contributing a vertex to .

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 and an integer goal , output whether there exists a subset of the vertices such that and that .

Clique is in , since we can check that and iterate through to ensure that each in time.

Let’s reduce Independent Set to Clique to show that it is NP-hard. Given as input a graph and goal to Independent Set, convert this graph to where . This function takes time.

Let’s show . If is a valid solution for , then all have no edges connecting them. By the definition of , each pair of those vertices must have edges connecting them. Therefore, this subset must be a valid solution of Clique with goal .

Next, let’s do . By the same logic as above, if is a valid clique, and we perform , the inverse of , .

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 and a goal , output whether and all are accessible by one edge from a .

Vertex Cover is in since we can confirm that in time and then iterate through and ensure that each edge has endpoint in in time.

Let’s reduce Independent Set to Vertex cover to show that it is NP-hard. Given as input a graph and goal to Independent Set, . Given a solution for Independent Set, we can return .

Let’s show . By the definition of Independent Set, our intermediate result is a set of vertices for which there exists no edge connecting them. This implies that contains vertices that must share edges that connect the vertices in . This is the definition of a Vertex Cover.

Next, let’s do . If there are vertices which cover all other vertices, then the vertices not in must not be connected to each other by an edge, as it is required that all edges in have at least one endpoint in . There must be an Independent Set of size .

We’ve shown that Vertex Cover is in , and reduced its NP-hardness from Independent Set; Vertex Cover is NP-complete.

Subset sum

Given a set of integers and a target , output whether there exists a subset such that the sum of the elements of is exactly .

Subset Sum is in since we can easily verify whether the sum of the elements of in polynomial time.

Let’s reduce 3SAT to Subset Sum to show that it is NP-hard. Given as input a boolean expression of variables and clauses to 3SAT, we will construct a set of elements where each variable and clause will have two elements in the set ; . For the reduction, if is true, we include in the sum, and never , and vice versa. We can set each of the values for the elements as digit numbers. We’re essentially coding the evaluation of 3SAT variables and clauses within a derived . This will take a form similar to 111333. In the first columns, we assign 1 to both and . In the last columns, we assign 1 to the literals in . We also have buffer variables, . If the possible sum can be less than 3, we add 1s in these buffer positions such that we reach 3. Our often takes the form 111333 because we must choose either or in each of the 1s and then the 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 , can we color the graph such that no two adjacent vertices share the same color?

3-Coloring is in since we can verify that a coloring assignment is valid if contains 3 colors total and that each vertex’s neighboring vertices share a different color in time.

Let’s reduce 3SAT to 3-Coloring to show that it is NP-hard. Consider the 3SAT input of variables and clauses. We construct a graph as follows:

  1. Create a triangle of nodes from each .
  2. For each variable , construct nodes labeled and connected in a triangle with the node Base. When is three-colored, either or will get a True coloring, which determines the assignment.
  3. For each , add an OR-gadget graph with input nodes and connect the output node of this gadget to the False node and Base. This ensures that if 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 . If is True, then we can color True and False in , and for each , at least one of the nodes will be True. The OR-gadget for 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 . We can determine the truth assignments of and 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.