9 Complexity
9 Complexity
Complexity
1 Complexity Theory
What are the problems that can or cannot be solved efficiently?
Definition The running time, or time complexity of a deterministic Turing Machine (DTM) M is a function
f : N → N, where f (n) is the maximum number of steps M takes on any input of length n.
3
NTM
7
f (n) ≥ DT M ≤ f (n)
3 7
3
Remark It actually suffices to consider only the accepting paths when measuring the time complexity f (n)
since any computation that takes longer than f (n) can safely be rejected.
Definition P is the class of languages that are accepted by some polynomial-time DTM.
Definition NP (non-deterministically polynomial) is the class of languages that are accepted by some
polynomial-time NTM.
2 P vs NP
Is P = NP?
NP NP P
P ⊂ NP P = NP
3 NP-Completeness
The “hardest” problems of NP.
NP-H ARD
NP-C
NP
• L ∈ NP, and
• Every language in NP can be reduced to L through a poly-time reduction, i.e. L is NP-H ARD.
(A ≤P L, ∀A ∈NP)
NP-C
A
NP
Remark If L is NP-C OMPLETE, and L has a poly-time solution (L ∈P), then P = NP.
Remark If L is NP-C OMPLETE, and L ≤P L0 for some L0 ∈NP, then L0 is NP-C OMPLETE.
4 Polynomial-Time Reduction
Definition A “computable function” f : Σ∗ → Σ∗ is a function such that a TM M on input w halts with
f (w) on its tape.
w ∈ A ⇐⇒ f (w) ∈ B
Σ∗ f Σ∗
A B
“Mapping Reducible”
A ≤P B
2
5 Additional remarks and definitions
5.1 Decision vs. Optimization
• P, NP deal with decidable languages (i.e., decision problems).
• But more general problems (e.g., optimization) usually have some decision version. For example,
“what is the shortest path between x and y?” vs. “is there a path between x and y shorter than n?”
Remark A satisfiable Boolean formula is a Boolean formula that has a satisfying assignment (i.e. that
evaluates to 1).
Example Φ = (x ∧ y) ∨ (x ∧ z) is satisfiable.
e.g. x = 0, y = 1, z = 1; or x = 1, y = 1, z = 0, etc.
1. S AT ∈ NP.
This is easy. An NTM can test every possible assignment; or, a DTM can verify a possible solution.
2. S AT ∈ NP-H ARD.
See the excerpt from the book posted on the course web page (Cook-Levin-Theorem.pdf).
7.1 3-S AT
3-S AT= {< Φ > | Φ is a Boolean formula in “3-CNF” that is satisfiable}
Definition CNF: Conjunctive Normal Form. A list of O R clauses connected by A ND, such as:
(. . . ∨ . . . ∨ . . .) ∧ (. . . ∨ . . .) ∧ (. . . ∨ . . . ∨ . . . ∨ . . .)
3-CNF: each clause consists of 3 literals.
Example
Φ = (x1 ∨ x2 ∨ x3 ) ∧ (x4 ∨ x5 ∨ x1 )
Fact: Every Boolean formula can be converted into 3-CNF in polynomial time.
3
Theorem 7.1 3-S AT is NP-C OMPLETE.
1. 3-S AT ∈ NP.
This is easy. An NTM can test every possible assignment; or, a DTM can verify a possible solution.
7.2 C LIQUE
A clique is a fully connected undirected graph.
2-clique
3-clique 4-clique
1. C LIQUE ∈ NP.
An NTM can test every possible subsets of k vertices to see whether it is a clique or not.
• If G has a k-clique, that clique must have one vertex from each clause which are not contradictory. If
we set these literals to 1 in Φ, it will be a satisfying assignment for Φ.
• If Φ has a satisfying assignment, that assignment must contain at least one literal from each clause set
to 1. If we choose one such literal from each clause and take its vertex, they will make a k-clique in
G.
4
Example Let Φ = (x1 ∨ x2 ∨ x3 ) ∧ (x1 ∨ x2 ∨ x3 ) ∧ (x1 ∨ x4 ∨ x2 )
| {z } | {z } | {z }
c1 c2 c3
c2
G: x1 x2 x3
x1 x1
c1 x2 x4 c3
x3 x2
k=3
7.3 I NDEPENDENT-S ET
Definition Independent set is a subset of vertices in a graph with no edges between them.
1 2
3 4
1. I NDEPENDENT-S ET ∈ NP.
An NTM can test every possible subsets of k vertices to see whether they are independent or not.
5
For a given graph G = (V, E) consider its complement G0 = (V, E) where an edge (x, y) is in E if and
only if it is not in E. For example:
1 2 1 2
G: G’:
3 4 3 4
Claim: A vertex subset V 0 ⊆ V is a clique in G if and only if it is an independent set in G0 .
Regarding C LIQUE ≤P I NDEPENDENT-S ET, given a C LIQUE instance G = (V, E) and k, generate an
I NDEPENDENT-S ET instance G0 = (V, E) and k. G0 has an independent set of size k if and only if G
has a k-clique. Furthermore, they must be the same set of vertices V 0 . Therefore, I NDEPENDENT-S ET is
NP-C OMPLETE.
1 2
3 4
1 2
3 4
1 2
3 4
Regarding I NDEPENDENT-S ET ≤P V ERTEX -C OVER, for a given independent set instance < G, k >,
generate the vertex cover instance < G, n − k >, where n = |V |. G has k independent vertices if and only
if G has a vertex cover of size n − k. Therefore V ERTEX -C OVER is NP-Complete.
6
7.5 S UBSET-S UM
Instance: a set of positive integer weights (not necessarily distinct) and a positive integer “target” t:
1. S UBSET-S UM ∈ NP.
An NTM can test every possible subsets of S to see whether their summation equals the target or not.
• For each variable xi , generate two elements yi and zi in S - one for xi and one for xi .
• yi and zi has the digit di set to 1. Also if xi appears in Cj , yi has ej set to 1. Similarly if xi appears
in Cj , zi has ej set to 1.
• di = 1 for all di .
• ei = 3 for all ei .
7
Example
Φ = (x1 ∨ x2 ∨ x3 ) ∧ (x1 ∨ x2 ∨ x3 ) ∧ (x1 ∨ x4 ∨ x2 )
d1 d2 d3 d4 e1 e2 e3
y1 1 0 0 0 0 1 0
z1 1 0 0 0 1 0 1
y2 1 0 0 0 1 0
z2 1 0 0 1 0 1
y3 1 0 1 1 0
z3 1 0 0 0 0
y4 1 0 0 1
z4 1 0 0 0
g1 1 0 0
h1 1 0 0
g2 1 0
h2 1 0
g3 1
h3 1
t 1 1 1 1 3 3 3
Let S 0 = {z1 , y2 , y3 , y4 , g1 , g2 , g3 }.
X
w = 1, 000, 101 + 100, 010 + 10, 110 + 1, 001 + 100 + 10 + 1 = 1, 111, 333
w∈S 0
x1 = x2 = x3 = x4 = 1
which satisfies Φ.
7.6 PARTITION
Instance: S = {w1 , w2 , ..., wk }
1. PARTITION ∈ NP
An NTM can test all possible subsets of S to check whether it is a partition or not.
8
Example Given a S UBSET-S UM problem S = {1, 3, 5}, t = 6 generate a new set S2 = {1, 3, 5, 3}. Here
we add 3. (3=2t − W , where W is the total weight of elements in S)
If S2 has an equal partition, take the subset with the new element and remove it:
S20 = {1, 3, 1}, S200 = 5
S2 = {w1 , w2 , ..., wk , wk+1 } where wk+1 = |2t − W | and W denotes the total weight of S.
7.7 K NAPSACK
Instance: S = {x1 , x2 , ..., xk } within a weight function w : S → N+ , a value function v : S → N+ a
weight limit W , a value limit V .
1. K NAPSACK ∈ NP
An NTM can pick an arbitrary subset S 0 and check whether it satisfies the weight and value limits in
polynomial time.
For a given S UBSET-S UM instance S = {w1 , w2 , ..., wk } and t, construct the K NAPSACK instance
S2 = {x1 , x2 , ..., xk } with w(xi ) = v(xi ) = wi and W = V = t.
Obviously, S2 has a subset that satisfies the weight and value limits ⇐⇒ S has a subset S 0 with total
weight equal to t.
9
7.8 D IRECTED H AMILTONIAN C IRCUIT
Definition A Directed Hamiltonian Circuit in a directed graph is a cycle that visits every vertex in the graph
exactly once.
G: 1→2→3→4→1
3 4
1. H AMILTONIAN -C IRCUIT ∈ NP
An NTM can try every possible cycle in the graph to check whether it is a Hamiltonian Circuit.
G1 1
G01 1’ 1”
2 3
G01 1 2’ 3’
We lost direction
information
2 3 2” 3”
10
G02 1’ 1”
G2 1 2’ 3’
2 3 2” 3”
Here we notice that with only two points for each G03 1’ 1” 1”’
vertex we find a H AMILTONIAN -C IRCUIT for this.
This is because we can’t preserve atomic structure
of a vertex. Add a point in between.
2’ 3’
G3 1
2” 3”
2 3 2”’ 3”’
For a given D IRECTED -H AMILTONIAN -C IRCUIT instance, directed graph G = (V, E), construct an
undirected graph G0 = (V 0 , E 0 ) as follows:
V 0 = {v 0 , v 00 , v 000 : v ∈ V }
Question: Does G contain a traveling salesman cycle with a total distance (weight) no more than k?
We can convert any H AMILTONIAN -C IRCUIT into T RAVELING -S ALESMAN -P ROBLEM as follows:
11
1
1 2
1 1 k = 4 (|V |)
1
3 4
1
H AMILTONIAN -C IRCUIT ≤p T RAVELING -S ALESMAN -P ROBLEM
For a given H AMILTONIAN -C IRCUIT instance G = (V, E) make it a weighted graph G0 by assigning
each edge the unit weight 1 and set k = |v|.
12