










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











The wonderful thing about standards is that there are so many of them to choose from. — Real Admiral Grace Murray Hopper
If a problem has no solution, it may not be a problem, but a fact — not to be solved, but to be coped with over time. — Shimon Peres
A generally-accepted minimum requirement for an algorithm to be considered ‘efficient’ is that its running time is polynomial: O ( nc^ ) for some constant c , where n is the size of the input.^1 Researchers recognized early on that not all problems can be solved this quickly, but we had a hard time figuring out exactly which ones could and which ones couldn’t. there are several so-called NP-hard problems, which most people believe cannot be solved in polynomial time, even though nobody can prove a super-polynomial lower bound. Circuit satisfiability is a good example of a problem that we don’t know how to solve in polynomial time. In this problem, the input is a boolean circuit : a collection of AND, OR, and NOT gates connected by wires. We will assume that there are no loops in the circuit (so no delay lines or flip-flops). The input to the circuit is a set of m boolean (TRUE/FALSE) values x 1 ,... , xm. The output is a single boolean value. Given specific input values, we can calculate the output of the circuit in polynomial (actually, linear ) time using depth-first-search, since we can compute the output of a k -input gate in O ( k ) time.
An And gate, an Or gate, and a Not gate.
A boolean circuit. Inputs enter from the left, and the output leaves to the right.
The circuit satisfiability problem asks, given a circuit, whether there is an input that makes the circuit output TRUE, or conversely, whether the circuit always outputs FALSE. Nobody knows how to solve this problem faster than just trying all 2 m^ possible inputs to the circuit, but this requires exponential time. On the other hand, nobody has ever proved that this is the best we can do; maybe there’s a clever algorithm that nobody has discovered yet!
(^1) This notion of efficiency was independently formalized by Alan Cobham (The intrinsic computational difficulty of functions. Logic, Methodology, and Philosophy of Science (Proc. Int. Congress), 24–30, 1965), Jack Edmonds (Paths, trees, and flowers. Canadian Journal of Mathematics 17:449–467, 1965), and Michael Rabin (Mathematical theory of automata. Proceedings of the 19th ACM Symposium in Applied Mathematics, 153–175, 1966), although similar notions were considered more than a decade earlier by Kurt Gödel and John von Neumann.
A decision problem is a problem whose output is a single boolean value: YES or NO.^2 Let me define three classes of decision problems:
For example, the circuit satisfiability problem is in NP. If the answer is YES, then any set of m input values that produces TRUE output is a proof of this fact; we can check the proof by evaluating the circuit in polynomial time. It is widely believed that circuit satisfiability is not in P or in co-NP, but nobody actually knows. Every decision problem in P is also in NP. If a problem is in P, we can verify YES answers in polynomial time recomputing the answer from scratch! Similarly, any problem in P is also in co-NP. One of the most important open questions in theoretical computer science is whether or not P = NP. Nobody knows. Intuitively, it should be obvious that P 6 = NP; the homeworks and exams in this class and others have (I hope) convinced you that problems can be incredibly hard to solve, even when the solutions are obvious in retrospect. But nobody knows how to prove it. A more subtle but still open question is whether NP and co-NP are different. Even if we can verify every YES answer quickly, there’s no reason to think that we can also verify NO answers quickly. For example, as far as we know, there is no short proof that a boolean circuit is not satisfiable. It is generally believed that NP 6 = co-NP, but nobody knows how to prove it.
What we think the world looks like.
A problem Π is NP-hard if a polynomial-time algorithm for Π would imply a polynomial-time algorithm for every problem in NP. In other words:
Π is NP-hard ⇐⇒ If Π can be solved in polynomial time, then P=NP (^2) Technically, I should be talking about languages , which are just sets of bit strings. The language associated with a decision problem is the set of bit strings for which the answer is YES. For example, for the problem is ‘Is the input graph connected?’, the corresponding language is the set of connected graphs, where each graph is represented as a bit string (for example, its adjacency matrix). (^3) More formally, P is the set of languages that can be recognized in polynomial time by a single-tape Turing machine. If you want to learn more about Turing machines, take CS 579.
To prove that a problem is NP-hard, we use a reduction argument. Reducing problem A to another problem B means describing an algorithm to solve problem A under the assumption that an algorithm for problem B already exists. You’re already used to doing reductions, only you probably call it something else, like writing subroutines or utility functions, or modular programming. To prove something is NP-hard, we describe a similar transformation between problems, but not in the direction that most people expect. You should tattoo the following rule of onto the back of your hand.
To prove that problem A is NP-hard, reduce a known NP-hard problem to A****.
In other words, to prove that your problem is hard, you need to describe an algorithm to solve a different problem, which you already know is hard, using a mythical algorithm for your problem as a subroutine. The essential logic is a proof by contradiction. Your reduction shows implies that if your problem were easy, then the other problem would be easy, too. Equivalently, since you know the other problem is hard, your problem must also be hard. For example, consider the formula satisfiability problem, usually just called SAT. The input to SAT is a boolean formula like
( a ∨ b ∨ c ∨ d ¯) ⇔ (( b ∧ ¯ c ) ∨ (¯ a ⇒ d ) ∨ ( c 6 = a ∧ b )),
and the question is whether it is possible to assign boolean values to the variables a , b , c ,... so that the formula evaluates to TRUE. To show that SAT is NP-hard, we need to give a reduction from a known NP-hard problem. The only problem we know is NP-hard so far is circuit satisfiability, so let’s start there. Given a boolean circuit, we can transform it into a boolean formula by creating new output variables for each gate, and then just writing down the list of gates separated by and. For example, we could transform the example circuit into a formula as follows:
x 1
x 2 x 3 x 4 x 5
y 1
y 2 y 3
y 4
y 5
y 6
y 7^ y^8
( y 1 = x 1 ∧ x 4 ) ∧ ( y 2 = x 4 ) ∧ ( y 3 = x 3 ∧ y 2 ) ∧ ( y 4 = y 1 ∨ x 2 ) ∧ ( y 5 = x 2 ) ∧ ( y 6 = x 5 ) ∧ ( y 7 = y 3 ∨ y 5 ) ∧ ( y 8 = y 4 ∧ y 7 ∧ y 6 ) ∧ y 8 A boolean circuit with gate variables added, and an equivalent boolean formula.
Now the original circuit is satisfiable if and only if the resulting formula is satisfiable. Given a satisfying input to the circuit, we can get a satisfying assignment for the formula by computing the output of every gate. Given a satisfying assignment for the formula, we can get a satisfying input the the circuit by just ignoring the gate variables yi. We can transform any boolean circuit into a formula in linear time using depth-first search, and the size of the resulting formula is only a constant factor larger than the size of the circuit. Thus, we have a polynomial-time reduction from circuit satisfiability to SAT:
boolean circuit
O ( n ) −−−→ boolean formula w w SAT TRUE or FALSE
trivial ←−−− TRUE or FALSE
T CSAT( n ) ≤ O ( n ) + T SAT( O ( n )) =⇒ T SAT( n ) ≥ T CSAT(Ω( n )) − O ( n )
The reduction implies that if we had a polynomial-time algorithm for SAT, then we’d have a polynomial- time algorithm for circuit satisfiability, which would imply that P=NP. So SAT is NP-hard. To prove that a boolean formula is satisfiable, we only have to specify an assignment to the variables that makes the formula TRUE. We can check the proof in linear time just by reading the formula from left to right, evaluating as we go. So SAT is also in NP, and thus is actually NP-complete.
A special case of SAT that is particularly useful in proving NP-hardness results is called 3SAT. A boolean formula is in conjunctive normal form (CNF) if it is a conjunction (AND) of several clauses , each of which is the disjunction (OR) of several literals , each of which is either a variable or its negation. For example:
︷ clause︸︸ ︷ ( a ∨ b ∨ c ∨ d ) ∧ ( b ∨ ¯ c ∨ d ¯) ∧ ( a ¯ ∨ c ∨ d ) ∧ ( a ∨ ¯ b )
A 3CNF formula is a CNF formula with exactly three literals per clause; the previous example is not a 3CNF formula, since its first clause has four literals and its last clause has only two. 3SAT is just SAT restricted to 3CNF formulas: Given a 3CNF formula, is there an assignment to the variables that makes the formula evaluate to TRUE? We could prove that 3SAT is NP-hard by a reduction from the more general SAT problem, but it’s easier just to start over from scratch, with a boolean circuit. We perform the reduction in several stages.
a = b ∧ c 7 −→ ( a ∨ ¯ b ∨ ¯ c ) ∧ ( a ¯ ∨ b ) ∧ (¯ a ∨ c ) a = b ∨ c 7 −→ (¯ a ∨ b ∨ c ) ∧ ( a ∨ ¯ b ) ∧ ( a ∨ ¯ c ) a = ¯ b 7 −→ ( a ∨ b ) ∧ ( a ¯ ∨ ¯ b )
a 7 −→ ( a ∨ x ∨ y ) ∧ ( a ∨ ¯ x ∨ y ) ∧ ( a ∨ x ∨ ¯ y ) ∧ ( a ∨ ¯ x ∨ ¯ y ) a ∨ b 7 −→ ( a ∨ b ∨ x ) ∧ ( a ∨ b ∨ ¯ x )
‾ ‾
a b
c
c
d
a
b
d
‾b
d
a‾
c‾
A graph derived from a 3CNF formula, and an independent set of size 4. Black edges join literals from the same clause; red (heavier) edges join contradictory literals.
Now suppose the original formula had k clauses. Then I claim that the formula is satisfiable if and only if the graph has an independent set of size k.
Thus, the reduction is correct. Since the reduction from 3CNF formula to graph takes polynomial time, we conclude that MAXINDSET is NP-hard. Here’s a diagram of the reduction:
3CNF formula with k clauses
O ( n ) −−−→ graph with 3 k nodes w w MAXINDSET TRUE or FALSE
O ( 1 ) ←−−− maximum independent set size
T 3SAT( n ) ≤ O ( n ) + T MAXINDSET ( O ( n )) =⇒ T MAXINDSET ( n ) ≥ T 3SAT(Ω( n )) − O ( n )
A clique is another name for a complete graph, that is, a graph where every pair of vertices is connected by an edge. The maximum clique size problem, or simply MAXCLIQUE, is to compute, given a graph, the number of nodes in its largest complete subgraph.
A graph with maximum clique size 4.
There is an easy proof that MAXCLIQUE is NP-hard, using a reduction from MAXINDSET. Any graph G has an edge-complement G with the same vertices, but with exactly the opposite set of edges—( u , v ) is an edge in G if and only if it is not an edge in G. A set of vertices is independent in G if and only if the same vertices define a clique in G. Thus, we can compute the largest independent in a graph simply by computing the largest clique in the complement of the graph.
graph G
O ( n ) −−−→ complement graph G w w MAXCLIQUE largest independent set
trivial ←−−− largest clique
A vertex cover of a graph is a set of vertices that touches every edge in the graph. The MINVERTEXCOVER problem is to find the smallest vertex cover in a given graph. Again, the proof of NP-hardness is simple, and relies on just one fact: If I is an independent set in a graph G = ( V , E ), then V \ I is a vertex cover. Thus, to find the largest independent set, we just need to find the vertices that aren’t in the smallest vertex cover of the same graph.
graph G = ( V , E )
trivial −−−→ graph G = ( V , E ) w w MINVERTEXCOVER largest independent set V \ S
O ( n ) ←−−− smallest vertex cover S
A k - coloring of a graph is a map C : V → { 1 , 2 ,... , k } that assigns one of k ‘colors’ to each vertex, so that every edge has two different colors at its endpoints. The graph coloring problem is to find the smallest possible number of colors in a legal coloring. To show that this problem is NP-hard, it’s enough to consider the special case 3COLORABLE: Given a graph, does it have a 3-coloring? To prove that 3COLORABLE is NP-hard, we use a reduction from 3SAT. Given a 3CNF formula, we produce a graph as follows. The graph consists of a truth gadget, one variable gadget for each variable in the formula, and one clause gadget for each clause in the formula. The truth gadget is just a triangle with three vertices T , F , and X , which intuitively stand for TRUE, FALSE, and OTHER. Since these vertices are all connected, they must have different colors in any 3-coloring. For the sake of convenience, we will name those colors TRUE, FALSE, and OTHER. Thus, when we say that a node is colored TRUE, all we mean is that it must be colored the same as the node T. The variable gadget for a variable a is also a triangle joining two new nodes labeled a and a to node X in the truth gadget. Node a must be colored either TRUE or FALSE, and so node a must be colored either FALSE or TRUE, respectively. Finally, each clause gadget joins three literal nodes to node T in the truth gadget using five new unlabeled nodes and ten edges; see the figure below. If all three literal nodes in the clause gadget are colored FALSE, then the rightmost vertex in the gadget cannot have one of the three colors. Since the variable gadgets force each literal node to be colored either TRUE or FALSE, in any valid 3-coloring, at least one of the three literal nodes is colored TRUE. I need to emphasize here that the final graph contains only one node T , only one node F , and only two nodes a and ¯ a for each variable.
A Hamiltonian cycle in a graph is a cycle that visits every vertex exactly once. This is very different from an Eulerian cycle , which is actually a closed walk that traverses every edge exactly once. Eulerian cycles are easy to find and construct in linear time using a variant of depth-first search. Finding Hamiltonian cycles, on the other hand, is NP-hard. To prove this, we use a reduction from the vertex cover problem. Given a graph G and an integer k , we need to transform it into another graph G ′, such that G ′^ has a Hamiltonian cycle if and only if G has a vertex cover of size k. As usual, our transformation uses several gadgets.
(v,u,1) (v,u,2) (v,u,3) (v,u,4) (v,u,5) (v,u,6) An edge gadget for ( u , v ) and the only possible Hamiltonian paths through it.
(v,w)
(w,v)
(v,x)
(x,v)
(v,y)
(y,v)
1 2 3
... k
(v,z)
(z,v)
The vertex chain for v : all edge gadgets involving v are strung together and joined to the k cover vertices. It’s not hard to prove that if { v 1 , v 2 ,... , vk } is a vertex cover of G , then G ′^ has a Hamiltonian cycle— start at cover vertex 1, through traverse the vertex chain for v 1 , then visit cover vertex 2, then traverse the vertex chain for v 2 , and so forth, eventually returning to cover vertex 1. Conversely, any Hamiltonian cycle in G ′^ alternates between cover vertices and vertex chains, and the vertex chains correspond to the k vertices in a vertex cover of G. (This is a little harder to prove.) Thus, G has a vertex cover of size k if and only if G ′^ has a Hamiltonian cycle.
1
2
u v
w x
(v,x)
(x,v)
(u,v)
(v,u)
(u,w)
(w,u)
(v,w)
(w,v)
(x,w)
(w,x)
The original graph G with vertex cover { v , w }, and the transformed graph G ′^ with a corresponding Hamiltonian cycle. Vertex chains are colored to match their corresponding vertices. The transformation from G to G ′^ takes at most O ( n^2 ) time, so the Hamiltonian cycle problem is NP-hard. Moreover, since we can easily verify a Hamiltonian cycle in linear time, the Hamiltonian cycle problem is in NP, and therefore NP-complete. A closely related problem to Hamiltonian cycles is the famous traveling salesman problem —Given a weighted graph G , find the shortest cycle that visits every vertex. Finding the shortest cycle is obviously harder than determining if a cycle exists at all, so the traveling salesman problem is also NP-hard.
The last problem that we will prove NP-hard is the SUBSETSUM problem considered in the very first lecture on recursion: Given a set X of integers and an integer t , determine whether X has a subset whose elements sum to t. To prove this problem is NP-hard, we apply a reduction from the vertex cover problem. Given a graph G and an integer k , we need to transform it into set of integers X and an integer t , such that X has a subset that sums to t if and only if G has an vertex cover of size k. Our transformation uses just two ‘gadgets’; these are integers representing vertices and edges in G. Number the edges of G arbitrarily from 0 to m − 1. Our set X contains the integer bi := 4 i^ for each edge i , and the integer av := 4 m^ +
i ∈∆( v )
4 i
for each vertex v , where ∆( v ) is the set of edges that have v has an endpoint. Alternately, we can think of each integer in X as an ( m + 1 )-digit number written in base 4. The m th digit is 1 if the integer represents a vertex, and 0 otherwise. For each i < m , the i th digit is 1 if the integer represents edge i or one of its endpoints, and 0 otherwise. Finally, we set the target sum
t := k · 4 m^ +
m ∑− 1
i = 0
2 · 4 i^.
for these problems, but you can find most of them in Garey and Johnson’s classic Scary Black Book of NP-Completeness.^6
a ∈ A
a =
b ∈ B
b?
This can be proved NP-hard by a simple reduction from SUBSETSUM. Like SUBSETSUM, the PARTITION problem is only weakly NP-hard.
i Si^. This is a generalization of both VERTEXCOVER^ and X3M.
i Si^ that hit every set in^ S^. This is also a generalization of VERTEXCOVER.
Most interesting puzzles and solitaire games have been shown to be NP-hard, or to have NP-hard generalizations. (Arguably, if a game or puzzle isn’t at least NP-hard, it isn’t interesting!) Some familiar examples include Minesweeper (by reduction from CIRCUITSAT)^8 , Tetris (by reduction from 3PARTITION)^9 , and Sudoku (by reduction from 3SAT)^10. Most two-player games^11 like tic-tac-toe, reversi, checkers, chess, go, mancala—or more accurately, appropriate generalizations of these constant-size games to arbitrary board sizes—are not just NP-hard, but PSPACE-hard or even EXP-hard.^12
(^8) Richard Kaye. Minesweeper is NP-complete. Mathematical Intelligencer 22(2):9–15, 2000. http://www.mat.bham.ac.uk/ R.W.Kaye/minesw/minesw.pdf (^9) Ron Breukelaar, Erik D. Demaine, Susan Hohenberger, Hendrik J. Hoogeboom, Walter A. Kosters, and David Liben- Nowell*. Tetris is Hard, Even to Approximate. International Journal of Computational Geometry and Applications 14:41–68,
3 4
0 4 2
3 0 2
1
(a) Gadget for planar 3-colorability. (b) Gadget for degree-4 planar 3-colorability. (c) A careful 5-coloring.
(a) Given two undirected graphs G and H , is G isomorphic to a subgraph of H? (b) Given an undirected graph G , does G have a spanning tree in which every node has degree at most 17? (c) Given an undirected graph G , does G have a spanning tree with at most 42 leaves?
(a) A double-Eulerian circuit in an undirected graph G is a closed walk that traverses every edge in G exactly twice. Given a graph G , does G have a double-Eulerian circuit? (b) A double-Hamiltonian circuit in an undirected graph G is a closed walk that visits every vertex in G exactly twice. Given a graph G , does G have a double-Hamiltonian circuit?
4
8
2
7
5
3
1
12 8
6 5 9
5
A heavy Hamiltonian cycle. The cycle has total weight 34; the graph has total weight 67.