NPC
NP-Complete Problems
1
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?
2
• 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)?
3
• boolean Proof by contradiction:
halts(Program p,Input i);
which returns true if p halts on
input i, and false if it doesn’t. • Assume: tS(tS) halts
and returns true.
• boolean
loopIfHalts(Program p,
Input i) { • loopIfHalts(tS,tS) =
true
if (halts(p,i))
• halts(tS,tS) = false
while (true) ;
• tS(tS) doesn't halt which
else is a contradiction
return true;
• Assume:
}
tS(tS) does
• boolean
not halt
tS(Program p)
• halts(tS,tS) = true
{return
• contradiction
loopIfHalts(p,p);
} 4
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!
5
Circuit Satisfiability:
• Find an assignment of truth values to
the inputs of a logic circuit, that
results in an output of 1.
x1
x2
x3
6
Hamiltonian Cycles:
• Given a graph G, find a simple cycle
that contains each vertex in G.
7
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.
8
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') 9
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}*
10
General picture:
Abstract Encodings, e Concrete
Problems Problems
For example:
Does e(G)
Is G a
represent a
connected
connected
graph?
graph?
11
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.
12
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.
13
The set of concrete decision
problems that are solvable in
polynomial time.
14
Polynomial Time Solvability for
Abstract Algorithms:
• We would like to extend these ideas to
abstract algorithms:
Abstract Encodings, e Concrete
Problems Problems
• An abstract decision problem Q is
solvable in polynomial time if the encoding
e(Q) is in P.
• This depends on the encoding used! 15
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.
16
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!
17
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.
e1 {0,1}*
Instances f g
e2
{0,1}* 18
• 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.
19
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 …
20
• 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.
21
• 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. 22
• 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?
23
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.
24
NP stands for
non-deterministic
polynomial
The set of concrete decision
problems that are verifiable in
polynomial time.
25
“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.
26
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.
27
Reductions
28
Motivation
• Reductions are our main tool for
comparison between problems.
29
Introduction
• Objectives:
– To formalize the notion of “reductions”.
• Overview:
– Karp reductions
– HAMPATH p HAMCYCLE
– Closeness under reductions
– Cook reductions
– Completeness
30
Reductions – Concept
IF we can translate problem A to problem B…
A p B
THEN B is at least as hard as A.
31
SIP 250
Reductions – Formal
I.e – there exists a polynomial time
TM which halts with f(w) on its tape
Definition when given input w.
Definition: Language A is polynomial
time reducible to language B if…
a polynomial time computable function
f:** exists, Denote: A B
p
where for every w,
wA f(w)B
f is called the polynomial time reduction of A to B.
32
Reductions and Algorithms
A B
f
* - A * - B
* *
33
Reductions and Algorithms
polynomial time algorithm for A
polynomial time
algorithm for B
f(w) f(w)B
w f ? wA
?
34
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)B
2. If f(w)B then wA
35
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.
36
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.
37
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.
38
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.
39
HAMPATH p HAMCYCLE
f( ) =
n
s s
t p t
40
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.
41
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.
42
How to Reduce?
1. Construct f.
2. Show f is polynomial time easy to
verify
computable.
3. Prove f is a reduction, i.e show:
1. If wHAMPATH then
f(w)HAMCYCLE
2. If f(w)HAMCYCLE then
wHAMPATH
43
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.
C
L‟
L
44
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)
45
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 B
polynomial time
algorithm for A
46
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
Make oracle
sure it‟s
efficient!
47
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.
48
• If L1 P L2 and L2 is in P, then so is L1.
x f(x) f(x)L2 xL1
F A2
Reduction Algorithm for
algorithm deciding L2
Algorithm for
deciding L1
49
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.
50
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.
51
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 …
52
NPC
Contains thousands of
distinct problem
NPC each reducible to all
others
exponential algorithms
?
efficient algorithms
53
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
54
P = NP co-NP ?
Any language in NP co-NP - NP ?
NP = co-NP
P = NP = co-NP
P
NP
co-NP co-NP NP co-NP
P = NP co-NP NP
P
Four possible relationships between complexity
classes 55
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.
56
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.
57
Circuit Satisfiability is NP-hard:
• 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.
58
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.
59
• 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 x y state
Logic M
circuit
A pc x y state
60
2. Every NP Problem ≤P CIRCUIT-SAT
(CIRCUIT-SAT is NP-hard) (2)
61
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.
62
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 part
Suppose 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.
63
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
64
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.
65
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
66
Formula Satisfiability (SAT)
=x10 (x4 x3)
(x5 (x1x2))
(x6 x4)
(x7 (x1x2x4))
(x8 (x5x6))
(x9 (x6x7))
(x10
(x7x8x9))
67
Dictionary
negation: not ()
conjunction: and ()
literal: (negated or not) Boolean variable disjunction: or ()
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)
68
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.
69
3SAT
Instance: a 3CNF formula
Problem: To decide if the formula is satisfiable.
A satifiable 3CNF formula (xyz)(xyz)
An unsatifiable 3CNF formula (xxx)(xxx)
70
SIP 259-260
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?
71
CNF 3CNF
(xy)(x1x2... xt)...
clauses with 1 or clauses with more than 3
2 literals literals
replication split
(xyx) (x1 x2 c11)(c11 x3 c12)... (c1t-3 xt-1xt)
72
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.
73
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.
74
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)
75
SIP 251-253
CLIQUE is NP-Complete
Proof: We‟ll show 3SATpCLIQUE.
(.. .. ..) ... (.. .. ..)
76
The Reduction
for any clause ()
|V| = formula‟s length
K= no. of clauses
connected
iff
77
Proof of Correctness
NOT
connected!
1
. a clique of size k must
contain one node from
.
every layer.
.
k
78
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.
79
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.
80
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)
81
INDEPENDENT-SET is NPC
Proof: By the previous claim and a
trivial reduction from CLIQUE.
there‟s a clique there‟s an IS of
of size k in a size k in its
graph complement
82
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.
83
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)
84
SIP 269-271
SUBSET-SUM is NP-
Complete
Proof: We‟ll show 3SATpSUBSET-
SUM.
(.. .. ..) ... (.. .. ..)
85
Examples SUBSET-SUM
2,4,8, 10 SUBSET - SUM … because 2+8=10
2,4,8, 11 SUBSET
- SUM
… because 11 cannot be made out of {2,4,8}
SUBSET - SUM { S, t | S x1,..., x k
there is a subset R S
such that y
yR
t}
86
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. 87
Subset Sum clause: 1 2 3 4
+x1 1 0 0 0 1 0 0 1
3CNF formula: –x1 1 0 0 0 0 1 0 0
+x2 1 0 0 1 1 0 0
( x1 x 2 x 3 ) –x2 1 0 0 0 0 1 0
( x1 x 2 x 4 ) +x3 1 0 1 0 0 0
(x2 x2 x3 ) –x3 1 0 0 0 1 1
+x4 1 0 1 0 0
( x1 x 3 x 4 )
–x4 1 0 0 0 1
1 0 0 0
1 0 0 0
Make the number table,
1 0 0
and the ‗target sum‘ t 1 0 0
1 0 dummies
1 0
1
1
1 1 1 1 3 3 3 3
+ 88
Reducing 3SAT to SubSet
•
Sum and variables
Let 3CNF with k clauses
x1,…,x.
• Create a Subset-Sum instance 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 t 11 3 3
k
89
( x1 x 2 x 3 ) ( x1 x 2 x 4 )
Subset ( x 2 x 2 x 3 ) ( x1 x 3 x 4 )
Sum1: The ―1111‖
Note
+x1
–x1
1
1
0 0 0 1
0 0 0 0 1
0 0 1
0 0
+x2 1 0 0 1 1 0 0
in the target forces
–x2 1 0 0 0 0 1 0
a proper assignment +x3 1 0 1 0 0 0
of the xi variables. –x3 1 0 0 0 1 1
+x4 1 0 1 0 0
–x4 1 0 0 0 1
Note 2: The target 1 0 0 0
―3333‖ is only possible 1 0 0 0
if each clause is satisfied. 1 0 0
1 0 0
(The dummies can add
1 0
maximally 2 extra.) 1 0
1
90
1 1 1 1 3
3 3 3 1
(x1 x 2 x 3 ) (x1 x 2 x 4 )
Subset (x 2 x 2 x 3 ) (x1 x 3 x 4 )
Sum +x1
–x1
1
1
0 0 0 1
0 0 0 0 1
0 0 1
0 0
1 0 0 0 1 0 0 1 +x2 1 0 0 1 1 0 0
1 0 0 0 0 1 0 –x2 1 0 0 0 0 1 0
1 0 0 0 1 1 +x3 1 0 1 0 0 0
1 0 1 0 0 –x3 1 0 0 0 1 1
1 0 0 0 +x4 1 0 1 0 0
1 0 0 0 –x4 1 0 0 0 1
1 0 0 1 0 0 0
1 0 0 1 0 0 0
1 0 1 0 0
1 + 1 0 0
1 1 1 1 3 3 3 3 1 0
1 0
x1, x 2 , x 3 , x 4 is a satisfying
1
assignment 1 1 1 1 3 3 3 1
3
91
(x1 x 2 x 3 ) (x1 x 2 x 4 )
Subset Sum
(x 2 x 2 x 3 ) (x1 x 3 x 4 )
+x1 1 0 0 0 1 0 0 1
–x1 1 0 0 0 0 1 0 0
1 0 0 0 0 1 0 0 +x2 1 0 0 1 1 0 0
1 0 0 0 0 1 0 –x2 1 0 0 0 0 1 0
1 0 0 0 1 1 +x3 1 0 1 0 0 0
1 0 1 0 0 –x3 1 0 0 0 1 1
1 0 0 0 +x4 1 0 1 0 0
1 0 0 0 –x4 1 0 0 0 1
? ? ? + 1 0 0 0
1 1 1 1 2 ? ? ? 1 0 0 0
1 0 0
x1, x 2 , x 3 , x 4 is not a 1 0 0
satisfying assignment 1 0
1 0
1
92
1 1 1 1 3
3 3 3 1
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
.
3SAT if and only if S ,1...13...3 Subset - Sum
93
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. 94
Directed Hamiltonian Path
• Given a directed graph G, does there exist
a Hamiltonian path, which visits all nodes
exactly once?
but this graph has no
• Two Examples: Hamiltonian path
this graph has a
Hamiltonian path
95
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 .
96
“Zig-Zag Graphs‖ s
There are 2L different Hamiltonian
paths from s to the target t. x1
For every i, ―Zig‖ x2
means
xi
xi=True
… while ―Zag‖
means xL
xi xi=False
t 97
Accessing the Clauses
If has K clauses, = C1 Ck,
then xi has K ―hubs‖:
Idea: Make K extra
xi nodes Cj that can only
be visited via the hub-
path that defines an
stands for
assignment of xi which
xi satisfies the clause
(using zig/zag = T/F).
1 2 K
98
Connecting the Clauses
Let the clause Cj contain xi:
Cj
If C j ( x i y z)
then xi=True=―zig‖ xi
reaches node Cj j
If C j ( x i y z)
Cj
then xi=False=―zag‖
reaches node Cj
xi
j
99
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.
SAT if and only if G HamPath
100
Example
( x1 x 2 x 3 ) ( x1 x 2 x 4 )
( x 2 x 2 x 3 ) ( x1 x 3 x 4 )
s
4 variables… x1
( x1 x 2 x 3 )
4 clauses…
x2 ( x1 x 2 x 4 )
(x2 x2 x3 )
Clauses x3
connected ( x1 x 3 x 4 )
via zig-zag
x4
―hubs‖
101
t
Example
( x1 x 2 x 3 ) ( x1 x 2 x 4 )
( x 2 x 2 x 3 ) ( x1 x 3 x 4 )
s
x1
x1, x 2 , x 3 , x 4 ( x1 x 2 x 3 )
assignment…
x2 ( x1 x 2 x 4 )
…satifies all four (x2 x2 x3 )
clauses; hence x3
it defines a ( x1 x 3 x 4 )
Hamiltonian Path
x4
102
t
( x1 x 2 x 3 ) ( x1 x 2 x 4 )
Example ( x 2 x 2 x 3 ) ( x1 x 3 x 4 )
s
x1
x1, x 2 , x 3 , x 4 ( x1 x 2 x 3 )
assignment…
x2 ( x1 x 2 x 4 )
…does not satify (x2 x2 x3 )
first clause; hence x3
the path misses ( x1 x 3 x 4 )
the C1 node
x4
103
t
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
104
Forcing Directions
Given 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 with
edges 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.
105
Example: Directions
x x
s y s y
becomes becomes
xin yin xin yin
xmid ymid xmid ymid
sout sout
xout yout xout yout
“Undirected HamPath‖ is NP-complete
106
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 107
CIRCUIT-SAT
SAT
3-CNF-SAT
CLIQUE HAM-CYCLE
TSP
VERTEX-COVER
SUBSET-SUM
108
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 ?
109