NP-Completeness and Approximation Algorithms
NP-Completeness and Approximation Algorithms
Professor Chien-Mo James Li Graduate Institute of Electronics Engineering National Taiwan University
Algorithms NTUEE
Outline
NP-completeness, CH34
Polynomial time verification NP-completeness and reducibility NP-completeness proofs NP-complete problems Approximation Algorithms, CH35
Algorithms
NTUEE
Algorithms
NTUEE
Algorithms
NTUEE
Complexity Classes P, NP
Complexity class P:
Set of problems that can be solved in polynomial time O(nk) for some constant k Polynomial time: O(n), O(n2), O(n3), O(n lg n) .... Problems in P are considered tractable Not polynomial time: O(2n), O(nn), O(n!) .... considered intractable Closed under addition, multiplication, composition, complement Complexity class NP (Nondeterministic Polynomial): Set of problems can be verified in polynomial time we know P NP, but P = NP ?
Think of a non-deterministic computer as a computer that magically guesses a solution, then has to verify that it is correct. If a solution exists, computer always guesses it. One way to imagine it: a parallel computer that can freely spawn an infinite number of processes.
Algorithms NTUEE
HC NP
Is HC NP?
Given a set of vertices Check if the tour visits every vertex exactly once Check whether each edge along the cycle actually exists Check if the first vertex = last vertex All can be done in O(n) time so HC NP
Algorithms
NTUEE
The P = NP Question
P = NP? is one of biggest open questions in computer science
Most theorists believe P NP Complexity class NP-complete (NPC): The set of problems that can be verified in polynomial time but not sure if it can be solved in polynomial time NPC is at crux of P = NP question If any NPC problem can be solved in polynomial time, then all problems in NP can be solved in polynomial time a US$1 million reward to anyone who has a formal proof !
NP P=NP=NPC
Algorithms
P
NTUEE
NPC 7
not likely
more likely
Outline
NP-completeness, CH34
polynomial time verification NP-completeness and reducibility NP-completeness proofs NP-complete problems Approximation Algorithms, CH35
Algorithms
NTUEE
Decision Problems
Decision problems: find answers to yes/no questions Examples of decision problems:
Minimum Spanning Tree Given a graph G=(V, E) and a bound K, is there a spanning tree with a cost at most K? Traveling salesman Problem (TSP): Given cities, distance between each pair of cities, and a bound B, is there a route that starts and ends at a given city, visits every city exactly once, and has total distance at most B ?
Algorithms
NTUEE
Polynomial-time Reduction
Let L1 and L2 be two decision problems
Suppose algorithm A2 can solve L2. Can we use A2 to solve L1? Polynomial-time reduction from L1 to L2: L1 P L2 Function f reduces input for L1 into an input for L2 reduced input is a yes input for L2 iff the original input is a yes input for L1 f is computable in polynomial time L1 is no more than a polynomial factor harder than L2
yes
Algorithms
yes
NTUEE
no
no
10
Lemma 34.3
(Lemma 34.3) If L1 P L2 then L2 P implies L1 P Fig 34.5
F is a reduction algorithm that computes the reduction function f from L1 to L2 in polynomial time A2 is a polynomial time algorithm that decides L2 A1 decides whether xL1 by using F to transform input x into f(x)
Algorithms
NTUEE
11
FFT
it takes more time to compute L1 than L2 why L1 P L2 ? not L1 P L2 ?
Algorithms
NTUEE
12
Create a set of n cities labeled with names in V Assign distance between u and v
Algorithms
NTUEE
13
HC P TSP (Step 2)
G has an HC iff reduced instance has a TSP with distance B
If x is a solution to HC , then f(x) must be a solution to TSP Suppose the HC is x = <v1, v2, , vn, v1>. Then, x is also a tour in the transformed TSP instance The distance of the tour h is n = B since there are n consecutive edges in E, and so has distance 1 in f(x) Thus, f(x) is a solution to TSP f(x) has a TSP tour with distance B
Algorithms
NTUEE
14
If f(x) is a solution to TSP, then x must be a solution to HC Suppose there is a TSP tour with distance n = B Let it be <v1, v2, , vn, v1> Since distance of the tour n and there are n edges in the TSP tour, the tour contains only edges in E Thus, <v1, v2, , vn, v1> is a Hamiltonian cycle
Algorithms
NTUEE
15
NP-Completeness
A decision problem L is NPC if both conditions are true
1. 2.
Algorithms
NTUEE
16
Combinational Circuit
Inverter x x x AND xy xy OR xy x+y XOR xy xy+xy XNOR x y xy xy+xy if and only if implication xy x+xy
Algorithms
NTUEE
17
Circuit-SAT
Circuit-Satisfiability Problem (Circuit-SAT):
Given a combinational circuit C of AND, OR, and NOT gates Is there an assignment of Boolean values to the inputs that makes output of C = 1? A circuit is satisfiable if there exists a set of Boolean input values that makes output of the circuit =1 Circuit (a) is satisfiable since x1, x2, x3= 1, 1, 0 makes output =1 how about (b)?
=1?
Algorithms
NTUEE
18
Circuit-SAT is NPC
To prove Circuit-SAT is NPC, we need two conditions
1. 2.
Algorithms
NTUEE
19
Outline
NP-completeness, CH34
polynomial time verification NP-completeness and reducibility NP-completeness proofs NP-complete problems Approximation Algorithms, CH35
Algorithms
NTUEE
20
4. 5.
Prove L NP Select a known NP-complete problem L' Construct a reduction function f that maps every instance of L' to an instance of L Prove that x L' iff f(x) L for all input x Prove that f is done in polynomial time
http://en.wikipedia.org/wiki/List_of_NP-complete_problems
Algorithms
NTUEE
21
22
( x6 x4 ) ( x7 ( x1 x2 x4 )) ( x8 ( x5 x6 )) ( x9 ( x6 x7 )) ( x10 ( x7 x8 x9 )) 144424443
clause
Algorithms
NTUEE
23
( x5 ( x1 x2 )) ( x6 x4 ) ( x7 ( x1 x2 x4 )) ( x8 ( x5 x6 )) ( x9 ( x6 x7 )) ( x10 ( x7 x8 x9 )) 144424443
clause
Algorithms
NTUEE
24
3-CNF Satisfiability
Conjunctive normal form (CNF) if a Boolean formula is expressed as
AND of clauses 3-CNF means each clauses has exactly three distinct literals literal is an occurrence of a variable or its negation Example: 3-CNF, 4 clauses, 12 literals
( x1 x1 x2 ) ( x3 x2 { x4 ) ( x1 x3 x4 ) ( x1 x3 x5 ) { 14 4 244 3
literal literal clause
Algorithms
NTUEE
25
Outline
NP-completeness, CH34
polynomial time verification NP-completeness and reducibility NP-completeness proofs NP-complete problems Approximation Algorithms, CH35
Algorithms
NTUEE
26
Subset Sum
TSP (decision)
27
Algorithms
NTUEE
28
given a set V of k vertices for each pair of vertices u,v V, check if there is an edge (u,v) can be done in polynomial time
Algorithms
NTUEE
29
given a 3CNF of k clauses: C1 C2 ... Ck construct a graph G such that each clause Cr is represented by a triple of 3 vertices vr1 vr2 vr3 an edge between vri and vsj of difference clauses (r s) their literals are consistent e.g. x and x are not consistent 3 3
Fig. 34.14
= ( x1 x2 x3 )
(x1 x2 x3 ) ( x1 x2 x3 )
f f
No edge
Algorithms
NTUEE
30
x satisfy f(x) is a clique of size k x satisfy so at least on literal =1 on each side correspond to a clique of size k in G example: x2=0, x3=1 f(x) is a clique of size k x satisfy f(x) is a clique so it contains exactly one vertex in every triple assign 1 to each literal in the clique without conflict thus, x satisfies
= ( x1 x2 x3 )
(x1 x2 x3 ) ( x1 x2 x3 )
Algorithms
NTUEE
31
Vertex-cover Problem
A vertex cover of undirected graph G=(V,E) is a subset V V
if (u,v) E, then u V or v V or both that means, every edges is covered by at least one vertex Cover size = number of vertices in V Minimum vertex-cover problem: find a vertex cover of minimum size Exercises: prove minimum vertex cover is NPC show that CLIQUE P VERTEX-COVER example: Fig 34.15 clique V = {u,v,y,x} Vertex cover of G =V-V={w, z}
f G
Algorithms NTUEE
G
32
Subset-sum Problem
Given a finite set of positive integers S, and a target integer t Find a subset S of S whose elements sum to t example:
S={1, 2, 7, 14, 49, 98, 343, 686, 2409, 16808, 17206, 117705, 117993} t = 138457 S={1, 2, 7, 98, 343, 686, 2409, 17206, 117705} Exercise: Prove Subset-Sum is NPC Hint: 3CNF-SAT P SUBSET-SUM
Algorithms
NTUEE
33
Outline
NP-completeness, CH34 Approximation Algorithms, CH35
Algorithms
NTUEE
34
Algorithms
NTUEE
35
Algorithms
NTUEE
36
Approximation Algorithms
In practice, near optimal solutions are good enough
approximation algorithms returns near-optimal solutions often in polynomial time Ratio bound, (n) 1 C* is optimal solution; C is our solution C C* max C* , C (n) if (n) = constant, we call it (n)-approximation algorithm Relative error bound, (n) (n)-1 | C C* | (n) C*
Algorithms
NTUEE
37
APPROX-VERTEX-COVER(G)
1 2 4 5 6 7 8
Algorithms
38
Ratio Bound = 2
(Theorem 35.1) APPROX-VERTEX-COVER is 2-approximation
algorithm Proof: Let A = set of edges picked ; C = set of vertices picked |C| = 2|A| Let C* is optimal cover no edges in A share an endpoint, so no two edges in A are covered by the same vertex from C* |A||C*| so |C|2|C*|
Algorithms
NTUEE
39
Outline
NP-completeness, CH34 Approximation Algorithms, CH35
Algorithms
NTUEE
40
c(u, v)
triangle inequality
Algorithms
NTUEE
41
H*
42
Ratio Bound = 2
(Theorem 35.2) APPROX-TSP is a polynomial time 2-approximation
algorithm for TSP with triangle inequality Proof: Let T = MST Let H* be the optimal tour. W H* is formed by some tree plus an edge c(T) c(H*) Let W = a full walk along T e.g. a, b, c, b, h, b, a, d, e, f, e, g, e, d, a since W traverse every edge of T twice H c(W) 2 c(T) H = removed from W all but first visit to each vertex e.g. a, b, c, h, d, e, f, g triangle inequality: removing vertex does not increase cost c(H) c(W) so, c(H) 2 c(H*)
Algorithms NTUEE
43
Algorithms
NTUEE
44
Outline
NP-completeness, CH34 Approximation Algorithms, CH35
Algorithms
NTUEE
45
Set-Covering Problem
Given a finite set X, and a family F of subsets of X subset S F covers its elements
X=U S
SF
Optimization problem
minimum size of C Decision problem exist a covering C size k? exercise: prove set covering is NPC (Hint: use vertex covering) Applications of Set-Covering find minimum of people (C) that perform all tasks (X) minimum 2-level combinational circuit design
NTUEE
Algorithms
46
Greedy-Set-Cover
Each iteration chooses the subset that covers most elements
Algorithms
47
Algorithms
NTUEE
48
Quine-McCluskey Algorithm
Invented in 1956, one of the earliest EDA algorithms Two-step process
1. Generate all Prime Implicants (PI) PI = product term that cannot be combined with other terms 2. Select minimum number of PI
set-covering
Algorithms
NTUEE
49
at least ONE of the following terms must be covered abcd + abcd + abcd + abcd + abcd + abcd + abcd called On-set abcd+abcd+abcd may or may not be covered ab a called Dont-care set cd 00 01 11 10 None of the others can be covered called Off-set 00 X 1 0 1
01
0 0 0
1 X 1 b
1 X 0
1 d 0 1
11
c
10
Algorithms
NTUEE
50
two 1
three 1 four 1
Algorithms NTUEE
51
52
NTUEE
7 Prime Implicants: 0-00 = a'c'd' 100- = ab'c' 1-01 = ac'd -1-1 = bd -000 = b'c'd' 10-0 = ab'd' 01- - = a'b
53
Karnaugh Map
PI on Karnaugh map: circles that can not be grow larger
Algorithms
NTUEE
54
PI Table
Algorithms
NTUEE
55
essential rows
distinguished columns 56
Algorithms
NTUEE
Algorithms
NTUEE
57
Final Result
Algorithms
NTUEE
58
FFT
Can Quine-McCluskey find optimal solution for all cases?
obviously not ... set-covering is NP-hard Give an example where PI table has neither essential rows nor distinguished columns Quine-McCluskey has trouble finding optimal solution
Algorithms
NTUEE
59
Algorithms
60
Reading
CH 34 CH35 REF: Roth, Fundamentals of Logic Design Cengage Learning, CH6
Algorithms
NTUEE
61