Lecture26 Number Theoretic Algorithms - NP - VectCov - Hamilt
Lecture26 Number Theoretic Algorithms - NP - VectCov - Hamilt
TSP
Approximation Algorithms
VECTOR COVER
HAMILTON
Set Covering Problem
NP-Completeness
• Polynomial-time algorithms
on inputs of size n, worst-case running time is
O(nk), for a constant k
• Not all problems can be solved in polynomial time
– Some problems cannot be solved by any computer no matter
how much time is provided (Turing’s Halting problem) – such
problems are called undecidable
– Some problems can be solved but not in O(nk)
CS 477/677 - Lecture 26 2
Class of “P” Problems
• Class P consists of (decision) problems that are solvable
in polynomial time:
CS 477/677 - Lecture 26 3
Optimization & Decision Problems
• Decision problems
– Given an input and a question regarding a problem, determine
if the answer is yes or no
• Optimization problems
– Find a solution with the “best” value
• Optimization problems can be cast as decision problems
that are easier to study
– E.g.: Shortest path: G = unweighted directed graph
• Find a path between u and v that uses the fewest edges
• Does a path exist from u to v consisting of at most k edges?
CS 477/677 - Lecture 26 4
Nondeterministic Algorithms
Nondeterministic algorithm = two stage procedure:
1) Nondeterministic (“guessing”) stage:
generate an arbitrary string that can be thought of as a
candidate solution (“certificate”)
2) Deterministic (“verification”) stage:
take the certificate and the instance to the problem and
return YES if the certificate represents a solution
• Nondeterministic polynomial (NP) = verification
stage is polynomial
CS 477/677 - Lecture 26 5
Class of “NP” Problems
CS 477/677 - Lecture 26 6
E.g.: Hamiltonian Cycle
• Given: a directed graph G = (V, E), determine a
simple cycle that contains each vertex in V
– Each vertex can only be visited once
• Certificate:
– Sequence: ⟨v1, v2, v3, …, v|V|⟩
hamiltonian
• Verification:
1) (vi, vi+1) ∈ E for i = 1, …, |V|
2) (v|V|, v1) ∈ E not
hamiltonian
CS 477/677 - Lecture 26 7
Polynomial Reduction Algorithm
yes
yes
𝛼 𝛽 Polynomial time
f algorithm to decide B no
no
Polynomial time algorithm to decide A
CS 477/677 - Lecture 26 8
Reductions
reducible to B (A ≤p B) if:
CS 477/677 - Lecture 26 9
NP-Completeness
• A problem B is NP-complete if: P NP-complete
1) B ∈ NP NP
2) A ≤p B for all A ∈ NP
CS 477/677 - Lecture 26 10
Reduction and NP-Completeness
yes
yes
𝛼 𝛽
f Problem B no
no
Problem A
• Suppose we know:
– No polynomial time algorithm exists for problem A
CS 477/677 - Lecture 26 11
Proving NP-Completeness
Theorem: If A is NP-Complete and A ≤p B
⇒ B is NP-Hard
P NP-complete
In addition, if B ∈ NP
NP
⇒ B is NP-Complete
Proof: Assume that B ∈ P
Since A ≤p B ⇒ A ∈ P contradiction, so B ∉ P
CS 477/677 - Lecture 26 12
Proving NP-Completeness
CS 477/677 - Lecture 26 13
Is P = NP?
• Any problem in P is also in NP: P NP-complete
P ⊆ NP NP
CS 477/677 - Lecture 26 14
P & NP-Complete Problems
• Shortest simple path
– Given a graph G = (V, E) find a shortest path from a
source to all other vertices
– Polynomial solution: O(VE)
CS 477/677 - Lecture 26 16
Boolean Formula Satisfiability
Formula Satisfiability Problem: a boolean formula 𝚽
composed of
1. n boolean variables: x1, x2, …, xn
2. m boolean connectives: ∧ (AND), ∨ (OR), ¬ (NOT), →
(implication), ⟷ (equivalence, “if and only if”)
3. Parentheses
• E.g.:
𝚽 = (x1 ∨ ¬x1 ∨ ¬x2) ∧ (x3 ∨ x2 ∨ x4) ∧
(¬x1 ∨ ¬x3 ∨ ¬ x4)
• 3-CNF is NP-Complete
CS 477/677 - Lecture 26 18
Clique
Clique Problem:
– Undirected graph G = (V, E)
– Clique: a subset of vertices in V all connected to each other
by edges in E (i.e., forming a complete graph)
– Size of a clique: number of vertices it contains
Decision problem:
– Does G have a clique of size k? Clique(G, 3) = YES
Clique(G, 4) = NO
CS 477/677 - Lecture 26 19
Clique Verifier
• Given: an undirected graph G = (V, E)
• Problem: Does G have a clique of size k?
• Certificate:
– A set of k nodes
• Verifier:
– Verify that for all pairs of vertices in this set there exists an
edge in E
• Let’s prove that the clique problem is NP-Complete
CS 477/677 - Lecture 26 20
3-CNF ≤p Clique
– 𝚽 = C1 ∧ C2 ∧ … ∧ Ck (k clauses)
• Idea:
CS 477/677 - Lecture 26 21
3-CNF ≤p Clique
C1 = x1 ∨ ¬x2 ∨ ¬x3
𝚽 = C1 ∧ C2 ∧ C3
x1 ¬x2 ¬x3
C2 = ¬x1 ∨ x2 ∨ x3 C3 = x1 ∨ x2 ∨ x3
¬x1 x1
x2 x2
x3 x3
• Claim: V’ is a clique
– ∀ vir, vjs ∈ V’ the corresponding literals are 1 ⇒ cannot be
complements
– by the design of G the edge (vir, vjs) ∈ E
CS 477/677 - Lecture 26 23
3-CNF ≤p Clique 𝚽 = C1 ∧ C2 ∧ C3
C1 = x1 ∨ ¬x2 ∨ ¬x3
• Suppose G has a clique x1 ¬x2 ¬x3
of size k ¬x1 x1
– No edges between nodes
in the same clause x2 x2
– Clique contains only one
x3 x3
vertex from each clause
– Assign 1 to vertices in C2 = ¬x1 ∨ x2 ∨ x3 C3 = x1 ∨ x2 ∨ x3
CS 477/677 - Lecture 26 24
The Traveling Salesman Problem
• G = (V, E), |V| = n, vertices represent u v
cities 1
2
• Cost: c(i, j) = cost of travel from city i 3
1
to city j
x w
• Problem: salesman should make a tour 5
• Certificate:
– Sequence of n vertices, cost u v
– E.g.: ⟨u, w, v, x⟩, 7 1
2
• Verification: 3
1
– Each vertex occurs only once x w
5
– Sum of costs is at most k
CS 477/677 - Lecture 26 26
HAM-CYCLE ≤p TSP
• Start with a Hamiltonian cycle G = (V, E)
u v
• Form the complete graph G’ = (V, E’) 1
CS 477/677 - Lecture 26 27
HAM-CYCLE ≤p TSP
1
u v u v
1 0
3 2 0 0
1 0
x w x w
5 0
CS 477/677 - Lecture 26 29
Local Search (Hill Climbing, Gradient
Descent)
• Explore the space of possible solutions, moving from a current
solution to a "nearby" one
1. Let S denote current solution
2. If there is a neighbor S' of S with strictly lower cost, replace
S with the neighbor whose cost is as small as possible
3. Otherwise, terminate the algorithm
S S
Optimal solution
Local minima
CS 477/677 - Lecture 26 31
The Vertex-Cover Problem
• Vertex cover of G = (V, E),
b c d
undirected graph
– A subset V’ ⊆ V that
a e f g
covers all the edges in G
CS 477/677 - Lecture 26 32
Gradient Descent: Vertex Cover
• Local optimum. No neighbor is strictly better.
CS 477/677 - Lecture 26 33
The Vertex-Cover Problem
• Vertex cover of G = (V, E),
b c d
undirected graph
– A subset V’ ⊆ V that
a e f g
covers all the edges in G
CS 477/677 - Lecture 26 34
APPROX-VERTEX-COVER(G)
b c d
1. C ← ∅
2. E’ ← E[G]
a e f g
3. while E’ ≠ ∅
4. do choose (u, v) b c d
arbitrary from E’
5. C ← C ⋃ {u, v} a e f g
CS 477/677 - Lecture 26 35
APPROX-VERTEX-COVER(G)
b c d
APPROX-VERTEX-COVER:
a e f g
b c d
Optimal VERTEX-COVER:
a e f g
X=⋃S
S∈F
Idea: S1
elements
Optimal: C = {S3, S4, S5}
CS 477/677 - Lecture 26 38
GREEDY-SET-COVER(X, F)
1. U ← X S1
2. C ← ∅ S2
3. while U ≠ ∅
S6
4. do select an S ∈ F that S3 S4 S5
maximizes |S ⋂ U| S1
5. U←U–S
6. C ← C ⋃ {S}
7. return C S3 S4 S5
CS 477/677 - Lecture 26 39
ADDITIONAL PROOFS
CS 477/677 - Lecture 26 40
Clique ≤p Vertex Cover
G u v GC u v
z w z w
y x y x
CS 477/677 - Lecture 26 41
Clique ≤p Vertex Cover (VC)
G GC G GC
Clique = 2 VC = 2 Clique = 2 VC = 3
CS 477/677 - Lecture 26 42
Clique ≤p Vertex Cover
G u v GC u v
z w z w
y x y x
• Prove: G has a clique V’⊆ V, |V’| = k ⇒ V-V’ is a VC in GC
• Let (v, w) ∈ EC ⇒ (v, w) ∉ E
⇒ v and w were not connected in E
⇒ at least one of v or w does not belong in the clique V’
⇒ at least one of v or w belongs in V - V’
⇒ edge (v, w) is covered by V – V’
⇒ edge (v, w) was arbitrary ⇒ every edge of EC is covered
CS 477/677 - Lecture 26 43
Clique ≤p Vertex Cover
G u v GC u v
z w z V’ w
y x y x V - V’
⇒ v ∈ V’ or w ∈ V’ or both ∈ V’
⇒ For all x, y ∈ V, if x ∉ V’ and y ∉ V’:
⇒ no edge between x, y in EG ⇒ (x,y) ∈ E
⇒ V – V’ is a clique, of size |V| - |V’| = k
CS 477/677 - Lecture 26 44
INDEPENDENT-SET
• Given a graph G = (V, E) and an integer k, is there a
subset of vertices S ⊆ V such that |S| ≥ k, and for
each edge at most one of its endpoints is in S?
independent set
CS 477/677 - Lecture 26 45
3-CNF ≤p INDEPENDENT-SET
• Given an instance 𝚽 of 3-CNF, we construct an instance
(G, k) of INDEPENDENT-SET that has an independent
set of size k iff 𝚽 is satisfiable
• Construction
– G contains 3 vertices for each clause, one for each literal.
– Connect 3 literals in a clause in a triangle.
– Connect literal to each of its negations.
k=3
CS 477/677 - Lecture 26 46
3-CNF ≤p INDEPENDENT-SET
• Claim: G contains independent set of size k = |𝚽| iff 𝚽
is satisfiable
• Proof: “⇒” Let S be independent set of size k
– S must contain exactly one vertex in each triangle
– Set these literals to true
– Truth assignment is consistent and all clauses are satisfied
k=3
CS 477/677 - Lecture 26 47
3-CNF ≤p INDEPENDENT-SET
• Claim: G contains independent set of size k = |𝚽| iff 𝚽
is satisfiable
• Proof: “⟸”
– Each triangle has a literal that evaluates to 1
– This is an independent set S of size k
• If there would be an edge between vertices in S, they would have to
conflict
k=3
CS 477/677 - Lecture 26 48
Polynomial-Time Reductions
to
es SET
Dick Karp (1972)
uc
red NT 1985 Turing
- CNF NDE
3 E
EP Award
IND
CS 477/677 - Lecture 26 50
INDEPENDENT-SET ≤p VERTEX-COVER
Proof “⇒” v
– Let S be any independent set
– Consider an arbitrary edge (u, v)
– S independent ⇒ u ∉ S or v ∉ S
⇒ u ∈ V - S or v ∈ V - S
– Thus, V - S covers (u, v)
independent set
vertex cover
CS 477/677 - Lecture 26 51
INDEPENDENT-SET ≤p VERTEX-COVER
Proof “⟸” v
– Let V - S be any vertex cover
– Consider two nodes u ∈ S and
v∈S
– Observe that (u, v) ∉ E since
V - S is a vertex cover
– Thus, no two nodes in S are joined
independent set
by an edge ⇒ S independent set vertex cover
CS 477/677 - Lecture 26 52
Set Cover
• Given a set U of elements, a collection S1, S2, . . . , Sm
of subsets of U, and an integer k, does there exist a
collection of ≤ k of these sets whose union is equal to
U?
• Example U = { 1, 2, 3, 4, 5, 6, 7 }
k=2
S1 = {3, 7} S4 = {2, 4}
S2 = {3, 4, 5, 6} S5 = {5}
S3 = {1} S6 = {1, 2, 6, 7}
CS 477/677 - Lecture 26 53
Set Cover
• Given a set U of elements, a collection S1, S2, . . . , Sm
of subsets of U, and an integer k, does there exist a
collection of ≤ k of these sets whose union is equal to
U?
• Sample application
– m available pieces of software
– Set U of n capabilities that the system should have
– The i-th piece of software provides the set Si ⊆ U of
capabilities
– Goal: achieve all n capabilities using fewest pieces of
software
CS 477/677 - Lecture 26 54
VERTEX-COVER ≤p SET-COVER
• Given a VERTEX-COVER instance G = (V, E), k, we
construct a set cover instance whose size equals the size
of the vertex cover instance
• Construction
– Create SET-COVER instance
• k = k, U = E, Sv = {e ∈ E : e incident to v }
– Set-cover of size ≤ k iff vertex cover of size ≤ k
VERTEX COVER SET COVER
a b
e7 e2 e4 U = { 1, 2, 3, 4, 5, 6, 7 }
e3
k=2
e6 Sa = {3, 7} Sb = {2, 4}
f c
Sc = {3, 4, 5, 6} Sd = {5}
k=2 e1 e5
Se = {1} Sf= {1, 2, 6, 7}
e d
CS 477/677 - Lecture 26 55
VERTEX-COVER ≤p SET-COVER
• Set-cover of size ≤ k iff vertex cover of size ≤ k
• Proof “⇒” (Si1, …., Sil are l ≤ k sets that cover U)
– Every edge in G is incident on one of the vertices i1,…, il, so
{i1,…, il} is a vertex cover of size l ≤ k
• Proof “⟸” {i1,…, il} is a vertex cover of size l ≤ k
– Then, the sets Si1, …., Sil cover U
VERTEX COVER SET COVER
a b
e7 e2 e4 U = { 1, 2, 3, 4, 5, 6, 7 }
e3
k=2
e6 Sa = {3, 7} Sb = {2, 4}
f c
Sc = {3, 4, 5, 6} Sd = {5}
k=2 e1 e5
Se = {1} Sf= {1, 2, 6, 7}
e d
CS 477/677 - Lecture 26 56
Hamiltonian Cycle
• Given an undirected graph G = (V, E), does there
exists a simple directed cycle 𝚪 that contains every
node in V?
• Claim: DIR-HAM-CYCLE ≤P HAM-CYCLE
• Construction
– Given a directed graph G = (V, E), construct an undirected
graph G' with 3n nodes: vin, v, vout
aout
a din
d
b v
bout vin v vout
e
c
ein
cout
G G'
CS 477/677 - Lecture 26 57
DIR-HAM-CYCLE ≤p HAM-CYCLE
• Claim: G has a Hamiltonian cycle iff G' does.
• Proof: “⇒”
– Suppose G has a directed Hamiltonian cycle 𝚪
– Then G' has an undirected Hamiltonian cycle (same order)
aout
a din
d
b v
bout vin v vout
e
c
ein
cout
G G'
CS 477/677 - Lecture 26 58
DIR-HAM-CYCLE ≤P HAM-CYCLE
• Claim: G has a Hamiltonian cycle iff G' does.
• Proof: “⟸”
– Suppose G' has an undirected Hamiltonian cycle 𝚪’
– 𝚪' must visit nodes in G' using one of following two orders:
• …, B, G, R, B, G, R, B, G, R, B, …
• …, B, R, G, B, R, G, B, R, G, B, …
– Blue nodes in 𝚪' make up directed Hamiltonian cycle 𝚪 in G,
or reverse of one
aout
a din
d
b v
bout vin v vout
e
c
ein
G CS 477/677
cout- Lecture 26 G' 59
3-Colorability
• Given an undirected graph G does there exists a way to color
the nodes red, green, and blue so that no adjacent nodes have
the same color?
yes instance
CS 477/677 - Lecture 26 60
Register Allocation
• Register allocation
– Assign program variables to machine register so that no more than k
registers are used and no two program variables that are needed at the
same time are assigned to the same register
• Interference graph
– Nodes are program variables names, edge between u and v if there exists
an operation where both u and v are "live" at the same time.
• Observation [Chaitin 1982]
– Can solve register allocation problem iff interference graph is k-
colorable
• Fact
– 3-COLOR ≤ P k-REGISTER-ALLOCATION for any constant k 3
CS 477/677 - Lecture 26 61
3-CNF ≤p 3-COLOR
• Given 3-CNF instance 𝚽, we construct an instance of
3-COLOR that is 3-colorable iff 𝚽 is satisfiable
• Construction
– For each literal, create a node
– Create 3 new nodes T, F, B; connect them in a triangle, and
connect each literal to B
– Connect each literal to its negation
– For each clause, add a 6-node subgraph
CS 477/677 - Lecture 26 62
3-CNF ≤p 3-COLOR
• For each literal, create a node
• Create 3 new nodes T, F, B; connect them in a
triangle, and connect each literal to B
• Connect each literal to its negation
true false
T F
B base
CS 477/677 - Lecture 26 63
3-CNF ≤p 3-COLOR
• Any 3-coloring implicitly determines a truth
assignment for variables in 3-CNF
– Nodes T, F, B must get different colors
– For xi and not-xi, one will take T color one F color
true false
T F
B base
CS 477/677 - Lecture 26 64
3-CNF ≤p 3-COLOR
• Must ensure that only satisfying assignments can
result in 3-coloring of the full graph
– For each clause, add a 6-node subgraph
6-node subgraph
true T F false
CS 477/677 - Lecture 26 65
3-CNF ≤p 3-COLOR
• Proof “⇒” Suppose graph is 3-colorable
– Proof by contradiction: assume that all three literals get a
False color
contradiction
true T F false
CS 477/677 - Lecture 26 66
3-CNF ≤p 3-COLOR
• Proof “⟸” Suppose 3-CNF formula 𝚽 is satisfiable
– Color all true literals T
– Color node below green node F, and node below B
– Color remaining middle row nodes B
– Color remaining bottom nodes T or F as forced
true T F false
CS 477/677 - Lecture 26 67
Directed Hamiltonian Cycle
• Given a digraph G = (V, E), does there exists a simple
directed cycle 𝚪 that contains every node in V?
• Idea:
– Given an instance 𝚽 of 3-CNF, we construct an instance of
DIR-HAM-CYCLE that has a Hamiltonian cycle iff 𝚽 is
satisfiable
• Construction
– Create a graph that has 2n Hamiltonian cycles which
correspond in a natural way to 2n possible truth
assignments
CS 477/677 - Lecture 26 68
3-CNF ≤p DIR-HAM-CYCLE
• Construction: given 3-CNF instance 𝚽 with n variables xi and
k clauses C1, …, Ck
– Construct n paths P1, …, Pn, with Pi containing vi1, vi2…, vib
– There are edges between adjacent vertices on path in each direction
– Hook the paths together with edges
P1
P2
P3
CS 477/677 - Lecture 26 69
3-CNF ≤p DIR-HAM-CYCLE
• Construction (continued)
– Add two vertices s and t and connect them with edges
– Add edge from t to s
– Intuition: cycle traverses path Pi from left to right set xi = 1
s
P1
P2
P3
CS 477/677 - Lecture 26 70
3k + 3
3-CNF ≤p DIR-HAM-CYCLE
• Construction (continued)
– For each clause: add a node and 6 edges
x1
x2
x3
CS 477/677t - Lecture 26 71
3-CNF ≤p DIR-HAM-CYCLE
• Claim: 𝚽 is satisfiable iff G has a Hamiltonian cycle
• Proof “⇒” Suppose 3-CNF has satisfying assignment x*
– Then, define Hamiltonian cycle in G as follows:
• If xi* = 1, traverse row i from left to right
• If xi* = 0, traverse row i from right to left
• For each clause Cj , there will be at least one row i in which we are
going in "correct" direction to splice node Cj into tour
CS 477/677 - Lecture 26 72
3-CNF ≤p DIR-HAM-CYCLE
• Claim: 𝚽 is satisfiable iff G has a Hamiltonian cycle
• Proof “⟸” Suppose G has a Hamiltonian cycle 𝚪
– If 𝚪 enters clause node Cj , it must depart on mate edge
• Nodes before and after Cj are connected by an edge e in G
• Removing Cj from cycle, replace it with edge e ⇒ Hamiltonian cycle on
G - { Cj }
– Continuing in this way, ⇒
Hamiltonian cycle 𝚪' in
G - { C 1 , C2 , . . . , C k }
– Set xi* = 1 iff 𝚪' traverses row i left
to right, otherwise set to 0
– Since 𝚪 visits each clause node Cj,
at least one of the paths is traversed
in “correct” direction, and each
clause is satisfied CS 477/677 - Lecture 26 73
Readings
• Chapters 25, 31
CS 477/677 - Lecture 26 74