算法设计 algorithm design 第八章 NP and Computational Intractability NP问题以及计算难解性
- 1 Definition 定义
- 2 Reduction 规约
- 3 NPC NP-完全问题
- 3.0 Circuit Satisfiability 电路可满足性
- 3.1 3-SAT
- 3.2 Covering problems and Packing problems
- 3.3 Sequencing problems
- 3.4 Partitioning problems
- 3.5 Numerical problems
- 3.6 总结
- 3.6.1 3-SAT ≡ P \equiv_P ≡P Independent Set将 3-SAT规约为独立集
- 3.6.2 Vertex cover ≡ P \equiv_P ≡P Independent set 点集覆盖多项式等价与独立集
- 3.6.3 Vertex Cover Reduces to Set Cover 点集覆盖规约为集合覆盖
- 3.6.4 3-SAT ≤ P \leq_P ≤P Dir-Ham-Cycle将3-SAT规约为有向哈密尔顿环
- 3.6.5 Dir-Ham-Cycle ≤ p \leq_p ≤p Ham-cycle 有向哈密尔顿图规约到无向
- 3.6.6 Ham-cycle ≤ p \leq_p ≤p TSP 哈密尔顿图规约到旅行商
- 3.6.7 3-SAT ≤ P \leq_P ≤P 3-color 3-SAT规约为3色问题
- 3.6.8 3-SAT ≤ P \leq_P ≤P SUBSET-SUM 3-SAT规约为子集求和问题
- 3.6.9 SUBSET-SUM ≤ P \leq_P ≤P Scheduling 子集求和规约为规划问题
- 4 co-NP与NPC
1 Definition 定义
1.1 decision problem 判定问题:
- 该问题称为X
- instance string s 一个实例s
- algorithm A solves problem X: A ( s ) = y e s A(s)=yes A(s)=yes iff s ∈ X s\in X s∈X
判定问题是指我们常说的判断题,返回yes或者no。注意定义中的
s
∈
X
s\in X
s∈X 等价于
A
(
s
)
=
y
e
s
A(s)=yes
A(s)=yes。
我们后续研究的问题都是判定问题。所有的优化问题可与规约到判定问题。
1.2 polynomial time 多项式时间:
- 对于所有的实例s, A(s)至多在多项式步骤内终止。
1.3 P 问题:
- 有多项式时间算法的判定问题
1.4 NP问题
问题,实例,可能的解,check
- 存在多项式时间的验证算法
验证算法certifier 与证据certificate
- certificate 证书 证据 理解为instance s 的一个可能解t
- certifier C(s,t) check 可能解t是否满足问题X的一系列约束,返回yes 或者no
- C(s,t) =yes iff s ∈ X s\in X s∈X
举个例子:问题 X 二元一次方程是否有解,s 是一个二元一次方程,t(certificate)是一个可能的解,certifier 验证t能否使方程成立。
1.5 EXP问题
- 存在指数时间算法的判定问题。
P
⊆
N
P
⊆
E
X
P
P \subseteq NP \subseteq EXP
P⊆NP⊆EXP
证明也很显然,P问题的多项式时间算法作为NP的验证算法,遍历NP问题的所有t(certificate)是指数时间。
2 Reduction 规约
常提到规约一般是多项式规约。
问题
X
≤
p
Y
X\le_p Y
X≤pY
- 针对X的实例x,可以构造一个Y的实例y
- x ∈ \in ∈ X, iff y ∈ \in ∈ Y,看前面定义,这里 ∈ \in ∈意味着返回值为yes
3 NPC NP-完全问题
- NPC是NP问题
- 所有的NP问题可以规约为NPC问题
- P=NP iff NPC问题可解
是否存在天然的NPC问题? Circuit Satisfiability
3.0 Circuit Satisfiability 电路可满足性
给定一个由或与非组成的电路,固定一部分输入(hard-code input),是否存在输入使得output为1。
- 该问题显然为NP问题,因为存在多项式时间验证算法,直接带入输入计算结果即可
- 对于任意一个NP问题 X X X,存在一个验证算法certifier C(s,t)。要判断是否有 s ∈ X s \in X s∈X,such that C(s,t)=yes。
- 我们将s作为hard-coded input。将t作为input 构建一个Circuit Satisfiability 实例 y。设计电路等价该certifier。
- y is yes iff s is yes.
Circuit Satisfiability是一个天然的NPC问题。
3.1 3-SAT
- Literal 文字 :一个布尔变量x或者它的否 x i x_i xi or ¬ x i \neg x_i ¬xi
- Clause 字句: 文字的disjunction(析取)(中文翻译感觉很离谱) C i = x 1 ∨ x 2 ∨ ¬ x 3 C_i=x_1\vee x_2\vee \neg x_3 Ci=x1∨x2∨¬x3
- Conjunctive normal form合取范式: Φ = C 1 ∧ C 2 ∧ C 3 \Phi=C_1\wedge C_2\wedge C_3 Φ=C1∧C2∧C3
- SAT: Given CNF formula Φ \Phi Φ, does it have a satisfying truth assignment。给变量赋值,是的 Φ \Phi Φ为真。
- 3-SAT: SAT where each clause contains exactly 3 literals。 每个字句有三个文字。
证明3-SAT是NPC问题。(CIRCUIT-SAT ≤ p \leq_p ≤p 3-SAT)
-
该问题显然是存在多项式时间的验证算法,因此是NP问题。
-
-
将CIRCUIT-SAT ≤ p \leq_p ≤p 3-SAT,电路中的或与非,等价的转化为一个或者多个字句都为真。
¬ \neg ¬, x 2 = ¬ x 3 x_2=\neg x_3 x2=¬x3等价于 x 2 ∨ x 3 x_2\vee x_3 x2∨x3, ¬ x 2 ∨ ¬ x 3 \neg x_2 \vee \neg x_3 ¬x2∨¬x3均为真
∨ \vee ∨, x 1 = x 4 ∨ x 5 x_1=x_4\vee x_5 x1=x4∨x5等价于 x 1 ∨ ¬ x 4 x_1\vee \neg x_4 x1∨¬x4, x 1 ∨ ¬ x 5 x_1\vee \neg x_5 x1∨¬x5, ¬ x 5 ∨ x 4 ∨ x 5 \neg x_5\vee x_4\vee x_5 ¬x5∨x4∨x5均为真
∧ \wedge ∧,等价于 ¬ x 0 ∨ x 1 \neg x_0 \vee x_1 ¬x0∨x1, ¬ x 0 ∨ x 2 \neg x_0 \vee x_2 ¬x0∨x2, x 0 ∨ ¬ x 1 ∨ ¬ x 2 x_0\vee \neg x_1\vee \neg x_2 x0∨¬x1∨¬x2均为真。
非常的巧妙,电路的每个操作都可以转化为多个子句为真。 -
对于hard-coded x 5 = 0 x_5=0 x5=0等价于 ¬ x 5 \neg x_5 ¬x5为真, x 0 = 1 x_0=1 x0=1等价于 x 0 x_0 x0为真
-
将小于三个文字的子句等价的转化为三个文字的子句,如何转化?
引入四个变量,保证由着四个变量组成得一些子句全为真时,其中两个一定为零。然后将这两个零变量加入只有一个文字的子句以及多个文字的子句。 -
上述两个例子的实例一个为真可以推出另一个为真。
3.2 Covering problems and Packing problems
3.2.1 indenpendent set 独立集
- Given G ( V , E ) G(V,E) G(V,E) and integer K K K
- S ⊆ V S\subseteq V S⊆V,such that ∣ S ∣ ≥ K |S|\ge K ∣S∣≥K and for each edge at most one of its endpoints is in S S S。对于每条边,最多有一个点在 S S S中。
- 判断是否存在
S
S
S。是否存在大于
K
K
K的点,使得这些点独立(点之间没有边)。
3.2.2 vertex cover 点集覆盖
- Given G ( V , E ) G(V,E) G(V,E) and integer K K K
- S ⊆ V S\subseteq V S⊆V,such that ∣ S ∣ ≤ K |S|\le K ∣S∣≤K and and for each edge, at least one of its endpoints is in S S S。对于每条边至少有一个点在 S S S中。
- 判断是否存在
S
S
S。是否存在小于
K
K
K个点覆盖(只要有一个点在
S
S
S中即可)所有边。
3.2.3 set cover 集合覆盖
- Given a set U, a collection S 1 , S 2 … , S n S_1,S_2 \dots,S_n S1,S2…,Sn and a integer k k k
- Does there exist a collection of ≤ k \leq k ≤k these whose union is equal to U U U?
- 判断是否存在小于
k
k
k个子集
S
i
S_i
Si的并,等价于U。
3.3 Sequencing problems
3.3.1 Directed Hamiltonian Cycle 有向汉密尔顿环
- Given diggraph G ( V , E ) G(V,E) G(V,E)
- Does there exist a simple cycle Γ \Gamma Γ contians every node in V V V
- 判定是否存在简单环 Γ \Gamma Γ 包含所有点。简单环:所有点只经过一次。
3.3.2 Hamiltonian Cycle 汉密尔顿环
- Given graph G ( V , E ) G(V,E) G(V,E)
- Does there exist a simple cycle Γ \Gamma Γ contians every node in V V V
- 判定是否存在简单环
Γ
\Gamma
Γ 包含所有点。简单环:所有点只经过一次。
3.3.3 Traveling Salesperson Problem 旅行商问题
- Given n cities and a pairwise distance d(u,v)。
- Is there a tour of length ≤ D \leq D ≤D
- 判断是否有个tour(经过所有城市并且返回), 长度小于
D
D
D。
3.4 Partitioning problems
3.4.1 graph 3-color 3色问题
- Given a undirected graph G
- exist a way to color the nodes RGB so that no adjacent nodes have the same color.
- 判断对这个无向图是否存在一种涂色,只用三个颜色,使得相邻节点颜色不同。
3.4.2 3D-MATCHING 3维匹配
- Given disjoint sets X , Y X,Y X,Yand Z Z Z。Each of size n n n and a set T ⊆ X × Y × Z T\subseteq X\times Y\times Z T⊆X×Y×Z。
- Is there a set of n n n triples in T, such that each element of X ∪ Y ∪ Z X\cup Y\cup Z X∪Y∪Z is in exactly one of these triples。
- 判断是否存在
n
n
n个元组,使得
X
∪
Y
∪
Z
X\cup Y\cup Z
X∪Y∪Z的元素都被包含一次。
3.5 Numerical problems
3.5.1 SUBSET-SUM 子集求和
- Given natural numbers w 1 , … , w n w_1,\dots,w_n w1,…,wn and integer W W W
- Is there a subset that adds up to exactly W?
- 判定是否存在一个子集使得求和为W
3.5.2 SCHEDULING 调度
- Given a set of n n n jobs with processing time t i t_i ti, release time r i r_i ri, and deadline d i d_i di。
- Is it possible to schedule all jobs on a single machine such that job i i i is processed with a contiguous slot of t i t_i titime units in the interval [ r i , d i ] [r_i, d_i ] [ri,di] 即工作i一发布就做。
- 判断是否是否能完成所有工作,并且工作
i
i
i的处理时间已经规定。
3.6 总结
下图问题全为NPC问题
A
≤
p
B
A\leq_p B
A≤pB将A规约为B,将A转为的B的形式求解。
3.6.1 3-SAT ≡ P \equiv_P ≡P Independent Set将 3-SAT规约为独立集
- 给出3-SAT的实例 Φ \Phi Φ,构造出独立集的实例,如下图所示,选出的节点为真
- 两者为真等价
3.6.2 Vertex cover ≡ P \equiv_P ≡P Independent set 点集覆盖多项式等价与独立集
证明 S 是Independent set当且仅当V-S是vertex cover
- 任意一条边,一定存在一点不在S中,则一定有一点在V-S中。
- S中任意两个点一定不是一条边,即V-S外任意两点不存在边。
3.6.3 Vertex Cover Reduces to Set Cover 点集覆盖规约为集合覆盖
给定一个Vertex Cover的实例 G=(V,E),k,将其构造为 Set Cover的实例(U,S,k)
- U=E,k=k, S v = { e ∈ E : e incident to v } S_v=\{e\in E:\text{e incident to v}\} Sv={e∈E:e incident to v} 每个点有一个子集,子集元素为与改点相连的边。
- Set-cover of size ≤ \leq ≤k iff vertex cover of size ≤ \leq ≤k
3.6.4 3-SAT ≤ P \leq_P ≤P Dir-Ham-Cycle将3-SAT规约为有向哈密尔顿环
- 给定一个3-SAT的实例,构造为有向哈密尔顿环,设计节点以及边。
- 文字由方向决定真假,字句由是否访问决定真假。
3.6.5 Dir-Ham-Cycle ≤ p \leq_p ≤p Ham-cycle 有向哈密尔顿图规约到无向
通过添加节点以及一个节点只能经过一次来控制方向。
- G有解则G’有解
- G’有解则顺序固定 B, G, R,或者B, R, G,则G有解
3.6.6 Ham-cycle ≤ p \leq_p ≤p TSP 哈密尔顿图规约到旅行商
Ham-cycle没有权重,TSP问题有权重以及距离限制。
只需要设置不同权重即可区分。
3.6.7 3-SAT ≤ P \leq_P ≤P 3-color 3-SAT规约为3色问题
- 给定3-SAT实例,构造G(V,E),对文字以及字句分别构造。
3.6.8 3-SAT ≤ P \leq_P ≤P SUBSET-SUM 3-SAT规约为子集求和问题
- 通过设定求和的值来判断是否SAT。
3.6.9 SUBSET-SUM ≤ P \leq_P ≤P Scheduling 子集求和规约为规划问题
- 设定W的值即可。
4 co-NP与NPC
-
co-NP:NP问题的补问题。
-
-
-
-
一个例子
-
-
-
所有NP问题可以规约为NPC问题。
-
所有问题可以规约为NP-hard问题