




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
NP Complete Satisfiability TSP travelling salesman Problem Euler and rudrata Integer Linear programming Reductions Factoring Subset sum
Typology: Study notes
1 / 36
This page cannot be seen from the preview
Don't miss anything!





























Over the past seven chapters we have developed algorithms for finding shortest paths and minimum spanning trees in graphs, matchings in bipartite graphs, maximum increasing sub- sequences, maximum flows in networks, and so on. All these algorithms are efficient , because in each case their time requirement grows as a polynomial function (such as n, n^2 , or n^3 ) of the size of the input.
To better appreciate such efficient algorithms, consider the alternative: In all these prob- lems we are searching for a solution (path, tree, matching, etc.) from among an exponential population of possibilities. Indeed, n boys can be matched with n girls in n! different ways, a graph with n vertices has nn−^2 spanning trees, and a typical graph has an exponential num- ber of paths from s to t. All these problems could in principle be solved in exponential time by checking through all candidate solutions, one by one. But an algorithm whose running time is 2 n, or worse, is all but useless in practice (see the next box). The quest for efficient algorithms is about finding clever ways to bypass this process of exhaustive search, using clues from the input in order to dramatically narrow down the search space.
So far in this book we have seen the most brilliant successes of this quest, algorithmic tech- niques that defeat the specter of exponentiality: greedy algorithms, dynamic programming, linear programming (while divide-and-conquer typically yields faster algorithms for problems we can already solve in polynomial time). Now the time has come to meet the quest’s most embarrassing and persistent failures. We shall see some other “search problems,” in which again we are seeking a solution with particular properties among an exponential chaos of al- ternatives. But for these new problems no shortcut seems possible. The fastest algorithms we know for them are all exponential—not substantially better than an exhaustive search. We now introduce some important examples.
247
248 Algorithms
According to the legend, the game of chess was invented by the Brahmin Sissa to amuse and teach his king. Asked by the grateful monarch what he wanted in return, the wise man requested that the king place one grain of rice in the first square of the chessboard, two in the second, four in the third, and so on, doubling the amount of rice up to the 64th square. The king agreed on the spot, and as a result he was the first person to learn the valuable—-albeit humbling—lesson of exponential growth. Sissa’s request amounted to 264 − 1 = 18, 446 , 744 , 073 , 709 , 551 , 615 grains of rice, enough rice to pave all of India several times over! All over nature, from colonies of bacteria to cells in a fetus, we see systems that grow exponentially—for a while. In 1798, the British philosopher T. Robert Malthus published an essay in which he predicted that the exponential growth (he called it “geometric growth”) of the human population would soon deplete linearly growing resources, an argument that influenced Charles Darwin deeply. Malthus knew the fundamental fact that an exponential sooner or later takes over any polynomial. In 1965, computer chip pioneer Gordon E. Moore noticed that transistor density in chips had doubled every year in the early 1960s, and he predicted that this trend would continue. This prediction, moderated to a doubling every 18 months and extended to computer speed, is known as Moore’s law. It has held remarkably well for 40 years. And these are the two root causes of the explosion of information technology in the past decades: Moore’s law and efficient algorithms. It would appear that Moore’s law provides a disincentive for developing polynomial al- gorithms. After all, if an algorithm is exponential, why not wait it out until Moore’s law makes it feasible? But in reality the exact opposite happens: Moore’s law is a huge incen- tive for developing efficient algorithms, because such algorithms are needed in order to take advantage of the exponential increase in computer speed. Here is why. If, for example, an O(2n) algorithm for Boolean satisfiability (SAT) were given an hour to run, it would have solved instances with 25 variables back in 1975, 31 vari- ables on the faster computers available in 1985, 38 variables in 1995, and about 45 variables with today’s machines. Quite a bit of progress—except that each extra variable requires a year and a half’s wait, while the appetite of applications (many of which are, ironically, re- lated to computer design) grows much faster. In contrast, the size of the instances solved by an O(n) or O(n log n) algorithm would be multiplied by a factor of about 100 each decade. In the case of an O(n^2 ) algorithm, the instance size solvable in a fixed time would be mul- tiplied by about 10 each decade. Even an O(n^6 ) algorithm, polynomial yet unappetizing, would more than double the size of the instances solved each decade. When it comes to the growth of the size of problems we can attack with an algorithm, we have a reversal: expo- nential algorithms make polynomially slow progress, while polynomial algorithms advance exponentially fast! For Moore’s law to be reflected in the world we need efficient algorithms. As Sissa and Malthus knew very well, exponential expansion cannot be sustained in- definitely in our finite world. Bacterial colonies run out of food; chips hit the atomic scale. Moore’s law will stop doubling the speed of our computers within a decade or two. And then progress will depend on algorithmic ingenuity—or otherwise perhaps on novel ideas such as quantum computation , explored in Chapter 10.
250 Algorithms
Figure 8.1 The optimal traveling salesman tour, shown in bold, has length 18.
4
5
6
3
3 3
2
4
1
2 3
a Horn formula , and a satisfying truth assignment, if one exists, can be found by the greedy algorithm of Section 5.3. Alternatively, if all clauses have only two literals, then graph the- ory comes into play, and SAT can be solved in linear time by finding the strongly connected components of a particular graph constructed from the instance (recall Exercise 3.28). In fact, in Chapter 9, we’ll see a different polynomial algorithm for this same special case, which is called 2SAT. On the other hand, if we are just a little more permissive and allow clauses to contain three literals, then the resulting problem, known as 3SAT (an example of which we saw earlier), once again becomes hard to solve!
In the traveling salesman problem (TSP) we are given n vertices 1 ,... , n and all n(n − 1)/ 2 distances between them, as well as a budget b. We are asked to find a tour , a cycle that passes through every vertex exactly once, of total cost b or less—or to report that no such tour exists. That is, we seek a permutation τ (1),... , τ (n) of the vertices such that when they are toured in this order, the total distance covered is at most b:
dτ (1),τ (2) + dτ (2),τ (3) + · · · + dτ (n),τ (1) ≤ b.
See Figure 8.1 for an example (only some of the distances are shown; assume the rest are very large). Notice how we have defined the TSP as a search problem : given an instance, find a tour within the budget (or report that none exists). But why are we expressing the traveling salesman problem in this way, when in reality it is an optimization problem , in which the shortest possible tour is sought? Why dress it up as something else? For a good reason. Our plan in this chapter is to compare and relate problems. The framework of search problems is helpful in this regard, because it encompasses optimization problems like the TSP in addition to true search problems like SAT. Turning an optimization problem into a search problem does not change its difficulty at all, because the two versions reduce to one another. Any algorithm that solves the optimization
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 251
TSP also readily solves the search problem: find the optimum tour and if it is within budget, return it; if not, there is no solution.
Conversely, an algorithm for the search problem can also be used to solve the optimization problem. To see why, first suppose that we somehow knew the cost of the optimum tour; then we could find this tour by calling the algorithm for the search problem, using the optimum cost as the budget. Fine, but how do we find the optimum cost? Easy: By binary search! (See Exercise 8.1.)
Incidentally, there is a subtlety here: Why do we have to introduce a budget? Isn’t any optimization problem also a search problem in the sense that we are searching for a solution that has the property of being optimal? The catch is that the solution to a search problem should be easy to recognize, or as we put it earlier, polynomial-time checkable. Given a po- tential solution to the TSP, it is easy to check the properties “is a tour” (just check that each vertex is visited exactly once) and “has total length ≤ b.” But how could one check the property “is optimal”?
As with SAT, there are no known polynomial-time algorithms for the TSP, despite much effort by researchers over nearly a century. Of course, there is an exponential algorithm for solving it, by trying all (n − 1)! tours, and in Section 6.6 we saw a faster, yet still exponential, dynamic programming algorithm.
The minimum spanning tree (MST) problem, for which we do have efficient algorithms, provides a stark contrast here. To phrase it as a search problem, we are again given a distance matrix and a bound b, and are asked to find a tree T with total weight
(i,j)∈T dij^ ≤^ b. The TSP can be thought of as a tough cousin of the MST problem, in which the tree is not allowed to branch and is therefore a path.^1 This extra restriction on the structure of the tree results in a much harder problem.
In the summer of 1735 Leonhard Euler (pronounced “Oiler”), the famous Swiss mathemati- cian, was walking the bridges of the East Prussian town of K ¨onigsberg. After a while, he noticed in frustration that, no matter where he started his walk, no matter how cleverly he continued, it was impossible to cross each bridge exactly once. And from this silly ambition, the field of graph theory was born.
Euler identified at once the roots of the park’s deficiency. First, you turn the map of the park into a graph whose vertices are the four land masses (two islands, two banks) and whose edges are the seven bridges:
(^1) Actually the TSP demands a cycle, but one can define an alternative version that seeks a path, and it is not hard to see that this is just as hard as the TSP itself.
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 253
Figure 8.2 Knight’s moves on a corner of a chessboard.
also the issue that one of them demands a path while the other requires a cycle. Which of these differences accounts for the huge disparity in computational complexity between the two problems? It must be the first, because the second difference can be shown to be purely cosmetic. Indeed, define the RUDRATA PATH problem to be just like RUDRATA CYCLE, except that the goal is now to find a path that goes through each vertex exactly once. As we will soon see, there is a precise equivalence between the two versions of the Rudrata problem.
A cut is a set of edges whose removal leaves a graph disconnected. It is often of interest to find small cuts, and the MINIMUM CUT problem is, given a graph and a budget b, to find a cut with at most b edges. For example, the smallest cut in Figure 8.3 is of size 3. This problem can be solved in polynomial time by n − 1 max-flow computations: give each edge a capacity of 1 , and find the maximum flow between some fixed node and every single other node. The smallest such flow will correspond (via the max-flow min-cut theorem) to the smallest cut. Can you see why? We’ve also seen a very different, randomized algorithm for this problem (page 150). In many graphs, such as the one in Figure 8.3, the smallest cut leaves just a singleton vertex on one side—it consists of all edges adjacent to this vertex. Far more interesting are small cuts that partition the vertices of the graph into nearly equal-sized sets. More precisely, the BALANCED CUT problem is this: given a graph with n vertices and a budget b, partition the vertices into two sets S and T such that |S|, |T | ≥ n/ 3 and such that there are at most b edges between S and T. Another hard problem. Balanced cuts arise in a variety of important applications, such as clustering. Consider for example the problem of segmenting an image into its constituent components (say, an elephant standing in a grassy plain with a clear blue sky above). A good way of doing this is to create a graph with a node for each pixel of the image and to put an edge between nodes whose corresponding pixels are spatially close together and are also similar in color. A single
254 Algorithms
Figure 8.3 What is the smallest cut in this graph?
object in the image (like the elephant, say) then corresponds to a set of highly connected vertices in the graph. A balanced cut is therefore likely to divide the pixels into two clusters without breaking apart any of the primary constituents of the image. The first cut might, for instance, separate the elephant on the one hand from the sky and from grass on the other. A further cut would then be needed to separate the sky from the grass.
Even though the simplex algorithm is not polynomial time, we mentioned in Chapter 7 that there is a different, polynomial algorithm for linear programming. Therefore, linear pro- gramming is efficiently solvable both in practice and in theory. But the situation changes completely if, in addition to specifying a linear objective function and linear inequalities, we also constrain the solution (the values for the variables) to be integer. This latter problem is called INTEGER LINEAR PROGRAMMING (ILP). Let’s see how we might formulate it as a search problem. We are given a set of linear inequalities Ax ≤ b, where A is an m × n matrix and b is an m-vector; an objective function specified by an n-vector c; and finally, a goal g (the counterpart of a budget in maximization problems). We want to find a nonnegative integer n-vector x such that Ax ≤ b and c · x ≥ g. But there is a redundancy here: the last constraint c · x ≥ g is itself a linear inequality and can be absorbed into Ax ≤ b. So, we define ILP to be following search problem: given A and b, find a nonnegative integer vector x satisfying the inequalities Ax ≤ b, or report that none exists. Despite the many crucial applications of this problem, and intense interest by researchers, no efficient algorithm is known for it. There is a particularly clean special case of ILP that is very hard in and of itself: the goal is to find a vector x of 0 ’s and 1 ’s satisfying Ax = 1 , where A is an m × n matrix with 0 − 1 entries and 1 is the m-vector of all 1 ’s. It should be apparent from the reductions in Section 7.1.4 that this is indeed a special case of ILP. We call it ZERO-ONE EQUATIONS (ZOE).
We have now introduced a number of important search problems, some of which are fa- miliar from earlier chapters and for which there are efficient algorithms, and others which are different in small but crucial ways that make them very hard computational problems. To
256 Algorithms
Figure 8.5 What is the size of the largest independent set in this graph?
intimate connection to the INDEPENDENT SET problem?) VERTEX COVER is a special case of SET COVER, which we encountered in Chapter 5. In that problem, we are given a set E and several subsets of it, S 1 ,... , Sm, along with a budget b. We are asked to select b of these subsets so that their union is E. VERTEX COVER is the special case in which E consists of the edges of a graph, and there is a subset Si for each vertex, containing the edges adjacent to that vertex. Can you see why 3D MATCHING is also a special case of SET COVER? And finally there is the CLIQUE problem: given a graph and a goal g, find a set of g ver- tices such that all possible edges between them are present. What is the largest clique in Figure 8.5?
We know the shortest-path problem can be solved very efficiently, but how about the LONGEST PATH problem? Here we are given a graph G with nonnegative edge weights and two distin- guished vertices s and t, along with a goal g. We are asked to find a path from s to t with total weight at least g. Naturally, to avoid trivial solutions we require that the path be simple , containing no repeated vertices. No efficient algorithm is known for this problem (which sometimes also goes by the name of TAXICAB RIP-OFF).
Recall the KNAPSACK problem (Section 6.4): we are given integer weights w 1 ,... , wn and integer values v 1 ,... , vn for n items. We are also given a weight capacity W and a goal g (the former is present in the original optimization problem, the latter is added to make it a search problem). We seek a set of items whose total weight is at most W and whose total value is at least g. As always, if no such set exists, we should say so. In Section 6.4, we developed a dynamic programming scheme for KNAPSACK with running
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 257
time O(nW ), which we noted is exponential in the input size, since it involves W rather than log W. And we have the usual exhaustive algorithm as well, which looks at all subsets of items—all 2 n^ of them. Is there a polynomial algorithm for KNAPSACK? Nobody knows of one. But suppose that we are interested in the variant of the knapsack problem in which the integers are coded in unary —for instance, by writing IIIIIIIIIIII for 12. This is admittedly an exponentially wasteful way to represent integers, but it does define a legitimate problem, which we could call UNARY KNAPSACK. It follows from our discussion that this somewhat artificial problem does have a polynomial algorithm. A different variation: suppose now that each item’s value is equal to its weight (all given in binary), and to top it off, the goal g is the same as the capacity W. (To adapt the silly break-in story whereby we first introduced the knapsack problem, the items are all gold nuggets, and the burglar wants to fill his knapsack to the hilt.) This special case is tantamount to finding a subset of a given set of integers that adds up to exactly W. Since it is a special case of KNAPSACK, it cannot be any harder. But could it be polynomial? As it turns out, this problem, called SUBSET SUM, is also very hard. At this point one could ask: If SUBSET SUM is a special case that happens to be as hard as the general KNAPSACK problem, why are we interested in it? The reason is simplicity. In the complicated calculus of reductions between search problems that we shall develop in this chapter, conceptually simple problems like SUBSET SUM and 3SAT are invaluable.
In short, the world is full of search problems, some of which can be solved efficiently, while others seem to be very hard. This is depicted in the following table.
Hard problems ( NP -complete) Easy problems (in P ) 3 SAT 2 SAT, HORN SAT TRAVELING SALESMAN PROBLEM MINIMUM SPANNING TREE LONGEST PATH SHORTEST PATH 3D MATCHING BIPARTITE MATCHING KNAPSACK UNARY KNAPSACK INDEPENDENT SET INDEPENDENT SET on trees INTEGER LINEAR PROGRAMMING LINEAR PROGRAMMING RUDRATA PATH EULER PATH BALANCED CUT MINIMUM CUT
This table is worth contemplating. On the right we have problems that can be solved efficiently. On the left, we have a bunch of hard nuts that have escaped efficient solution over many decades or centuries.
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 259
that P 6 = NP —the task of finding a proof for a given mathematical assertion is a search problem and is therefore in NP (after all, when a formal proof of a mathematical statement is written out in excruciating detail, it can be checked mechanically, line by line, by an efficient algorithm). So if P = NP , there would be an efficient method to prove any theorem, thus eliminating the need for mathematicians! All in all, there are a variety of reasons why it is widely believed that P 6 = NP. However, proving this has turned out to be extremely difficult, one of the deepest and most important unsolved puzzles of mathematics.
Even if we accept that P 6 = NP , what about the specific problems on the left side of the table? On the basis of what evidence do we believe that these particular problems have no efficient algorithm (besides, of course, the historical fact that many clever mathematicians and computer scientists have tried hard and failed to find any)? Such evidence is provided by reductions , which translate one search problem into another. What they demonstrate is that the problems on the left side of the table are all, in some sense, exactly the same problem , except that they are stated in different languages. What’s more, we will also use reductions to show that these problems are the hardest search problems in NP —if even one of them has a polynomial time algorithm, then every problem in NP has a polynomial time algorithm. Thus if we believe that P 6 = NP , then all these search problems are hard. We defined reductions in Chapter 7 and saw many examples of them. Let’s now specialize this definition to search problems. A reduction from search problem A to search problem B is a polynomial-time algorithm f that transforms any instance I of A into an instance f (I) of B, together with another polynomial-time algorithm h that maps any solution S of f (I) back into a solution h(S) of I; see the following diagram. If f (I) has no solution, then neither does I. These two translation procedures f and h imply that any algorithm for B can be converted into an algorithm for A by bracketing it between f and h.
I
Instance (^) Instance f (I) f
Algorithm for A
for B
Algorithm
Solution S of f (I)
No solution to f (I) (^) No solution to I
h(S) of I h^ Solution
And now we can finally define the class of the hardest search problems.
A search problem is NP -complete if all other search problems reduce to it.
This is a very strong requirement indeed. For a problem to be NP -complete, it must be useful in solving every search problem in the world! It is remarkable that such problems exist. But they do, and the first column of the table we saw earlier is filled with the most famous examples. In Section 8.3 we shall see how all these problems reduce to one another, and also why all other search problems reduce to them.
260 Algorithms
Figure 8.6 The space NP of all search problems, assuming P 6 = NP.
Increasing difficulty
So far in this book the purpose of a reduction from a problem A to a problem B has been straightforward and honorable: We know how to solve B efficiently, and we want to use this knowledge to solve A. In this chapter, however, reductions from A to B serve a somewhat perverse goal: we know A is hard, and we use the reduction to prove that B is hard as well!
If we denote a reduction from A to B by
A −→ B
then we can say that difficulty flows in the direction of the arrow, while efficient algorithms move in the opposite direction. It is through this propagation of difficulty that we know NP -complete problems are hard: all other search problems reduce to them, and thus each NP -complete problem contains the complexity of all search problems. If even one NP -complete problem is in P , then P = NP.
Reductions also have the convenient property that they compose.
If A −→ B and B −→ C, then A −→ C.
To see this, observe first of all that any reduction is completely specified by the pre- and postprocessing functions f and h (see the reduction diagram). If (fAB , hAB ) and (fBC , hBC ) define the reductions from A to B and from B to C, respectively, then a reduction from A to C is given by compositions of these functions: fBC ◦ fAB maps an instance of A to an instance of C and hAB ◦ hBC sends a solution of C back to a solution of A. This means that once we know a problem A is NP -complete, we can use it to prove that a new search problem B is also NP -complete, simply by reducing A to B. Such a reduction establishes that all problems in NP reduce to B, via A.
262 Algorithms
Figure 8.7 Reductions between search problems.
3D MATCHING
SUBSET SUM RUDRATA CYCLE
TSP
ILP
ZOE
All of NP
SAT
3SAT
VERTEX COVER
INDEPENDENT SET
CLIQUE
We shall now see that the search problems of Section 8.1 can be reduced to one another as depicted in Figure 8.7. As a consequence, they are all NP -complete. Before we tackle the specific reductions in the tree, let’s warm up by relating two versions of the Rudrata problem.
Recall the RUDRATA CYCLE problem: given a graph, is there a cycle that passes through each vertex exactly once? We can also formulate the closely related RUDRATA (s, t)-PATH problem, in which two vertices s and t are specified, and we want a path starting at s and ending at t that goes through each vertex exactly once. Is it possible that RUDRATA CYCLE is easier than RUDRATA (s, t)-PATH? We will show by a reduction that the answer is no. The reduction maps an instance (G = (V, E), s, t) of RUDRATA (s, t)-PATH into an instance G′^ = (V ′, E′) of RUDRATA CYCLE as follows: G′^ is simply G with an additional vertex x and two new edges {s, x} and {x, t}. For instance:
G G′
s
t t
s
x
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 263
So V ′^ = V ∪ {x}, and E′^ = E ∪ {{s, x}, {x, t}}. How do we recover a Rudrata (s, t)- path in G given any Rudrata cycle in G′? Easy, we just delete the edges {s, x} and {x, t} from the cycle.
Instance:
nodes s, t
G = (V, E) {s, x}, {x, t}
G′^ = (V ′, E′) RUDRATA CYCLE and edges {s, x},^ {x, t} No solution
Solution: Add node x (^) path Solution: cycle
No solution
Delete edges
RUDRATA (s, t)-PATH
To confirm the validity of this reduction, we have to show that it works in the case of either outcome depicted.
Since the new vertex x has only two neighbors, s and t, any Rudrata cycle in G′^ must consec- utively traverse the edges {t, x} and {x, s}. The rest of the cycle then traverses every other vertex en route from s to t. Thus deleting the two edges {t, x} and {x, s} from the Rudrata cycle gives a Rudrata path from s to t in the original graph G.
In this case we must show that the original instance of RUDRATA (s, t)-PATH cannot have a solution either. It is usually easier to prove the contrapositive, that is, to show that if there is a Rudrata (s, t)-path in G, then there is also a Rudrata cycle in G′. But this is easy: just add the two edges {t, x} and {x, s} to the Rudrata path to close the cycle. One last detail, crucial but typically easy to check, is that the pre- and postprocessing functions take time polynomial in the size of the instance (G, s, t).
It is also possible to go in the other direction and reduce RUDRATA CYCLE to RUDRATA (s, t)-PATH. Together, these reductions demonstrate that the two Rudrata variants are in essence the same problem—which is not too surprising, given that their descriptions are al- most the same. But most of the other reductions we will see are between pairs of problems that, on the face of it, look quite different. To show that they are essentially the same, our reductions will have to cleverly translate between them.
One can hardly think of two more different problems. In 3SAT the input is a set of clauses, each with three or fewer literals, for example
(x ∨ y ∨ z) (x ∨ y ∨ z) (x ∨ y ∨ z) (x ∨ y),
and the aim is to find a satisfying truth assignment. In INDEPENDENT SET the input is a graph and a number g, and the problem is to find a set of g pairwise non-adjacent vertices. We must somehow relate Boolean logic with graphs!
S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 265
For any variable x, the set S cannot contain vertices labeled both x and x, because any such pair of vertices is connected by an edge. So assign x a value of true if S contains a vertex labeled x, and a value of false if S contains a vertex labeled x (if S contains neither, then assign either value to x). Since S has g vertices, it must have one vertex per clause; this truth assignment satisfies those particular literals, and thus satisfies all clauses.
It is usually cleaner to prove the contrapositive, that if I has a satisfying assignment then G has an independent set of size g. This is easy: for each clause, pick any literal whose value under the satisfying assignment is true (there must be at least one such literal), and add the corresponding vertex to S. Do you see why set S must be independent?
This is an interesting and common kind of reduction, from a problem to a special case of itself. We want to show that the problem remains hard even if its inputs are restricted somehow—in the present case, even if all clauses are restricted to have ≤ 3 literals. Such reductions modify the given instance so as to get rid of the forbidden feature (clauses with ≥ 4 literals) while keeping the instance essentially the same, in that we can read off a solution to the original instance from any solution of the modified one. Here’s the trick for reducing SAT to 3SAT: given an instance I of SAT, use exactly the same instance for 3SAT, except that any clause with more than three literals, (a 1 ∨ a 2 ∨ · · · ∨ ak) (where the ai’s are literals and k > 3 ), is replaced by a set of clauses,
(a 1 ∨ a 2 ∨ y 1 ) (y 1 ∨ a 3 ∨ y 2 ) (y 2 ∨ a 4 ∨ y 3 ) · · · (yk− 3 ∨ ak− 1 ∨ ak),
where the yi’s are new variables. Call the resulting 3SAT instance I ′. The conversion from I to I′^ is clearly polynomial time. Why does this reduction work? I ′^ is equivalent to I in terms of satisfiability, because for any assignment to the ai’s,
{ (a 1 ∨ a 2 ∨ · · · ∨ ak) is satisfied
there is a setting of the yi’s for which (a 1 ∨ a 2 ∨ y 1 ) (y 1 ∨ a 3 ∨ y 2 ) · · · (yk− 3 ∨ ak− 1 ∨ ak) are all satisfied
To see this, first suppose that the clauses on the right are all satisfied. Then at least one of the literals a 1 ,... , ak must be true—otherwise y 1 would have to be true, which would in turn force y 2 to be true, and so on, eventually falsifying the last clause. But this means (a 1 ∨ a 2 ∨ · · · ∨ ak) is also satisfied. Conversely, if (a 1 ∨ a 2 ∨ · · · ∨ ak) is satisfied, then some ai must be true. Set y 1 ,... , yi− 2 to true and the rest to false. This ensures that the clauses on the right are all satisfied.
Thus, any instance of SAT can be transformed into an equivalent instance of 3SAT. In fact, 3 SAT remains hard even under the further restriction that no variable appears in more than
266 Algorithms
Figure 8.9 S is a vertex cover if and only if V − S is an independent set.
S
three clauses. To show this, we must somehow get rid of any variable that appears too many times. Here’s the reduction from 3SAT to its constrained version. Suppose that in the 3SAT in- stance, variable x appears in k > 3 clauses. Then replace its first appearance by x 1 , its second appearance by x 2 , and so on, replacing each of its k appearances by a different new variable. Finally, add the clauses
(x 1 ∨ x 2 ) (x 2 ∨ x 3 ) · · · (xk ∨ x 1 ).
And repeat for every variable that appears more than three times. It is easy to see that in the new formula no variable appears more than three times (and in fact, no literal appears more than twice). Furthermore, the extra clauses involv- ing x 1 , x 2 ,... , xk constrain these variables to have the same value; do you see why? Hence the original instance of 3SAT is satisfiable if and only if the constrained instance is satisfiable.
Some reductions rely on ingenuity to relate two very different problems. Others simply record the fact that one problem is a thin disguise of another. To reduce INDEPENDENT SET to VERTEX COVER we just need to notice that a set of nodes S is a vertex cover of graph G = (V, E) (that is, S touches every edge in E) if and only if the remaining nodes, V − S, are an independent set of G (Figure 8.9). Therefore, to solve an instance (G, g) of INDEPENDENT SET, simply look for a vertex cover of G with |V | − g nodes. If such a vertex cover exists, then take all nodes not in it. If no such vertex cover exists, then G cannot possibly have an independent set of size g.
INDEPENDENT SET and CLIQUE are also easy to reduce to one another. Define the complement of a graph G = (V, E) to be G = (V, E), where E contains precisely those unordered pairs of vertices that are not in E. Then a set of nodes S is an independent set of G if and only if S is a clique of G. To paraphrase, these nodes have no edges between them in G if and only if they have all possible edges between them in G. Therefore, we can reduce INDEPENDENT SET to CLIQUE by mapping an instance (G, g)