Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
NP-Completeness
o Complexity
o Easy and Hard Problems (Intractability)
o P, NP, NP-Complete
o NP-Completeness Proofs
• Informal Illustration
• Key Concepts
• Formal definitions: P,NP,NP-Complete
• First NP-Complete Problem
• NP-Completeness proofs
• Examples
2
• How do computer scientists differentiate
between good (efficient) and bad (not
efficient) algorithms?
3
Polynomial vs Exponential growth
Ordered functions by their growth rates Order
c constant 1
lg n logarithmic 2
lgc n polylogarithmic 3
nr ,0<r<1 sublinear 4
n linear 5
nr ,1<r<2 subquadratic 6
n2 quadratic 7
n3 cubic 8
nc,c≥1 polynomial 9
Efficient algorithms
Not efficient algorithms
4
Polynomial vs Exponential growth
5
Polynomial vs Exponential growth
6
Easy vs Hard Problems
• Easy Problems
A problem X to be “easy” or efficiently solvable, if there is a
polynomial time algorithm A for solving X
Class P of problems are solvable in polynomial time
• Hard problems
Any problem for which there is no polynomial time algorithm
is an intractable problem
7
Problem 1: Problem 2:
Input: A road map of cities Input: A road map of cities
with distances attached to the with distances attached to the
road map, and two designated road map, and two designated
cities A and B cities A and B
Output: A description of the Output: A description of the
shortest path between A and B longest path between A and B
9
Polynomial-time Algorithm
(Tractable)
o Polynomial-time algorithms are “Tractable”, “Easy”,
eg. O(n), O(1), O(n3), O(n lg n)
o Superpolynomial time: “Intractable”, “Hard”,
eg. O(2n),O(nn),O(n!)
10
11
P, NP, NPC
Traditional definition of NP
• Turing machine model of computation
– Simple model where data is on an infinite capacity tape
– Only operations are reading char stored in current tape cell, writing a
char to current tape cell, moving tape head left or right one square
12
P, NP, NPC
Nondeterministic Running Time
3- 15
Nondeterministic operations and functions
17
P, NP, NPC
Showing a problem is in NP (illustration)
2
Guess Phase
1 3 (Nondeterministic)
Yes input graph ---------------
123 132 213 231 312 321 Verify Phase
(Deterministic)
2
Guess Phase
1 3 (Nondeterministic)
No input graph ---------------
123132213 231 312321 Verify Phase
(Deterministic)
18
P, NP, NPC
Alternate definition of NP
• Preliminary Definitions
– Let P be a decision problem
– Let I be an input instance of P
– Let Y(P) be the set of yes input instances of P
– Let N(P) be the set of no input instances of P
• P belongs to NP iff
• For any I ε Y(P), there exists a “certificate” [solution] C(I)
such that a deterministic algorithm can verify I ε Y(P) in
polynomial time with the help of C(I)
• For any I ε N(P), no “certificate” [solution] C(I) will convince
the algorithm that I ε Y(P).
19
P, NP, NPC
Showing a problem is in NP (illustration)
• Certificate [Solution]
Guess Phase • A Hamiltonian Path
(Nondeterministic)
--------------- • C(I1): 123 or 321
123 132 213 231 312 321 Verify Phase • C(I2): none
(Deterministic)
• Verification Alg:
• Verify certificate is a
possible HP
• Check for edge
2 between all adjacent
nodes in path
1 3
Yes input graph
20
P, NP, NPC
Proving problem in NP
21
P, NP, NPC
Proving problem in NP: Clique problem
• Clique Problem
– Input: Undirected graph G = (V,E), integer k
– Y/N Question: Does G contain a clique of size ≥ k?
• Certificate
– A clique C of size at least k
• Verification algorithm
– Verify this is a potential clique of size k
– Verify that all nodes in C are connected in E
22
P, NP, NPC
Proving problem in NP: Vertex Cover problem
• Vertex Cover
– Input: Undirected graph G = (V,E), integer k
– Y/N Question: Does G contain a vertex cover of size ≤ k?
• Vertex cover: A set of vertices C such that for every edge (u,v) in E,
either u is in C or v is in C (or both are in C)
• Certificate
– A vertex cover C of size at most k
• Verification algorithm
– Verify C is a potential vertex cover of size at most k
– Verify that all edges in E contain a node in C
23
P, NP, NPC
Proving problem in NP: Satisfiability problem
• Satisfiability
– Input: Set of variables X and set of clauses C over X
– Y/N Question: Is there a satisfying truth assignment T for
the variables in X such that all clauses in C are true?
• Certificate?
• Verification algorithm?
24
Certifiers and Certificates: 3-Satisfiability
Certifier. Check that each clause in has at least one true literal.
Ex.
x1 x2 x3 x1 x2 x3 x1 x2 x4 x1 x3 x4
instance s
x1 1, x2 1, x3 0, x4 1
certificate t
25
Certifiers and Certificates: Hamiltonian Cycle
instance s certificate t
26
27
P, NP, NPC
29
P, NP, NPC
NP NP-
Hard
P
NP Complete
30
P, NP, NPC
Class NP
A decision problem is solvable in Non-deterministic Polynomial time:
– The solution can be verified in polynomial time, eg. verifying that a
path has length greater than K
– If we have an exponential number of processors, we can check all
possible solutions simultaneously and therefore answer in
polynomial time
Class NPC
The word complete means that every problem in the class NPC can be
reduced (in polynomial time) into another problem of the class NPC.
The class of NPC problems is the class of the hardest computational
problems in the class NP: every NP problem can be transformed
into an NPC problem (the reverse is not true!!!)
31
P, NP, NPC
32
P, NP, NPC
• Completeness
– if someone were to find a polynomial time solution for a single
problem in the class NP-complete all the problems could be
solved in polynomial time!!!
– if someone were to prove an exponential lower bound for a
single problem in the class NP-complete all the problems in
the class would be intractable !!!
33
P, NP, NPC
Class NPC (The class of NP-complete problems):
34
P, NP, NPC
Why study P algorithms?
35
P, NP, NPC
Why study NP-completeness?
36
Examples
1. Shortest vs longest simple paths
Single-source shortest paths can be found in O(ve) time.
But finding longest simple path between 2 vertices is NP-complete
37
Examples
3. 2-CNF satisfiability vs 3-CNF satisfiability (k-conjunctive normal form)
A boolean formula is satisfiable if there is some assignment of the 0/1 variable
values that makes the formula results in “1”.
Example:
2 elements 3 elements
38
How to prove NP-Completeness
Informal illustration:
• The concept of “Polynomial-time reducibility”
- For comparing the hardness of problems.
• How to prove a problem to be NP-complete
(the first NP-complete problem).
• Based on some already known NP-complete problems, how to
prove some other problems are NP-complete.
39
How to prove NP-Completeness
Informal Illustration
Given 2 problems: P1 and P2, Instance of P1
• We map an input problem instance of
P1 to P2 , Solve
• Then solve for P2, for P1 Map instance of P1 to
Polynomial-time
Instance of P2
Reduction
• And then obtain the answer for P1
directly from the answer from P2. Instance of P2
If the mapping (reduction) is in
polynomial time, then we say: Solve for P2
Answer of
P1 is polynomial-time reducible to P2. instance of P2
We can use P2 as a subroutine of P1, with Answer of
only a polynomial amount of overhead. instance of P1
Note that, if we can solve a problem P1 by mainly solving P2, this does not imply P1
is harder than P2. Indeed, we can say the P2 algorithm is a powerful engine that
can solve for P1, although maybe P1 is only a piece of cake.
40
How to prove NP-Completeness
Informal Illustration
41
How to prove NP-Completeness
Informal Illustration
42
How to prove NP-Completeness
Informal Illustration
1
We can reduce the H problem to the T problem as shown.
Then we solve the T problem with k=number of edges in the H problem.
Then we use the result of the T problem to answer the H problem.
44
How to prove NP-Completeness
3 Key concepts for the proofs
B. Reductions
• We have 2 decision problems: A and B. An algorithm
to decide A
• We have a a polynomial-time reduction
Polynomial-time
algorithm that transforms any instance of A
reduction algorithm
into some instance of B such that the
answers (yes/no) to and are the same.
An algorithm
• “Problem A is hard” => “ ” to decide B
• “A is NP-Complete” => “ ” yes no
if B is also in NP.
yes no
48
Accepted vs Decided (Class P)
• Decision problems : map the problem instances to {0,1} (True/False).
• Given an algorithm A and an input instance x A(x) will returns 1 if A accepts
x and 0 if A rejects x.
• Consider all inputs x1,x2,..., xn that results “1”.
Suppose L is a set such that L = {x1,x2,..., xn}.
Then L is accepted by A if every element xi in L is accepted by A.
(But A may loop forever for an element x L without rejecting it)
L is decided by A if every element xi in L is accepted by A and
every element not in L is rejected by A.
For Turing’s Halting Problem, there exists an accepting algorithm, but no
decision algorithm exists.
• The complexity class P: A problem belongs to P if and only if it has
a polynomial time decision algorithm.
49
Verification Algorithm (CLASS NP)
• Consider the decision problem PATH, for a given problem instance
x = <G,u,v,k>, we need to determine whether it is true or false.
If we are also given a path from u to v (a certificate) that helps to prove for k,
then the verification of x is easy.
50
NP-Completeness and the Proofs
• A problem, A, is NP-Complete: CIRCUIT-SAT
(1) A NP, and
(2) L p A, for every L NP. [NP-hard] SAT
• We also say that A belongs to NPC
3-CNF-SAT
• Some famous NP-Complete problems:
CIRCUIT-SAT, SAT, 3-CNF-SAT, CLIQUE, ..
CLIQUE SUBSET-SUM
• Alternatively, we can conclude that a
problem, A, is NP-Complete if: VERTEX-COVER
(1) , and
(2’) , for some L NPC [=> ] HAM-COVER
x1
x2
Output
x3
TSP
56
SAT Problem
Formula-Satisfiability Given a boolean formula of n boolean
Problem variables, m boolean connectives, and required
(SAT) parenthesis, is it satisfiable?
57
SAT Problem
Part A of the proof: SATNP
2-CNF x? x? x? x? x? x?
Eg. (x1x2)(x3x4)(x1x3) or?x
(x or?x
or?)(x or?x
or ?)(x or ?)..
x? x? x? x? x? x?
3-CNF
Eg. ( x1x1x2) x? x? x? x? x? x? x? x? x?
or?x
(x or?x or?or??x
or?)(x or?x
or?)(x or?x
or?)..
(x3x2x4) x? x? x? x? x? x? x? x? x?
(x1x3x4)
61
The 3-CNF-SAT Problem
Part A of the proof: 3-CNF-SATNP
3-CNF-SAT is NP-complete
2 parts of the proof: A 3-CNF Example:
A. 3-CNF-SAT NP (x1x1x2)(x3x2x4)(x1x3x4)
B. 3-CNF-SAT is NP-hard
The Reduction :
y1
Step 1: Create a binary “parse” tree for the formula.
y2
Step 2: Rewrite it in the form:
y3 y4 x2
y1 (y1 (y2 x2))
(y2 (y3 y4))
(y3 (x1 x2 )) x1 x2 y5
(y4 y5)
(y5 (y6 x4)) y6 x4
(y6 ( x1 x3))
x1 x3 63
Part B of the proof: 3-CNF-SAT is NP-hard
Show that SAT p 3-CNF-SAT
A formula: eg. ((x1 x2) ((x1 x3 ) x4 )) x2
ie. Any instance of formula satisfiability
can be reduced in polynomial time to an 3-CNF eg. (x1x1x2)(x3x2x4)(x1x3x4)…
instance of 3-CNF formula satisfiability.
The Reduction :
Step 3: Change each sub-clause of the following to an OR of literals:
y1 (y1 (y2 x2)) Eg. y1 y2 x2 (y1 (y2 x2)) (y y x )
(y2 (y3 y4)) 1 1 1 0 1 2 2
(y3 (x1 x2 )) 1 1 0
1 0 1
1
0 (y1y2x2)
(y4 y5) 1 0 0 0
(y5 (y6 x4))
0 1 1
0 1 0
1
0
(y1y2x2)
(y6 ( x1 x3)) 0 0 1
0 0 0
1
1 (y1y2 x2)
SAT
3-CNF-SAT
CLIQUE SUBSET-SUM
VERTEX-COVER
HAM-CYCLE
TSP
66
The Clique Problem
A complete graph A Clique in an undirected graph
G=(V,E) is a complete subgraph of
u v G.
u v
y x z w
y x
Clique Problem
Optimization problem: Find a clique of maximum size in a graph.
Decision problem: Whether a clique of a given size k exists in the graph.
67
The CLIQUE Problem
Part A of the proof: CLIQUENP
CLIQUE is NP-complete u v
2 parts of the proof:
A. CLIQUE NP
z w
B. CLIQUE is NP-hard
y x
Part A of the proof: CLIQUE NP
Consider an algorithm:
bool Verify_CLIQUE( Input_Graph,
Certificate /*a set of vertices in the input graph*/ )
This algorithm checks whether the set of vertices in the certificate are
linked up as a complete graph.
This is a 2-input, polynomial-time verification algorithm for CLIQUE.
Since we can find such an algorithm for CLIQUE, we say that CLIQUE can
be verified in polynomial time, and CLIQUENP.
68
The CLIQUE Problem
Part B of the proof: CLIQUE is NP-hard
Part B of the proof: CLIQUE is NP-hard A 3-CNF Example:
Show that 3-CNF-SAT p CLIQUE (x1x2x3)(x1x2x3)(x1x2x3)
ie.Any instance of 3-CNF formula satisfiability can x x
be reduced in polynomial time to an instance of CLIQUE. x1 2 3
The Reduction :
Describe a 3-CNF formula with k sub-clauses as: x x1
1
(l11 l12 l13) (l21 l22 l23) .. (lk1 lk2 lk3)
x2 x2
Reduce it to a clique problem such that it is
satisfiable if and only if a corresponding graph x3 x3
has a clique of size k.
Polynomial time reduction
Step 1: Represent each “term” lr as a vertex v ri .
i
A 3-CNF-SAT instance
Step 2: Create the edges for any two
A CLIQUE instance
vertices: vri and vsj if: s.t. result of 3-CNF-
r s and lri is not the negation of lsj SAT instance = result
of CLIQUE instance69
The VERTEX-COVER Problem
A vertex Covers A Vertex-Cover in an undirected graph is a set of
a set of edges: vertices that cover all edges in the graph.
u v u v u v
z w z w z w
y x y x y x
Vertex-Cover = {w,z}
Vertex-Cover Problem
Optimization problem: Find a vertex-cover of minimum size in a graph.
Decision problem: Whether a graph has a vertex-cover of a given size k.
70
The VERTEX-COVER Problem
Part A of the proof: VERTEX-COVERNP
VERTEX-COVER is NP-complete
2 parts of the proof: u v
A. VERTEX-COVER NP
B. VERTEX-COVER is NP-hard z w
y x
Part A of the proof: VERTEX-COVER NP
Consider an algorithm:
bool Verify_VERTEX_COVER( Input_Graph,
Certificate /*a set of vertices*/ )
This algorithm checks whether the set of vertices in the certificate cover all
edges in the input graph.
This is a 2-input, polynomial-time verification algorithm for VERTEX-COVER.
Since we can find such an algorithm for VERTEX-COVER, we say that VERTEX-
COVER can be verified in polynomial time, and VERTEX-COVER NP.
71
The VERTEX-COVER Problem
Part B of the proof: VERTEX-COVER is NP-hard
Part B of the proof: VERTEX-COVER is NP-hard u v
Show that CLIQUE p VERTEX-COVER
z w
ie.Any instance of CLIQUE satisfiability can be reduced
in polynomial time to an instance of VERTEX-COVER. y x
The Reduction :
For any CLIQUE satisfiability problem < G=(V,E), k>, we can create a
VERTEX-COVER problem <G’,|V|-k> such that G’ is the complement of G.
Example: G G’ = complement of G
CLIQUE problem: VERTEX-COVER
In the graph u v u v problem:
G=(V,E), can we In the graph G’, can
z w z w
find a clique of we find a clique of
size k=4? y x y x size |V|-k=2?
The HAM-CYCLE z w
Problem
Does a graph have a
hamiltonian cycle? y x
HAM-CYCLE is NP-complete
2 parts of the proof:
A. HAM-CYCLE NP
B. HAM-CYCLE is NP-hard
73
The HAM-CYCLE Problem
Part B of the proof: HAM-CYCLE is NP-hard
Part A of the proof: HAM-CYCLE NP
Consider an algorithm:
bool Verify_HAM-CYCLE (Input_Graph,Certificate /*a seq of vertices of a ham-cycle*/ )
It checks: 1. The certificate covers all vertices in input_graph exactly once.
2. The certificate is a valid path (each vertex links to next one).
3. An edge joins the first and the last vertex in the certificate.
This is a 2-input, polynomial-time verification algorithm for HAM-CYCLE.
Since we can find such an algorithm for HAM-CYCLE, we say that
HAM-CYCLE can be verified in polynomial time, and HAM-CYCLE NP.
75