109
1 NPC NP-Complete Problems

1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

Embed Size (px)

Citation preview

Page 1: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

1

NPCNP-Complete Problems

Page 2: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

2

We can solve any problem(!)(?)

• … but perhaps not with a computer!• Some problems are not computable:

• The Halting Problem: Write a function

boolean halts(Program p, Input i);

which returns true if p halts on input i, and false if it doesn’t.

• A bonus homework exercise?

Page 3: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

3

• If we could write the halts function, then we could also write:

boolean loopIfHalts(Program p, Input i) { if (halts(p,i)) while (true) ; else return true;}boolean testSelf(Program p) { return loopIfHalts(p,p);}

• What is testSelf(testSelf)?

Page 4: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

4

Proof by contradiction:• boolean

halts(Program p,Input i);

which returns true if p halts on input i, and false if it doesn’t.

• boolean loopIfHalts(Program p, Input i) {

if (halts(p,i))

while (true) ;

else

return true;

}• boolean

tS(Program p)

{return

loopIfHalts(p,p);}

• Assume: tS(tS) halts

and returns true.• loopIfHalts(tS,tS) =

true• halts(tS,tS) = false• tS(tS) doesn't halt which

is a contradiction

• Assume: tS(tS) does not halt

• halts(tS,tS) = true• contradiction

Page 5: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

5

We can solve any computable problem!

• … but perhaps not with an “efficient” algorithm.

• Of course we could solve any problem by:– Enumerating all possible solutions;– Testing each one to see if it is a solution.

• That may take a long time …

• … but sometimes it seems that’s the best we can do!

Page 6: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

6

Circuit Satisfiability:• Find an assignment of truth values

to the inputs of a logic circuit, that results in an output of 1.

x1x2

x3

Page 7: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

7

Hamiltonian Cycles:• Given a graph G, find a simple

cycle that contains each vertex in G.

Page 8: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

8

Subset Sum:

• Given a finite set of natural numbers S and a target natural number t, find a subset S’ of S whose sum is t.

• For example, find:

S’{1,3,6,34,93,243,654,1040,1436,1879}

with sum S’ = 2361.

Page 9: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

9

What is a problem?

• Abstractly, a problem is a set of pairs (I,S), where I is an instance of the problem and S is a solution.

• A decision problem is a problem in which the solutions are either yes or no. (i.e., 0 or 1)

• An optimization problem O is a subset of a problem P in which, if (I,S)O and (I,S’)P, then SS’, with respect to some ordering . (I.e. the solution S is better than S')

Page 10: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

10

Decision Problems:• To solve a decision problem on a computer:

– Provide the computer with an instance;– Wait for a yes/no answer.

• In most cases, we will need to encode the instance in a form that the computer can manipulate.

• We will assume that instances are coded as strings of bits: i.e., by elements of {0,1}*

Page 11: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

11

Encodings, eAbstract Problems

Concrete Problems

Is G a connected

graph?

Does e(G) represent a connected

graph?

General picture:

For example:

Page 12: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

12

Problem Solving:

• An algorithm solves a problem in time O(T(n)) if, for any instance I, the algorithm can produce a solution S in time O(T(|I|)).

• A problem is polynomial-time solvable if there is an algorithm to solve it in time O(nk), for some constant k.

Page 13: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

13

Polynomial time = Tractable?

• The computable polynomial-time algorithms that we encounter in practice usually have relatively low values of k.

• The set of polynomial-time problems is the same in many reasonable models of computation.

• The set of polynomial-time algorithms has nice closure properties.

Page 14: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

14

The set of concrete decision problems that are solvable in polynomial time.

Page 15: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

15

Polynomial Time Solvability for Abstract Algorithms:

• We would like to extend these ideas to abstract algorithms:

• An abstract decision problem Q is solvable in polynomial time if the encoding e(Q) is in P.

• This depends on the encoding used!

Encodings, eAbstract Problems

Concrete Problems

Page 16: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

16

Not all encodings are equal!

• Suppose we have A(k) = (k) – for example: A(k) = 1+2+3+ ... +k

• If we code the integer k in binary notation, then we require n = log k bits of input.

• If we code an integer k in unary notation, then we require n = k+1 bits of input.

• Then algorithm A will have:– Exponential complexity, (2n), if we use the

binary representation;– Linear complexity, (n), if we use the unary

representation.

Page 17: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

17

This seems counter intuitive:

• The "better" the encoding the worse the complexity!

• In reality the same work is happening in both cases, we just inflated the size of the input (in the unary encoding) so the complexity appears better.

• Moral of the Story:If comparing two Abstract decision problems

be sure the two encodings you are using are comparable!

Page 18: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

18

Relating Encodings:• A function f : {0,1}*{0,1}* is polynomial-

time computable if there is an algorithm that, given any input x, produces f(x) as output in polynomial time.

• Encodings e1 and e2 are polynomially related if there are polynomial-time computable functions f and g mapping between them. {0,1}*

{0,1}*

Instances

e1

e2

f g

Page 19: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

19

• If e1 and e2 are polynomially related encodings on the instances of an abstract decision problem Q, then:

e1(Q)P e2(Q)P

• So, as long as we stick to reasonable, concise encodings, the exact encoding doesn’t alter our ability to solve a problem in polynomial time.

Page 20: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

20

Accepting and Deciding:• Given a string x{0,1}*, an algorithm A:

– accepts x, if it outputs 1 given input x.– rejects x, if it outputs 0 given input x.– decides x, if it either accepts or rejects x.– is there another choice?

• A language is a set of strings x{0,1}*.

• The set of strings x{0,1}* for which an algorithm A outputs 1 is called the language accepted by A.

• Similar definitions for the languages rejected or decided by an algorithm …

Page 21: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

21

• Languages correspond to concrete decision problems:

– The language for a problem Q is just the set of strings that have solution 1.

• In this setting, P is the set of all problems that can be decided in polynomial time.

Page 22: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

22

• This is the same as the set of languages that can be accepted in polynomial-time!

– Suppose L can be accepted in polynomial time by an algorithm A. Then A will accept L after at most cnk steps for some constants c, k.

– To decide L in polynomial time, simulate cnk steps in the execution of A. (A polynomial time overhead.) If it hasn’t terminated with an answer after that many steps, then it won’t ever be accepted, and so we can return 0.

Page 23: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

23

• Write down 5 problems that are in P:1. 2. 3. 4. 5.

• How many decision problems can you think of that are not in P?

Page 24: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

24

Verifying Solutions:• It is often much easier to verify that a given

value is a solution of a problem, than it is to calculate a solution directly.

• A verification algorithm takes two inputs:– A string x, representing a problem instance;– A string y, called a certificate.

• Algorithm A verifies a language L if L is precisely the set of strings xL for which there exists a certificate y such that A(x,y) returns 1.

Page 25: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

25

The set of concrete decision problems that are verifiable in polynomial time.

NP stands fornon-deterministic

polynomial

Page 26: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

26

“ Nondeterministic Polynomial”:

• If a decision problem is in NP, then we can calculate solutions for an instance x by:– Enumerating all possible answers y;– Verifying each one using A(x,y) until we find a solution.

• If there are N possible solutions, this will take O(Nnk) time. But often N is O(cn), and so the overall time is O(cn) too (since cn grows much faster than nk

as n goes to infinity).

• The best case time is O(nk) --- if the first answer we try is a solution.

Page 27: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

27

P NP:

• If L is in P, then we can construct a polynomial-time verification algorithm A(x,y) that simply ignores the certificate y, and accepts precisely those strings that it determines to be in L.

• Is P=NP? After more than three decades, this is still an open question … but many researchers believe that they are not equal.

Page 28: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

28

Reductions

Page 29: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

29

Motivation

• Reductions are our main tool for comparison between problems.

Page 30: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

30

Introduction

• Objectives:– To formalize the notion of “reductions”.

• Overview:– Karp reductions

– HAMPATH p HAMCYCLE

– Closeness under reductions– Cook reductions– Completeness

Page 31: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

31

Reductions – Concept

A p B

IF we can translate problem A to problem B…

THEN B is at least as hard as A.

Page 32: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

32

Reductions – Formal Definition

Definition: Language A is polynomial time reducible to language B if…

I.e – there exists a polynomial time TM which halts with f(w) on

its tape when given input w.

where for every w,wA f(w)B

f is called the polynomial time reduction of A to B.

Denote: A p B

a polynomial time computable function f:** exists,

SIP 250

Page 33: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

33

Reductions and Algorithms

*

A

* - A

f

*

B

* - B

Page 34: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

34

Reductions and Algorithms

f

polynomial time algorithm for A

polynomial time algorithm for B

wf(w)B

?f(w) wA

?

Page 35: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

35

How to Reduce?

1. Construct f.2. Show f is polynomial time

computable.3. Prove f is a reduction, i.e show:

1. If wA then f(w)B2. If f(w)B then wA

Page 36: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

36

Terminology

• The type of reductions we’ve just described is also called:– Polynomial-time mapping reduction– Polynomial-time many-one reduction– Polynomial-time Karp reduction

• We’ll always refer to such reductions unless otherwise specified.

Page 37: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

37

Example

• To demonstrate a reduction, we’ll revisit the example we gave in the introduction.

• We’ll rephrase and formalize the seating and tour problems

• And demonstrate a slightly different reduction between them.

Page 38: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

38

Hamiltonian Path

Instance: a directed graph G=(V,E) and two vertices stV.

Problem: To decide if there exists a path from s to t, which goes through each node once.

In the version presented in the introduction we asked for some path, without specifying the start and end

vertices.

Page 39: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

39

Hamiltonian Cycle

Instance: a directed graph G=(V,E).Problem: To decide if there exists a simple

cycle in the graph which goes through each node exactly once.

Page 40: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

40

f( <G=(V,E),s,t> ) = <G’=(V{u},E{(u,s),(t,u)})>f( <G=(V,E),s,t> ) = <G’=(V{u},E{(u,s),(t,u)})>

HAMPATH p HAMCYCLE

p

f( <G=(V,E),s,t> ) = <G’=(V{u},E{(u,s),(t,u)})>f( <G=(V,E),s,t> ) = <G’=(V{u},E{(u,s),(t,u)})>

s

t

s

t

n

Page 41: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

41

Correctness

• (Completeness) If there exists a Hamiltonian path (v0=s,v1,…,vn=t) in the original graph, then (u,v0=s,v1,…,vn=t,u) is a Hamiltonian cycle in the new graph.

Page 42: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

42

Correctness

• (Soundness) (u,s) and (t,u) must be in any Hamiltonian cycle in the constructed graph, thus removing u yields a Hamiltonian path from s to t.

Page 43: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

43

How to Reduce?

1. Construct f.2. Show f is polynomial time

computable.3. Prove f is a reduction, i.e show:

1. If wHAMPATH then f(w)HAMCYCLE

2. If f(w)HAMCYCLE then wHAMPATH

easy to

verify

Page 44: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

44

Closeness Under Reductions

Definition: A complexity class C is closed under reductions if, whenever L is reducible to L’ and L’C, then L is also in C.

L

L’ C

Page 45: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

45

Observation

Theorem: P, NP, PSPACE and EXPTIME are closed under polynomial-time Karp reductions.

Proof: Filling in the exact details is left to the reader. (See sketch)

Page 46: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

46

Other Types of Reductions

• Cook Reduction: Use an efficient “black box” (procedure) that decides B, to construct a polynomial-time machine which decides A.

polynomial time algorithm

for A

polynomial time algorithm for B

Page 47: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

47

Cook reduction of HAMCYCLE to HAMPATH.

• For each edge (u,v)E,– if there’s a Hamiltonian path from v to

u in the graph where (u,v) is removed, output ‘YES’, exit

• Output ‘NO’HAMPATH oracleMake

sure it’s efficient!

Page 48: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

48

Reducibility:• Problem Q can be reduced to problem

Q’ if every instance of Q can be rephrased as an instance of Q’.

• A language L1 is polynomial-time reducible to a language L2, written L1P

L2, if there is a polynomial-time computable function f such that:

x L1 f(x) L2

• f is a reduction function; an algorithm that computes f is a reduction algorithm.

Page 49: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

49

• If L1 P L2 and L2 is in P, then so is L1.

Fx f(x)

Reduction algorithm

A2

f(x)L2

Algorithm for deciding L2

xL1

Algorithm for deciding L1

Page 50: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

50

NP-completeness:

• A language L is NP-complete if:– L is in NP; and– L’ P L for every L’ in NP. A language that

satisfies just this property is said to be NP-hard.

Page 51: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

51

NP-completeness and P=NP:

• If any NP-complete problem is polynomial time solvable, then P=NP:– Suppose LP is NP-complete. Then for any

L’NP, we have L’ P L, and hence L’P.

• If any problem in NP is not in P, then no NP-complete problem is in P.– Suppose LNP and LP. Now, if there is an

L’P that is NP-complete, then L P L’, and

hence LP, which is a contradiction.

Page 52: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

52

To disprove P=NP:

• Just find a polynomial-time algorithm for an NP-complete problem …

• Fame and fortune will be yours ...

• But nobody has found one yet …

Page 53: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

53

NPC

NPC

Contains thousands of distinct problem

exponential algorithms

efficient algorithms

?

each reducible to all others

Page 54: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

54

How Can Studying Complexity Make You a Millionaire?

• This is the most fundamental open question of computer science.

• Resolving it would grant the solver a great honor

• … as well as substantial fortune…www.claymath.org/prizeproblems/pvsnp.htm

Page 55: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

55

P = NP co-NP ?

Any language in NP co-NP - NP ?

P = NP = co-NP

NP = co-NP

P

co-NPNP

P = NP co-NP co-NPNP

NP co-NP

P

Four possible relationships between complexity classes

Page 56: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

56

Circuit Satisfiability is NP-complete:

• Of course, the first step is to find an NP-complete algorithm …

• We will show that circuit satisfiability is NP-complete, using the following two steps:– Prove that it is NP;– Prove that it is NP-hard.

Page 57: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

57

Circuit Satisfiability is in NP:

Certificate supplies value at each input and output wire;We check the values at each gate;Return the value at the final gate’s output.

Page 58: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

58

• We need to show that every language in NP is polynomial-time reducible to circuit-satisfiability.

• So suppose that L is in NP. We will describe a polynomial time reduction function f such that xL if and only if f(x) is a circuit satisfiable problem.

Circuit Satisfiability is NP-hard:

Page 59: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

59

Basic idea of proof

Represent the computation A as a sequence of configurations and M is a combinational circuit that implement the mapping of one configuration to another.

Configuration : the program, the program counter, working storage, machine states.

Page 60: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

60

• There is an algorithm A that verifies L in polynomial time.

• Let T(n) = worst-case time for A on input strings of length n.

• Each step in execution of A can be represented as a transformation from one machine configuration to the next:

A pc statex y

A pc statex y

MLogic circuit

Page 61: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

61

2. Every NP Problem ≤P CIRCUIT-SAT (CIRCUIT-SAT is NP-hard) (2)

Page 62: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

62

Ci --M--> C i+1

If A run at most T(n) steps the output appears in C T(n)

Construct M that computes all configurations.

F : given x computes C = f(x) that is satisfiable iff there exists a certificate y such that A(x,y) = 1.

when F obtains x , it first computes n = |x| and construct C’ consists of T(n) copies of M.

Page 63: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

63

Proof

1. F correctly computes f. C is satisfiable iff there exists y such that A(x,y) = 1.

2. F runs in polynomial time.

Part 1 if partSuppose there exists y, length O(nk) such that

A(x,y) = 1. Apply y to the input of C, the output of C is C(y) = A(x,y) = 1. Thus if a certificate exists then C is satisfiable.

Page 64: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

64

Part 1 only if

Suppose C is satisfiable, hence there exists an input y to C such that C(y) = 1, from which we conclude that A(x,y) =1 .

Part 2 (A runs in polynomial time)

The number of bits required to represent a configuration is polynomial in n (n = |x|).

Program A has constant size

Page 65: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

65

The length of input x is n

The length of the certificate y is O(nk)

The algorithm runs at most O(nk) steps, the amount of working storage required by A is polynomial of n.

The length of a configuration is polynomial in O(nk)

M has the size polynomial in the length of a configuration, hence is polynomial in n.

Page 66: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

66

The circuit C consists of at most t = O(nk) copies of M, hence it has size in polynomial of n.

The construction of C from x can be accomplished in polynomial time by the reduction algorithm F, since each step of construction takes polynomial time.

Theorem 36.7

The circuit-satisfiability problem is NP-complete

Page 67: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

67

Formula Satisfiability (SAT)

=x10 (x4 x3)

(x5 (x1x2))

(x6 x4)

(x7 (x1x2x4))

(x8 (x5x6))

(x9 (x6x7))

(x10 (x7x8x9))

Page 68: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

68

Dictionary

literal: (negated or not) Boolean variable

Examples: x, x

clause: several literals connected with

Example: (xyz)

CNF (Conjunctive Normal Form): several clauses connected with

Example: (x y)(xyz)

3CNF: a CNF formula with three literals in each clause.

Example: (xyz)(xyz)

negation: not ()

conjunction: and ()

disjunction: or ()

Page 69: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

69

New Base Problems

• The only NP-Complete problem we currently know of is SAT.

• Unfortunately, it’s not very comfortable to work with.

• Thus we’ll start by introducing several useful variants of SAT.

• We’ll use them as our base problems.

Page 70: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

70

3SAT

Instance: a 3CNF formulaProblem: To decide if the formula is satisfiable.

(xyz)(xyz)(xyz)(xyz)

(xxx)(xxx)(xxx)(xxx)

A satifiable 3CNF formula

An unsatifiable 3CNF formula

Page 71: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

71

3SAT is NP-Complete

• 3SAT is a special case of SAT, and is therefore clearly in NP.

• In order to show it’s also NP-Complete, we’ll alter the proof of SAT’s NP-Completeness,

• so it produces 3CNF formulas.

Why would that be enough?

SIP 259-260

Page 72: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

72

CNF 3CNF

(xy)(x1x2... xt)...clauses with 1

or 2 literals

(xyx)

replication

clauses with more than 3 literals

split

(x1 x2 c11)(c11 x3 c12)... (c1t-3 xt-1xt)

Page 73: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

73

3SAT is NP-Complete

• Since we’ve shown a reduction from any NP problem to 3SAT,

• and 3SAT is in NP,• 3SAT is NP-Complete.

Page 74: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

74

CLIQUE

Instance: A graph G=(V,E) and a goal k.Problem: To decide if there is a set of

nodes C={v1,...,vk}V, s.t for any u,vC: (u,v)E.

Page 75: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

75

CLIQUE is in NP

On input G=(V,E),k:

• Guess C={v1,...,vk}V

• For any u,vC: verify (u,v)E• Reject if one of the tests fail,

accept otherwise.

The length of the certificate: O(n) (n=|V|)

Time complexity: O(n2)

Page 76: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

76

CLIQUE is NP-Complete

Proof: We’ll show 3SATpCLIQUE.

..)..(.......)..(..

SIP 251-253

Page 77: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

77

The Reduction

for any clause ()

connected iff

|V| = formula’s length

K= no. of clauses

Page 78: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

78

Proof of Correctness

1

.

.

.

k

a clique of size k must contain one node from every

layer.

NOT connected!

Page 79: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

79

Correctness

..)..(.......)..(.. ...

given a k-clique, an assignment which satisfies all the literals of the

clique satisfies the formula

given a satisfying assignment, a set comprising of one satisfied literal of each clause forms a k-

clique.

Page 80: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

80

INDEPENDENT-SET

Instance: A graph G=(V,E) and a goal k.Problem: To decide if there is a set of

nodes I={v1,...,vk}V, s.t for any u,vI: (u,v)E.

Page 81: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

81

INDEPENDENT-SET NP

On input G=(V,E),k:

• Guess I={v1,...,vk}V

• For any u,vC: verify (u,v)E• Reject if one of the tests fail,

accept otherwise.

The length of the certificate: O(n) (n=|V|)

Time complexity: O(n2)

Page 82: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

82

INDEPENDENT-SET is NPC

Proof: By the previous claim and a trivial reduction from CLIQUE.

there’s a clique of size k in a graph

there’s an IS of size k in its

complement

Page 83: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

83

SUBSET-SUM

Instance: A set of numbers denoted S and a target number t.

Problem: To decide if there exists a subset YS, s.t yYy=t.

Page 84: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

84

SUBSET-SUM is in NP

On input S,t:• Guess YS

• Accept iff yYy=t.

The length of the certificate: O(n) (n=|S|)Time complexity: O(n)

Page 85: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

85

SUBSET-SUM is NP-Complete

Proof: We’ll show 3SATpSUBSET-SUM.

SIP 269-271

..)..(.......)..(..

Page 86: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

86

Examples SUBSET-SUM

SUM-SUBSET10,2,4,8

SUM-SUBSET11,2,4,8

… because 2+8=10

… because 11 cannot be made out of {2,4,8}

t}y that such

SR subset a is there

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

Ry

k1

Page 87: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

87

Reducing 3SAT to SubSet Sum

• Proof idea:

• Choosing the subset numbers from the set S corresponds to choosing the assignments of the variables in the 3CNF formula.

• The different digits of the sum correspond to the different clauses of the formula.

• If the target t is reached, a valid and satisfying assignment is found.

Page 88: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

88

1 0 0 0 1 0 0 11 0 0 0 0 1 0 0

1 0 0 1 1 0 01 0 0 0 0 1 0

1 0 1 0 0 01 0 0 0 1 1

1 0 1 0 01 0 0 0 1

1 0 0 01 0 0 0

1 0 01 0 0

1 01 0

11

)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 4clause: Subset Sum 3CNF formula:

Make the number table,and the ‘target sum’ t

dummies

Page 89: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

89

Reducing 3SAT to SubSet Sum• Let 3CNF with k clauses and variables

x1,…,x.

• Create a Subset-Sum instance <S,t> by: 2+2k elements of S = {y1,z1,…,y,z,g1,h1,…,gk,hk}

– yj indicates positive xj literals in clauses

– zj indicates negated xj literals in clauses

– gj and hj are dummies

– and k

3311t

Page 90: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

90

Subset Sum

)xxx()xxx(

)xxx()xxx(

431322

421321

1 0 0 0 1 0 0 11 0 0 0 0 1 0 0

1 0 0 1 1 0 01 0 0 0 0 1 0

1 0 1 0 0 01 0 0 0 1 1

1 0 1 0 01 0 0 0 1

1 0 0 01 0 0 0

1 0 01 0 0

1 01 0

111 1 1 1 3 3 3 3

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4

Note 1: The “1111”in the target forcesa proper assignmentof the xi variables.

Note 2: The target“3333” is only possibleif each clause is satisfied.(The dummies can add maximally 2 extra.)

Page 91: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

91

Subset Sum

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

)xx(x)xxx(

)xxx()xx(x

431322

421321

1 0 0 0 1 0 0 11 0 0 0 0 1 0 0

1 0 0 1 1 0 01 0 0 0 0 1 0

1 0 1 0 0 01 0 0 0 1 1

1 0 1 0 01 0 0 0 1

1 0 0 01 0 0 0

1 0 01 0 0

1 01 0

111 1 1 1 3 3 3 3

1 0 0 0 1 0 0 11 0 0 0 0 1 0

1 0 0 0 1 11 0 1 0 0

1 0 0 01 0 0 0

1 0 01 0 0

1 01

1 1 1 1 3 3 3 3

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4

+

Page 92: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

92

Subset Sum

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 11 0 0 0 0 1 0 0

1 0 0 1 1 0 01 0 0 0 0 1 0

1 0 1 0 0 01 0 0 0 1 1

1 0 1 0 01 0 0 0 1

1 0 0 01 0 0 0

1 0 01 0 0

1 01 0

111 1 1 1 3 3 3 3

1 0 0 0 0 1 0 01 0 0 0 0 1 0

1 0 0 0 1 11 0 1 0 0

1 0 0 01 0 0 0

? ? ?1 1 1 1 2 ? ? ?

+x1

–x1

+x2

–x2

+x3

–x3

+x4

–x4+

Page 93: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

93

Proof 3SAT P Subset Sum• For every 3CNF , take target t=1…13…3

and the corresponding set S.

• If 3SAT, then the satisfying assignment defines a subset that reaches the target.

• Also, the target can only be obtained via a set that gives a satisfying assignment for .

Sum-Subset3...13...1,S ifonly and if SAT3

Page 94: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

94

Finding the Solution• If the decision problem Subset-Sum is

solvable in polynomial time, then we can also find the subset that reaches the target in poly-time.

• How?

• By asking smart questions about several variants of the problem.

• That way, we can determine which variables xi are involved in the solution.

Page 95: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

95

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 96: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

96

3SAT to Hamiltonian Path

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

• … a Hamiltonian path is possible if and only if there is a zig-zag path through G,

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

Page 97: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

97

“ Zig-Zag Graphs”s

t

1x

Lx

2x

There are 2L different Hamiltonian paths from s to the target t.

For every i, “Zig” means xi=True ix

… while “Zag” means xi=False ix

Page 98: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

98

Accessing the ClausesIf 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 99: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

99

Connecting the Clauses

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

Page 100: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

100

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”

• If SAT, then the satisfying assignment defines a Hamiltonian path, and vice versa.

HamPathG ifonly and if SAT

Page 101: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

101

Example )xxx()xxx(

)xxx()xxx(

431322

421321

4 variables…)xxx( 321

)xxx( 322

)xxx( 421

)xxx( 431

s

1x

4x

3x

2x

t

4 clauses…

Clauses connectedvia zig-zag “hubs”

Page 102: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

102

Example )xxx()xxx(

)xxx()xxx(

431322

421321

assignment…

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

Page 103: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

103

Example )xxx()xxx(

)xxx()xxx(

431322

421321

assignment…

4321 x,x,x,x

s

t

…does not satifyfirst clause; hence the path misses the C1 node

1x

4x

3x

2x

)xxx( 321

)xxx( 322

)xxx( 421

)xxx( 431

Page 104: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

104

More on Hamiltonian Path• Useful for proving NP-completeness of

“optimal routing problems”

• Typical example: NP-completeness of “Traveling Salesman Problem”

• Issue of directed versus undirected graphs

Page 105: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

105

Forcing DirectionsGiven a directed graph with s,x1,…,xk,t

Replace s with sout, the t with tin ,and every xi with the triplet “xi,in—xi,mid — xi,out”

Redraw the original directed edges withedges going from out-nodes to in-nodes.

If and only if the directed graph has a HamPath from s to t, then so has this graph.

Page 106: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

106

Example: Directionss y

x s yx

“Undirected HamPath” is NP-complete

xin

soutxmid

xout

becomes

ymid

yout

yin xin

soutxmid

xout

becomes

ymid

yout

yin

Page 107: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

107

Minimizing Miles

• Given k cities, with distance graph G• “Is there a route that visits all k cities with

less than t miles of traveling?”

• The Traveling Salesman Problem is in NP.

• Close connection with Undirected-Ham-Path

• One can show: TSP is NP-complete

Page 108: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

108

CIRCUIT-SAT

3-CNF-SAT

SAT

TSP

HAM-CYCLECLIQUE

VERTEX-COVER

SUBSET-SUM

Page 109: 1 NPC NP-Complete Problems 2 We can solve any problem(!)(?) … but perhaps not with a computer! Some problems are not computable: The Halting Problem:

109

Solving hard problems

Relax the problem -- use approx. algo.

Relax the method -- use probabilistic algo and give up total correctness

Relax the architecture -- use parallel

Relax the machine -- use analog computer ?