[Link]
com/cs/p-np-np-complete-np-hard
CSCE 423/823
NP-completeness
Dr. Qiuming Yao
Fall 2024
*adapted from Cormen et al. 2022, Chapter 34.1, 34.2, 34.3,34.4,34.5.1,34.5.2,34.5.4
*adapted from Cormen et al. 2009, Chapter 34.1, 34.2, 34.3,34.4,34.5.1,34.5.2,34.5.4
1
Algorithms (Solutions)
• What are “efficient” algorithms?
- Running time: linear, polynomial, exponential?
- Polynomial: O(Nk), k can be a large or small
constant
- Superpolynomial: beyond polynomial
• When we talk about asymptotically polynomial, we
refer to the input size N.
- sometimes, N is affected by the encoding of the
inputs.
2
Problems
• A problem may have “efficient” and “not-efficient”
algorithms/solutions.
• A problem may only have “not-efficient” algorithms.
• A problem may not have any solution. (unsolvable, eg. the
famous “Halting Problem”)
• In this chapter, we need a find way to classify the problems
(not algorithms).
• We only focus on decision problems.
- Optimization problems can be converted to decision
problems. (by imposing a bound to the value to be optimized)
eg. shortest path is less than some number.
- Decision problem: with “YES” or “NO” answers.
- If decision problem is hard, optimization problem is hard.
3
P problems (why important?)
• Polynomial solvable problems.
• These are considered easy problems.
• We are interested in P problems, because:
- P problems can be improved easily.
eg. O(N100) algorithm exists, more efficient
algorithms may exist
- P problems can be across different computer
models/architects
- An ensemble (add or multiplication) of P problems
can be still a P problem.
4
NP problems (nondeterministic
polynomial)
• P: problem can be resolved in polynomial time.
• NP: We don’t know if a decision problem can be solved
in polynomial time. But we at least know the problem
can be verified or validated in polynomial time.
-Given a graph G=<V,E>, HAM-CYCLE(G) is to return if a
Hamiltonian cycle exists or not.
- Hamiltonian cycle: a cycle visits each vertex once. It
needs to cover all vertices, but not all edges in graph.
- We may not have a good way to explore all possible
cycles in graph G, but we can verify a given cycle easily.
5
NP problems (nondeterministic
polynomial)
• Given a graph G=<V,E>, HAM-CYCLE(G) is to return
if a Hamiltonian cycle exists or not.
• HAM-CYCLE is NP.
But to check the Hamiltonian cycle is easy, given a cycle <v1,…Vk,v1> you only need to
check those edges exist or not in the graph. This only takes polynomial time.
6
*adapted from Cormen et al. 2009
P, NP, NPC, NP-hard
• P must be NP problems.
• P⊆NP
• P = NP? Unknown
• NP-hard: a problem is at least as hard as all other NP
problems.
• NPC (NP-complete problem) is a special group of NP
problem that is worthwhile to study. Why?
- Every NPC problem is at least as hard as all other NP
problems. (NP-hard)
- They still belong to NP (verified in polynomial).
- If we solve one in polynomial, we can solve all. Then we
would be able to answer if P=NP.
7
Problem reduction
• We need to know the meaning of “equivalent”, “as
easy as”, “ as hard as”…etc.
• Reduction is a good way to convert problems and
compare problems
• For example, if we don’t know how to solve A, we
can try to reduce it to B. If we know how to solve B,
we can solve A.
B and A should share the same YES or NO questions for a good reduction. Polynomial
conversion of the inputs is needed for a good reduction. 8
*adapted from Cormen et al. 2009
Problem reduction
• Step1: transform A’s inputs into B’s inputs in P time.
• Step2: solve B, get decision answer Y/N
• Step3: Use answer B for answer A
• This is a typical reduction from A to B. AàB.
If there exists a A to B reduction: A is as easy as B, B is at least as hard as A.
(Scenario 1): B is known to be solvable in P time, then A is also solvable in P time.
(Scenario 2): A is known to be solvable in P time, no guarantee B is in P.
(Scenario 3): A is NP-hard, then B is also NP-hard.
(Scenario 4): If A is NP-complete, and B is known as NP, then B is also NP-complete.
9
*adapted from Cormen et al. 2009
Use reduction to classify NPC
• Define a A to B reduction in polynomial time
A≤ 𝑝B
• Method 1:
(1) For every A in NP, you can reduce to B (B is NP-
hard).
(2) B is known in NP. (describe a P method to verify)
This is practically difficult since how to argue for
every A in NP?
10
Use reduction to classify NPC
• We have several problems known as NPC.
• Method 2:
(1) Pick A from known NPC problems
(2) Reduce to the target problem: A≤ 𝑝B
(3) B is NP. (verify B at polynomial time)
Then B is NPC.
11
Properties of NPC
• NPC is NP
• NPC is NP-hard
12
*adapted from Cormen et al. 2009
CIRCUIT-SAT problem
Review for digital logic.
13
*adapted from Cormen et al. 2009
CIRCUIT-SAT problem
Given a circuit C, if CIRCUIT-SAT(C) returns YES or NO.
Satisfiable in this circuit. Not satisfiable in this circuit
since you would never get 1.
14
*adapted from Cormen et al. 2009
CIRCUIT-SAT problem
We can now use CIRCUIT-SAT as an NPC problem to prove other NPC problems.
15
*adapted from Cormen et al. 2009
A set of NPC problems
• CIRCUIT-SAT: a give circuit can be satisfiable or not.
• SAT: a Boolean formula can be satisfiable or not.
• 3-CNF-SAT: does a 3-CNF formula can be satisfied or not?
• CLIQUE: does a graph contain a clique (complete subgraph)?
(given vertex number k)
• VERTEX-COVER: does a graph contain a set of vertex that
touches/cover all edges? (given vertex number k)
• HAM-CYCLE: does a graph contain Hamiltonian cycle?
• TSP: does a complete weighted graph contain Hamiltonian
cycle of total weight <= k?
• SUBSET-SUM: does it exist a subset of inputs adding up to
target value k?
16
To prove them needs reduction
17
*adapted from Cormen et al. 2009
Review of the proof procedure
Using the formal language model.
18
*adapted from Cormen et al. 2009
Review of the proof procedure
(1) Pick A from known NPC problems
(2) B is NP.
(2.1)Provide an algorithm to verify B.
(2.2)This can be done at polynomial time.
(3) Reduce to the target problem: A≤ 𝑝B
(3.1)Provide a transformation from A’s inputs to B’s inputs
(3.2)This transformation is polynomial time.
(3.3)If B has “YES” answer, A has “YES” answer. Vice versa to argue
answers are preserved.
Then B is NPC.
19
From CIRCUIT-SAT to SAT
• SAT can be verified at polynomial time. (because
each logic operations can be easily verified)
• Example:
Given a Boolean formula, and an assignment for
variables, you can quickly verify it satisfies the
formula or not.
x y xày xßày
20
*adapted from Cormen et al. 2009
From CIRCUIT-SAT to SAT
(1) Given the input from CIRCUIT-SAT, we can get input for SAT problem. In the
following example, the input (x1,x2,x3) can be transformed to (x1,x2,x3,
x4=not(x3), x5=(x1 or x2),….x10)
SAT formula
CIRCUIT-SAT
(2) This transformation is polynomial.
(3) Clause in SAT is correspondent to the logic components in CIRCUIT-SAT. In
this way the YES answer preserves.
So, SAT is NPC.
21
*adapted from Cormen et al. 2009
From SAT to 3-CNF SAT
3-CNF: each clause has 3 variables, and they are connected by AND operations.
Each clause can contain OR or NOT operations.
How to convert an arbitrary SAT formula to this specific form? Use
tree structure to perform each of the operations: ¬ ∧∨
y1
SAT = x1 ∨ (¬𝑥2 ∧ 𝑥3) ∨
y2
Draw a tree structure
x1 ∧
Mark final and intermediate results using y1, …
y3
Write down new formula
¬ x3
(¬𝑥2 ↔ y3) ∧ (𝑦3 ∧ 𝑥3 ↔ y2) ∧ (𝑥1 ∨ 𝑦2 ↔ y1) ∧ 𝑦1
x2 22
From SAT to 3-CNF SAT
• Convert the new formula to 3-CNF
(¬𝑥2 ↔ y3) ∧ (𝑦3 ∧ 𝑥3 ↔ y2) ∧ (𝑥1 ∨ 𝑦2 ↔ y1) ∧ 𝑦1
Remove ↔ and ∧ operation in each clause, using truth table and Demorgan’s law
Each clause should now contain less than or equal to 3 variables.
If 2 variables: x1 ∨ x2 = (x1 ∨ x2 ∨ p) ∧ (x1 ∨ x2 ∨ ¬p)
If only 1 variable:
3-CNF-SAT is NP. Logical operations can be verified at polynomial time.
The above conversion requires transformation from x to (x, y, p, q), but it’s
polynomial.
The above conversion maintains answers for the Boolean formula.
So, 3-CNF-SAT is NPC. 23
CLIQUE
Clique is complete graph with edges connecting to all pairs of nodes.
The following graph contains a 5-clique (of course 4-clique and 3-clique)
CLIQUE is NP.
24
Figures from online resources
FROM 3-CNF-SAT to CLIQUE
(1). Connect the variables in different clause, but not with the negations of itself.
Conversion from 3-CNF to a graph is polynomial
(2). Clique can be verified in P.
(3). 3-CNF-SAT is YES only and only if when graph has 3-cliques.
Intuition: nodes are connected can be simultaneously assigned to 1
without any potential contradiction.
25
So, CLIQUE is NPC. *adapted from Cormen et al. 2009
FROM CLIQUE to VERTEX-COVER
These nodes {z,w} can cover all edges in the graph.
Given a set of nodes and a graph, VERTEX-COVER can be verified in
polynomial time.
26
*adapted from Cormen et al. 2009
FROM CLIQUE to VERTEX-COVER
K-CLIQUE in the original graph means |V|-K nodes can cover all
edges in VERTEX-COVER problem in complement graph.
Build complementary graph is polynomial.
If K-CLUQUE exists, |V|-K nodes must provide all edge coverage.
Answers are preserved.
Vertex coverage can be verified in Polynomial time.
So, VERTEX-COVER is NPC.
27
*adapted from Cormen et al. 2009
FROM HAM-CYCLE to TSP
• TSP is to resolve if a Hamiltonian cycle exists for total
cost ≤ k in a complete and weighted graph.
• HAM-CYCLE is defined in a graph without weight and
may not be complete.
• First, TSP is NP for which we can easily verify.
• Second, convert graph to a weighted complete graph.
Assign weights to one if edge is not in original graph,
zero if edge is in original graph. This is polynomial.
• Third, HAM-CYCLE can be reduced to TSP problem to
resolve if a cycle exists for total cost ≤ 0. This preserves
the answers.
• So, TSP is NPC.
28
*adapted from Cormen et al. 2009
summary
• P, NP, NPC, NP-hard
• Problem reduction
• NPC problems
29
*adapted from Cormen et al. 2009