Algorithm II
8. Intractability I
WU Xiaokun 吴晓堃
xkun.wu [at] gmail
Algorithm design patterns.
Algorithm design anti-patterns.
Q. Which problems will we be able to solve in
practice?
A working definition. Those with poly-time
algorithms.
Theory. Definition is broad and robust.
Practice. Poly-time algorithms scale to huge
problems.
Idea. Classify problems: can be solved in poly-time and those that cannot.
Provably requires exponential time.
Frustrating news. Huge number of fundamental problems have defied classification for decades.
Idea. Suppose we could solve problem Y in polynomial time. What else could we solve in polynomial time?
Reduction. Problem X polynomial-time reduces to problem Y if arbitrary instances of problem X can be solved using:
Notation. X \le_P Y.
Note. We pay for time to write down instances of Y sent to oracle \Rightarrow instances of Y must be of polynomial size.
Common mistake. Confusing X \le_P Y with Y \le_P X.
Suppose that X \le_P Y. Which of the following can we infer?
A. If X can be
solved in polynomial time, then so can Y.
B. X can be solved in
poly time iff Y can be solved in poly
time.
C. If X cannot be
solved in polynomial time, then neither can Y.
D. If Y cannot be
solved in polynomial time, then neither can X.
C. contrapositive
Design algorithms. If X \le_P Y and Y can be solved in polynomial time, then X can be solved in polynomial time.
Establish intractability. If X \le_P Y and X cannot be solved in polynomial time, then Y cannot be solved in polynomial time.
Establish equivalence. If both X \le_P Y and Y \le_P X, we use notation X \equiv_P Y.
Bottom line. Reductions classify problems according to relative difficulty.
INDEPENDENT-SET. Given a graph G = (V, E) and an integer k, is there a subset of k (or more) vertices such that no two are adjacent?
Ex. Is there an independent set of size \ge 7?
Optimization: [Packing] What is the maximum size independent set?
VERTEX-COVER. Given a graph G = (V, E) and an integer k, is there a subset of \le k vertices that each edge is incident to at least one vertex in the subset?
Ex. Is there a vertex cover of size \le 3?
Optimization: [Covering] What is the minimum size vertex cover?
Theorem. INDEPENDENT-SET \equiv_P VERTEX-COVER.
Pf. We show S is an
independent set of size k iff V - S is a vertex cover of size n - k.
Theorem. INDEPENDENT-SET \equiv_P VERTEX-COVER.
Pf. We show S is an
independent set of size k iff V - S is a vertex cover of size n - k.
Theorem. INDEPENDENT-SET \equiv_P VERTEX-COVER.
Pf. We show S is an
independent set of size k iff V - S is a vertex cover of size n - k.
SET-COVER. Given a set U of elements, a collection S of subsets of U, and an integer k, are there \le k of these subsets whose union is equal to U?
Sample application. software-services.
1 | 2 | 3 | 4 | 5 | 6 | 7 | |
---|---|---|---|---|---|---|---|
A | + | ||||||
B | + | + | |||||
C | + | + | + | + | |||
D | + | ||||||
E | + | ||||||
F | + | + | + | + |
Theorem. VERTEX-COVER \le_P SET-COVER.
Pf. Given a VERTEX-COVER instance G = (V, E) and k, we construct a SET-COVER instance (U, S, k) that has a set cover of size k iff G has
a vertex cover of size k.
Construction.
Lemma. G = (V, E)
contains a vertex cover of size k iff
(U, S, k) contains a set cover of size
k.
Pf. \Rightarrow Let
X \subseteq V be a vertex cover of size
k in G.
Lemma. G = (V, E)
contains a vertex cover of size k iff
(U, S, k) contains a set cover of size
k.
Pf. \Leftarrow Let
Y \subseteq S be a set cover of size
k in (U, S,
k).
Literal. A Boolean variable or its negation: x_i, \bar{x_i}.
Clause. A disjunction of literals: eg., C_j = x_1 \vee \bar{x_2} \vee x_3.
Conjunctive normal form (CNF). A propositional formula \Phi that is a conjunction of clauses: eg., \Phi = C_1 \wedge C_2 \wedge C_3.
SAT. Given a CNF formula \Phi, does it have a satisfying truth assignment?
3-SAT. SAT where each clause contains exactly 3 literals (and each literal corresponds to a different variable).
Ex. \Phi = ( x_1 \vee x_2 \vee x_3) \wedge ( x_1 \vee x_2 \vee x_3) \wedge ( x_1 \vee x_2 \vee x_4 )
Key application. Electronic design automation (EDA).
Scientific hypothesis. There does not exist a poly-time algorithm for 3-SAT.
\mathcal{P} vs. \mathcal{NP}. This hypothesis is equivalent to \mathcal{P} \ne \mathcal{NP} conjecture.
Theorem. 3-SAT \le_P INDEPENDENT-SET.
Pf. Given a 3-SAT instance \Phi, we construct a INDEPENDENT-SET instance
(G, k) that has a size k = |\Phi| independent set iff \Phi is satisfiable.
Construction.
\Phi = ( x_1 \vee x_2 \vee x_3) \wedge ( x_1 \vee x_2 \vee x_3) \wedge ( x_1 \vee x_2 \vee x_4 )
Lemma. \Phi is
satisfiable iff G contains an
independent set of size k =
|\Phi|.
Pf. \Rightarrow
Consider any satisfying assignment for \Phi.
Lemma. \Phi is
satisfiable iff G contains an
independent set of size k =
|\Phi|.
Pf. \Leftarrow Let
S be independent set of size k.
true
(and remaining literals
consistently).Basic reduction strategies.
Transitivity. If X \le_P
Y and Y \le_P Z, then X \le_P Z.
Pf idea. Compose those two algorithms.
Ex. 3-SAT \le_P INDEPENDENT-SET \equiv_P VERTEX-COVER \le_P SET-COVER.
Decision problem. Does there exist a vertex cover of size \le k?
Search problem. Find a vertex cover of size \le k.
Optimization problem. Find a vertex cover of minimum size.
Goal. Show that all three problems poly-time reduce to one another.
VERTEX-COVER. Does there exist a vertex cover of
size \le k?
FIND-VERTEX-COVER. Find a vertex cover of size \le k.
Theorem. VERTEX-COVER \equiv_P FIND-VERTEX-COVER.
Pf. \le_P Decision problem is a special case of search problem.
Pf. \ge_P To find a vertex cover of size \le k:
FIND-VERTEX-COVER. Find a vertex cover of size \le k.
FIND-MIN-VERTEX-COVER. Find a vertex cover of minimum
size.
Theorem. FIND-VERTEX-COVER \equiv_P FIND-MIN-VERTEX-COVER.
Pf. \le_P Search problem is a special case of optimization problem.
Pf. \ge_P To find vertex cover of minimum size:
HAM-CYCLE. Given an undirected graph G = (V, E), does there exist a cycle \Gamma that visits every node exactly once?
Sequencing Problems. Search over all permutations of a collection of objects.
DIR-HAM-CYCLE. Given a directed graph G = (V, E), does there exist a directed cycle \Gamma that visits every node exactly once?
Theorem. DIR-HAM-CYCLE \le_P HAM-CYCLE.
Pf. Given a directed graph G
= (V, E), construct a graph G' with 3n nodes.
Lemma. G has a directed Hamilton cycle iff G' has a Hamilton cycle.
Pf. \Rightarrow
Pf. \Leftarrow
Theorem. 3-SAT \le_P DIR-HAM-CYCLE.
Pf. Given an instance \Phi of 3-SAT, we construct an instance G of DIR-HAM-CYCLE that has a Hamilton cycle
iff \Phi is satisfiable.
Construction overview. Let n denote the number of variables in \Phi.
We will construct a graph G that has
2n Hamilton cycles, with each cycle
corresponding to one of the 2n possible
truth assignments.
Construction. Given 3-SAT instance \Phi with n variables x_i and k clauses.
true
.Which is truth assignment corresponding to Hamilton cycle below?
true
, x_2 = true
, x_3 = true
true
, x_2 = true
, x_3 = false
false
, x_2 = false
, x_3 = true
false
, x_2 = false
, x_3 = false
Construction. Given 3-SAT instance \Phi with n variables x_i and k clauses.
Construction. Given 3-SAT instance \Phi with n variables x_i and k clauses.
Lemma. \Phi is
satisfiable iff G has a Hamilton
cycle.
Pf. \Rightarrow
true
,
traverse row i from left to rightfalse
,
traverse row i from right to leftLemma. \Phi is
satisfiable iff G has a Hamilton
cycle.
Pf. \Leftarrow
true
if
\Gamma' traverses row i left-to-right; otherwise, set x^\ast_i = false
.3D-MATCHING
. Given n
instructors, n courses, and n times, and a list of the possible courses
and times each instructor is willing to teach, is it possible to make an
assignment so that all courses are taught at different times?
Ex. Three courses, Mon.-Wed. afternoon.
I | M | T | W |
---|---|---|---|
A | AI, AD | ||
B | AI | AI, SE | |
C | AI, SE | SE |
{A, AD, W}, {B, AI, T}, {C, SE, M}
3D-MATCHING. Given 3 disjoint sets X, Y, Z, each of size n and a set T \subseteq X \times Y \times Z of triples, does there exist a set of n triples in T such that each element of X \cup Y \cup Z is in exactly one of these triples?
Remark. Generalization of bipartite matching.
Theorem. 3-SAT \le_P 3D-MATCHING.
Pf. Given an instance \Phi of 3-SAT, we construct an instance of
3D-MATCHING that has a perfect matching iff \Phi is satisfiable.
Construction. (variable)
Construction. (variable)
true
) or all blue ones (odd: x_i = false
).Construction. (clause)
Construction. (cleanup)
Lemma. Instance (X, Y, Z) has a perfect matching iff \Phi is satisfiable.
Q. What are X, Y, and Z?
Lemma. Instance (X, Y, Z) has a perfect matching iff \Phi is satisfiable.
Q. What are X,
Y, and Z?
A. X =
black
, Y =
white
, and Z =
blue
.
Lemma. Instance (X, Y, Z) has a perfect matching iff \Phi is satisfiable.
Pf. \Rightarrow If 3d-matching, then assign x_i according to gadget x_i.
Pf. \Leftarrow If \Phi is satisfiable, use any true literal in C_j to select gadget C_j triple.
3-COLOR. Given an undirected graph G, can the nodes be colored
black
, white
, and blue
so that no
adjacent nodes have the same color?
How difficult to solve 2-COLOR?
A. O(m + n) using
BFS or DFS.
B. O(mn) using maximum
flow.
C. \Omega(2^n) using
brute force.
D. Not even Tarjan knows.
A graph G is 2-colorable if and only if it is bipartite.
Register allocation. Assign program variables to machine registers so that: (i) no more than k registers are used, (ii) and no two program variables that are needed at the same time are assigned to the same register.
Interference graph. Nodes are program variables; edge between u and v if there exists an operation where both u and v are “live” at the same time.
Observation. [Chaitin 1982] Can solve register allocation problem iff interference graph is k-colorable.
Fact. 3-COLOR \le_P K-REGISTER-ALLOCATION for any constant k \ge 3.
Theorem. 3-SAT \le_P 3-COLOR.
Pf. Given 3-SAT instance \Phi, we construct an instance of 3-COLOR that is 3-colorable iff \Phi is satisfiable.
Construction.
Lemma. Graph G is
3-colorable iff \Phi is
satisfiable.
Pf. \Rightarrow
Suppose graph G is 3-colorable.
black
, F is
white
, and B is
blue
.black
literals to
true
(and white
to false
).black
or
white
.white
if its negation is
black
(and vice versa).Lemma. Graph G is
3-colorable iff \Phi is
satisfiable.
Pf. \Rightarrow
Suppose graph G is 3-colorable.
black
.
white
in
some 3-coloringblue
,white
&
black
,
Lemma. Graph G is
3-colorable iff \Phi is
satisfiable.
Pf. \Leftarrow Suppose
3-SAT instance \Phi is satisfiable.
true
literals black
and all
false
literals white
.true
literal; color node below that node
white
, and node below that blue
.blue
.black
or
white
, as forced.SUBSET-SUM. Given n natural numbers w_1, \ldots, w_n and an integer W, is there a subset that adds up to exactly W?
Ex. \{ 215, 215, 275, 275,
355, 355, 420, 420, 580, 580, 655, 655 \}, W = 1505.
Yes. 215 + 355 + 355 + 580 =
1505.
We solved it using dynamic programming with time O(nW).
Remark. With arithmetic problems, input integers are encoded in binary. Poly-time reduction must be polynomial in binary encoding.
We referred to such problem as Pseudo-polynomial.
Theorem. 3-SAT ≤_P SUBSET-SUM.
Pf. Given an instance \Phi of 3-SAT, we construct an instance of SUBSET-SUM that has solution iff \Phi is satisfiable.
Construction. Given 3-SAT instance \Phi with n variables and k clauses, form 2n + 2k decimal integers, each having n + k digits:
Key property. No carries possible \Rightarrow each digit yields one equation.
Lemma. \Phi is
satisfiable iff there exists a subset that sums to W.
Pf. \Rightarrow
Suppose 3-SAT instance \Phi has
satisfying assignment x^\ast.
true
,
select integer in row x_i; otherwise,
select integer in row \neg x_i.Lemma. \Phi is
satisfiable iff there exists a subset that sums to W.
Pf. \Leftarrow Suppose
there exists a subset S^\ast that sums
to W.
true
; otherwise,
assign x^\ast_i =
false
.Karp [1972], 1985 Turing Award.