Algorithmique Npcompleteness 2011c
Algorithmique Npcompleteness 2011c
Algorithmique
Fall semester 2011/12
1
What is an Algorithm?
But formally, to define what an algorithm is, we need to concept of a Turing Machine.
This cannot be developed fully in this course, and is typically part of a course in Theoretical
Computer Science.
2
Turing Machine
Consists of
• Input/Output alphabet (0-1)
• Infinite tape (memory)
• Read/Write head (I/O)
• Control (program) Alan Turing, 1912-1954
0 1 1 0 0 1 0 1 0 1 1 1 0 0 1 0 1 0
3
Dictionary
Tape Memory
Control Program
4
Turing Machines
Turing machines provide a very good abstraction of the notion of computing, and a framework for
studying the complexity of computational problems.
They lie at the heart of the theory of NP-completeness, which we are going to discuss in this class.
However, for time reasons we will not be able to use them. Instead, we rely on an intuitive
concept of an algorithm and its running time (as the number of “operations” they use).
5
Computational Problem
Example
O = {true,false}
O Set of outputs
(G,true) in R iff G is connected
6
Decision Problem
O = {true, false}
I
R⊆I ×O
R is equivalent to this
part
7
Examples
I=N 170141183460469231731687303715884105727 is in R
I = set of graphs × N
(G,3) is not in R
G=
R = {(G, n) | G has an independent set of size ≥ n} (G,2) is in R
Independent set
Set of vertices no two of which
are connected
Clique
Set of vertices any two of
which are connected
8
Algorithms for Decision Problems
Example
Algorithm: DFS
Example
Example
Algorithm: ?????
9
Input Lengths
The length |ι| of an input ι ∈ I is the number of bits sufficient to represent it.
Example
Example
10
Polynomial Functions
Examples
11
The Class P
Class of all decision problems R ⊆ I for which there is an algorithm which decides for every
given ι ∈ I whether ι ∈ R and for which the running time is polynomial in |ι|
Example
Example
I = {(G,s,t,c,M) | G directed graph, c:E -> N integer capacities, s,t distinct vertices, M integer}
FLOW is in P:
• The algorithm of Ford and Fulkerson with BFS shortest path selection has running time
polynomial in the input size.
P is the class of all decision problems for which there is a polynomial time decision algorithm.
12
Why Polynomial Time?
13
The Class P
PRIMALITY is in P ?
CLIQUE is in P ?
INDEP-SET is in P ?
14
The Class P
CLIQUE is in P ? Unknown
INDEP-SET is in P ? Unknown
15
NP Completeness
Theory of NP-completeness tries to understand why we have not been able to find
polynomial time algorithms for some fundamental computational problems.
If you could solve problem X in polynomial time, then could solve a whole lot of other very
hard problems in polynomial time as well.
To some researchers, it means that there are no polynomial time algorithms for such
problems.
16
Polynomial Reduction
If we can solve decision problem X, then we can also solve decision problem Y using an
algorithm for the solution of problem X. We want to reduce Y to X
How can we use an algorithm for problem X for inputs for problem Y?
If the output of the algorithm is true, then we want to deduce that y belongs to Y
If the output of the algorithm is false, then we want to deduce that y doesn’t belong to Y
X ⊆ I, Y ⊆ J decision problems.
18
Polynomial Reduction
true false
true false
X
19
Example
CLIQUE ≤ INDEP-SET
INDEP-SET ≤ CLIQUE
Same reasoning as above
20
Example
A node cover in a graph is a set S of vertices such that every edge has at least one endpoint
in S.
INDEP-SET ≤ NODE-COVER
21
Transitivity
X ≤ Y and Y ≤ Z =⇒ X ≤ Z
22
The Class NP
However, these problems have an interesting feature: while it may be difficult to decide for
every input whether it belongs to these sets, it is rather easy to prove the claim that an input
belongs to these sets.
23
Examples
239180344702445517659253489067517158015016827169517973733973209 is in COMPOSITES!
You don’t need to factor the number to see that I am right: I provide you with a “short” proof:
239180344702445517659253489067517158015016827169517973733973209 =
15465456498352886242205023347881 * 15465456498352886242205023347889
You just need to perform the multiplication to get convinced (which is much easier than
factoring the integer).
(G,10) is in INDEP-SET
24
Examples
239180344702445517659253489067517158015016827169517973733973209 is in COMPOSITES!
You don’t need to factor the number to see that I am right: I provide you with a “short” proof:
239180344702445517659253489067517158015016827169517973733973209 =
15465456498352886242205023347881 * 15465456498352886242205023347889
You just need to perform the multiplication to get convinced (which is much easier than
factoring the integer).
(G,10) is in INDEP-SET
25
The Class NP
Decision problems in the class NP are problems R for which there is an efficient proof of
membership to R.
26
Closed under Polynomial Reductions
27
Includes P
P is a subset of NP
Suppose that R is in P, and that f is a poly-time algorithm for R.
Given a, the witness w is empty, and the verifier is f itself: note that f(a)=1 iff a is in R.
NP
28
Examples
INDEP-SET ∈ NP
(G,10) is in INDEP-SET
29
Examples
COMPOSITES ∈ NP
239180344702445517659253489067517158015016827169517973733973209 is in COMPOSITES!
15465456498352886242205023347881 , 15465456498352886242205023347889
The verifier multiplies these numbers and checks whether the result is the original number
The verifier can perform this task in time polynomial in the number of digits of the original
number (using the school method)
30
PRIMES
PRIMES ∈ NP?
What is NOT a verifier is an algorithm that checks all the potential divisors of n.
This is because this algorithm runs in time proportional to sqrt(n), which is NOT polynomial in
the input length |n| = O(log(n)).
31
Elementary Number Theory
• Check that p1,....,pk are all the prime divisors of p-1 Easy to do (division)
• Check that w(p−1)/p1 , . . . , w(p−1)/pk �≡ 1 mod p Easy to do (binary method of exponentiation)
32
Example: Pratt Tree
n w
1223, 5
2, 1 47, 5 13, 2
23, 5 2, 1 2, 1 3, 2
11, 2 2, 1 2, 1
2, 1 5, 2
Process the tree
from bottom to top
2, 1
33
Example: Pratt Tree
1223, 5
2, 1 47, 5 13, 2
23, 5 2, 1 2, 1 3, 2
11, 2 2, 1 2, 1
2, 1 5, 2
2, 1 prime
34
Example: Pratt Tree
1223, 5
2, 1 47, 5 13, 2
23, 5 2, 1 2, 1 3, 2
11, 2 2, 1 2, 1
2, 1 prime
35
Example: Pratt Tree
1223, 5
2, 1 47, 5 13, 2
23, 5 2, 1 2, 1 3, 2
11-1=2 x 5,
22 = 1 mod 11, 25 = 1 mod 11 11, 2 2, 1 prime 2, 1 prime
So 11 is prime
2, 1 prime
36
Example: Pratt Tree
1223, 5
2, 1 47, 5 13, 2
23-1=2 x 11,
3-1=2, 2 = 1 mod 3,
52 = 1 mod 23, 511 = 1 mod 23 23, 5 2, 1 prime 2, 1 3, 2
so 3 prime
So 23 is prime
11-1=2 x 5,
22 = 1 mod 11, 25 = 1 mod 11 11, 2 2, 1 prime 2, 1 prime
So 11 is prime
2, 1 prime
37
Example: Pratt Tree
1223, 5
11-1=2 x 5,
22 = 1 mod 11, 25 = 1 mod 11 11, 2 2, 1 prime 2, 1 prime
So 11 is prime
2, 1 prime
38
Example: Pratt Tree
1223-1=2 x 13 x 47,
1223, 5 5611 = 1 mod 1223, 594 = 1 mod 1223,
526 = 1 mod 1223, so 1223 is prime
11-1=2 x 5,
22 = 1 mod 11, 25 = 1 mod 11 11, 2 2, 1 prime 2, 1 prime
So 11 is prime
2, 1 prime
39
PRIMES in NP
PRIMES ∈ NP
40
NP-Completeness
Such a problem is a hardest problem in NP (if you can solve it efficiently, then you can solve
any problem in NP efficiently)
41
A Little Logic
A boolean variable x is a variable that can take values in the set {false,true} (or {0,1})
From boolean variables x and y we can obtain new variables via logical operations
x y ¬x x∨y x∧y
0 0 1 0 0
0 1 1 1 0
1 0 0 1 0
1 1 0 1 1
¬x = not x
x ∨ y = x or y
x ∧ y = x and y
42
Literals, Clauses, Formulas
Examples
Variable x1 , x2 , . . . , xn x1 , x3 , . . .
43
Length of a Formula
x10 " 0.1010 Bit representation of 10 - The first “0.” is to say that the literal is not negated.
¬x5 " 1.101 Bit representation of 5 - The first “1.” is to say that the literal is negated.
44
Satisfiability Problem
Formula F is called satisfiable if there is a setting of the variables that makes the formula
evaluate to “true” (or 1).
Such a setting of the variables is called a satisfying assignment.
Checking whether a formula is satisfiable can be hard: there may be only 1 satisfying assignment (out of
the 2n possible).
45
Satisfiability Problem
46
The Cook-Levin Theorem
SAT is NP-complete.
If you can find an polynomial time algorithm for solving SAT, then you can solve other hard problems, like
for example factoring integers, finding large independent sets, cliques, node covers, etc.
47
Are there Other NP-Complete Problems?
48
Example: 3-SAT
Step 2: Show that SAT ≤ 3-SAT For X in NP, we have X ≤ SAT ≤ 3-SAT, so X ≤ 3-SAT.
49
Reduction of SAT to 3-SAT
50
Reduction of SAT to 3-SAT
51
Reduction of SAT to 3-SAT
52
Reduction of SAT to 3-SAT
53
Reduction of SAT to 3-SAT
54
MAX-2-SAT
2-SAT is in P.
But if a 2-formula is not satisfiable, can we efficiently find the maximum number of
satisfiable clauses in the formula?
Theorem:
• If (x ∨ y ∨ z) = 1, then there is assignment for w such that 7 clauses of G are satisfied.
• If (x ∨ y ∨ z) = 0, then no matter how w is chosen, at most 6 clauses of G are satisfied.
55
MAX-2-SAT
If x∨y∨z = 1, then w can be chosen such that exactly seven of the other clauses are 1.
x y z w ¬x ∨ ¬y ¬z ∨ ¬y ¬x ∨ ¬z x ∨ ¬w y ∨ ¬w z ∨ ¬w x∨y∨z
0 0 0 0 1 1 1 1 1 1 0
0 0 0 1 1 1 1 0 0 0 0
0 0 1 0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 0 0 1 1
0 1 0 0 1 1 1 1 1 1 1
0 1 0 1 1 1 1 0 1 0 1
0 1 1 0 1 0 1 1 1 1 1
0 1 1 1 1 0 1 0 1 1 1
1 0 0 0 1 1 1 1 1 1 1
1 0 0 1 1 1 1 1 0 0 1
1 0 1 0 1 1 0 1 1 1 1
1 0 1 1 1 1 0 1 0 1 1
1 1 0 0 0 1 1 1 1 1 1
1 1 0 1 0 1 1 1 1 0 1
1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
56
MAX-2-SAT
If x∨y∨z = 1, then w can be chosen such that exactly seven of the other clauses are 1.
x y z w ¬x ∨ ¬y ¬z ∨ ¬y ¬x ∨ ¬z x ∨ ¬w y ∨ ¬w z ∨ ¬w x∨y∨z
0 0 0 0 1 1 1 1 1 1 0
0 0 0 1 1 1 1 0 0 0 0
0 0 1 0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 0 0 1 1
0 1 0 0 1 1 1 1 1 1 1
0 1 0 1 1 1 1 0 1 0 1
0 1 1 0 1 0 1 1 1 1 1
0 1 1 1 1 0 1 0 1 1 1
1 0 0 0 1 1 1 1 1 1 1
1 0 0 1 1 1 1 1 0 0 1
1 0 1 0 1 1 0 1 1 1 1
1 0 1 1 1 1 0 1 0 1 1
1 1 0 0 0 1 1 1 1 1 1
1 1 0 1 0 1 1 1 1 0 1
1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
57
MAX-2-SAT
If x∨y∨z = 1, then w can be chosen such that exactly seven of the other clauses are 1.
x y z w ¬x ∨ ¬y ¬z ∨ ¬y ¬x ∨ ¬z x ∨ ¬w y ∨ ¬w z ∨ ¬w x∨y∨z
0 0 0 0 1 1 1 1 1 1 0
0 0 0 1 1 1 1 0 0 0 0
0 0 1 0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 0 0 1 1 Choose w = 0
0 1 0 0 1 1 1 1 1 1 1
0 1 0 1 1 1 1 0 1 0 1
0 1 1 0 1 0 1 1 1 1 1
0 1 1 1 1 0 1 0 1 1 1
1 0 0 0 1 1 1 1 1 1 1
1 0 0 1 1 1 1 1 0 0 1
1 0 1 0 1 1 0 1 1 1 1
1 0 1 1 1 1 0 1 0 1 1
1 1 0 0 0 1 1 1 1 1 1
1 1 0 1 0 1 1 1 1 0 1
1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
58
MAX-2-SAT
If x∨y∨z = 1, then w can be chosen such that exactly seven of the other clauses are 1.
x y z w ¬x ∨ ¬y ¬z ∨ ¬y ¬x ∨ ¬z x ∨ ¬w y ∨ ¬w z ∨ ¬w x∨y∨z
0 0 0 0 1 1 1 1 1 1 0
0 0 0 1 1 1 1 0 0 0 0
0 0 1 0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 0 0 1 1
0 1 0 0 1 1 1 1 1 1 1
0 1 0 1 1 1 1 0 1 0 1
0 1 1 0 1 0 1 1 1 1 1
0 1 1 1 1 0 1 0 1 1 1
1 0 0 0 1 1 1 1 1 1 1
1 0 0 1 1 1 1 1 0 0 1
1 0 1 0 1 1 0 1 1 1 1
1 0 1 1 1 1 0 1 0 1 1
1 1 0 0 0 1 1 1 1 1 1
1 1 0 1 0 1 1 1 1 0 1
1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
59
MAX-2-SAT
If x∨y∨z = 0, then no matter how we choose w, at most six of the clauses are 1.
x y z w ¬x ∨ ¬y ¬z ∨ ¬y ¬x ∨ ¬z x ∨ ¬w y ∨ ¬w z ∨ ¬w x∨y∨z
0 0 0 0 1 1 1 1 1 1 0
0 0 0 1 1 1 1 0 0 0 0
0 0 1 0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 0 0 1 1
0 1 0 0 1 1 1 1 1 1 1
0 1 0 1 1 1 1 0 1 0 1
0 1 1 0 1 0 1 1 1 1 1
0 1 1 1 1 0 1 0 1 1 1
1 0 0 0 1 1 1 1 1 1 1
1 0 0 1 1 1 1 1 0 0 1
1 0 1 0 1 1 0 1 1 1 1
1 0 1 1 1 1 0 1 0 1 1
1 1 0 0 0 1 1 1 1 1 1
1 1 0 1 0 1 1 1 1 0 1
1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
60
MAX-2-SAT is NP-Complete
61
MAX-2-SAT is NP-Complete
62
MAX-2-SAT is NP-Complete
63
MAX-2-SAT is NP-Complete
• F satisfiable:
• By theorem, there is setting for the wj such that each Dj has 7 satisfied clauses.
• (H,7m) is in MAX-2-SAT
64
MAX-2-SAT is NP-Complete
• F satisfiable:
• By theorem, there is setting for the wj such that each Dj has 7 satisfied clauses.
• (H,7m) is in MAX-2-SAT
• F not satisfiable:
• By theorem, no matter how wj is chosen, Dj cannot have more than 6 satisfied clauses.
• Hence H cannot have more than 6m satisfied clauses.
• (H,7m) is not in MAX-2-SAT
65
Not-all-equal 3-SAT
66
NAE-3-SAT is NP-Complete
67
NAE-3-SAT is NP-Complete
68
NAE-3-SAT is NP-Complete
69
NAE-3-SAT is NP-Complete
70
NAE-3-SAT is NP-Complete
• G NAE-satisfiable:
• (x1,x2,...,xn,w1,...,wk,z=1) NAE-satisfies G iff (¬x1,...,¬xn,¬w1,...,¬wk,z=0) does
• Can assume z = 0 F is satisfiable.
• Then Dj is NAE-satisfiable iff λ1 ∨ λ2 = 1 or λ3 = 1, so iff Cj is satisfiable.
71
Lots more NP-Complete Problems
This paper took the theory of NP-completeness a leap forward by compiling a list of 21 well-
known computational problems which he showed to be NP-complete as well.
Reminder:
INDEP-SET = { (G,m) | G is a graph with an independent set of size ≥ m}
73
INDEP-SET is NP-Complete
74
INDEP-SET is NP-Complete
Example:
x1 ¬x1 ¬x1
For every clause create a triangle with nodes labeled by the literals.
Connect all the nodes of the triangle.
x3 ¬x2 ¬x3 x2 x3
x2
75
INDEP-SET is NP-Complete
Example:
x1 ¬x1 ¬x1
For every clause create a triangle with nodes labeled by the literals.
Connect all the nodes of the triangle.
x3 ¬x2 ¬x3 x2 x3
Connect literals by an edge who are negations of one another.
x2
76
INDEP-SET is NP-Complete
Example:
x1 ¬x1 ¬x1
Pick satisfying assignment (1,0,1):
Pick literal x1 in the first clause, ¬x2 in the second, and x3 in the third.
Corresponding nodes form an independent set. x3 ¬x2 ¬x3 x3
x2
x2
77
INDEP-SET is NP-Complete
x1 ¬x1 ¬x1
Pick independent set given on the right.
This corresponds to the satisfying assignment: x1=0, x2=1, x3=0
x3 ¬x2 ¬x3 x2 x3
x2
78
INDEP-SET is NP-Complete
79
Cuts
The value of the cut is the sum of the edge values of all edges connecting some
node in A to some node in B.
Graph G
Cut of value 7
all edge weights are 1
80
Min-Cut
This problem is in P:
81
Max-Cut
This problem cannot be solved the same way (simply multiplying edge weights
with -1 and reducing to MIN-CUT doesn’t work since to solve MIN-CUT the edge
values have to be positive).
MAX-CUT is NP-complete!
82
Max-Cut is NP-Complete
83
The Gadget
84
The Gadget
85
The Gadget
x3 ¬x3
x4 ¬x4
86
The Gadget
x4 ¬x4
87
The Gadget
2
x4 ¬x4
88
The Gadget
2
x4 ¬x4
89
The Gadget
2
x4 ¬x4
90
The Gadget
2
x4 ¬x4
91
The Gadget
2
• Connect the nodes xi and ¬xi by an edge of weight ni, where ni is
x4 5 ¬x4
the number of times xi or its negation appears in the formula.
(NOTE: the sum of all the ni is the total number of literals in the
formula, so 3t) n1=2, n2=3, n3=n4=5
92
The Gadget
x3 5 ¬x3
• Connect the nodes xi and ¬xi by an edge of weight ni, where ni is 2
the number of times xi or its negation appears in the formula.
2
(NOTE: the sum of all the ni is the total number of literals in the x4 5 ¬x4
formula, i.e., 3t)
93
The Gadget
x1 2 ¬x1
x2 3 ¬x2
2
x3 5 ¬x3
2
2
x4 5 ¬x4
Cut of value 25 = 5t
94
The Gadget
Cut of value 25 = 5t
95
The Gadget
2
x4 5 ¬x4
Cut of value 25 = 5t
96
The Gadget
2
x4 5 ¬x4
97
The Gadget
x3 5 ¬x3
2
2
x4 5 ¬x4
98
The Gadget
(x1 ∨ x3 ∨ x4) ∧ (x2 ∨ x3 ∨ x4) ∧ (x2 ∨ ¬x3 ∨ x4) ∧ (x1 ∨ x3 ∨ ¬x4) ∧ (¬x2 ∨ ¬x3 ∨ ¬x4) x3 5 ¬x3
2
has NAE-satisfying assignment x1=x2=1, x3=x4=0.
2
This gives cut of value 25 = 5t. x4 ¬x4
5
99