78
3- 1 Chapter 3 The Theory of NP- Completeness

3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

Embed Size (px)

Citation preview

Page 1: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 1

Chapter 3

The Theory of NP-Completeness

Page 2: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 2

What are the fundamental capabilities and limitations of computers?

What makes some problems computationally hard and others easy?

Page 3: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 3

Example1 main() { printf(“hello, word\n”); }

Example 2: int exp(int i,n){ int ans,j; ans=1; for(j=1;j<=n;j++)ans*=i; return ans;}

Page 4: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 4

main(){ int n,total,x,y,z; scanf("d",&n); total=3; while(1){ for(x=1;x<=total-2;x++) for(y=1;y<=total-x-1;y++){ z=total-x-y; if(exp(x,n)+exp(y,n)==exp(z,n)) printf("hello word\n"); }//for total++; }//while

}

Page 5: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 5

NP-Completeness Some problems are intractable:

as they grow large, we are unable to solve them in reasonable time

What constitutes reasonable time? Standard working definition: polynomial time On an input of size n the worst-case running

time is O(nk) for some constant k Polynomial time: O(n2), O(n3), O(1), O(n lg n) Not in polynomial time: O(2n), O(nn), O(n!)

Page 6: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 6

Polynomial-Time Algorithms

Are some problems solvable in polynomial time? Of course: every algorithm we’ve studied provides

polynomial-time solution to some problem We define P to be the class of problems solvable in

polynomial time Are all problems solvable in polynomial time?

No: Turing’s “Halting Problem” is not solvable by any computer, no matter how much time is given

Such problems are clearly intractable, not in P

Page 7: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 7

NP-Complete Problems

The NP-Complete problems are an interesting class of problems whose status is unknown No polynomial-time algorithm has been

discovered for an NP-Complete problem No suprapolynomial lower bound has been

proved for any NP-Complete problem, either We call this the P = NP question

The biggest open problem in CS

Page 8: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 8

P and NP

As mentioned, P is set of problems that can be solved in polynomial time

NP (nondeterministic polynomial time) is the set of problems that can be solved in polynomial time by a nondeterministic computer What the hell is that?

Page 9: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 9

Nondeterminism

Think of a non-deterministic computer as a computer that magically “guesses” a solution, then has to verify that it is correct If a solution exists, computer always guesses it One way to imagine it: a parallel computer that can

freely spawn an infinite number of processes Have one processor work on each possible solution All processors attempt to verify that their solution works If a processor finds it has a working solution

So: NP = problems verifiable in polynomial time

Page 10: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 10

Nondeterministic operations and functions

[Horowitz 1998] Choice(S) : arbitrarily chooses one of the elements

in set S Failure : an unsuccessful completion Success : a successful completion Nonderministic searching algorithm: j ← choice(1 : n) /* guessing */

if A(j) = x then success /* checking */ else failure

Page 11: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 11

A nondeterministic algorithm terminates unsuccessfully iff there exist no a set of choices leading to a success signal.

The time required for choice(1 : n) is O(1). A deterministic interpretation of a non-deterministic

algorithm can be made by allowing unbounded parallelism in computation.

Page 12: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 12

Nondeterministic sorting

B ← 0 /* guessing */for i = 1 to n do

j ← choice(1 : n) if B[j] ≠ 0 then failure B[j] = A[i]

/* checking */for i = 1 to n-1 do

if B[i] > B[i+1] then failure success

Page 13: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 13

Nondeterministic SAT

/* guessing */

for i = 1 to n do

xi ← choice( true, false )

/* checking */

if E(x1, x2, … ,xn) is true then success

else failure

Page 14: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 14

P and NP Summary so far:

P = problems that can be solved in polynomial time

NP = problems for which a solution can be verified in polynomial time

Unknown whether P = NP (most suspect not) Hamiltonian-cycle problem is in NP:

Cannot solve in polynomial time Easy to verify solution in polynomial time

(How?)

Page 15: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 15

NP-Complete Problems We will see that NP-Complete problems are

the “hardest” problems in NP: If any one NP-Complete problem can be solved in

polynomial time… …then every NP-Complete problem can be solved

in polynomial time… …and in fact every problem in NP can be solved

in polynomial time (which would show P = NP) Thus: solve hamiltonian-cycle in O(n100) time,

you’ve proved that P = NP. Retire rich & famous.

Page 16: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 16

Reduction The crux of NP-Completeness is reducibility

Informally, a problem P can be reduced to another problem Q if any instance of P can be “easily rephrased” as an instance of Q, the solution to which provides a solution to the instance of P What do you suppose “easily” means? This rephrasing is called transformation

Intuitively: If P reduces to Q, P is “no harder to solve” than Q

Page 17: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 17

Reducibility

An example: P: Given a set of Booleans, is at least one TRUE? Q: Given a set of integers, is their sum positive? Transformation: (x1, x2, …, xn) = (y1, y2, …, yn) where

yi = 1 if xi = TRUE, yi = 0 if xi = FALSE

Another example: Solving linear equations is reducible to solving

quadratic equations How can we easily use a quadratic-equation solver to solve

linear equations?

Page 18: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 18

Using Reductions

If P is polynomial-time reducible to Q, we denote this P p Q

Definition of NP-Complete: If P is NP-Complete, P NP and all problems R

are reducible to P Formally: R p P R NP

If P p Q and P is NP-Complete, Q is also NP-Complete This is the key idea you should take away today

Page 19: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 19

An Aside: Terminology

To simplify things, we will worry only about decision problems with a yes/no answer Many problems are optimization problems,

but we can often re-cast those as decision problems

Page 20: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 20

Decision problems Decision version of sorting:

Given a1, a2,…, an and c, is there a permutation of ai

s ( a1, a2

, … ,an ) such

that∣a2–a1

∣+∣a3–a2

∣+ … +∣an–an-1

∣< c ? Not all decision problems are NP problems

E.g. halting problem : Given a program with a certain input data,

will the program terminate or not? NP-hard Undecidable

Page 21: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 21

NP-Hard and NP-Complete

If P is polynomial-time reducible to Q, we denote this P p Q

Definition of NP-Hard and NP-Complete: If all problems R NP are reducible to P, then

P is NP-Hard We say P is NP-Complete if P is NP-Hard

and P NP If P p Q and P is NP-Complete, Q is also

NP- Complete

Page 22: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 22

Why Prove NP-Completeness?

Though nobody has proven that P != NP, if you prove a problem NP-Complete, most people accept that it is probably intractable

Therefore it can be important to prove that a problem is NP-Complete Don’t need to come up with an efficient

algorithm Can instead work on approximation

algorithms

Page 23: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 23

Proving NP-Completeness

What steps do we have to take to prove a problem P is NP-Complete? Pick a known NP-Complete problem Q Reduce Q to P

Describe a transformation that maps instances of Q to instances of P, s.t. “yes” for P = “yes” for Q

Prove the transformation works Prove it runs in polynomial time

Oh yeah, prove P NP (What if you can’t?)

Page 24: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 24

The SAT Problem

One of the first problems to be proved NP-Complete was satisfiability (SAT): Given a Boolean expression on n variables, can

we assign values such that the expression is TRUE?

Ex: ((x1 x2) ((x1 x3) x4)) x2

Cook’s Theorem: The satisfiability problem is NP-Complete Note: Argue from first principles, not

reduction Proof: not here

Page 25: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 25

Conjunctive Normal Form Even if the form of the Boolean expression is

simplified, the problem may be NP-Complete Literal: an occurrence of a Boolean or its negation A Boolean formula is in conjunctive normal form, or

CNF, if it is an AND of clauses, each of which is an OR of literals Ex: (x1 x2) (x1 x3 x4) (x5)

3-CNF: each clause has exactly 3 distinct literals Ex: (x1 x2 x3) (x1 x3 x4) (x5 x3 x4) Notice: true if at least one literal in each clause is true

Page 26: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 26

3-satisfiability problem (3-SAT)

Def: Each clause contains exactly three literals. (I)  3-SAT is an NP problem (obviously) (II) SAT 3-SAT Proof:

(1) One literal L1 in a clause in SAT :in 3-SAT :

L1 v y1 v y2

L1 v -y1 v y2

L1 v y1 v -y2

L1 v -y1 v -y2

Page 27: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 27

(2) Two literals L1, L2 in a clause in SAT :in 3-SAT :

L1 v L2 v y1

L1 v L2 v -y1

(3) Three literals in a clause : remain unchanged.

(4) More than 3 literals L1, L2, …, Lk in a clause :in 3-SAT :

L1 v L2 v y1

L3 v -y1 v y2

Lk-2 v -yk-4 v yk-3

Lk-1 v Lk v -yk-3

Page 28: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 28

The instance S in 3-SAT :

x1 v x2 v y1

x1 v x2 v -y1

-x3 v y2 v y3

-x3 v -y2 v y3

-x3 v y2 v -y3

-x3 v -y2 v -y3

x1 v -x2 v y4

x3 v -y4 v y5

-x4 v x5 v -y5

an instance S in SAT :

x1 v x2

-x3

x1 v -x2 v x3 v -x4 v x5

SAT transform 3-SAT S S

Example of transforming 3-SAT to SAT

Page 29: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 29

Proof : S is satisfiable S is satisfiable “” 3 literals in S (trivial) consider 4 literals

S : L1 v L2 v … v Lk

S: L1 v L2 v y1

L3 v -y1 v y2

L4 v -y2 v y3

Lk-2 v -yk-4 v yk-3

Lk-1 v Lk v -yk-3

Page 30: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 30

S is satisfiable at least Li = T

Assume : Lj = F j i

assign : yi-1 = F yj = T j i-1

yj = F j i-1

( Li v -yi-2 v yi-1 ) S is satisfiable.

“” If S is satisfiable, then assignment satisfying

S can not contain yi’s only.

at least Li must be true. (We can also apply the resolution principle).

Thus, 3-SAT is NP-complete.

Page 31: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 31

Comment for 3-SAT

If a problem is NP-complete, its special cases may or may not be NP-complete.

The reason we care about the 3-CNF problem is that it is relatively easy to reduce to others Thus by proving 3-CNF NP-Complete we can

prove many seemingly unrelated problems NP-Complete

Page 32: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 32

3-CNF Clique

What is a clique of a graph G? A: a subset of vertices fully connected to

each other, i.e. a complete subgraph of G The clique problem: how large is the

maximum-size clique in a graph? Can we turn this into a decision problem? A: Yes, we call this the k-clique problem Is the k-clique problem within NP?

Page 33: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 33

3-CNF Clique

What should the reduction do? A: Transform a 3-CNF formula to a

graph, for which a k-clique will exist (for some k) iff the 3-CNF formula is satisfiable

Page 34: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 34

3-CNF Clique

The reduction: Let B = C1 C2 … Ck be a 3-CNF formula with k

clauses, each of which has 3 distinct literals For each clause put a triple of vertices in the graph,

one for each literal Put an edge between two vertices if they are in

different triples and their literals are consistent, meaning not each other’s negation

Run an example: B = (x y z) (x y z ) (x y z )

Page 35: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 35

3-CNF Clique

Prove the reduction works: If B has a satisfying assignment, then each clause

has at least one literal (vertex) that evaluates to 1 Picking one such “true” literal from each clause

gives a set V’ of k vertices. V’ is a clique (Why?) If G has a clique V’ of size k, it must contain one

vertex in each triple (clause) (Why?) We can assign 1 to each literal corresponding

with a vertex in V’, without fear of contradiction

Page 36: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 36

Clique Vertex Cover

A vertex cover for a graph G is a set of vertices incident to every edge in G

The vertex cover problem: what is the minimum size vertex cover in G?

Restated as a decision problem: does a vertex cover of size k exist in G?

Thm : vertex cover is NP-Complete

Page 37: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 37

Clique Vertex Cover

First, show vertex cover in NP (How?) Next, reduce k-clique to vertex cover

The complement GC of a graph G contains exactly those edges not in G

Compute GC in polynomial time G has a clique of size k iff GC has a

vertex cover of size |V| - k

Page 38: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 38

Clique Vertex Cover

Claim: If G has a clique of size k, GC has a vertex cover of size |V| - k Let V’ be the k-clique Then V - V’ is a vertex cover in GC

Let (u,v) be any edge in GC

Then u and v cannot both be in V’ (Why?) Thus at least one of u or v is in V-V’ (why?), so

edge (u, v) is covered by V-V’ Since true for any edge in GC, V-V’ is a vertex

cover

Page 39: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 39

Clique Vertex Cover

Claim: If GC has a vertex cover V’ V, with |V’| = |V| - k, then G has a clique of size k For all u,v V, if (u,v) GC then u V’ or

v V’ or both (Why?) Contrapositive: if u V’ and v V’, then

(u,v) E In other words, all vertices in V-V’ are

connected by an edge, thus V-V’ is a clique Since |V| - |V’| = k, the size of the clique is k

Page 40: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 40

Sum of subsets problem

Def: A set of positive numbers A = { a1, a2, …, an }

a constant C

Determine if A A ai = C

e.g. A = { 7, 5, 19, 1, 12, 8, 14 } C = 21, A = { 7, 14 } C = 11, no solution

a Ai

Page 41: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 41

子集问题 Subset-Sum in NP

t}y that such

SR subset a is there

x,...,xS|tS,{SUM-SUBSET

Ry

k1

商品价格口袋中零钱

子集和 问题是 NP 的。 S 有 2|S| 个子集,猜—测法 或排序后一个个测试,似乎要指数时间。

不确定算法如下

并行调度 do // 不确定

{ 选择 S 的一个子集,记为 c, (猜 , P-time )

return (C 中元素的和 == t) ; ( 试 ,P-time )

} while(! finish) 下面要证明它是 NP- 完全的

Page 42: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 42

Reducing 3SAT to SubSet Sum

Proof idea: 把 3SAT 多项式 归约 为 不找补 购物 1. 设 3SAT 已经定义好 2 选 S 的子集 ,对应于 3SAT 的合取范式( P-time

完成) 3 不同的和 对应 不同的子句 4 当和 =t ( 不找补 购物), 找到 3SAT 的满足指

派 这样 可以 不找补 购物 3SAT 可满足 作法: 设合取范式已经有了,下面要找一些 10 进位数, yi, zi, hi, gi 和 总和 t

Page 43: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 43

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0 0

1 0 0 1 1 0 0

1 0 0 0 0 1 0

1 0 1 0 0 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0 1

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1 0

1

1

)xxx(

)xxx(

)xxx(

)xxx(

431

322

421

321

+1 1 1 1 3 3 3 3

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4

1 2 3 4子句 对应于 10 进位数字

3CNF formula:

Xk 对应与 YK

~ XK 对应 ZK

子句 c1 中至少一真,要使和为 3, 可能要补差额 2 ,这里备用同上,为子句 c4 备用补差

Dummies凑数的硬币

子句c1C2C3c4 这页看不懂

看下页

子句号数

Page 44: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 44

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0 0

1 0 0 1 1 0 0

1 0 0 0 0 1 0

1 0 1 0 0 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0 1

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1 0

1

1

)xxx(

)xxx(

)xxx(

)xxx(

431

322

421

321

+1 1 1 1 3 3 3 3

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4

1 2 3 4子句 对应于 10 进位数字

3CNF formula:

子句 c1 中至少一真,要使和为 3, 可能要补差额 2 ,这里备用同上,为子句 c4 备用补差

dummies

Ep270Cp179

子句c1C2C3c4

子句号数

2n 和 2n+1行中只保留一行,根据赋值选留,留下的每行10 进位数是

子集,

•和,是 10进位数

Page 45: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 45

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0 0

1 0 0 1 1 0 0

1 0 0 0 0 1 0

1 0 1 0 0 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0 1

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1 0

1

1

)xxx(

)xxx(

)xxx(

)xxx(

431

322

421

321

+1 1 1 1 3 3 3 3

+x1

y1

–x1 Z1

+x2 y2

–x2 Z2

+x2

y2

–x 3 Z3

+x4

y4

–x4 Z4

1 2 3 4子句号数 变量与数表的对应

3CNF formula:

前 L 个 1, 后 K 个 31 表示每个 Xi (或 ~Xi )出现。

给定合取式,造表只要多项式 时间 O((L+k)2)

Dummies后面的 gi

hi

表示 ~x1 出现在子句 2 中

表示 ~x4 出现在子句 C4中

Page 46: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 46

Subset Sum )xxx()xxx(

)xxx()xxx(

431322

421321

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0 0

1 0 0 1 1 0 0

1 0 0 0 0 1 0

1 0 1 0 0 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0 1

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1 0

1

11 1 1 1 3 3 3 3

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4

Note 1: The “1111”每列加起来为 1, 表示同一子句中 Xi 和 ~xi 中只用了其中为真的那一行,

Note 2: 这块每列和为 1—3, 表示每子句三个元中至少一个为真,

适当补充若干 gi hi, 可使得每列和为 3

Page 47: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 47

Subset Sum )xxx()xxx(

)xxx()xxx(

431322

421321

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0 0

1 0 0 1 1 0 0

1 0 0 0 0 1 0

1 0 1 0 0 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0 1

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1 0

1

11 1 1 1 3 3 3 3

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4

Note 1: The “1111”每列加起来为 1, 表示同一子句中 Xi 和 ~xi 中只用了其中为真的那一行,

Note 2: 这块每列和为 1—3, 表示每子句三个元中至少一个为真,

适当补充若干 gi hi, 可使得每列和为 3

Page 48: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 48

Subset Sum )xxx()xxx(

)xxx()xxx(

431322

421321

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0 0

1 0 0 1 1 0 0

1 0 0 0 0 1 0

1 0 1 0 0 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0 1

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1 0

1

11 1 1 1 3 3 3 3

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4

Note 1: The “1111”每列加起来为 1, 表示同一子句中 Xi 和 ~xi 中只用了一个

Note 2: 这块每列和为 1—3, 表示每子句三个元中至少一个为真,

适当补充若干 gi hi, 可使得每列和为 3凑数的硬币

Page 49: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 49

设合取式满足,可选出 xi yi 如下其和为最后一行,对应一个子集和

4321 x,x,x,x 是一个可 满足的赋值

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0 0

1 0 0 1 1 0 0

1 0 0 0 0 1 0

1 0 1 0 0 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0 1

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1 0

1

11 1 1 1 3 3 3 3

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1

1 1 1 1 3 3 3 3

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4

+

用这里补差额

总和

•根据赋值, 选出来的行, 10进位数(不是二进制,才能有 3 )

4321 x,x,x,x

Page 50: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 50

不满足加不够3

4321 x,x,x,x is not a satisfying assignment

)xx(x)xxx(

)xxx()xx(x

431322

421321

1 0 0 0 1 0 0 1

1 0 0 0 0 1 0 0

1 0 0 1 1 0 0

1 0 0 0 0 1 0

1 0 1 0 0 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0 1

1 0 0 0

1 0 0 0

1 0 0

1 0 0

1 0

1 0

1

11 1 1 1 3 3 3 3

1 0 0 0 0 1 0 0

1 0 0 0 0 1 0

1 0 0 0 1 1

1 0 1 0 0

1 0 0 0

1 0 0 0

? ? ?

1 1 1 1 2 ? ? ?

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4+

子句 C1 不满足,此列上面部分 无 1, 全部加起来为 2

Page 51: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 51

Partition problem

Def: Given a set of positive numbers A = { a1,a2,…,an },

determine if a partition P, ai = ai ip ip

e. g. A = {3, 6, 1, 9, 4, 11} partition : {3, 1, 9, 4} and {6, 11}

<Theorem> sum of subsets partition

Page 52: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 52

Sum of subsets partition

proof : instance of sum of subsets : A = { a1, a2, …, an }, C instance of partition : B = { b1, b2, …, bn+2 }, where bi = ai, 1 i n bn+1 = C+1 bn+2 = ( ai )+1 C 1in

C = ai ( ai )+bn+2 = ( ai )+bn+1 ai

S aiS ai

S

partition : { bi aiS {bn+2}

and { bi aiS }{bn+1}

S

S’

A C

Page 53: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 53

Why bn+1 = C+1 ? why not bn+1 = C ? To avoid bn+1 and bn+2 to be

partitioned into the same subset.

Page 54: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 54

Bin packing problem

Def: n items, each of size ci , ci > 0

bin capacity : C Determine if we can assign the items into

k bins, ci C , 1jk. ibinj

<Theorem> partition bin packing.

Page 55: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 55

0/1 knapsack problem Def: n objects, each with a weight wi > 0

a profit pi > 0 capacity of knapsack : M

Maximize pixi 1in

Subject to wixi M 1in

xi = 0 or 1, 1 i n Decision version :

Given K, pixi K ? 1in

Knapsack problem : 0 xi 1, 1 i n.<Theorem> partition 0/1 knapsack decision problem.

Page 56: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 56

An NP-Complete Problem:Hamiltonian Cycles

An example of an NP-Complete problem: A hamiltonian cycle of an undirected

graph is a simple cycle that contains every vertex

The hamiltonian-cycle problem: given a graph G, does it have a hamiltonian cycle?

Page 57: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 57

Directed Hamiltonian Path 问题 Given a directed graph G, does there

exist a Hamiltonian path, which visits all nodes exactly once?

问题 能沿箭头不走重复路 游完各景点吗(不必走遍所有小道)?

Two Examples:

this graph has aHamiltonian path

but this graph has noHamiltonian path

Page 58: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 58

3SAT to Hamiltonian Path

Proof idea: Given a 3CNF , make a graph G such that

对给定 3 合取范式,造图 G , 使得

… 有 Hamiltonian path ,有 zig-zag path through G

… where the directions (zig or zag) determine the assignments (true or false) of the variables x1,…,xL of .

Page 59: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 59

大致思路,帆船游水乡设合取式如右: )xxx()xxx(

)xxx()xxx(

431322

421321

4 变量的对 T 的指派 T T T T使子句 Ck 满足,才连接到 CK 的 Hub 线,顺应风向

4321 x,x,x,x

s

合取范式 与Hamiltonian 路对应 .

可沿红线游完所有景点

t

)xxx( 321

)xxx( 322

)xxx( 421

)xxx( 431

1x

4x

3x

2x

码头也是景点 4 个子句

•重要景点,但只有单行道,控制了风向

Page 60: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 60

“ Zig-Zag Graphs” t 个变量一个合取式子句有2t 种赋值组合,用 2t 路径来对应

s

t

1x

Lx

2x

每层一分为二(左路 右路), t 层 2t 种路径。比喻 从北往南 行帆船,只有东西风,第 i 层的风向确定如下:当满足时:如 xi=True 西风, xi=False 东风

For every i, “Zig” 左转 右转 means xi=True 西风

ix

… while “Zag” 右转 左转 means xi=False 东风

ix

Page 61: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 61

Accessing the Clauses k 个子句的与运算如何表示?

If has K clauses, = C1 Ck,在菱形的中横上加节点then xi has K “hubs”:

stands for

ix

1 K2ix

Idea: Make K extranodes Cj that can onlybe visited via the hub-path that defines anassignment of xi whichsatisfies the clause(using zig/zag = T/F).

这些双向的最后只留单向,子句中不出现的变量被跳过

Page 62: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 62

Connecting the Clauses 与 Hub 中子句的连接

jC

ixj

jC

ixj

Let the clause Cj contain xi:

If then xi=True=“zig”reaches node Cj

)zyx(C ij

If then xi=False=“zag”reaches node Cj

)zyx(C ij

大潮流:西风 左转 右转

大潮流东风 右转 左转

Xi 在 Cj 中出现

~ Xi 在 Cj 中出现

•单行道到半山亭,控制风向

Page 63: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 63

Proof 3SAT P HamPath Given a 3CNF (x1,…,xL) with K clauses Make graph G with a zig/zag levels for every

xi

对应与合取范式,作图 ( 多项式时间可以作出) Connect the clauses Cj via the proper “hubs” 用 hub 连接子句 If SAT, 满足的指派 Hamiltonian path,

下页 用例子说明

HamPathG ifonly and if SAT

Page 64: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 64

思路 合取范式如右: )xxx()xxx(

)xxx()xxx(

431322

421321

4 变量…

)xxx( 321

)xxx( 322

)xxx( 421

)xxx( 431

s

1x

4x

3x

2x

t

4 子句

子句通过 hub连接菱形网络

Page 65: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 65

Example )xxx()xxx(

)xxx()xxx(

431322

421321

T T T T由此可定出各层 东西风向, 指派 使子句 Ck

满足,才连接到 CK 的Hub 线,要顺应风向

4321 x,x,x,x s

…satifies all four clauses; hence it defines a Hamiltonian Path沿红线游完景点不走重复路

t

)xxx( 321

)xxx( 322

)xxx( 421

)xxx( 431

1x

4x

3x

2x

满足时, X1X4

为真,西风

Page 66: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 66

Example )xxx()xxx(

)xxx()xxx(

431322

421321

T T T T由此可定出各层 东西风向, 指派 使子句 Ck

满足,才连接到 CK 的Hub 线,要顺应风向

4321 x,x,x,x s

…satifies all four clauses; hence it defines a Hamiltonian Path沿红线游完景点不走重复路

t

)xxx( 321

)xxx( 322

)xxx( 421

)xxx( 431

1x

4x

3x

2x

满足时, X1X4 为真, 1,4 层西风

满足时, X2 X3 为假, 2,3 层东风

Page 67: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 67

Example )xxx()xxx(

)xxx()xxx(

431322

421321

T T T T由此可定出各层 东西风向, 指派 使子句 Ck

满足,才连接到 CK 的Hub 线,要顺应风向

4321 x,x,x,x s

… 指派满足 4 非子句,每个码头都游遍了。沿红线游完景点不走重复路

t

)xxx( 321

)xxx( 322

)xxx( 421

)xxx( 431

1x

4x

3x

2x

满足时, X1X4

为真,西风

Page 68: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 68

Example )xxx()xxx(

)xxx()xxx(

431322

421321

T T T T 指派不满足 C1 。无法连接 hub 线,这一景点被错过注意,连接HUB 的单行道控制了风向

4321 x,x,x,xs

t

1x

4x

3x

2x

)xxx( 321

)xxx( 322

)xxx( 421

)xxx( 431

Page 69: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 69

Directed Hamiltonian Cycle

Undirected Hamiltonian Cycle

What was the hamiltonian cycle problem again?

For my next trick, I will reduce the directed hamiltonian cycle problem to the undirected hamiltonian cycle problem before your eyes Which variant am I proving NP-Complete?

Draw a directed example on the board What transformation do I need to effect?

Page 70: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 70

Transformation:Directed Undirected Ham.

Cycle

Transform graph G = (V, E) into G’ = (V’, E’): Every vertex v in V transforms into 3 vertices

v1, v2, v3 in V’ with edges (v1,v2) and (v2,v3) in E’ Every directed edge (v, w) in E transforms into the

undirected edge (v3, w1) in E’ (draw it) Can this be implemented in polynomial time? Argue that a directed hamiltonian cycle in G implies

an undirected hamiltonian cycle in G’ Argue that an undirected hamiltonian cycle in G’

implies a directed hamiltonian cycle in G

Page 71: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 71

Undirected Hamiltonian Cycle

Thus we can reduce the directed problem to the undirected problem

What’s left to prove the undirected hamiltonian cycle problem NP-Complete?

Argue that the problem is in NP

Page 72: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 72

Directed Undirected Ham. Cycle

Prove the transformation correct: If G has directed hamiltonian cycle, G’ will have

undirected cycle (straightforward) If G’ has an undirected hamiltonian cycle, G will

have a directed hamiltonian cycle The three vertices that correspond to a vertex v in G must

be traversed in order v1, v2, v3 or v3, v2, v1, since v2 cannot be reached from any other vertex in G’

Since 1’s are connected to 3’s, the order is the same for all triples. Assume w.l.o.g. order is v1, v2, v3.

Then G has a corresponding directed hamiltonian cycle

Page 73: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 73

Hamiltonian Cycle TSP The well-known traveling salesman problem:

Optimization variant: a salesman must travel to n cities, visiting each city exactly once and finishing where he begins. How to minimize travel time?

Model as complete graph with cost c(i,j) to go from city i to city j

How would we turn this into a decision problem? A: ask if a TSP with cost < k

Page 74: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 74

Hamiltonian Cycle TSP

The steps to prove TSP is NP-Complete: Prove that TSP NP (Argue this) Reduce the undirected hamiltonian cycle

problem to the TSP So if we had a TSP-solver, we could use it to

solve the hamilitonian cycle problem in polynomial time

How can we transform an instance of the hamiltonian cycle problem to an instance of the TSP?

Can we do this in polynomial time?

Page 75: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 75

Proof of Hamiltonian TSP

2

1

Page 76: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 76

The TSP

Random asides: TSPs (and variants) have enormous

practical importance E.g., for shipping and freighting

companies Lots of research into good approximation

algorithms Recently made famous as a DNA

computing problem

Page 77: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 77

General Comments

Literally hundreds of problems have been shown to be NP-Complete

Some reductions are profound, some are comparatively easy, many are easy once the key insight is given

You can expect a simple NP-Completeness proof on the final

Page 78: 3- 1 Chapter 3 The Theory of NP-Completeness. 3- 2 What are the fundamental capabilities and limitations of computers? What makes some problems computationally

3- 78

Any Question ?

Thank you !!!Thank you !!!