NP-Completeness: Problems Proofs Approximations
NP-Completeness: Problems Proofs Approximations
NP-Completeness: Problems Proofs Approximations
Problems
Proofs
Approximations
Decision Problems
Given Some Universal Set X,
Let R be a subset of X.
The decision problem for R is:
Given an arbitrary element a of X, does
a belong to R?
Note: X is usually assumed to be a set of
strings, but this can be interpreted loosely.
The class P
Let R be a set of strings. If there exists
a Polynomial time algorithm: O(n), O(n 2), ...
that solves the decision problem for R,
Then R is in the class P.
Note the use of the Big O notation.
Sorting, O(n lg n), is in P.
Binary Search, O(lg n), is in P.
What is Nondeterminism?
A
A
This is a deterministic
Finite State Machine.
B
B
A
B
A
A
A Nondeterministic Machine
B
A
B
A
This machine is
nondeterministic
There may be
two output arcs
with the same
label.
There may be no
output arc for some
inputs.
X
A
B
X
Y
B
A
B
B
A
B
B
A
B
A
B
B
A
Simple:
No Extra
Storage
A
A
B
A
PDM:
An auxiliary
Stack
Turing Machine:
An auxiliary
Read/Write Tape
Deterministic Conversions
Any Non-Deterministic FSM (no aux.
storage) Can be converted to a deterministic
machine in quickly. (All FSMs run in O(n)
time.)
Non-Deterministic PDMs are more powerful
than deterministic PDMs. All PDMs run in
O(n) time, but converting from Non-Det.
PDM to a real algorithm might produce an
O(n3) algorithm.
TM Deterministic Conversion
Deterministic and Non-Deterministic
TMs are equally Powerful.
Any Non-Deterministic TM can be
converted to a deterministic TM
The conversion may cause an
exponential slow-down in running time.
(We dont know if this is neccessary, but
no one has proven that it isnt.)
Non-Deterministic Algorithms
Working with Turing Machines is too
difficult to be practical.
Since TMs and programming
languages are equivalent, TMs are
always represented as HLL programs.
NonDeterminism is introduced using the
statement: V := SELECT(A,B,C,...);
NonDeterministic Acceptance
Why NonDeterminism?
NP is the set of problems that can be solved in
Polynomial time by nondeterministic
algorithms.
Many interesting problems are easy to
formulate as polynomial time nondeterministic
algorithms.
No known polynomial time algorithms exist for
these problems.
In general we dont know if P=NP.
Completeness
Given a class of problems K, (remember
that K must be a set of sets of strings)
A Problem R is K-Hard, if a solution to R
would allow us to solve every problem in
K.
A problem R is K-Complete, if it is KHard, and a member of K.
NP-Completeness
A problem R is NP complete if:
R is in NP (i.e. there exists a nondeterministic
polynomial time algorithm that recognizes the
elements of R)
R is NP-Hard (i.e. finding a deterministic
polynomial-time algorithm that recognizes R,
would allow us to recognize any problem in
NP in polynomial time.
To Show NP-Completeness
To show that R is NP-Complete:
First construct a Nondeterministic
Polynomial time algorithm for R.
Then show that if XNP then X can be
transformed into an instance of R in
polynomial time.
SAT is in NP
SAT(e)
k = the number of distinct variables in e;
Allocate a boolean array v of size k;
for i=1 to k do
v[i] := select({TRUE,FALSE});
endfor
evaluate e on v and assign the result to R;
if (R=TRUE) then
accept;
endif
end SAT
3-SAT (3-Satisfiability)
Given a CNF boolean expression
C=c[1]&c[2]& ... &c[m] such that every clause
c[i] has exactly 3 literals, is C satisfiable?
VC (Vertex Cover)
Given a graph G=(V,E) and a positive integer K|
V|, is there a set V V such that |V|<K and for
each {u,v} E, at least one of u or v is in V?
CLIQUE
Given a graph G=(V,E) and a positive integer J|
V|, does G have a subgraph which is a complete
graph with J vertices?
HC (Hamiltonian Circuit)
Given a graph G=(V,E) is there a simple cycle in G
that contains all vertices of G.
PARTITION
Given a set of positive integers A, is there a subset
A A such that the sum of the elements of A is
exactly half the sum of the elements of A?
MINIMUM COVER
Given a collection C of subsets of a set S, and a
positive integer K, is there a set CC such that |
C|K and every element of S is contained in at
least one element of C.
HITTING SET
Given a collection C of subsets of a set S and a
positive integer K, is there a set SS such that |
S|K and S contains at least one element from
every set in C?
SUBGRAPH ISOMORPHISM
Given two graphs G=(V,E) and H=(V,H), does G
contain an exact copy of H as a subgraph?
KNAPSACK
Given a finite set U such that every element
uU has a size s(u) and a value v(u), both
of which are positive integers, and given
two positive integers B and K, is there a
subset U of U such that the total size of
the elements of U is less than or equal to
B and the total value fo the elements of U
is greater than or equal to K?
MULTIPROCESSOR SCHEDULING
Given a set A of tasks, such that each aA
has a length l(a) which is a positive integer,
and given a number of processors m, and a
deadline D, both of which are positive
integers, is there a pertition of A into disjoint
subsets A=A1A2... Am such that for
eany subset Ai, the total length of all tasks in
Ai is less than or equal to D?
3-SAT - 2
Example Only
4 or more is
similar
if c[j] = (A B C D E F)
(A B S[j,1])
(S[j,1] C S[j,2])
(S[j,2] D S[j,3])
(S[j,3] D F)
3-Sat: Proof
Left as an exercise
For each of the four different
transformations, show that the
generated set of clauses can be set to
TRUE if and only if the original clause
can be set to TRUE
One pair
per clause
u1
a1
b1
b2
a4
One structure
per Variable
u4
u2
a2
b4
a3
b3
u3
u3
u2
Modeling
TRUE
and
FALSE
3DM: Notes
One Star is constructed for each
variable.
There are 2 points for each clause
A different set of ax and bx variables are
used for each star
To form a complete matching AT LEAST
one triangle must be selected from
each star.
3DM Notes
To cover all the ax and bx variables, it is
necessary to select every other point.
Either the ux or the ux points must be
selected. All of one and none of the
other.
This models a variable being TRUE or
FALSE.
3D Match
C la u s e 1 : (U ^ V ^ W )
U1
Satisfaction
Tester
e1
New
Triple
Specification
S1
T1
e2
V1
W 1
e3
3DM Notes
3DM Notes
3D Matching
There much be one
blade for each point
in each star.
Garbage
Collection
U1
2nm blades
in each stack.
U1
U2
There must
be one stack
for each
unused star
point
U2
m(n-1) stacks.
G1
H1
2nmm(n-1)
Elements
Vertex Cover
A Vertex Cover of a Graph G=(V,E) is a
set VV such that for every edge
(a,b)E, either aV or bV.
That is, V contains at least one
endpoint of every edge.
Optimization: Find the smallest vertex
cover of G.
Decision: Does G have a vertex cover
of size K?
The complement of G
G
Complete Graph
on N Vertices
Complement
of G
Vertex Cover
Transformation from 3-Sat.
Transform Each variable into a pair of
vertices labeled with the variable and its
x: x
x'
complement.
Transform each clause into a ring of 3 y'
{x,y',z}:
vertices labeled with the literals.
x
Connect identically labeled vertices with
edges. (See Next Slide.)
Vertex Cover
N Variables
M Clauses
K=2M+N
e
Join
Clause
to
Literal
Hamiltonian Circuit
Transformation from Vertex Cover
The U-Side
U
The V-Side
Q
V
The V-Side
W
+ K an Integer
The W-Side
Hamiltonian Circuit
The U-Side
The V-Side
The U-Side
The V-Side
The U-Side
The V-Side
Hamiltonian Circuit
Join all the U-Sides together into a loop,
(and all the W-Sides ...)
Let the ends dangle for the moment.
The U-Side
The W-Side
The V-Side
The U-Side
The U-Side
The X-Side
The Q-Side
The U-Side
The U-Side
...
A2
A3
...
AK
Hamiltonian Path
Transformation from vertex cover is
identical.
Break A1 into two vertices A1a and A1b.
Traveling Salesman
Given a complete graph G with
weighted edges, What is the
Hamiltonian Cycle of least weight?
(Every permutation of the vertices is a
Hamiltonian Cycle.)
Decision Problem: Does G have a
Hamiltonian Cycle of weight K?
Traveling Salesman
Conversion from Hamiltonian Cycle.
Given an arbitrary graph G, assign the
weight 1 to each edge.
Add additional edges to G making a
complete graph.
Assign the weight 2 to each new edge.
Set K=n where n is the number of
vertices in G.
Partition
Partition is the key to a number of
numeric problems
An instance of Partition is a set of
numbers A.
The question is Is it possible to divide A
into two disjoint sets A=B U C such that
the sum of the elements of B is equal to
the sum of the elements of C
Partition: Proof
Start with 3DM
Given Four Sets:
W={w1,w2, ,wn}
X={x1,x2, , xn}
Y={y1,y2, , yn}
M={m1,m2, , mk}WXY
...
...
...
Transforming M
We add one segmented number in A for
each ordered triple in M.
If (wi,xj,yh)M then we set the three
segments corresponding to wi, xj, and yh
equal to 1.
All other segments are set equal to 0.
We use ax to denote the number
associated with mxM
Transforming M 2
mx=(wi,xj,yh)
0 0 ...
...
0 0 ...
=ax
...
0 0 ...
...
m1
m2
mk
1 1 ...
...
1 1 ...
...
1 1 ...
...
a1
+a2
+ak
C
Verification
The total of all numbers in A is
C+P+Q=C+2C-B+C+B=4C
If A has a partition, each set must add
up to 2C
If A has a partition, then P and Q must
be in different sets. (P+Q=3C)
A has a partition if and only if there is a
subset A of A whose elements sum to
B.
Verification 2
Consider the set containing P=2C-B. To
reach the target size of 2C, we must
add elements totaling B to this set.
Suppose A has such a set A. Let M be
the subset of M (in 3DM) that
corresponds to A. M is a complete
matching for M.
Verification 3
If any element of W, X, or Y were
missing, a segment of the sum of A
would be zero.
If any element of W, X, or Y appears
twice in M the the sum of A would not
have a 1 in the position corresponding
to that element. (Segments cannot
overflow into one another.)
Verification 4
If M has a complete matching M then
the subset A of A corresponding to M
has the sum B.
Each element of W, X, and Y appears
exactly once in M, so each segment of
the sum must equal one.
Graph 3-Colorability 1
From 3-Sat
For each clause, {A,B,C}, convert it into
the following graph.
A
Note: A, B and C cant
all be the same color.
A, B, and C are the
Literal Vertices
This is the Clause
B
C
Component
Bin Packing
Input: a set of objects B along with a set
of associated sizes, S, such that every
biB there is a size siS. (Sizes not
unique)
For all siS, 0 si 1.
Bin Packing
Decision Problem: Will all objects fit in K
bins?
Transformation from partition.
Given A, let X be the sum of all
elements of A.
Multiply each element by 2/X, and add
to S.
Ask the question, will the elements of S
fit in 2 bins?
Subset Sum
Given a set of numbers S (with possible
duplicates) and an integer K, is there a
subset of S whose sum is equal to K?
Optimization problem: What is the
subset of S with the maximum sum not
exceeding K.
Transformation from partition. Use the
same base set. Let X be sum of all
elements of A. K=X/2.
Knapsack
Knapsack
From Partition:
Let the objects be the numbers from the
partition problem. Set both the size and
the value of the number to be equal to
its value.
Set m=k=half the total size of all
elements.
3-Colorability 2
Create the following
graph segment
x'
Each variable
appears in both
complemented x
and
uncomplemented
form.
y'
z
U
z'
F
3-Colorability 3
The graph segment on the previous
slide is the truth-setting component
The color assigned to the T vertex will
represent True, the color assigned to
the F vertex will represent False, and
the color assigned to the U vertex will
represent other.
3-Colorability 4
Go back to the Clause Components,
and connect each Literal Vertex to the T
vertex of the Truth-Setting component.
If a Literal Vertex corresponds the
variable x, then connect the literal
vertex to the x vertex of the TruthSetting Component
If it represents x, then connect it to the
x vertex.
3-Colorability: Proof
The resultant graph is 3-Colorable, if
and only if the original expression is
satisfiable.
Assign colors in the truth setting
component to be consistent with the
truth assignment.
Because the assignment is satisfying,
at least one literal in each clause must
be assigned the True color.
3-Colorability: Proof 2
Each literal vertex has two neighbors,
one of which has the True color, the
other of which may have either the
True color or the False color.
Since the Truth-Setting component is
colored consistently with a satisfying
assignment, each clause component
will have a Literal Vertex with two True
colored neigbors.
3-Colorability: Proof 3
Use the False color to
color the vertex with two
True neighbors.
Complete the coloring
as follows,
(Red=False,
Blue=True,
B
Yellow=Other)
3-Colorability: Proof 4
Now assume the graph is
3-colorable.
No Literal Vertex can be
colored True.
In a Clause component,
it is impossible to color
all Literal Vertices
B
Other.
?
C
3-Colorability: Proof 5
A least one vertex in every Clause
Component must be colored False.
(The corresponding Vertex In TruthSetting Component is colored True)
Every coloring of the Truth-Setting
Component corresponds to a truthassignment of the original expression.
A three coloring corresponds to a
satisfying assignment.
Exercises (Easy) - 1
LONGEST PATH
Given a graph G=(V,E), and a positive integer K|V|, does G contain a
simple path with K or more edges?
SET PACKING
Given a collection C of finite sets, and a positive integer K|C|, Does C
contain K disjoint sets?
Partition Into Hamiltonian Subgraphs
Given a graph G=(V,E) and a positive integer K|V|, can the vertices of
G be partitioned into kK disjoint sets V1, ..., Vk such that 1ik, the
subgraph induced by Vi contains a Hamiltonian circuit?
Exercises (Easy) - 2
s( a)
i 1 a Ai
k
Exercises (Medium) - 1
Exercises (Medium) - 2
Exercises (Hard)
Approximation Theory
Used For Optimization Problems
Feasible solution: A not-necessarily
optimal solution to the problem
Approximation Theory
Approximation Theory
Given an approximation algorithm A,
and an Input I, A(I) is the approximate
solution, and Size(A(I)) is its size.
The quality ratio of a solution A(I) 1rA(I)
is defined as follows:
size( A( I ))
rA ( I )
opt ( I )
Minimization
opt ( I )
rA ( I )
size( A( I ))
Maximization
Approximation Theory
Approximation Theory
RA(m) is a measure of how close to the
optimal value I can get, regardless of
input size.
RA(m) is infinite for some problems
SA(m) is a measure of how close to the
optimal value one can get, taking input
size into account.
SA(m) is finite.
Approximation Theory
RA = lub {RA(m) | m>0}
SA = lub {SA(m) | m>0}
For some bin-packing approximations,
RA 4/3
For graph coloring, approximation quality
depends on graph size. For existing
algorithms, there are families of 3colorable graphs that require an
arbitrarily large number of colors. R A is
infinite.
Approximations
Bin Packing
Subset Sum
Vertex Cover
Graph Coloring
Euclidean Traveling Salesman
General Traveling Salesman
First Fit
Try each element in each bin, starting with Bin 1.
Add element to new bin if it wont fit in any existing
bin
Elements are not sorted in any way
ei Bi 1
Opt ( I )
(e B ) Opt ( I )
i 1
.2
.4
.3
.3
.3
.3
.3
.5
.3
Niff Solution
.5
.4
.2
Optimal Solution
SNiff3/2
1
k
k k
k
...
n-k n-k+1
...
{1,3,4}
{1,5,6}
{2,4,6}
{4,5,6}
K vertices a1
on the top,
K on the
bottom
a2
a3
a4
b1
b2
b3
b4
Every vertex on
a5 the top
connected to
every vertex on
the bottom
except the one
directly below it.
b6
Composition