Ceng491 Lecture 14
Ceng491 Lecture 14
Example: Let 𝐴 = a𝑘 b𝑘 𝑘 ≥ 0 .
Q: How many steps are needed to decide 𝐴?
Depends on the input.
We give an upper bound for all inputs of length 𝑛.
Called “worst-case complexity”.
2
P and N P
Example 13-2
▶ Given a sorted array A, write an algorithm in pseudocode that determines the
location of element x. Then, find the time complexity of that algorithm.
Example 13-2
▶ Given a sorted array A, write an algorithm in pseudocode that determines the
location of element x. Then, find the time complexity of that algorithm.
▶
Example 13-18 (Shortest Path Problem)
▶
▶ For a graph with n vertices and adjacency matrix representation, the complexity
will be O(n2 ). Is the Shortest Path Problem in P? Yes.
NP
NP
What if you need to search a large
space for a single object?
Verifiers – Again
7 6 1
3 5 2
3 1 5 9 7
6 5 3 8 9
1 2
8 2 1 5 4
1 3 2 7 8
5 7 4
4 8 7
4 9 1 8 7 3 6 5 2
3 8 6 1 2 5 9 4 7
6 4 5 7 3 2 8 1 9
7 1 9 5 4 8 3 2 6
8 3 2 6 1 9 5 7 4
1 6 3 2 5 7 4 9 8
5 7 8 4 9 6 2 3 1
9 2 4 3 8 1 7 6 5
4 3 11 4
9 9 2
7 13 5 6 1 12 7
2 8 0 10
4 3 11 4
9 9 2
7 13 5 6 1 12 7
2 8 0 10
2 4
1 6
5 3
Is there a simple path that goes
through every node exactly once?
Verifiers
● Recall that a verifier for L is a TM V
such that
● V halts on all inputs.
● w∈L iff ∃c ∈ Σ*. V accepts ⟨w, c⟩.
Polynomial-Time Verifiers
● A polynomial-time verifier for L is a
TM V such that
● V halts on all inputs.
● w∈L iff ∃c ∈ Σ*. V accepts ⟨w, c⟩.
● V's runtime is a polynomial in |w| (that is, V's
runtime is O(|w|k) for some integer k)
The Complexity Class NP
●
The complexity class NP (nondeterministic
polynomial time) contains all problems that can be
verified in polynomial time.
● Formally:
NP = { L | There is a polynomial-time
verifier for L }
●
The name NP comes from another way of
characterizing NP. If you introduce nondeterministic
Turing machines and appropriately define
“polynomial time,” then NP is the set of problems
that an NTM can solve in polynomial time.
Example 13-19 (Coloring Problem)
▶ Given a graph with n vertices and an integer k, is there a way to color the vertices
with k colors such that adjacent vertices are colored differently? Is this problem in
N P?
Example 13-19 (Coloring Problem)
▶ Given a graph with n vertices and an integer k, is there a way to color the vertices
with k colors such that adjacent vertices are colored differently? Is this problem in
N P?
▶ For i=1 to n-1
For j=i+1 to n
If vertices i-j are connected
If Color(i) == Color(j)
Return REJECT
End
End
End
End
Return ACCEPT
Example 13-19 (Coloring Problem)
▶ Given a graph with n vertices and an integer k, is there a way to color the vertices
with k colors such that adjacent vertices are colored differently? Is this problem in
N P?
▶ For i=1 to n-1
For j=i+1 to n
If vertices i-j are connected
If Color(i) == Color(j)
Return REJECT
End
End
End
End
Return ACCEPT
▶ The algorithm takes O(n2 ) time, i.e. polynomial time. Therefore, coloring
problem is in N P.
Example 13-20 (Minimum Spanning Tree Problem)
▶ A minimum spanning tree for a weighted, connected, undirected graph G is a
subgraph that connects all vertices and has minimum weight. For example,
NP = { L | There is a polynomial-time
verifier for L }
P ⊆ NP
Which Picture is Correct?
P NP
Which Picture is Correct?
P NP
Does P = NP?
P ≟ NP
● The P ≟ NP question is the most important question in
theoretical computer science.
● With the verifier definition of NP, one way of phrasing
this question is
If a solution to a problem can be checked efficiently,
can that problem be solved efficiently?
● An answer either way will give fundamental insights
into the nature of computation.
Why This Matters
● The following problems are known to be efficiently
verifiable, but have no known efficient solutions:
● Determining whether an electrical grid can be built to link up
some number of houses for some price (Steiner tree problem).
● Determining whether a simple DNA strand exists that multiple
gene sequences could be a part of (shortest common
supersequence).
● Determining the best way to assign hardware resources in a
compiler (optimal register allocation).
● Determining the best way to distribute tasks to multiple
workers to minimize completion time (job scheduling).
● And many more.
● If P = NP, all of these problems have efficient solutions.
● If P ≠ NP, none of these problems have efficient solutions.
Why This Matters
● If P = NP:
● A huge number of seemingly difficult problems
could be solved efficiently.
● Our capacity to solve many problems will scale
well with the size of the problems we want to
solve.
● If P ≠ NP:
● Enormous computational power would be required
to solve many seemingly easy tasks.
● Our capacity to solve problems will fail to keep up
with our curiosity.
The Million-Dollar Question
Σ* Σ*
Polynomial-Time Reductions
● If f is a polynomial-time reduction from A
to B, then
∀w ∈ Σ*. (w ∈ A ↔ f(w) ∈ B)
Σ*
A Σ*
Polynomial-Time Reductions
● If f is a polynomial-time reduction from A
to B, then
∀w ∈ Σ*. (w ∈ A ↔ f(w) ∈ B)
A B
Σ*
A B Σ*
Polynomial-Time Reductions
● If f is a polynomial-time reduction from A
to B, then
∀w ∈ Σ*. (w ∈ A ↔ f(w) ∈ B)
f(w)
A B
f(w)
Σ*
A B Σ*
Reductions, Programmatically
● Suppose we have a solver for problem B that's
defined in terms of problem A in this specific
way:
boolean solveProblemA(input) {
return solveProblemB(transform(input));
}
● The reduction from A to B is the function
transform in the above setup: it maps “yes”
answers to A to “yes” answers to B and “no”
answers to A to “no” answers to B.
Reducibility among Problems
● Suppose that A and B are languages
where there's a polynomial-time
reduction from A to B.
● We'll denote this by writing
A ≤p B
● You can read this aloud as “A polynomial-
time reduces to B” or “A poly-time
reduces to B.”
Two Key Theorems
● Theorem 1: If A ≤p B and B ∈ P, then A ∈ P.
● Theorem 2: If A ≤p B and B ∈ NP, then A ∈ NP.
● Intuitively, if A ≤p B, then A is “not harder than”
B and B is “at least as hard as” A.
● Proof idea: For (1), show that applying the
reduction from A to B and solving B solves A in
polynomial time. For (2), show that applying the
reduction from A to B lets us use a verifier for B
to verify answers to A.
Reducibility, Summarized
● A polynomial-time reduction is a way of
transforming inputs to problem A into inputs to
problem B that preserves the correct answer.
● If there is a polynomial-time reduction from A
to B, we denote this by writing A ≤p B.
● Two major theorems:
● Theorem: If B ∈ P and A ≤p B, then A ∈ P.
● Theorem: If B ∈ NP and A ≤p B, then A ∈ NP.
NP-Hardness and NP-Completeness
Polynomial-Time Reductions
● If A ≤P B and B ∈ P, then A ∈ P.
If L1 ≤P L2 and L2 ∈ NP, then L1 ∈ NP.
P
Polynomial-Time Reductions
● If A ≤P B and B ∈ P, then A ∈ P.
If L1 ≤P L2 and L2 ∈ NP, then L1 ∈ NP.
P
Polynomial-Time Reductions
● If A ≤P B and B ∈ P, then A ∈ P.
If L1 ≤P L2 and L2 ∈ NP, then L1 ∈ NP.
P
Polynomial-Time Reductions
● If A ≤P B and B ∈ P, then A ∈ P.
● If A ≤P B and B ∈ NP, then A ∈ NP.
P
Polynomial-Time Reductions
● If A ≤P B and B ∈ P, then A ∈ P.
● If A ≤P B and B ∈ NP, then A ∈ NP.
P NP
Polynomial-Time Reductions
● If A ≤P B and B ∈ P, then A ∈ P.
● If A ≤P B and B ∈ NP, then A ∈ NP.
P NP
Polynomial-Time Reductions
● If A ≤P B and B ∈ P, then A ∈ P.
● If A ≤P B and B ∈ NP, then A ∈ NP.
P NP
NP-Hardness
● A language L is called NP-hard if for every A ∈ NP, we have
A ≤P L.
NP
P
NP-Hardness
● A language L is called NP-hard if for every A ∈ NP, we have
A ≤P L.
NP NP-Hard
P
NP-Hardness
● A language L is called NP-hard if for every A ∈ NP, we have
A ≤P L.
NP NP-Hard
P
NP-Hardness
● A language L is called NP-hard if for every A ∈ NP, we have
A ≤P L.
Intuitively:
Intuitively: LLhas
has to
to be
be at
atleast
leastas
as
A language in L ishard NP-complete
calledas every iff
problem L is
inNP-hard
NP, and
since
L ∈ NP. hard as every problem in NP, since
an
analgorithm
algorithmforforLLcan
canbe beused
usedto to
The class NPC is the set of NP-complete problems.
decide
decideall
allproblems
problems inNP.
in NP.
NP NP-Hard
P
NP-Hardness
● A language L is called NP-hard if for every A ∈ NP, we have
A ≤P L.
NP NP-Hard
P
NP-Hardness
● A language L is called NP-hard if for every A ∈ NP, we have
A ≤P L.
What's
A language in L is called NP-complete iff L in
in here?
is NP-hard
What's and
here?
L ∈ NP.
The class NPC is the set of NP-complete problems.
NP NP-Hard
P
NP-Hardness
● A language L is called NP-hard if for every A ∈ NP, we have
A ≤P L.
● A language in L is called NP-complete if L is NP-hard and
L ∈ NP.
● The class NPC is the set of NP-complete problems.
NP NP-Hard
P
NP-Hardness
● A language L is called NP-hard if for every A ∈ NP, we have
A ≤P L.
● A language in L is called NP-complete if L is NP-hard and
L ∈ NP.
● The class NPC is the set of NP-complete problems.
NP NP-Hard
NPC
P
A Feel for NP-Completeness
● If a problem is NP-complete, then under the
assumption that P ≠ NP, there cannot be an
efficient algorithm for it.
● In a sense, NP-complete problems are the
hardest problems in NP.
● All known NP-complete problems are
enormously hard to solve:
● All known algorithms for NP-complete problems
run in worst-case exponential time.
● Most algorithms for NP-complete problems are
infeasible for reasonably-sized inputs.
NP-Completeness
Theorem: Let A and B be languages. If
A ≤P B and A is NP-hard, then B is NP-hard.
Theorem: Let A and B be languages where
A ∈ NPC and B ∈ NP. If A ≤P B, then
B ∈ NPC.
NP
P
NPC
Sample NP-Complete Problems
● Given a graph, is that graph 3-colorable?
● Given a graph, does that graph contain a Hamiltonian
path?
● Given a set of cities and a set of substation locations,
can you provide power to all the cities using at most k
substations?
● Given a set of jobs and workers who can perform those
tasks in parallel, can you complete all the jobs in at
most T units of time?
● Given a set of numbers S, can you split S into two
disjoint sets with the same sum?
● And many, many more!
A Feel for NP-Completeness
● There are NP-complete problems in
● formal logic (SAT),
● graph theory (3-colorability),
● operations research (job scheduling),
● number theory (partition problem),
● and basically everywhere.
● You will undoubtedly encounter NP-
complete problems in the real world.
SAT (Boolean Satisfiability Problem)
◼ the first problem that was shown to be
NP-Complete
◼ A given boolean expression,
determining if there exists a truth
assignment to its variables for which the
expression is true is defined as the
satisfiability problem.
3
SAT
• We’ve tested all possible
assignments.
• There are 5 satisfying truth
assignments for the
expression
and hence it is satisfiable
But the expression is false for all possible truth assignments
and hence it is unsatisfiable.
This takes
If there are n variables in the boolean expression, then the truth table method
would take time to determine if there is a truth assignment that makes
the expression true.
4
3-CNF (conjuction normal form)
◼ (x1 AND x2 AND x3) OR (NOT x1 AND
NOT x2 AND NOT x3)
◼ To be satisfiable, all X1,X2, X3 must be
true or all of them must be false.
5
A 3-coloring of a graph is a way of coloring its
nodes one of three colors such that no two connected
nodes have the same color.
The 3-Coloring Problem
● The 3-coloring problem is
Given an undirected graph G,
is there a legal 3-coloring of its
nodes?
● As a formal language:
3COLOR = { ⟨G⟩ | G is an undirected
graph with a legal 3-coloring. }
● This problem is known to be NP-complete
by a reduction from 3SAT.
3COLOR ∈ NP
● We can prove that 3COLOR ∈ NP by
designing a polynomial-time
nondeterministic TM for 3COLOR.
● M = “On input ⟨G⟩:
● Nondeterministically guess an assignment
of colors to the nodes.
● Deterministically check whether it is a 3-
coloring.
● If so, accept; otherwise reject.”
A Note on Terminology
● Although 3COLOR and 3SAT both have “3” in
their names, the two are very different
problems.
● 3SAT means “there are three literals in every
clause.” However, each literal can take on only
one of two different values.
● 3COLOR means “every node can take on one of
three different colors.”
● Key difference:
● In 3SAT variables have two choices of value.
● In 3COLOR nodes have three choices of value.