34.NP Completeness

92
34.NP Completeness Hsu, Lih-Hsing

description

34.NP Completeness. Hsu, Lih-Hsing. 為何學 NP-Complete. 50 年前的電腦 ( 一間房間大 ) 可解的問題的極限與現在電腦 ( 個人電腦 ) 可解的極限理論上是相同的 , 除非電腦的製程有重大突破 ( 即不是用目前半導體技術 ) 。 請問 C , C++ , java, Which one is best? 學習的態度是如果你發現你的問題是屬於 NP-C 則大部份的努力的方向是要放在找 Approximate algorithm 而不是 fast algorithm. P and NP. - PowerPoint PPT Presentation

Transcript of 34.NP Completeness

Page 1: 34.NP Completeness

34.NP Completeness

Hsu, Lih-Hsing

Page 2: 34.NP Completeness

Chapter 34 P.2

Computer Theory Lab.

為何學 NP-Complete 50 年前的電腦 ( 一間房間大 ) 可解的問題的極

限與現在電腦 ( 個人電腦 ) 可解的極限理論上是相同的 , 除非電腦的製程有重大突破 ( 即不是用目前半導體技術 ) 。

請問 C , C++ , java, Which one is best? 學習的態度是如果你發現你的問題是屬於 NP-

C 則大部份的努力的方向是要放在找 Approximate algorithm 而不是 fast algorithm.

Page 3: 34.NP Completeness

Chapter 34 P.3

Computer Theory Lab.

P and NP

Non-formal description P: solvable polynomial time NP:

nondeterministic polynomial time Verifiable in polynomial time by

deterministic Turing machine.

Page 4: 34.NP Completeness

Chapter 34 P.4

Computer Theory Lab.

NP-complete NP-Complete: No polynomial-time

algorithm has yet been discovered for an NP-computer problem, nor has anyone yet been able to prove that no polynomial-time algorithm can exist for any one of them.

Page 5: 34.NP Completeness

Chapter 34 P.5

Computer Theory Lab.

千禧年大獎問題 2000 年 5 月 7 大問題懸賞求解 100 萬美元。由美國

克萊數學院提供 P 與 NP

Page 6: 34.NP Completeness

Chapter 34 P.6

Computer Theory Lab.

Polynomial time algorithms: on inputs of size n, their

worst-case running time is )( knO .

It is natural to wonder whether all problems can be solved

in polynomial time. The answer is no. For example, the

Halting Problem.

Given a description of a program and a finite input, decide whether the program Finishes running or will run forever.

Page 7: 34.NP Completeness

Chapter 34 P.7

Computer Theory Lab.

Generally, we think of problems that are solvable by

polynomial-time algorithms are being tractable, and problems

that requires superpolynomial time are being intractable.

Polynomial Intractables

NP-CompleteProblem

?

Page 8: 34.NP Completeness

Chapter 34 P.8

Computer Theory Lab.

The subject of this chapter, however, is an interesting class of problems, called the “ NP-complete” problems, whose status is unknown. No polynomial-time algorithm has yet been discovered for an NP-computer problem, nor has anyone yet been able to prove that no polynomial-time algorithm can exist for any one of them. This so-called P ≠ NP question has been one of the deepest, most perplexing open research problems in theoretical computer science since it was first posed in 1971.

Page 9: 34.NP Completeness

Chapter 34 P.9

Computer Theory Lab.

NP-complete problem: status are unknown.

If any single NP-complete problem can be solved in

polynomial time, then every NP-complete problem has a

polynomial time algorithm.

To become a good algorithm designer, you must

understand the rudiments of the theory of

NP-completeness.

Page 10: 34.NP Completeness

Chapter 34 P.10

Computer Theory Lab.

The difference between these problems

Shortest vs. longest simple paths: Euler tour vs. hamiltonian cycle: 2-CNF satisfiability vs. 3 CNF satisfiablility NP-completeness and the classes P and NP Overview of showing problems to be NP-compl

ete Decision problems vs. optimization problems

Page 11: 34.NP Completeness

Chapter 34 P.11

Computer Theory Lab.

Reductions

Suppose that there is a different decision problem, say B, that we already know how to solve in polynomial time. Finally, suppose that we have a procedure that transforms any instance of A into some instance of B with the following characteristics:1.The transformation takes polynomial time.

2.The answer are the same. That is, the answer for is “yes” if and only if the answer for is also “yes.”

Page 12: 34.NP Completeness

Chapter 34 P.12

Computer Theory Lab.

We can call such a procedure a polynomial-time reduction algorithm and, it provides us a way to solve problem A in polynomial time:1.Given an instance of problem A, use a polynomial-

time reduction algorithm to transform it to an instance of problem B.

2.Run the polynomial-time decision algorithm for B on the instance .

3.Use the answer for as the answer for .

Polynomial-time reduction algorithm

Polynomial-time algorithm to decide A

Polynomial-timeAlgorithm to decide B

yes yes

no no

Page 13: 34.NP Completeness

Chapter 34 P.13

Computer Theory Lab.

A First NP-complete problem

Because the technique of reduction relies on having a problem already known to be NP-complete in order to prove a different problem NP-complete, we need a “first” NPC problem.

Circuit-satisfiability problem

Page 14: 34.NP Completeness

Chapter 34 P.14

Computer Theory Lab.34.1 Polynomial time

Polynomial time solvable problem are regarded as tractable.

Even if the current best algorithm for a problem has a

running time of (n100), it is likely that an algorithm with

a much better running time will soon be discovered.

Problems for many reasonable models of computation,

can be solved in one model can be solved in polynomial

in another.

Polynomial-time solvable problems has a nice closure

property.

f g are polynomial

f g is also polynomial

,

( )

Page 15: 34.NP Completeness

Chapter 34 P.15

Computer Theory Lab.

Abstract Problems: An abstract problem Q is a binary

relation on a set of problem instances and a set S of

problem solutions.

Decision problems: those having yes/no solution.

Optimization problems: recast by imposing a bound on the

value to be optimized.

An encoding of a set S of abstract objects is a mapping e

from S to the set of binary string, for example:

{0,1,2,3,…}={0,1,10,11,…}

Page 16: 34.NP Completeness

Chapter 34 P.16

Computer Theory Lab.

We call a problem whose instance sets is the set of binary

strings a concrete problem.

We say that an algorithm solves a concrete problem in time

O T n( ( )) if when it is provided a problem instance i if

length n=|i|, the algorithm can produce the solution in at

most O T n( ( )) time.

Page 17: 34.NP Completeness

Chapter 34 P.17

Computer Theory Lab.

A concrete problem is polynomial-time solvable if there

exists an algorithm to solve it in time O nk( ) for some

constant k.

The complexity class P is the set of concrete decision

problems that are solvable in polynomial time.

Page 18: 34.NP Completeness

Chapter 34 P.18

Computer Theory Lab.

Abstract problem concrete problem

e Iencoding

: { , } * 0 1

Problem input k complexity O(k)

unary k 11 1... ( )k

binary n k lg ( ) ( )k n 2

Page 19: 34.NP Completeness

Chapter 34 P.19

Computer Theory Lab.

We say that a function f : { , }* { , } *0 1 0 1 is

polynomial-time computable if there exists a

polynomial-time algorithm A that given any x { , } *0 1 ,

produces as output f x( ).

Page 20: 34.NP Completeness

Chapter 34 P.20

Computer Theory Lab.

For any set I of problem instances, we say that two

encodings e1 and e2 are polynomial related if there exist

two polynomial-time computable functions f12 and f21

such that for any i I , we have f e i e i12 1 2( ( )) ( ) and

f e i e i21 2 1( ( )) ( ) .

Page 21: 34.NP Completeness

Chapter 34 P.21

Computer Theory Lab.

Lemma 34.1. Let Q be an abstract decision problem on

an instance set I, let e1 and e2 be polynomially related

encodings on I. Then e Q P1( ) if and only if e Q P2( ) .

Using reasonable encoding to neglect the distinction

between abstract and concrete problems.

Page 22: 34.NP Completeness

Chapter 34 P.22

Computer Theory Lab.

A fo rm a l- la n g u a g e f ra m e w o rk A n a lp h a b e t i s a f in i te s e t o f s y m b o ls . A la n g u a g e L o v e r i s a n y s e t o f s t r in g s m a d e u p o f

s y m b o ls f ro m . e m p ty s tr in g : . e m p ty la n g u a g e : .

* L e t L L1 2, b e tw o la n g u a g e s . W e c a n d e f in e

Page 23: 34.NP Completeness

Chapter 34 P.23

Computer Theory Lab.

...}{*

: of star) (Kleen closure The

tion)(concatena

}|{

t)(complemen

ion)(intersect

(union)

32

22112121

21

21

LLLL

L

LxandLxxxLL

L

LL

LL

Page 24: 34.NP Completeness

Chapter 34 P.24

Computer Theory Lab.

The set of instances of any decision problem Q is the set of

* , where { , }0 1 . Since Q is entirely characterized by

those problem instances that produces a 1 (yes) answer. We

can view Q as the language L over * , where

L x Q x { * | ( ) } 1 .

Page 25: 34.NP Completeness

Chapter 34 P.25

Computer Theory Lab.

Algorithm A accepts a string x { , } *0 1 if the given input

x, the algorithm output A(x)=1.

The language accepts by an algorithm A is the set

L x A x { * | ( ) } 1 .

The algorithm A rejects a string x if A(x)=0.

Page 26: 34.NP Completeness

Chapter 34 P.26

Computer Theory Lab.

Even if language L is accepted by an algorithm A, the

algorithm will not necessarily reject a string x L

provided as input to it. For example, the algorithm may

loop forever.

A language L is decided by an algorithm A if every binary

string is either accepted or rejected by the algorithm.

Page 27: 34.NP Completeness

Chapter 34 P.27

Computer Theory Lab.

A language L is accepted in polynom ial tim e by an

algorithm A if for any length n string x L , the algorithm

accepts x in tim e O n k( ) for som e constant k .

A language L is decided in polynom ial tim e by an

algorithm A if for any length n string x { , } *0 1 , the

algorithm decides x in tim e O n k( ) for som e constant k .

Page 28: 34.NP Completeness

Chapter 34 P.28

Computer Theory Lab.

Example:

PATH PROBLEM:

PATH={<G,u,v,k> | G=(V,E) is an undirected graph,

u v V k, , 0 is an integer, and there is a path from u to v

whose length is at most k}.

Page 29: 34.NP Completeness

Chapter 34 P.29

Computer Theory Lab.

Can be accepted in polynomial time.

Can be decided in polynomial time.

HALTING PROBLEM:

There exists an accepting algorithm, but no decision

algorithm exists.

Page 30: 34.NP Completeness

Chapter 34 P.30

Computer Theory Lab.

We can informally define a complexity class as a set of

languages, membership in which is determined by a

complexity measure, such as running time, on an algorithm

that determines whether a string x belongs to language L.

Page 31: 34.NP Completeness

Chapter 34 P.31

Computer Theory Lab.

We define the complexity class P as: P L { { , } * |0 1

there exists an algorithm A that decides L in polynomial

time}.

Theorem 34.2. P L{ | L is accepted by a polynomial

algorithm}.

Page 32: 34.NP Completeness

Chapter 34 P.32

Computer Theory Lab.

HAMILTONIAN CYCLE PROBLEM:

HAM_CYCLE={<G> | G is a hamiltonian graph}

verification: polynomial

decision problem: ?

Page 33: 34.NP Completeness

Chapter 34 P.33

Computer Theory Lab.

34.2 Polynomial-time verification

PATH PROBLEM:

PATH={<G,u,v,k> | G=(V,E) is an undirected graph,

u v V k, , 0 is an integer, and there is a path from u to v

whose length is at most k}.

verification: linear time.

Decision problem: polynomial

Page 34: 34.NP Completeness

Chapter 34 P.34

Computer Theory Lab.

naive algorithm:

input size: If we use the reasonable encoding of a graph as

its adjacency matrix, the number m of vertices is ( )n ,

where n = |<G>| is the length of the encoding of G. There

are m! possible permutations of the vertices. Therefore the

running time is ( !) ( !) ( )m n n 2 . This is not

a polynomial algorithm.

Page 35: 34.NP Completeness

Chapter 34 P.35

Computer Theory Lab.

Verification algorithms:

A verification algorithm is a two-argument algorithm A,

where one argument is an ordinary input string x and the

other is a binary string y called a certificate. A

two-argument algorithm A verifies an input x if there exists

a certificate y such that A(x,y)=1. The language verified by

a verification algorithm A is

L x y s t A x y { { , } * | { , } * . . ( , ) }0 1 0 1 1 .

Page 36: 34.NP Completeness

Chapter 34 P.36

Computer Theory Lab.

The complexity class NP

The complexity class NP is the class of languages that can

be verified by a polynomial-time algorithm. More precise, a

language L belongs to NP if and only if there exists a

two-input polynomial-time algorithm A and a constant c

such that

L x { { , } * |0 1 there exists a certificate y with

| | (| | )y O x c such that A(x,y)=1}.

NP (HAM_CYCLE NP.)

P NP .

Page 37: 34.NP Completeness

Chapter 34 P.37

Computer Theory Lab.

Problem:

1. P NP ?

2. Complexity class co-NP

co NP L L NP { | } .

NP co NP ?

3. Obviously P NP co NP .

P NP co NP ?

Page 38: 34.NP Completeness

Chapter 34 P.38

Computer Theory Lab.

P=NP=co-NP

P

NP=co-NP

co-NPNP P

P NP co NP

co-NPNP P

P NP co NP

Page 39: 34.NP Completeness

Chapter 34 P.39

Computer Theory Lab.

34.3 NP-completeness and reducibility

NP-completeness problem: if any one NP-complete

problem can be solved in polynomial time, then every

problem in NP has a polynomial-time solution, that is

NP=P.

Page 40: 34.NP Completeness

Chapter 34 P.40

Computer Theory Lab.

Reducibility:

ax b

ax bx c

0

02

Page 41: 34.NP Completeness

Chapter 34 P.41

Computer Theory Lab.

A language L1 is polynomial-time reducible to a language

L2 , written L LP1 2 if there exists a polynomial-time

computable function f : { , }* { , } *0 1 0 1 such that for all

x L 1 if and only if f x L( ) 2 . We call the function f

the reduction function, and a polynomial algorithm F that

computes f is called a reduction algorithm.

Page 42: 34.NP Completeness

Chapter 34 P.42

Computer Theory Lab.

L1L2

{ , } *0 1{ , } *0 1f

Page 43: 34.NP Completeness

Chapter 34 P.43

Computer Theory Lab.

Lemma 34.3. If L1 , L2 0 1{ , } * are languages

such that L LP1 2 , then L P2 implies L P1 .

x f x( )

A1

A2F

2)(, Lxfyes

2)(, Lxfno

1)(, Lxfno

1)(, Lxfyes

Page 44: 34.NP Completeness

Chapter 34 P.44

Computer Theory Lab.

NP-Completeness

A language L { , } *0 1 is NP-complete if

1. L NP , and

2. L LP' for every L NP'

Page 45: 34.NP Completeness

Chapter 34 P.45

Computer Theory Lab.

If a language L satisfies property 2, but not necessarily

property 1, we say that L is NP-hard.

We also define NPC to be the class of NP-complete

language.

Page 46: 34.NP Completeness

Chapter 34 P.46

Computer Theory Lab.

Theorem 34.4. If any NP-complete problem is

polynomial-time solvable, then NP=P. If any problem is

not polynomial-time solvable, then all NP-complete

problem are not polynomial-time solvable.

Proof. By Lemma 34.3.

P

NPC

NP

Page 47: 34.NP Completeness

Chapter 34 P.47

Computer Theory Lab.Circuit satisfiability

01

1

11

0

1

1 1

1

1

x1 1

1

1x2

x3

Satisfiable

0

1

1

x1

x2

x3

Not Satisfiable!

Page 48: 34.NP Completeness

Chapter 34 P.48

Computer Theory Lab.

Circuit-satisfiability problem: Given a boolean

combinational circuits composed of AND, OR, or NOT

gates, is it satisfiable?

CIRCUIT_SAT={<C> | C is a satisfiable boolean

combinational circuit}.

Page 49: 34.NP Completeness

Chapter 34 P.49

Computer Theory Lab.

Lemma 34.5. The circuit-satisfiability problem belongs

to the class NP.

Lemma 34.6. The circuit-satisfiability problem is NP-hard.

Proof. L CIRCUIT SAT L NPP _ .

Theorem 34.7. The circuit-satisfiability problem is

NP-Complete.

Page 50: 34.NP Completeness

Chapter 34 P.50

Computer Theory Lab.

A working storagexAux Machine StateProgram Counter y

M

A working storagexAux Machine StateProgram Counter y

M

A working storagexAux Machine StateProgram Counter y

A working storagexAux Machine StateProgram Counter y

Input bits

0/1 output

C0

C2

C1

CT(n)

Page 51: 34.NP Completeness

Chapter 34 P.51

Computer Theory Lab.

34.4 NP-Completeness Proof

Lemma 34.8. If L is a language such that L LP' for

some L NPC' , then L is NP-hard. Moreover, if L NP

then L NPC .

Page 52: 34.NP Completeness

Chapter 34 P.52

Computer Theory Lab.

Method for proving a language L is NPC:

1. Prove L NP .

2. Select a known NPC language L'

3. Describe an algorithm that computes a function f

mapping every instance of L' to an instance of L.

4. Prove that the function f satisfies x L ' if and only if

f x L( ) for all x { , } *0 1 .

5. Prove that the algorithm computing f runs in polynomial

time.

Page 53: 34.NP Completeness

Chapter 34 P.53

Computer Theory Lab.

Formula satisfiability:

An instance of SAT is a boolean formula composed of

1. boolean variables: x x1 2, ,...

2. boolean connectives: any boolean function with one or

two input and one output

3. parentheses

Page 54: 34.NP Completeness

Chapter 34 P.54

Computer Theory Lab.

SAT={< > | is a satisfiability formula}

Example:

(( ) (( ) ))

, ,

(( ) (( ) ))

( ( ))

( )

x x x x x x

x x x x1 2 1 3 4 2

1 2 3 40 0 1, 1

0 0 0 1 1 0

1 1 1 1

1 0 1

1

Page 55: 34.NP Completeness

Chapter 34 P.55

Computer Theory Lab.

Theorem 34.9 Satisfiability of boolean formula is

NP-complete.

Proof.

SAT NP

CIRCUIT SAT SATP_

x1

x2

x3

x5

x6

x7x4

x8

x9

x10

Page 56: 34.NP Completeness

Chapter 34 P.56

Computer Theory Lab.

x x x x x x

x x x x x x

x x x x x x

x x x x

10 4 3 5 1 2

6 4 7 1 2 4

8 5 6 9 6 7

10 7 8 9

( ) ( ( ))

( ) ( ( ))

( ( )) ( ( ))

( ( ))

Page 57: 34.NP Completeness

Chapter 34 P.57

Computer Theory Lab.

3-CNF satisfiability

literal

conjunction normal form (CNF)

3-conjunction normal form (3-CNF)

)(

)()(

431

423211

xxx

xxxxxx

Page 58: 34.NP Completeness

Chapter 34 P.58

Computer Theory Lab.

Theorem 34.10. Satisfibility boolean formula in 3-CNF

is NP complete.

Proof.

3 CNF SAT NP

SAT CNF SATP 3

Page 59: 34.NP Completeness

Chapter 34 P.59

Computer Theory Lab. (( ) (( ) )x x x x x x1 2 1 3 4 4

x1

x4

x3x1

x2

x4

y1

y5

y4y3

y2

y6

Page 60: 34.NP Completeness

Chapter 34 P.60

Computer Theory Lab.

))(())((

)())((

))(())((

316465

54213

4324211

xxyxyy

yyxxy

yyyxyyy

Page 61: 34.NP Completeness

Chapter 34 P.61

Computer Theory Lab.

)()(

)()(

ruleMorgan De

)()(

)()(

TableTruth

)(

221221

221221 1

221221

221221 1

221 1

xyyxyy

xyyxyy

xyyxyy

xyyxyy

xyy

Page 62: 34.NP Completeness

Chapter 34 P.62

Computer Theory Lab.

| |Ci Ci3

)()(

2||

212121 pllpllllC

Ci

i

| |Ci 1

C l l p q l p q

l p q l p qi

( ) ( )

( ) ( )

Page 63: 34.NP Completeness

Chapter 34 P.63

Computer Theory Lab.

34.5 NP-Complete Problems

Page 64: 34.NP Completeness

Chapter 34 P.64

Computer Theory Lab.

CIRCUIT-SAT

SAT

3CNF-SAT

Clique HAM-CYCLE

VERTEX COVER TSP

SUBSET-SUM

Page 65: 34.NP Completeness

Chapter 34 P.65

Computer Theory Lab.

34.5.1 The clique problem

A clique in a undirected graph G = (V,E) is a subset

VV ' of vertices, each pair of which is connected by an

edge in E. The size of a clique is the number of vertices it

contains. The clique problem is the optimization problem

of finding a clique of maximum size in a graph.

Page 66: 34.NP Completeness

Chapter 34 P.66

Computer Theory Lab.

CLIQUE={<G, k> |G is a graph with clique size k}

naïve algorithm: (| |

)kV

k2

Page 67: 34.NP Completeness

Chapter 34 P.67

Computer Theory Lab.

Theorem 34.11. The clique problem is NP-complete.

Proof.

clique NP

3 CNF SAT cliqueP

)(

)()(

321

321321

xxx

xxxxxx

Page 68: 34.NP Completeness

Chapter 34 P.68

Computer Theory Lab.

x1 x2 x3

x1 x1

x2 x2

x3 x3

C x x x1 1 2 3

C x x x2 1 2 3 C x x x3 1 2 3

Page 69: 34.NP Completeness

Chapter 34 P.69

Computer Theory Lab.

C C Ck1 2 ...

( , )( )

( )v v E

r s

l lir

js

ir

js

1

2

clique size k

Page 70: 34.NP Completeness

Chapter 34 P.70

Computer Theory Lab.

34.5.2 The vertex-cover problem

A vertex cover of an undirected graph G=(V,E) is a

subset V V' such that if ( , )u v E then u V ' or

v V ' (or both).

The vertex cover problem is to find a vertex cover of

minimum size in a given graph.

VERTEX-COVER={<G, k> | graph G has a vertex cover

of size k}.

Page 71: 34.NP Completeness

Chapter 34 P.71

Computer Theory Lab.

Theorem 34.12. The vertex-cover problem is NP-complete.

Proof.

VERTEX COVER NP

CLIQUE VERTEX COVERP

Page 72: 34.NP Completeness

Chapter 34 P.72

Computer Theory Lab.

u

z w

v

xy

u

z w

v

xy

Page 73: 34.NP Completeness

Chapter 34 P.73

Computer Theory Lab.

34.5.3The hamiltonian-cycle problem

Theorem 34.13. The hamiltonian cycle problem is

NP-complete.

Proof.

HAM CYCLE NP

3CNF SAT HAM CYCLEP

kinds of wedges

Page 74: 34.NP Completeness

Chapter 34 P.74

Computer Theory Lab.

B

b1,1

b1,4

b1,3

b1,2

B

b2,1

b2,4

b2,3

b2,2

B

b3,1

b3,4

b3,3

b3,2

x1'

x1''

e1 e1

x2'

x2''

e2 e2

A

A

A

A

A

A

A

A

A

x3'

x3''

e3 e3

( ) ( ) ( )x x x x x x x x x1 2 3 1 2 3 1 2 3

Page 75: 34.NP Completeness

Chapter 34 P.75

Computer Theory Lab.

b1,4

b1,3

b3,4

b3,3

x3'

x3''

e3

A

Ab1,4

b1,3x3'

b3,4 x3''

b3,3

Page 76: 34.NP Completeness

Chapter 34 P.76

Computer Theory Lab.

34.5.4 The traveling-salesman problem

TSP={<G, c, k> | G = (V, E) is a complete graph, c is a

function from V V into Z, k Z , and G has a traveling

salesman tour with cost at most k}.

3

1

2

5

1

4u

w

v

x

Page 77: 34.NP Completeness

Chapter 34 P.77

Computer Theory Lab.

Theorem 34.13

The hamiltonian cycle problem is NP-complete.

Page 78: 34.NP Completeness

Chapter 34 P.78

Computer Theory Lab.

Proof.

First, show that HAM-CYCLE belongs to NP. We now prove that VERTEX-COVER p HAM-

CYCLE, which shows that HAM-CYCLE is NP-complete.

Given an undirected graph G=(V,E) and an integer k, we construct an undirected graph G’=(V’,E’) that has a hamiltonian cycle iff G has a vertex cover of size k.

Page 79: 34.NP Completeness

Chapter 34 P.79

Computer Theory Lab.

widget

[v,u,1]

[v,u,2]

[v,u,3]

[v,u,4]

[v,u,5]

[v,u,6]

[u,v,1]

[u,v,2]

[u,v,3]

[u,v,4]

[u,v,5]

[u,v,6]

Wuv

[v,u,1]

[v,u,6]

[u,v,1]

[u,v,6]

Wuv

[v,u,1]

[v,u,6]

[u,v,1]

[u,v,6]

Wuv

[v,u,1]

[v,u,6]

[u,v,1]

[u,v,6]

Wuv

(a) (b) (c) (d)

Page 80: 34.NP Completeness

Chapter 34 P.80

Computer Theory Lab.The reduction of an instance of the vertex-cover problem to an instance of the hamiltonian-cycle problem. (a) An undirected graph G with a vertex of

size 2, consisting if the lightly shaded vertices w and y.

(b) the undirected graph G’ produced by the reduction, with the hamiltonian path corresponding to the vertex cover shaded.

The vertex cover {w,y} corresponds to edges (s1,[w,x,1]) and (s2,[y,x,1]) appearing in the hamiltonian cycle.

Page 81: 34.NP Completeness

Chapter 34 P.81

Computer Theory Lab.

(a)

(b)

w

z

x

y

[x,w,1]

[x,w,6]

[w,x,1]

[w,x,6]

Wuv

[y,x,1]

[y,x,6]

[x,y,1]

[x,y,6]

Wuv

[y,w,1]

[y,w,6]

[w,y,1]

[w,y,6]

Wuv

[z,w,1]

[z,w,6]

[w,z,1]

[w,z,6]

Wuv

Page 82: 34.NP Completeness

Chapter 34 P.82

Computer Theory Lab.

Three types of edges in E’

1. Edges in widget.2. {([u,u(i),6],[u,u(i+1),1]) : 1 i

degree(u)-1} 3. {(sj,[u,u(1),1]) : uV and 1 j k}

{(sj,[u,u(degree(u)),6]) : uV and 1 j

k}

Page 83: 34.NP Completeness

Chapter 34 P.83

Computer Theory Lab.

The reduction performed in polynomial time |V’| = 12|E| + k 12|E| + |V|

|E’| = (14|E|) + (2|E| -|V|) + (2k|V|) = 16|E| + (2k-1)|V| 16|E| + (2|V|-1)|V|

Page 84: 34.NP Completeness

Chapter 34 P.84

Computer Theory Lab.

The transformation from graph G to G’ is a reduction.

That is, G has a vertex cover of size k iff G’ has a hamiltonian cycle.

Page 85: 34.NP Completeness

Chapter 34 P.85

Computer Theory Lab.

Theorem 34.14. The traveling salesman problem is

NP-complete.

Proof.

TSP NP

HAM CYCLE TSPP

CARDY
Page 86: 34.NP Completeness

Chapter 34 P.86

Computer Theory Lab.

34.5.5 The subset-sum problem

S={1,4,16,64,256,1040,1041,1093,1284,1344}

t=3754

S'={1,16,64,256,1040,1093,1284}

SUBSET-SUM={<S, t> | there exists a subset S S' such

that t ss S

'

Page 87: 34.NP Completeness

Chapter 34 P.87

Computer Theory Lab.

Theorem 34.15

The subset-sum problem is NP-complete.

Page 88: 34.NP Completeness

Chapter 34 P.88

Computer Theory Lab.

Proof.

First, show that SUBSET-SUM is in NP. We now show that 3-CNF-SAT p SUBSET-SU

M. Given a 3-CNF formula over variables x1, x2,

…, xn with clauses C1, C2,…, Ck, each containing exactly three distinct literals.

The reduction algorithm constructs an instance <S,t> of the subset-sum problem such that is satisfiable iff there is a subset of S whose sum is exactly t.

Page 89: 34.NP Completeness

Chapter 34 P.89

Computer Theory Lab.

Example

The formula in 3-CNF is = C1 C2 C3 C4, where C1 = (x1 ¬x2 ¬x3), C2 = (¬x1 ¬x2 ¬x3), C3 = (¬x1 ¬x2 x3), and C4 = (x1 x2 x3).

A satisfying assignment of is <x1

= 0, x2 = 0, x3 = 1>.

Page 90: 34.NP Completeness

Chapter 34 P.90

Computer Theory Lab.

The reduction of 3-CNF-SAT to SUBSET-SUM

x1 x2 x3 C1 C2 C3 C4

v1 = 1 0 0 1 0 0 1

v1’ = 1 0 0 0 1 1 0

v2 = 0 1 0 0 0 0 1

v2’ = 0 1 0 1 1 1 0

v3 = 0 0 1 0 0 1 1

v3’ = 0 0 1 1 1 0 0

s1 = 0 0 0 1 0 0 0

s1’ = 0 0 0 2 0 0 0

s2 = 0 0 0 0 1 0 0

s2’ = 0 0 0 0 2 0 0

s3 = 0 0 0 0 0 1 0

s3’ = 0 0 0 0 0 2 0

s4 = 0 0 0 0 0 0 1

s4’ = 0 0 0 0 0 0 2

t = 1 1 1 4 4 4 4

C4 has no ¬x1

C4 has x2

Page 91: 34.NP Completeness

Chapter 34 P.91

Computer Theory Lab.

The reduction performed in polynomial time The set S contains 2n+2k values,

each of which has n+k digits, and the time to produce each digit is polynomial in n+k.

The target t has n+k digits, and the reduction produces each in constant time.

Page 92: 34.NP Completeness

Chapter 34 P.92

Computer Theory Lab.

3-CNF formula is satisfiable iff there is a subset S’ S whose sum is t.