Logo

8. NP-Complete Problems

8.1 Search Problems

Throughout this book (a.k.a. throughout these notes), we have discussed efficient algorithms that frequently search for a desired solution in polynomial runtime among an exponential search space. However, not all such problems can be efficiently solved.

SAT

Consider the SAT or Satisfiability problem. We are provided a Boolean formula in conjunctive normal form (CNF), which is composed of the logical and (conjunction) of several clauses, each the logical or (disjunction) of several literals, where a literal is either a Boolean variable or the not of one. For instance, here's an example of a CNF formula.

(xyz)(xy)(yz)(zx)(xyz)(x\lor y\lor z)(x\lor \overline{y})(y\lor \overline{z})(z\lor \overline{x})(\overline{x}\lor \overline{y}\lor \overline{z})

The SAT problem asks us to find an assignment of the Boolean variables such that this statement is true or else report that none exists. This problem, in particular, does not have a solution. But, in general, it's difficult to efficiently find a solution: the search space is 2n2^{n}, where nn is the number of variables, and there is no known efficient algorithm for the general SAT problem.

Note, though, that the SAT problem is again a typical search problem. Provided an instance II (CNF formula), we are asked to find a solution SS or report that none exists. In particular, a search problem must also have the property that SS's validity as a solution for II can be checked in polynomial runtime. Formally,

A search problem is specified by an algorithm C\mathcal{C} that takes two inputs, an instance II and a proposed solution SS, and runs in time polynomial in I\lvert I \rvert. We say SS is a solution to II     \iff C(I,S)=true\mathcal{C}(I,S)=\text{true}

There do, however, exist two notable variants of SAT that can be efficiently solved. If each clause contains at most one positive literal (i.e. xx is positive, x\overline{x} is negative), then the Boolean formula is a Horn formula. We discussed an efficient greedy solution to this problem in Section 5.3. Meanwhile, if all clauses have at most two literals, then the problem is called a 2SAT problem, and it can be solved in linear time by identifying the SCCs of a graph constructed from the instance. (Recall the efficient algorithm we derived for this in Section 3.4).

Conversely, some problems that seem easier than the general SAT problem are still hard to solve! If all clauses have at most three literals, we have what is known as a 3SAT problem—and this problem is actually just as hard as the general SAT problem!

TSP

In the Traveling Salesman Problem, we are provided an instance with nn vertices 1,,n1,\dots,n, all pairwise distances between them, and a budget bb. The desired solution is a tour, a cycle that passes through each vertex precisely once, of total cost at most bb (or else report none).

Typically, TSP is actually discussed as an optimization problem, in which the shortest possible tour is desired. However, here we frame it as a search problem. Why? So that we can compare TSP with other search problems. Search problems are such a general description of problems that other types of problems can often be framed as search problems, like for TSP. More accurately, in this instance, the optimization version of TSP (which we will call Min-TSP henceforth) reduces to TSP. In essence, this means that solving Min-TSP is at least as hard as solving TSP. For TSP, this intuitively is because we can find a solution for Min-TSP by binary searching the budget bb for TSP. We formalize this notion later.

Also, interestingly enough, one might note that Min-TSP seems like a difficult analogue to the minimum spanning tree problem, which we have developed efficient greedy algorithms for in Section 5.1. In essence, Min-TSP just replaces the minimum spanning tree with a more difficult constraint—a tour. This simple change results in an exponentially harder problem (literally!).

Eulerian and Rudrata/Hamiltonian Paths

Now, we turn to another sibling pair of graph problems.

Several hundred years ago, Euler sought to solve a problem—given a graph GG, does there exist a path in the graph such that each edge in the graph is traversed exactly once? It turns out that this problem has an exceedingly elegant solution—there exists a Eulerian path     \iff all vertices, except for two (start, end), must have even degree.

As before, let's now define this problem as a search problem. The Eulerian path problem provides an instance with a graph GG, and the desired solution is a valid Eulerian path. We won't discuss the solution in detail—but, it follows from Euler's observation that the search problem can also be solved in polynomial time.

Before Euler solved his namesake graph problem, though, Rudrata sought to solve another, very similar problem. Given a graph GG, does there exist a path in the graph such that each vertex in the graph is traversed exactly once? Again, we frame this problem as a search problem, too. The Rudrata path (also known as Hamiltonian path) problem provides an instance with a graph GG, and the desired solution is a valid Rudrata path.

This seems very similar to Euler's problem! Despite this, there is no known efficient algorithm to solve the Rudrata path problem.

You may have heard the more familiar namesakes Eulerian cycle or Hamiltonian cycle, rather than the path variants described above. Well, as we will see later, these problems are effectively equivalent to their path variants!

Cuts and Bisections

Recall our minimum cut problem from Section 7.2. As aforementioned, a cut is a set of edges whose removal leaves a graph disconnected. We can generalize the minimum cut problem to a search problem: given an instance with a graph GG and budget bb, the desired solution is a cut with at most bb edges.

Of course, the minimum cut problem has a polynomial time algorithm. However, there exist other cut problems that, as far as we know, cannot be efficiently solved! In particular, the Balanced Cut problem is a variant of the general cut search problem: given an instance with a graph GG and budget bb, the desired solution is a partition of vertices into sets S,TS,T such that S,Tn3\lvert S \rvert,\lvert T \rvert\geq \frac{n}{3} and there are at most bb edges between SS and TT.

Balanced cuts actually have many important applications, such as clustering! However, its intractability forces such applications to use efficient approximations of solutions.

Integer LP

We discussed linear programming extensively in Section 7, and noted that, though the simplex algorithm does not always run in polynomial time, there does exist a polynomial algorithm for linear programming—at least, when the solutions are allowed to be anything in R\mathbb{R}. When we constrain solutions to be in Z\mathbb{Z}, i.e. Integer Linear Programming (ILP), the problem becomes intractable!

More formally, let us frame ILP as the following search problem: given an instance of an m×nm\times n matrix A\mathbf{A} and an mm-vector b\mathbf{b}, the desired solution is a nonnegative integer vector x\mathbf{x} satisfying Axb\mathbf{Ax}\leq \mathbf{b}.

There is additionally a special case of ILP that is just as hard, known as Zero-One Equations (ZOE). In particular, we constrain A\mathbf{A} to have entries of only either 00 or 11, x\mathbf{x} to be a vector of only 00's and 11's, and b=1\mathbf{b}=\mathbf{1} (the mm-vector of all 11's).

Three-Dimensional Matching

Recall the bipartite matching problem we discussed in Section 7.3, which can be efficiently solved with the Edmonds-Karp algorithm. There exists an intractable generalization of this known as 3D Matching: given an instance with nn boys, nn girls, and nn pets, and compatibilities among them described by a set of triples of the form (bb,gg,pp), the desired solution is a set of nn disjoint triples that is a subset of the set of compatible triples.

Independent Set, Vertex Cover, Clique

The Independent Set problem: given an instance with a graph GG and an integer kk, the desired solution is a set of kk independent vertices, i.e. no pair of vertices have an edge between them. Recall from Section 6.7 that, via dynamic programming, we could efficiently solve this problem on a tree. However, for general graphs, there exists no known polynomial-time algorithm!

The Vertex Cover problem: given an instance with a graph GG and budget bb, the desired solution is a set of bb vertices such that each edge in the graph is adjacent to at least one vertex in this set. This is actually a special case of the Set Cover problem: given an instance with a set EE, S1,,SmES_{1},\dots,S_{m}\subseteq E, and a budget bb, the desired solution is a set of bb subsets such that their union is EE. Note that we briefly discussed a greedy approximation of a solution to the minimum size Set Cover problem in Section 5.4. Regardless, these problems are both intractable.

There's an interesting parallel between the Independent Set and Vertex Cover problems! Try and see if you can figure it out :)

We'll discuss why this is the case soon... sorry for the suspense :P

3D Matching is also a special case of the Set Cover problem! Try and figure this one out too :>

EE is all nn boys, nn girls, and nn pets, the subsets S1,,SmS_{1},\dots,S_{m} are precisely the triples of (b,g,p)(b,g,p), and the budget is nn.

Finally, the Clique problem: given an instance with a graph GG and goal kk, the desired solution is a set of kk vertices such that all possible edges between them are present (i.e. these vertices form a fully connected subgraph in GG).

Longest Path

Recall from Section 4 that the shortest path problem can be solved efficiently using Dijkstra's Algorithm or Bellman-Ford's Algorithm. What about the analogue, longest path? Formally, given an instance with a graph GG whose edges are assigned nonnegative edge weights, two vertices s,ts,t, and a goal gg, the desired solution is a simple path from sts\to t with total weight at least gg.

It might seem that simply negating all the edge weights, and running a shortest path algorithm that can deal with negative weights on this graph would efficiently solve this problem. However, the issue with this is that these algorithms do not actually find the shortest simple path on a graph with a negative cycle! And, a negative cycle can certainly be created since all edges in the longest path problem are nonnegative (so negating them will make them all nonpositive). And, in fact, there is no known polynomial time algorithm to solve the longest path problem.

Knapsack and Subset Sum

We now frame knapsack as a search problem: given an instance with a weight capacity WW, a goal gg, and a set of nn items with weights wiw_{i} and vales viv_{i}, the desired solution is a set of items whose total weight is at most WW and whose total value is at least gg.

In Section 6.4, we discussed algorithms for solving the knapsack problem that run in O(nW)O(nW) time. Is this polynomial time, though? Well, not in nn, since WW can be arbitrarily large. If we try and remove WW from the time complexity, then it turns out that the best we can do is O(2n)O(2^{n}), checking all possible subsets of items. So, there isn't actually a polynomial time algorithm for knapsack!

Actually, we can derive a polynomial time for a somewhat contrived variant: Unary Knapsack, in which we represent, e.g., 33 as IIIIII. This isn't too useful, but, yes, this does have a polynomial time algorithm, due to the limitation this places on WW.

Meanwhile, an equally hard special case of knapsack is also of particular interest. The Subset Sum problem: given an instance with nn integers xix_{i} and a capacity WW, the desired solution is a subset of the provided integers that add up to precisely WW. Note that this is a special case of knapsack where vi=wiv_{i}=w_{i} for each item ii and g=Wg=W, and that the constraints vig\sum v_{i}\geq g and wiW\sum w_{i}\leq W in knapsack is equivalent to wi=W\sum w_{i}=W here.

Why is this special case of knapsack interesting, if it's equally as hard as knapsack? The point is simplicity; as we'll soon see, exploring reductions between problems is much easier with simpler problems like Subset Sum.

8.2 NP-Complete Problems

Tractability

Consider the following table of sibling hard vs. easy search problems (we'll discuss what NP-Complete and P are soon).

Hard (NP-Complete) Easy (P)
3SAT 2SAT, Horn SAT
TSP MST
Longest Path Shortest Path
3D Matching Bipartite Matching
Knapsack Unary Knapsack
Independent Set Independent Set (Trees)
ILP LP
Rudrata Path Eulerian Path
Balanced Cut Minimum Cut

On the right are easy problems, which are all easy for a variety of different reasons/algorithms (DP, greedy, etc.). On the left, though, are all hard problems, which researchers have been unable to efficiently solve over many, many years. The fascinating thing about these hard problems is that they are all difficult for the same reason! This may seem incredibly counterintuitive—they're all different problems, and vary widely. How could they possibly all be the same difficulty?! Well, as we'll soon discuss, these are actually all the same problem! That is, these problems are all equivalent; as we'll soon discuss, these problems can all be reduced to each other.

P, NP

No, P/NP does not mean Pass/No Pass here. P\mathrm{P} and NP\mathrm{NP} are what's known as complexity classes. You've probably heard of these before too; perhaps in the context of the Millennium Problems—each problem within this set of problems has a $1 million bounty, and one such problem is proving (or disproving!) that PNP\mathrm{P}\neq \mathrm{NP}. But what do these actually mean?

Formally, NP\mathrm{NP} denotes the class of all search problems. We formally defined these [[#8.1 Search Problems|above]], but, in short, a search problem must possess an efficient algorithm CC to check if a solution SS is valid for a given instance II. Efficient, here, means polynomial in I\lvert I \rvert. Note that this includes both intractable (e.g. 3SAT) and tractable (e.g. 2SAT) problems.

P\mathrm{P}, meanwhile, denotes the class of all search problems that can be solved in polynomial time. Importantly, PNP\mathrm{P}\subseteq \mathrm{NP}. (And thus, the Millennium problem essentially seeks to prove the conjecture PNP\mathrm{P}\subset \mathrm{NP}; generally speaking, we assume PNP\mathrm{P}\neq \mathrm{NP} is true. Proving the contrary would mean that all these "hard" problems are solvable in polynomial time!).

Reductions

Well, now that we've defined our complexity classes, and now that we've clarified our assumption that PNP\mathrm{P}\neq \mathrm{NP}, how do we prove that these hard problems have no efficient algorithm? This is done via reductions, transformations that show one problem is at least as hard as another. Researchers have leveraged reductions to show that all the problems on the left side of the above table are essentially the exact same problem, as aforementioned. What's more, they have shown that these problems are in fact the hardest search problems in NP\mathrm{NP}. If any of these problems has a polynomial time algorithm, then every problem in NP\mathrm{NP} would have a polynomial time algorithm.

Let's first formalize the notion of a reduction. A reduction from search problem AA to search problem BB is a pair of polynomial time algorithms (f,h)(f,h) such that

  1. ff transforms any instance II of AA into an instance f(I)f(I) of BB
  2. hh transforms any solution SS of f(I)f(I) for BB into a solution h(S)h(S) of II for AA. Or, if f(I)f(I) has no solution for BB, then it can be concluded that II has no solution for AA.

Note how this reduction from ABA\to B effectively shows that BB is at least as hard as AA, since, if there exists a polynomial time algorithm for BB, there naturally exists a polynomial time algorithm for AA derived from BB's via the reduction.

Make sure you understand that a reduction from ABA\to B means that BAB\geq A, with regards to tractability/difficulty. This is easy to accidentally mix up!

Now, we can formally define the class of the hardest search problems, too (the left side of the above table). A search problem is NP\mathrm{NP}-Complete if all other search problems reduce to it.

Composition of Reductions
One final note about reductions is that they compose, i.e. ABA\to B and BCB\to C implies ACA\to C. This should hopefully make sense intuitively.

NP-Hard?

You might have heard of the term NP\mathrm{NP}-Hard to refer to the hardest problems in computer science. So what's this NP\mathrm{NP}-Complete term then?

Well, NP\mathrm{NP}-Hard is actually yet another complexity class. NP\mathrm{NP}-Hard denotes the class of all problems at least as hard as NP\mathrm{NP}-Complete problems.

Wait—I thought the problems in NP\mathrm{NP}-Complete were the hardest problems??

Not quite. NP\mathrm{NP}-Complete denotes the class of the hardest search problems. There exist non-search problems at least as hard as those in NP\mathrm{NP}-Complete—those are the ones in NP\mathrm{NP}-Hard.

Note that, similar to how PNP\mathrm{P}\subseteq \mathrm{NP}, NP\mathrm{NP}-Complete \subseteq NP\mathrm{NP}-Hard. Also, NP\mathrm{NP}-Hard is not a subset of NP\mathrm{NP}! Remember that NP\mathrm{NP} denotes the class of all search problems. Thus, NP\mathrm{NP}-Complete denotes the intersection of NP\mathrm{NP}-Hard and NP\mathrm{NP}.

Finally, you should hopefully be able to understand the following diagram!

complexity-classes.png

Factoring

Factoring is a famously difficult search problem: given an integer nn, the desired solution is the prime factorization of nn. So, does Shor's Algorithm, i.e. a polynomial time quantum algorithm to prime factorize a number, solve all of NP\mathrm{NP}?

Not quite. In fact, factoring isn't one of the hardest problems in NP\mathrm{NP}, i.e. it isn't an NP\mathrm{NP}-Complete problem! One critical difference between factoring and the NP\mathrm{NP}-Complete problems is that, for factoring, there is no "or else" clause—there is always a prime factorization for nn. In any case, factoring is within NP\mathrm{NP}, but not NP\mathrm{NP}-Complete. So, while quantum computers are able to efficiently solve factoring, it still remains to be seen whether or not all of NP\mathrm{NP} can be solved efficiently by quantum computers! (Though, current research seems to indicate that it is very likely that this is not the case).

8.3 Reductions, Reductions, Reductions

Rudrata (s,t)(s,t)-Path \to Rudrata Cycle

Consider adding an additional vertex xx and two new edges xsx\to s and txt\to x to the graph GG to produce GG'. Then, any Rudrata cycle of the graph GG must contain the edges xsx\to s and txt\to x. Proving this reduction's validity requires showing that it works for (1)(1) when the Rudrata Cycle problem has a solution and (2)(2) when it does not. Moreover, it must be shown that (3)(3) the preprocessing and postprocessing functions take polynomial time.

1. Existing Solution
Then, the Rudrata (s,t)(s,t)-Path problem must also have a solution. Consider that removing the added edges from the Rudrata cycle of GG' produces the Rudrata path from sts\to t in GG, as desired, since the other half of the cycle, tst\to s, must traverse through xx (otherwise xx would not be visited by the cycle, and thus it would not be a valid Rudrata cycle for GG').

2. No Solution
It's typically easier to show the contrapositive, for this case. For this instance, it requires proving that, if there exists a Rudrata (s,t)(s,t)-path in GG, then there also exists a Rudrata cycle in GG'. This is trivially true; just add the edges to the Rudrata path to close the cycle.

3. Preprocessing & postprocessing
The preprocessing step just requires adding one node and two edges. The postprocessing step just requires removing two edges. Thus, these steps are clearly polynomial time.

Thus, solving the Rudrata Cycle problem is at least as hard as the Rudrata (s,t)(s,t)-Path problem. In other words, the Rudrata (s,t)(s,t)-Path problem reduces to the Rudrata Cycle. Below is a diagram that illustrates the reduction visually.

rudrata-reduction.png

It's also possible to reduce in the other way, i.e. Rudrata Cycle \to Rudrata (s,t)(s,t)-Path. In other words, this pair of reductions show that these two problems are essentially the same problem.

3SAT \to Independent Set

This reduction is most succinctly described by the following diagram.

3sat-independent-set.png

In essence, the idea is that precisely one literal is chosen to be true in every clause, in order to make that clause true. Moreover, each node, WLOG xx, is connected directly to all of its negations, i.e. all nodes x\overline{x}. This ensures that, if xx is chosen in a clause, x\overline{x} is never chosen in any other clause, since this would violate the property of an independent set. Note that multiple literals in a clause can actually be true, despite the graph ensuring only one is chosen for the independent set! This is okay, since we don't need to represent all literals that are true within a clause; only one needs to be true (in the independent set) to make that clause true. (Note that including one xx node in the independent set, i.e. marking it as true, will not immediately add all other xx nodes to the independent set; rather, it will simply preclude all x\overline{x} nodes from being part of the independent set).

SAT \to 3SAT

This is an example of an interesting yet common reduction: reducing a problem to a special case of itself. In essence, this shows that the hardness of the problem is equivalent to the hardness of a special case.

The entire reduction relies on a single trick. Consider any clause in the SAT problem with more than three literals, i.e.

(a1a2ak)(1)(a_{1}\lor a_{2}\lor\dots \lor a_{k}) \qquad(1)

where k>3k>3. Then, we can rewrite this as a set of 3SAT clauses

(a1a2y1)(y1a3y2)(y2a4y3)(yk3ak1ak)(2)(a_{1}\lor a_{2}\lor y_{1})(\overline{y_{1}}\lor a_{3}\lor y_{2})(\overline{y_{2}}\lor a_{4}\lor y_{3})\dots (\overline{y_{k-3}}\lor a_{k-1}\lor a_{k}) \qquad(2)

The conversion is clearly polynomial in kk (and thus polynomial in the SAT instance II's size, I\lvert I \rvert), and the conversion from 3SAT back to SAT is clearly O(1)O(1) (since all variables were already solved for in 3SAT).

Make sure you see why these two expressions are equivalent! More formally, (1)(1) is satisfied     \iff (2)(2) is satisfied.

We can go even further than this, though. We can additionally reduce the 3SAT problem to a variant such that no variable appears in more than three clauses. Consider any variable xx that appears in k>3k>3 clauses. Then, replace its iith appearance with xix_{i}, and add the clause

(x1x2)(x2x3)(xkx1)(\overline{x_{1}}\lor x_{2})(\overline{x_{2}}\lor x_{3})\dots (\overline{x_{k}}\lor x_{1})

Note how xix_{i} appears thrice, twice in the above expression and once in its original clause.

Again, make sure you understand why the above expression ensures x1=x2==xkx_{1}=x_{2}=\dots=x_{k}.

Independent Set \to Vertex Cover

We alluded to this reduction earlier in [[#8.1 Search Problems#Independent Set, Vertex Cover, Clique|Section 8.1]]. Consider that a set of nodes SS is a vertex cover of graph GG     \iff VSV-S is an independent set of GG.

Forward Direction:
We proceed via proof by contraposition. Let VSV-S not be an independent set of GG. Then, u,vVS\exists u,v\in V-S such that the edge (u,v)E(u,v)\in E. Then, u,v∉Su,v\not\in S, and thus the edge (u,v)(u,v) is not covered by SS. Therefore, SS is not a valid vertex cover.

Backward Direction:
Again, we can prove this by contraposition. The details are left as a (hopefully easy) exercise to the reader! >:)

Independent Set \to Clique

Define the complement of a graph G=(V,E)G=(V,E) to be G=(V,E)\overline{G}=(V,\overline{E}), where E\overline{E} consists of all edges (u,v)(u,v) not in EE. Then a set of nodes SS is an independent set of GG     \iff SS is a clique of G\overline{G}. That is, the nodes in SS have no edges between them in GG     \iff all possible edges exist between them in G\overline{G}. This should hopefully make sense intuitively.

3SAT \to 3D Matching

This is a very unintuitive reduction. But, I'll do my best to explain the transformation.

Consider the following 3D Matching diagram, where each triangle node represents a triple.

3sat-3dmatching.png

Suppose b0,b1b_{0},b_{1} and g0,g1g_{0},g_{1} are not involved in any other triples. Some of the pets p0,,p3p_{0},\dots,p_{3} must belong to other triples, of course, since at most two triples can be chosen here (and thus at most two pets can be "used up" here). In fact, precisely two pets must be "used up" here, since, in order for a perfect matching, b0,b1,g0,g1b_{0},b_{1},g_{0},g_{1} must all be "used up" in this diagram, since they are not involved with any other triples.

Consider choosing the triple (b0,g0,p1)(b_{0},g_{0},p_{1}). Then, this forces the other triple in this diagram to be (b1,g1,p3)(b_{1},g_{1},p_{3}). Similarly, choosing the triple (b0,g1,p0)(b_{0},g_{1},p_{0}) forces the other triple to be (b1,g0,p2)(b_{1},g_{0},p_{2}). Therefore, any matching involving this gadget must include either both (b0,g0,p1)(b_{0},g_{0},p_{1}) and (b1,g1,p3)(b_{1},g_{1},p_{3}) or both (b0,g1,p0)(b_{0},g_{1},p_{0}) and (b1,g0,p2)(b_{1},g_{0},p_{2}). In other words, this gadget behaves like a Boolean variable!

So, to transform an instance of 3SAT into an instance of 3D Matching, we first create a gadget for each variable. Let the nodes for a variable xx be denoted bx0,bx1,gx0,gx1,px0,px1,px2,px3b_{x0},b_{x1},g_{x0},g_{x1},p_{x0},p_{x1},p_{x2},p_{x3}. We will let x=truex=\text{true} be denoted by the case where bx0b_{x0} is matched with girl gx1g_{x1}, and x=falsex=\text{false} be denoted by the case where bx0b_{x0} is matched with gx0g_{x0}.

Now, consider a clause, e.g. c=(xyz)c=(x\lor \overline{y}\lor z). For each clause, we introduce a new boy bcb_{c} and new girl gcg_{c}. We will associate each with three triples, one for each literal in the clause. In essence, we want this boy and girl pair to be matched with a single pet in one of the literals' gadgets, such that this match implies that this literal was chosen to be true.

For this clause, we can have (1) x=true, (2) y=false, (3) z=true(1)\ x=\text{true},\ (2)\ y=\text{false},\ (3)\ z=\text{true}. For (1)(1), we form the triple (bc,gc,px1)(b_{c},g_{c},p_{x1}). This is because, if xx is chosen to be true, then px0,px2p_{x0},p_{x2} would be taken in the gadget. Then, the triple (bc,gc,px1)(b_{c},g_{c},p_{x1}) can be included in the 3D Matching, effectively marking that x=truex=\text{true} makes clause cc true. In contrast, if xx is chosen to be false, then px1p_{x1} would be taken in the gadget     \implies xx is not used to make the clause true. We extend this to the other two literals, i.e. yy has the triple (bc,gc,py0)(b_{c},g_{c},p_{y0}) and zz has the triple (bc,gc,pz1)(b_{c},g_{c},p_{z1}).

Additionally, we have to ensure that, for every occurrence of a literal in clause cc, there is a different pet to match with bcb_{c} and gcg_{c}. For instance, if a literal is used in 5 different clauses, we won't have enough pets to match with bc,gcb_{c},g_{c} for each clause cc! However, recall that we actually showed a reduction from 3SAT to a special case in which each literal appears in at most two clauses. Therefore, if we first reduce 3SAT to a special case, we can ensure each literal appears at most twice, in which case we actually do have enough pets—if a variable x=truex=\text{true}, we have two pets p1p_{1} and p3p_{3} that can each be matched in clauses, and if x=falsex=\text{false}, we have p0p_{0} and p2p_{2}.

Note that a pet is only assigned if the corresponding literal is true in that clause. If the literal is x\overline{x}, a pet is assigned to this literal if x=falsex=\text{false}. Conversely, if the literal is xx, a pet is assigned if x=truex=\text{true}.
But I thought the reduction showed that each literal appeared in at most three, not two, clauses?

I was confused about this too. Actually, the earlier reduction showed that each variable appeared in at most three clauses. Notably, though, in the expression derived to ensure x1=x2==xkx_{1}=x_{2}=\dots=x_{k}, each variable was used once as xix_{i} and once as xi\overline{x_{i}}. Meanwhile, the original clause contains one of xix_{i} or xi\overline{x_{i}}. Therefore, each literal, i.e. xix_{i} and xi\overline{x_{i}} are the literals, appears at most twice. Confusing, I know; I wish the book explained this.

We're almost done now. The last thing we need to ensure is that no pet is left unmatched. For instance, consider a variable that is used only in one clause—it'll be left with an unmatched pet, which isn't allowed in 3D Matching! More precisely, with nn variables and mm clauses in 3D SAT, precisely 2nm2n-m pets will be left unmatched. Thus, it suffices to simply add 2nm2n-m boy-girl couples that can match with every pet, and have them take up the remaining pets!

Why are exactly 2nm2n-m pets left unmatched?

Each variable's gadget will have two pets that are not matched within the gadget itself. Each clause will take precisely one pet. Thus, 2nm2n-m pets left unmatched. Note that 2nm>02n-m>0 always since each variable will appear at most thrice, and there are at least two literals in every clause.

3D Matching \to ZOE

For each triple, create a variable xix_{i}, where xi=1x_{i}=1 means the triple was chosen. Our vector x=x1,,xn\mathbf{x}=\langle x_{1},\dots,x_{n} \rangle. Then, for each boy/girl/pet, let the triples containing it are xj1,xj2,,xjkx_{j_{1}},x_{j_{2}},\dots,x_{j_{k}}. Then, we can set a constraint

i=1kxji=1\sum_{i=1}^{k} x_{j_{i}}=1

since each boy/girl/pet can be included in at most one triple. Then, the rest is just solving the corresponding ZOE problem,

Ax=1\mathbf{Ax}=\mathbf{1}

where each column of A\mathbf{A} corresponds to a triple, and each row corresponds to a boy/girl/pet.

ZOE \to Subset Sum

This is a reduction between two special cases of ILP, and is literally just based on the fact that 010-1 vectors can essentially act as binary representations of a number!

For instance, consider the ZOE problem with matrix

A=[10000001011010000100]\mathbf{A}=\begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \end{bmatrix}

Think of the columns as binary; then, effectively,

A=[18548]\mathbf{A}=\begin{bmatrix} 18 & 5 & 4 & 8 \end{bmatrix}

. Then,

Ax=1\mathbf{Ax}=\mathbf{1}

reduces to the subset sum problem where we are searching for a subset of {18,5,4,8}\{ 18,5,4,8 \} that sums to 111112=3111111_{2}=31.

Well, not quite. There's still one more consideration—carries from addition can result in a valid solution for Subset Sum but an invalid solution for ZOE. To fix this, we can slightly modify the transformation: instead of thinking of the column vectors as integers in base 22, we consider them as integers in base n+1n+1. Therefore, this ensures that carries never occur, and thus a solution to Subset Sum is always a solution to ZOE.

ZOE \to ILP

As aforementioned, ZOE is a special case of ILP. Then, this effectively means that ZOE reduces to ILP, since being able to solve ILP in polynomial time trivially equates to being able to solve ZOE in polynomial time, since there isn't even a transformation required.

This is a very useful way of establishing that a problem is NP\mathrm{NP}-Complete, i.e. by noting it is a generalization of a known NP\mathrm{NP}-Complete problem.

ZOE \to Rudrata Cycle

This transformation is complex and fairly long, so I will attempt to explain it concisely as best as I can.

First, we will try to reduce ZOE to a generalization of Rudrata Cycle: Rudrata Cycle with Paired Edges, and then we will reduce this generalization to Rudrata Cycle. (Recall that this is valid since reductions are transitive).

ZOE \to Rudrata Cycle with Paired Edges

In Rudrata Cycle with Paired Edges, a graph G=(V,E)G=(V,E) and a set CE×EC\subseteq E\times E comprise the provided instance. The desired solution is a cycle that (1)(1) visits all vertices once and (2)(2) for every pair of edges (e,e)C(e,e')\in C, traverses precisely one of the two.

Now, consider the structure of a ZOE problem. We have nn variables x1,,xnx_{1},\dots,x_{n} that can either be 00 or 11, and mm equations xj1++xjk=1x_{j_{1}}+\dots+x_{j_{k}}=1, where ji{1,,n}j_{i}\in \{ 1,\dots,n \}. For variable xix_{i}, we create a component consisting of two nodes and two edges ei0e_{i0} and ei1e_{i1} between these two nodes, where the Rudrata cycle traversing ei0e_{i0} corresponds to xi=0x_{i}=0, and it traversing ei1e_{i1} corresponds to xi=1x_{i}=1. For equation ii, we create a component consisting of two nodes and mm edges ei1,,eime_{i1}',\dots,e_{im}', where e.g. the Rudrata cycle traversing ei1e'_{i1} corresponds to the variable xj1=1x_{j_{1}}=1 in equation ii. Then, we compose these components as shown in the diagram below.

zoe-rudrata-cycle-paired.png

Then, for every equation, for every variable xix_{i} appearing in it, we add to CC the pair (e,e)(e,e') where ee corresponds to the edge representing xix_{i} in the equation and ee' corresponds to the edge represent xi=0x_{i}=0. This ensures that if xi=0x_{i}=0, it's never used as the value 11 in any equation. Therefore, ZOE reduces to the Rudrata Cycle with Paired Edges problem.

Rudrata Cycle with Paired Edges \to Rudrata Cycle

This transformation can be summed up in a single diagram.

paired-edges-rudrata-cycle.png

In essence, by replacing every pair of edges (e,e)(e,e') with this gadget, we ensure that only one of {e,e}\{ e,e' \} is traversed. Thus, the Rudrata Cycle with Paired Edges problem reduces to the Rudrata Cycle problem, and by the transitivity of reductions, ZOE reduces to the Rudrata Cycle Problem.

What if some edge ee is involved in multiple pairs?

Then we can simply concatenate all of its gadgets together. Make sure you see why this works!

Rudrata Cycle \to TSP

Given a graph G=(V,E)G=(V,E), construct a TSP where the set of cities is precisely the same as VV, the distance between cities uu and vv is 11 if (u,v)E(u,v)\in E and otherwise is 1+α1+\alpha, for some α>1\alpha>1. The budget bb is V\lvert V \rvert.

If GG has a Rudrata cycle, then the same cycle is also a tour within the budget of the TSP instance, clearly. Conversely, if GG has no Rudrata cycle, then there is no solution, as the cheapest possible TSP tour has a cost n+α\geq n+\alpha (since it traverses at least one edge that doesn't exist in GG, and thus has length 1+α1+\alpha).

Why the α\alpha parameter? Can't we just set α=1\alpha=1?

Varying α\alpha can lead to two interesting results. If α=1\alpha=1, then this TSP instance actually satisfies the triangle inequality, which produces a special case of TSP that, as we will discuss in Section 9, can be efficiently approximated.

Conversely, if α\alpha is sufficiently large, then it has the property that it either (1)(1) has a solution of cost n\leq n or (2)(2) has a solution of cost n+α\geq n+\alpha. Critically, there are 00 solutions with cost cc such that n<c<n+αn<c<n+\alpha. This is known as the gap property, and implies that unless P=NP\mathrm{P}=\mathrm{NP}, there exists no efficient approximation algorithm.

Any Problem in NP\mathrm{NP} \to SAT

ts too hard... TL;DR is Any Problem in NP\mathrm{NP} \to Circuit SAT \to SAT.

Summary

Now, you should hopefully understand the tree of reductions shown in the below diagram :)

reductions-graph.png

Aside: Unsolvable Problems

For all NP\mathrm{NP}-Complete problems, there at least exists some algorithm to solve each, even if intractably so. For such problems, there is no existing algorithm at all! One such problem is the arithmetical version of SAT, where the provided instance is a polynomial equation in many variables and the desired solution is a satisfying assignment of the variables. Another very famous unsolvable problem is the halting problem.