0% found this document useful (0 votes)
26 views

Lecture26 Number Theoretic Algorithms - NP - VectCov - Hamilt

The document discusses NP-complete problems and the relationship between classes P and NP. It defines the classes P and NP, and what problems fall into each class. It also describes how to prove that a problem is NP-complete by showing that a known NP-complete problem can be reduced to the problem in polynomial time.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lecture26 Number Theoretic Algorithms - NP - VectCov - Hamilt

The document discusses NP-complete problems and the relationship between classes P and NP. It defines the classes P and NP, and what problems fall into each class. It also describes how to prove that a problem is NP-complete by showing that a known NP-complete problem can be reduced to the problem in polynomial time.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 74

NP

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:

there exists an algorithm that can solve the


problem in O(nk), k constant
• Problems in P are also called tractable
• Problems not in P are also called intractable
– Can be solved in reasonable time only for small inputs

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

• Class NP consists of problems that are verifiable in

polynomial time (i.e., could be solved by


nondeterministic polynomial algorithms)
– If we were given a “certificate” of a solution, we could

verify that the certificate is correct in time polynomial to


the size of the input

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

• To solve a decision problem A in polynomial time


1. Use a polynomial time reduction algorithm to transform A
into B
2. Run a known polynomial time algorithm for B
3. Use the answer for B as the answer for A

CS 477/677 - Lecture 26 8
Reductions

• Given two problems A, B, we say that A is

reducible to B (A ≤p B) if:

1. There exists a function f that converts the input of A to an

input of B in polynomial time

2. A(i) = YES ⟺ B(f(i)) = YES (for every input i)

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

• If B satisfies only property 2) we say that B is NP-hard


• No polynomial time algorithm has been discovered for an NP-
Complete problem
• No one has ever proven that no polynomial time algorithm can
exist for any NP-Complete problem

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

– We have a polynomial reduction f from A to B

⇒ No polynomial time algorithm exists for B

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

If B ∈ NP ⇒ B ∈ NP-Complete (by definition of NP-C)


If B ∉ NP ⇒ B ∈ NP-Hard (by definition of NP-H)

CS 477/677 - Lecture 26 12
Proving NP-Completeness

1. Prove that the problem B is in NP


– A randomly generated string can be checked in
polynomial time to determine if it represents a solution

2. Show that one known NP-Complete problem can be


transformed to B in polynomial time
– No need to check that all NP-Complete problems are
reducible to B

CS 477/677 - Lecture 26 13
Is P = NP?
• Any problem in P is also in NP: P NP-complete

P ⊆ NP NP

• We can solve problems in P, even without having a


certificate
• The big (and open question) is whether P = NP

Theorem: If any NP-Complete problem can be solved in


polynomial time ⇒ then P = 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)

• Longest simple path


– Given a graph G = (V, E) find a longest path from a source
to all other vertices
– NP-complete
CS 477/677 - Lecture 26 15
P & NP-Complete Problems
• Euler tour
– Given G = (V, E) a connected, directed graph, find a cycle
that traverses each edge of G exactly once (may visit a
vertex multiple times)
– Polynomial solution O(E)
• Hamiltonian cycle
– G = (V, E) a connected, directed graph find a cycle that
visits each vertex of G exactly once
– NP-complete

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

Satisfying assignment: an assignment of values (0, 1) to variables


xi that causes 𝚽 to evaluate to 1
E.g.: 𝚽 = (x1 ∨ x2) ∧ (x1 ∨ ¬ x2) ∧ (¬ x1 ∨ ¬ x2)
Certificate: x1 = 1, x2 = 0 ⇒ 𝚽 = 1 ∧ 1 ∧ 1 = 1
– Formula Satisfiability is first to be proven NP-Complete
CS 477/677 - Lecture 26 17
3-CNF Satisfiability
3-CNF (clause normal form) Satisfiability Problem:
– n boolean variables: x1, x2, …, xn
– Literal: xi or ¬ xi (a variable or its negation)
– Clause: cj = an OR of three literals
– Formula: 𝚽 = c1 ∧ c2 ∧ … ∧ cm (m clauses)

• 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

Optimization problem: Clique(G, 2) = YES


Clique(G, 3) = NO
– Find a clique of maximum size

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

• Start with an instance of 3-CNF:

– 𝚽 = C1 ∧ C2 ∧ … ∧ Ck (k clauses)

– Each clause Cr has three literals: Cr = l1r ∨ l2r ∨ l3r

• Idea:

– Construct a graph G such that 𝚽 is satisfiable if and only if

G has a clique of size k

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

• For each clause Cr = l1r ∨ l2r ∨ l3r place a triple of


vertices v1r, v2r, v3r in V
• Put an edge between two vertices vir and vjs if:
– vir and vjs are in different triples
– lir is not the negation of ljs
CS 477/677 - Lecture 26 22
3-CNF ≤p Clique 𝚽 = C1 ∧ C2 ∧ C3
• Suppose 𝚽 has a satisfying C1 = x1 ∨ ¬x2 ∨ ¬x3
x1 ¬x2 ¬x3
assignment
– Each clause Cr has some ¬x1 x1
literal assigned to 1 – this
corresponds to a vertex vir x2 x2
– Picking one such literal from
x3 x3
each Cr ⇒ a set V’ of k
vertices C2 = ¬x1 ∨ x2 ∨ x3 C3 = x1 ∨ x2 ∨ 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

the clique (we can do it because the literals of these vertices


cannot belong to complementary literals)
– Each clause is satisfied ⇒ 𝚽 is satisfied

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

(hamiltonian cycle): ⟨u, w, v, x⟩


– Visit each city only once
– Finish at the city he started from
– Total cost is minimum
• TSP = tour with cost at most k
CS 477/677 - Lecture 26 25
TSP ∈ NP

• 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

E’ = {(i, j): i, j ∈ V and i ≠ j} 3 2


1
0 if (i, j) ∈ E
c(i, j) = x w
1 if (i, j) ∉ E 5
1
u v
• Let’s prove that: 0

• G has a hamiltonian cycle ⟺ 0 G’ has


0
0
a tour of cost at most 0
x w
0

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

• G has a hamiltonian cycle h


⇒ Each edge in h ∈ E ⇒ has cost 0 in G’
⇒ h is a tour in G’ with cost 0
• G’ has a tour h’ of cost at most 0
⇒ Each edge on tour must have cost 0
⇒ h’ contains only edges in E
CS 477/677 - Lecture 26 28
Approximation Algorithms
Various ways to get around NP-completeness:
1. If inputs are small, an algorithm with exponential time
may be satisfactory
2. Isolate special cases, solvable in polynomial time
3. Find near-optimal solutions in polynomial time
• Approximation algorithms
• Local search (hill climbing)

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

A funnel A jagged funnel


CS 477/677 - Lecture 26 30
Vertex Cover
u v
• G = (V, E), undirected graph
• Vertex cover = a subset V’ ⊆ V z w

which covers all the edges y x


– if (u, v) ∈ E then u ∈ V’ or v ∈ V’ or both.
• Size of a vertex cover = number of vertices in it
Problem:
– Find a vertex cover of minimum size
– Does graph G have a vertex cover of size k?

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

• Hill climbing (gradient descent) idea:


– Start with a solution S = V
– If there is a neighbor S' that is a vertex cover and has lower
cardinality, replace S with S'.
– Algorithm ends after at most n steps (each update decreases
the size of the cover by one)

CS 477/677 - Lecture 26 32
Gradient Descent: Vertex Cover
• Local optimum. No neighbor is strictly better.

optimum = center node only optimum = all nodes on left side


local optimum = all other nodes local optimum = all nodes on right side

optimum = even nodes


local optimum = omit every third node

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

• Approximate solution (greedy):


– Start with a list of all edges
– Repeatedly pick an arbitrary edge (u, v)
– Add its endpoints u and v to the vertex-cover set
– Remove from the list all edges incident on u or v

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

6. remove from E’ all b c d


edges incident on u, v
7. return C 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

It can be proven that the approximation algorithm returns a


solution that is no more than twice the optimal vertex cover.
CS 477/677 - Lecture 26 36
The Set Covering Problem
• Finite set X
• Family F of subsets of X: F = {S1, S2, …, Sn}

X=⋃S
S∈F

• Find a minimum-size subset C ⊆ F that covers all the


elements in X
• Decision: given a number k find if there exist k sets
Si1, Si2, …, Sik such that:
Si1 ⋃ Si2 ⋃ … ⋃ Sik = X
CS 477/677 - Lecture 26 37
Greedy Set Covering

Idea: S1

• At each step pick a set S


S2
that covers the greatest
S6
number of remaining S3 S4 S5

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

• G = (V, E) ⇒ complement graph GC = (V, EC)


EC = {(u, v):, u, v ∈V, and (u, v) ∉E}
Idea:
⟨G, k⟩ (clique) → ⟨GC, |V|-k⟩ (vertex cover)

CS 477/677 - Lecture 26 41
Clique ≤p Vertex Cover (VC)
G GC G GC

Clique = 2 VC = 2 Clique = 2 VC = 3

Size[Clique](G) + Size[Vertex Cover](GC) = n

• G has a clique of size k ⟺ GC has a vertex cover of


size n – k
• S is a clique in G ⟺ V – S is a vertex cover in GC

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’

• Prove: GC has a vertex cover V’⊆ V, |V’| = |V| - k ⇒ V-V’ is a clique in G


• For all v, w ∈ V, if (v, w) ∈ EC

⇒ 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?

• Is there an independent set


of size ≥ 6?
– Yes.
• Is there an independent set
of size ≥ 7?
– No.

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

constraint satisfaction 3-CNF

to
es SET
Dick Karp (1972)
uc
red NT 1985 Turing
- CNF NDE
3 E
EP Award
IND

INDEPENDENT SET DIR-HAM-CYCLE GRAPH 3-COLOR SUBSET-SUM

VERTEX COVER HAM-CYCLE PLANAR 3-COLOR SCHEDULING

SET COVER TSP

packing and covering sequencing partitioning numerical


CS 477/677 - Lecture 26 49
Vertex Cover
u v
• G = (V, E), undirected graph
• Vertex cover = a subset V’ ⊆ V z w

which covers all the edges y x


– if (u, v) ∈ E then u ∈ V’ or v ∈ V’ or both.
• Size of a vertex cover = number of vertices in it
Problem:
– Find a vertex cover of minimum size
– Does graph G have a vertex cover of size k?

CS 477/677 - Lecture 26 50
INDEPENDENT-SET ≤p VERTEX-COVER

• We show S is an independent set iff V ⟺ S is a


vertex cover
u

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

• We show S is an independent set iff V ⟺ S is a


vertex cover
u

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

B not 3-colorable if all are red

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

a literal set to true in 3-SAT assignment


B

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

clause node clause node

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

You might also like