cs5371 theory of computationwkhon/toc07-lectures/lecture22.pdfd-hampath is np-complete (14) in the...
TRANSCRIPT
CS5371Theory of Computation
Lecture 22: Complexity VII(More NP-complete Problems)
•We shall continue to look at more NP-complete problems:
–Directed Hamiltonian Path–Hamiltonian Path–SUBSET SUM–PARTITION
•Some more if we have time today
Objectives
Directed HAMPATH
Theorem: D-HAMPATH is NP-complete.
Let G be a directed graph. A directedHamiltonian path in G is a path that visitsall the vertices of G once and only once.
Let D-HAMPATH be the language
{ G,s,t| G has a directed Hamiltonianpath from s to t }
D-HAMPATH is NP-complete (2)
Proof: First, D-HAMPATH is in NP (easyto show). Then, we show it is NP-completeby reduction from 3SAT.
To determine if Fis in 3SAT, we shallconstruct G with two special vertices s, tsuch that
F3SAT G,s,tD-HAMPATH
D-HAMPATH is NP-complete (3)
Let x1, x2, …, xj be the variables in the3cnf-formula F. For each variable xi, wecreate a ‘diamond-shaped structure’thatcontains a horizontal row of nodes:
xi …
D-HAMPATH is NP-complete (4)
Let C1, C2, …, Ck be the clauses in F. Foreach clause Cm, we create a node:
Cm
Next slide shows the global structure of G.
It shows all the elements of G and theirrelationship, except the relationship of thevariables to the clauses that contains them
C2
…
Ck
C3
C1
s
x1
…x2
…xj
tHigh-level structure of G
Diamond Structure
Each diamond structure contains ahorizontal row of nodes connected byedges running in both directions.
There are 3k+1 nodes in each row (inaddition to the two nodes on the end thatbelongs to the diamond)
Diamond StructureThe nodes in the horizontal row aregrouped into adjacent pairs, one for eachclause, with extra separator nodes next tothe pair:
…
pairsfor C1
pairsfor C2
xi
Connection with Clauses
If variable xi appears in clause Cm, we addthe following two edges from the mth pairin the xi’s diamond structure to node Cm
…
pairsfor Cm
xi…
Cm
Connection with Clauses (2)
If :xi appears in clause Cm, we add thefollowing two edges from the mth pair inthe xi’s diamond structure to node Cm
…
pairsfor Cm
xi…
Cm
Note thedirection change
D-HAMPATH is NP-complete (5)
After we add all the edges correspondingto each occurrence of xi or : xi in eachclause, the construction of G is finished
We claim that this is our desired reduction:
(=>) Suppose that F is satisfiable. Todemonstrate an Hamiltonian path from s tot in G, we first ignore the clause nodes
D-HAMPATH is NP-complete (6)
The path begins at s, goes through eachdiamond in turn, and ends up at t
To hit the horizontal nodes in a diamond,the path is either one of the following way:
… …
Left-to-Right Right-to-Left
D-HAMPATH is NP-complete (7)
If xi is assigned TRUE in the satisfyingassignment of F, we use Left-to-Rightmethod to traverse the correspondingdiamond. Otherwise, if xi is assignedFALSE, we use the Right-to-Left method
So far, the path covers all the nodes in Gexcept the clause nodes. We can easilyinclude them by adding detours at thehorizontal node
D-HAMPATH is NP-complete (8)
In each clause, we select a literal that isassigned TRUE in the satisfying assignmentof F
Suppose we select xi in clause Cm. Then, inour current path, the horizontal nodes in xiare from Left-to-Right. Also, by ourconstruction of edges in P. 11, we can seethat we can easily detour at the mth pair ofhorizontal nodes, and cover Cm
D-HAMPATH is NP-complete (9)
Similarly, if we select : xi in clause Cm, thenin our current path, the horizontal nodes inxi are from Right-to-Left. Also, by ourconstruction of edges in P. 12, we can seethat we can easily detour at the mth pair ofhorizontal nodes, and cover Cm
Thus, if F is satisfiable, G has a Hamiltonianpath from s to t (proof of the => direction done)
D-HAMPATH is NP-complete (10)
(<=) For the other direction, if G has aHamiltonian path from s to t, we shalldemonstrate a satisfying assignment for F
Firstly, we take a look at the Hamiltonianpath. If it is “normal”--- that is, visitingthe diamonds in the order from top one tothe bottom one (excluding the detours toclause nodes) --- we can obtain a satisfyingassignment as follows: (next slide)
D-HAMPATH is NP-complete (11)
•If the path is Left-to-Right in thediamond for xi, we assign TRUE to xi
•If the path is Right-to-Left in thediamond for xi, we assign FALSE to xi
Because each clause node is visited once inthe Hamiltonian path, by determining howthe detour is taken, we know that at leastone literal in each clause is TRUE
D-HAMPATH is NP-complete (12)
It remains to show is:
Hamiltonian path must be normal
We prove this by contradiction.
Suppose on the contrary that the path isnot normal. Then, the Hamiltonian pathmust have entered a clause from onediamond, but left the clause to anotherdiamond, as shown in next slide:
When Hamiltonian Path not Normal
…
…
Cm
a1 a2 a3
b
D-HAMPATH is NP-complete (13)
The Hamiltonian path goes from a1 to c butinstead of returning to a2, it goes to b ina different diamond
However, in our graph G, a2 is connected toat most three nodes: a1 , a3, and c (why?)
If a2 is a separator: connects to a1 and a3 only
Else: connects to a1, a3, and c only
D-HAMPATH is NP-complete (14)
In the current path, a1 and c have both beenvisited now. So a2 cannot find two distinctnodes (one incoming neighbor, one outgoing neighbor)that connects it to the current path
Thus, current path is not Hamiltonian !!
and a contradiction occurs (where?)
So, all Hamiltonian path from s to t must benormal. This implies that if such a pathexists, F is satisfiable (proof of <= direction done)
D-HAMPATH is NP-complete (15)
In conclusion, we have
F3SAT G,s,tD-HAMPATH
As it is easy to see that the above reductionfrom 3SAT to D-HAMPATH takes onlypolynomial time, D-HAMPATH is thereforeNP-complete
Undirected HAMPATH
Theorem: HAMPATH is NP-complete.
Let HAMPATH be the language
{ G,s,t| G has a (undirected) Hamiltonianpath from s to t }
HAMPATH is NP-completeWe just give a sketch of the proof:
1. First, HAMPATH is in NP (easy to check).
2. Next, to see why it is NP-complete, wereduce 3SAT to HAMPATH, using similarconstruction as we use in D-HAMPATH.
However, the graph is now undirected.Instead of using directed edges in thereduction in D-HAMPATH before, we …
HAMPATH is NP-complete (2)…replace every node u in previous graph by3 nodes, uin, umid, uout, in the new graph
A directed edge from u to v in the previousgraph is now replaced by an undirectededge joining uout and vin
This completes the reduction, and we canshow that this reduction works (exerciseat home!) and takes polynomial time
Thus, HAMPATH is NP-complete
How about HAM-CIRCUIT ?
Theorem: HAM-CIRCUIT is NP-complete.
Let HAM-CIRCUIT be the language
{ G| G has a Hamiltonian circuit }
HAMCIRCUIT is NP-complete (2)
Proof: First, HAMCIRCUIT is in NP (easyto show). Then, we show it is NP-completeby reduction from HAMPATH.
To determine if G,s,tis in HAMPATH, weconstruct G’by adding to G a new vertex v,and two edges {v,s} and {v,t}. Then it iseasy to see that:
G,s,tHAMPATH G’HAMCIRCUIT
SUBSET-SUM is NP-Complete
Theorem: SUBSET-SUM is NP-complete.
Let S be a set of positive integers.
Let SUBSET-SUM be the language
{ S,t| S has a subset whose sum is t }
SUBSET-SUM is NP-complete (2)
Proof: First, SUBSET-SUM is in NP (easyto show). We show it is NP-complete byreduction from 3SAT
Let F be a Boolean formula in 3cnf-form.Let x1, x2, …, xj be its variables and let C1,C2, …, Ck be its clauses.
We transform F into a set S of 2j+2k (verylarge) numbers, with each having j+k digits(the first j digits corresponds to variables, and thefollowing k digits corresponds to clauses)
SUBSET-SUM is NP-complete (3)
For each variable xi, we create two numbersyi and zi, such that their ith digit (correspondingto xi) is set to 1. Also,
•if xi appears in Cm, set the (j+m)th digitof yi (which corresponds to Cr) to 1
•if :xi appears in Cm, set the (j+m)th digitof zi (which corresponds to Cm) to 1
The remaining digits of yi and zi are set to 0
Constructing the numbers in S
001000zj
101000yj
010010z2
100010y2
000001z1
010001y1
CkC2C1j321
Assume C1 = ( x1 _ : x2 _ x3) and C2 = ( x2 _ : x3 _ xj)
SUBSET-SUM is NP-complete (4)
In addition, S contains one pair of numbers,gm and hm for each clause Cm
These two numbers are equal, with the(j+m)th digit (which corresponds to Cm) set to 1[Later, they are used as ‘fillers’to get a subset sum]
Let t = 1111 3333 [j 1s followed by k 3s]be the target number. We shall show:
F is satisfiable a subset of S adds to t
SUBSET-SUM is NP-complete (5)
(=>) Suppose F is satisfiable. We select yi ifxi is set to TRUE. Else, we select zi
If we add the numbers selected so far,
1. Leftmost j digits are correct (why?)
2. Each of the remaining k digits isbetween 1 and 3 (why?)
Now, we can select suitable gm or hm (or both)to fill up the differences, thus getting t
SUBSET-SUM is NP-complete (6)
(<=) On the other hand, suppose a subset ofS adds up to t. Then, exactly one of theyi or zi is in this subset (why?)
By setting xi to TRUE when yi is in thesubset, and FALSE when zi is in thesubset
We claim F is satisfied: Consider thenumbers in the subset whose (j+m)th bit(corresponding to Cm) is 1
SUBSET-SUM is NP-complete (7)
There must be 3 such numbers (why?), soone of them must be some yi or zi (why?)
•If it is yi, it means (i) Cm contains xi,and (ii) we have assigned xi to TRUE
•If it is zi, it means (i) Cm contains : xi,and (ii) we have assigned xi to FALSE
In both cases, Cm is satisfied
Thus, all clauses are satisfied, and so is F
SUBSET-SUM is NP-complete (8)
Now, we have shown that
F3SAT S,tSUBSET-SUM
Also, it is easy to check that the abovereduction takes polynomial time (in termsof the length of F).
Thus, SUBSET-SUM is NP-complete.
PARTITION is NP-Complete
Theorem: PARTITION is NP-complete.
Let S be a set of positive integers.
Let PARTITION be the language
{ S| S can be partitioned into two groupssuch that the sum in each group isthe same }
PARTITION is NP-complete (2)
Proof: First, PARTITION is in NP (easy toshow). Then, we show it is NP-complete byreduction from SUBSET-SUM.
To determine if S,k is in SUBSET-SUM, letX = sum of values in S. We construct S’byadding the two numbers 2X-k and X+k to S.Then it is easy to see that (why??):
S,kSUBSET-SUM S’PARTITION
Brain Teaser 1: HITTING SET
Theorem: HITTING-SET is NP-complete.
Let C be a collection of subsets of S. A setof S’is called a hitting set for C if everysubset of C has at least one element in S’.
Let HITTING-SET be the language
{ C,k| C is a collection of subsets with ahitting set of size k }
Brain Teaser 2:SUBGRAPH ISOMORPHISM
Theorem: SUBGRAPH-ISO is NP-complete
We say two graph H=(V,E) and H’=(V’,E’)are isomorphic if there exists a one-to-onefunction f: V’ V such that
{ u,v } in E if and only if { f(u),f(v) } in E’
Let SUBGRAPH-ISO be the language
{ G,H| G has a subgraph isomorphic to H}
SUBGRAPH-ISO is NP-complete
Proof: First, SUBGRAPH-ISO is in NP(easy to show). Then, we show it is NP-complete by reduction from CLIQUE.
Given G,k, we construct G’,H’as follows:
Set G’= G. Set H’= k-clique. Then it iseasy to see that:
G,kCLIQUE G’,H’SUBGRAPH-ISO
Brain Teaser 3:BOUNDED-DEG SPANTREE
Theorem: Bounded-Deg-ST is NP-complete
A spanning tree of a graph G=(V,E) is atree containing every vertex in G, andwhose edges are from E. A degree-kspanning tree is a spanning tree such thatdegree of each internal node is at most k
Let Bounded-Deg-ST be the language
{ G,k| G has a degree-k spanning tree }
Bounded-Deg-ST is NP-complete
Proof: First, Bounded-Deg-ST is in NP(easy to show). Then, we show it is NP-complete by reduction from HAMPATH
Hint: What is a degree-2 spanning tree?
A degree-2 spanning tree is a Hamiltonianpath in the graph!!!
Bounded-Deg-ST is NP-complete (2)
Now, given a graph G, we can transform Ginto G’by adding two nodes, u and v, andtwo edges, {u,s} and {v,t}. Then, we can see
G,s,tHAMPATH
G’,2Bounded-Deg-ST
So, Bounded-Deg-ST is NP-complete
Brain Teaser 4: KNAPSACK
Theorem: KNAPSACK is NP-complete.
Let S be a set of items, each item x in Shas a positive integral value v(x) and apositive integral weight w(x).
Let KNAPSACK be the language
{ S,b,k| a subset of items in S of totalweight at most b, but whose totalvalue is at least k }
KNAPSACK is NP-completeProof: First, KNAPSACK is in NP (easy toshow). Then, we show it is NP-complete byreduction from PARTITION
Let S = {s1,s2,…,sj} be a set of +ve integers.We want to construct S’, b, and k such that
SPARTITION S’,b,kKNAPSACK
The construction of S’is as follows:
KNAPSACK is NP-complete (2)For each si in S, we create xi in S’such that
w(xi) = v(xi) = si
Also, set b = k = Y/2, where Y = s1+s2+…+sj
Now, if S has a partition, then a subset ofnumbers in S adds up to Y/2. By choosingthe items of S’that corresponds to thissubset, the total weight b and the totalvalue k (why?)
Thus, SPARTITION S’,b,kKNAPSACK
KNAPSACK is NP-complete (3)
On the other hand, if a subset of items ofS’have total weight b and total value k,then the sum of the corresponding si in Swill be at most b and at least k (why?).
Since b = k = Y/2, we have the sum ofthose items in S = Y/2Thus, S’,b,kKNAPSACK SPARTITION
As the reduction can be done in polynomialtime, KNAPSACK is NP-complete.
What we have learnt
• The class P, and the class NP• Some problems in NP are the most
difficult ones in the set. We call themNP-complete problems
• SAT is NP-complete• Other problems in NP can be shown to be
NP-complete using polynomial timereduction (from what to what?)
Next Time
• Chapter 8 (not in exam) or Revision?
Decision vs Optimization• The problems we have seen so far are
called decision problems, because we wantto decide if a string is in a set or not
• For instance, in the vertex cover problem,we have a language VERTEX-COVER
{ G,k| G is a graph with a vertex coverof size k }
and our task is to decide if a string is inthe set or not
Decision vs Optimization• For most decision problems we studied,
we can define its optimization version
• E.g, in vertex cover problem, we can ask:
Given a graph G, what is the minimumsubset of vertices that can cover G?
• For the above problem, we don’t want ayes/no answer, but we want a specificcover whose size is minimum
Decision vs Optimization
• Recall: VERTEX-COVER is NP-complete Unlikely to solve its optimization (min)
version in polynomial time
• Other than give up, we may find:• Better exponential-time algorithm
• improve from O(2n) to O(1.28n)• Approximation algorithm• Randomized algorithm• Simplified version
• restrict G to be bipartite
Approximation Algorithm?• Algorithm with performance guarantee
• E.g., an algorithm that can always find acover whose size is at most k times theoptimal one
We call this a k-approximationalgorithm for the vertex cover problem
Approximation Algorithm• In general, a k-approximation algorithm
• for minimization problem guarantees asolution at most k times the optimalone; while
• for maximization problem (such asfinding a maximum clique in G)guarantees a solution at least 1/k timesthe optimal one
• smaller k = better guarantee
Approximation Algorithm• For vertex cover, can we find a good
approximation algorithm?
• Good means:• It runs fast (in polynomial time)• It has small k
• Let us try to find a 2-approximationalgorithm for vertex cover