122
NP-complete Problems: Reductions Alexander S. Kulikov Steklov Institute of Mathematics at St. Petersburg Russian Academy of Sciences Advanced Algorithms and Complexity Data Structures and Algorithms

Алгоритмы для NP-трудных задач, осень 2016: Сведения

Embed Size (px)

Citation preview

Page 1: Алгоритмы для NP-трудных задач, осень 2016: Сведения

NP-complete Problems:Reductions

Alexander S. KulikovSteklov Institute of Mathematics at St. Petersburg

Russian Academy of Sciences

Advanced Algorithms and ComplexityData Structures and Algorithms

Page 2: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Outline1 Reductions

2 Showing NP-completeness

3 Independent Set → Vertex Cover

4 3-SAT → Independent Set

5 SAT → 3-SAT

6 All of NP → SAT

7 Using SAT-solvers

Page 3: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Informally

We say that a search problem A is reducedto a search problem B and write A → B , if apolynomial time algorithm for B can be used(as a black box) to solve A in polynomialtime.

Page 4: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Page 5: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Algorithm for B

Algorithm for A

Page 6: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Algorithm for B

Algorithm for A f

Page 7: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Algorithm for B

Algorithm for A f

instance f (I ) of B

Page 8: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Algorithm for B

Algorithm for A f

instance f (I ) of B

no solution to f (I )

Page 9: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Algorithm for B

Algorithm for A f

instance f (I ) of B

no solution to f (I )

no solution to I

Page 10: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Algorithm for B

Algorithm for A f

instance f (I ) of B

no solution to f (I )

no solution to I

solution S to f (I )

Page 11: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Algorithm for B

Algorithm for A f

instance f (I ) of B

no solution to f (I )

no solution to I

solution S to f (I )

h

Page 12: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction: A → B

instance I of A

Algorithm for B

Algorithm for A f

instance f (I ) of B

no solution to f (I )

no solution to I

solution S to f (I )

h

solution h(S) to I

Page 13: Алгоритмы для NP-трудных задач, осень 2016: Сведения

FormallyDefinitionWe say that a search problem A is reducedto a search problem B and write A → B , ifthere exists a polynomial time algorithm f

that converts any instance I of A into aninstance f (I ) of B , together with apolynomial time algorithm h that convertsany solution S to f (I ) back to a solutionh(S) to A. If there is no solution to f (I ),then there is no solution to I .

Page 14: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Graph of Search Problems

NP

Page 15: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Graph of Search Problems

NP

Page 16: Алгоритмы для NP-трудных задач, осень 2016: Сведения

NP-complete ProblemsDefinitionA search problem is called NP-complete ifall other search problems reduce to it.

NP

Page 17: Алгоритмы для NP-трудных задач, осень 2016: Сведения

NP-complete ProblemsDefinitionA search problem is called NP-complete ifall other search problems reduce to it.

NP

Page 18: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Do they exist?

It’s not at all immediate that NP-completeproblems even exist. We’ll see later that allhard problems that we’ve seen in theprevious part are in fact NP-complete!

Page 19: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Outline1 Reductions

2 Showing NP-completeness

3 Independent Set → Vertex Cover

4 3-SAT → Independent Set

5 SAT → 3-SAT

6 All of NP → SAT

7 Using SAT-solvers

Page 20: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Two ways of using A → B :

1 if B is easy (can be solved in polynomialtime), then so is A

2 if A is hard (cannot be solved inpolynomial time), then so is B

Page 21: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reductions Compose

LemmaIf A → B and B → C , then A → C .

Page 22: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Proof

The reductions A → B and B → C aregiven by pairs of polytime algorithms(fAB , hAB) and (fBC , hBC ).

To transform an instance IA of A to aninstance IC of C we apply a polytimealgorithm fBC ∘ fAB : IC = fBC (fAB(IA)).To transform a solution SC to IC to asolution SA to IA we apply a polytimealgorithm hAB ∘ hBC :SA = hAB(hBC (SC )).

Page 23: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Proof

The reductions A → B and B → C aregiven by pairs of polytime algorithms(fAB , hAB) and (fBC , hBC ).To transform an instance IA of A to aninstance IC of C we apply a polytimealgorithm fBC ∘ fAB : IC = fBC (fAB(IA)).

To transform a solution SC to IC to asolution SA to IA we apply a polytimealgorithm hAB ∘ hBC :SA = hAB(hBC (SC )).

Page 24: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Proof

The reductions A → B and B → C aregiven by pairs of polytime algorithms(fAB , hAB) and (fBC , hBC ).To transform an instance IA of A to aninstance IC of C we apply a polytimealgorithm fBC ∘ fAB : IC = fBC (fAB(IA)).To transform a solution SC to IC to asolution SA to IA we apply a polytimealgorithm hAB ∘ hBC :SA = hAB(hBC (SC )).

Page 25: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Pictorially

NP

Page 26: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Pictorially

NP

Page 27: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Pictorially

NP

Page 28: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Showing NP-completenessCorollary

If A → B and A is NP-complete, then sois B .

Page 29: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Showing NP-completenessCorollary

If A → B and A is NP-complete, then sois B .

NP

A B

Page 30: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Showing NP-completenessCorollary

If A → B and A is NP-complete, then sois B .

NP

A B

Page 31: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Showing NP-completenessCorollary

If A → B and A is NP-complete, then sois B .

NP

A B

Page 32: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Showing NP-completenessCorollary

If A → B and A is NP-complete, then sois B .

NP

A B

Page 33: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Showing NP-completenessCorollary

If A → B and A is NP-complete, then sois B .

NP

A B

Page 34: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Plan

vertex cover

independent set

Page 35: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Plan

vertex cover

independent set

3-SAT

Page 36: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Plan

vertex cover

independent set

3-SAT

SAT

Page 37: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Plan

vertex cover

independent set

3-SAT

SAT

Page 38: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Outline1 Reductions

2 Showing NP-completeness

3 Independent Set → Vertex Cover

4 3-SAT → Independent Set

5 SAT → 3-SAT

6 All of NP → SAT

7 Using SAT-solvers

Page 39: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Independent set

Input: A graph and a budget b.Output: A subset of at least b vertices such

that no two of them are adjacent.

Vertex cover

Input: A graph and a budget b.Output: A subset of at most b vertices that

touches every edge.

Page 40: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Independent set

Input: A graph and a budget b.Output: A subset of at least b vertices such

that no two of them are adjacent.

Vertex cover

Input: A graph and a budget b.Output: A subset of at most b vertices that

touches every edge.

Page 41: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Example

A

B C

D

E

F G

H

Page 42: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Example

A

B C

D

E

F G

H

Independent sets:

Page 43: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Example

Independent sets:

{E ,C}

A

B C

D

E

F G

H

Page 44: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Example

Independent sets:

{E ,C} {A,C , F ,H}

A

B C

D

E

F G

H

Page 45: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Example

Independent sets:

{E ,C} {A,C , F ,H}

Vertex covers:

{A,B ,D, F ,G ,H}

A

B C

D

E

F G

H

Page 46: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Example

Independent sets:

{E ,C} {A,C , F ,H}

Vertex covers:

{A,B ,D, F ,G ,H}{B ,D,E ,G}A

B C

D

E

F G

H

Page 47: Алгоритмы для NP-трудных задач, осень 2016: Сведения

LemmaI is an independent set of G (V ,E ), if andonly if V − I is a vertex cover of G .

Proof⇒ If I is an independent set, then there is

no edge with both endpoints in I .Hence V − I touches every edge.

⇐ If V − I touches every edge, then eachedge has at least one endpoint in V − I .Hence I is an independent set.

Page 48: Алгоритмы для NP-трудных задач, осень 2016: Сведения

ReductionIndependent set → vertex cover: to checkwhether G (V ,E ) has an independent set ofsize at least b, check whether G has a vertexcover of size at most |V | − b:

f (G (V ,E ), b) = (G (V ,E ), |V | − b)

h(S) = V − S

Page 49: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Outline1 Reductions

2 Showing NP-completeness

3 Independent Set → Vertex Cover

4 3-SAT → Independent Set

5 SAT → 3-SAT

6 All of NP → SAT

7 Using SAT-solvers

Page 50: Алгоритмы для NP-трудных задач, осень 2016: Сведения

3-SAT

Input: Formula F in 3-CNF (a collectionof clauses each having at mostthree literals).

Output: An assignment of Boolean values tothe variables of F satisfying allclauses, if exists.

Page 51: Алгоритмы для NP-трудных задач, осень 2016: Сведения

GoalDesign a polynomial time algorithm that,given a 3-CNF formula F , outputs a graph G

and an integer b, such that:F is satisfiable, if and only if G hasan independent set of size atleast b.

Page 52: Алгоритмы для NP-трудных задач, осень 2016: Сведения

We need to find an assignment of Booleanvalues to variables, such that each clausecontains at least one satisfied literal.

Example

Setting x = 1, y = 1, z = 1 satisfies aformula (x ∨ y ∨ z)(x ∨ y)(y ∨ z).Setting x = 1, y = 0, z = 0 also satisfiesit: (x ∨ y ∨ z)(x ∨ y)(y ∨ z).

Page 53: Алгоритмы для NP-трудных задач, осень 2016: Сведения

We need to find an assignment of Booleanvalues to variables, such that each clausecontains at least one satisfied literal.

Example

Setting x = 1, y = 1, z = 1 satisfies aformula (x ∨ y ∨ z)(x ∨ y)(y ∨ z).

Setting x = 1, y = 0, z = 0 also satisfiesit: (x ∨ y ∨ z)(x ∨ y)(y ∨ z).

Page 54: Алгоритмы для NP-трудных задач, осень 2016: Сведения

We need to find an assignment of Booleanvalues to variables, such that each clausecontains at least one satisfied literal.

Example

Setting x = 1, y = 1, z = 1 satisfies aformula (x ∨ y ∨ z)(x ∨ y)(y ∨ z).Setting x = 1, y = 0, z = 0 also satisfiesit: (x ∨ y ∨ z)(x ∨ y)(y ∨ z).

Page 55: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Alternatively, we need to select at least oneliteral from each clause, such that the set ofselected literals is consistent: it does notcontain a literal ℓ together with itsnegation ℓ.

Example: (x ∨ y ∨ z)(x ∨ y)(y ∨ z)

Consistent: {x , x , z}, {x , x , y},{x , y , z}.Inconsistent: {y , y , z}, {z , x , z}.

Page 56: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Alternatively, we need to select at least oneliteral from each clause, such that the set ofselected literals is consistent: it does notcontain a literal ℓ together with itsnegation ℓ.

Example: (x ∨ y ∨ z)(x ∨ y)(y ∨ z)

Consistent: {x , x , z}, {x , x , y},{x , y , z}.

Inconsistent: {y , y , z}, {z , x , z}.

Page 57: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Alternatively, we need to select at least oneliteral from each clause, such that the set ofselected literals is consistent: it does notcontain a literal ℓ together with itsnegation ℓ.

Example: (x ∨ y ∨ z)(x ∨ y)(y ∨ z)

Consistent: {x , x , z}, {x , x , y},{x , y , z}.Inconsistent: {y , y , z}, {z , x , z}.

Page 58: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Using Alternative Statement

(x ∨ y ∨ z)(x ∨ y)(y ∨ z)(z ∨ x)(x ∨ y ∨ z)

Page 59: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Using Alternative Statement

(x ∨ y ∨ z)(x ∨ y)(y ∨ z)(z ∨ x)(x ∨ y ∨ z)

x

y

z x

y

y

z

z

x

x

y

z

Page 60: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Using Alternative Statement

(x ∨ y ∨ z)(x ∨ y)(y ∨ z)(z ∨ x)(x ∨ y ∨ z)

x

y

z x

y

y

z

z

x

x

y

z

Page 61: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Using Alternative Statement

(x ∨ y ∨ z)(x ∨ y)(y ∨ z)(z ∨ x)(x ∨ y ∨ z)

x

y

z x

y

y

z

z

x

x

y

z

Page 62: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Using Alternative Statement

(x ∨ y ∨ z)(x ∨ y)(y ∨ z)(z ∨ x)(x ∨ y ∨ z)

x

y

z x

y

y

z

z

x

x

y

z

the formula is satisfiable iff the resultinggraph has independent set of size 5

Page 63: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceFor each clause of the input formula F ,introduce three (or two, or one) verticesin G labeled with the literals of thisclause. Join every two of them.

Join every pair of vertices labeled withcomplementary literals.F is satisfiable if and only if G hasindependent set of size equal to thenumber of clauses in F .Transformation takes polynomial time.

Page 64: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceFor each clause of the input formula F ,introduce three (or two, or one) verticesin G labeled with the literals of thisclause. Join every two of them.Join every pair of vertices labeled withcomplementary literals.

F is satisfiable if and only if G hasindependent set of size equal to thenumber of clauses in F .Transformation takes polynomial time.

Page 65: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceFor each clause of the input formula F ,introduce three (or two, or one) verticesin G labeled with the literals of thisclause. Join every two of them.Join every pair of vertices labeled withcomplementary literals.F is satisfiable if and only if G hasindependent set of size equal to thenumber of clauses in F .

Transformation takes polynomial time.

Page 66: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceFor each clause of the input formula F ,introduce three (or two, or one) verticesin G labeled with the literals of thisclause. Join every two of them.Join every pair of vertices labeled withcomplementary literals.F is satisfiable if and only if G hasindependent set of size equal to thenumber of clauses in F .Transformation takes polynomial time.

Page 67: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming a Solution

Given a solution S for G , just read thelabels of the vertices from S to get asatisfying assignment of F (takespolynomial time).

If there is no solution for G , then F isunsatisfiable: indeed, a satisfyingassignment for F would give a requiredindependent set in G .

Page 68: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming a Solution

Given a solution S for G , just read thelabels of the vertices from S to get asatisfying assignment of F (takespolynomial time).If there is no solution for G , then F isunsatisfiable: indeed, a satisfyingassignment for F would give a requiredindependent set in G .

Page 69: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Outline1 Reductions

2 Showing NP-completeness

3 Independent Set → Vertex Cover

4 3-SAT → Independent Set

5 SAT → 3-SAT

6 All of NP → SAT

7 Using SAT-solvers

Page 70: Алгоритмы для NP-трудных задач, осень 2016: Сведения

GoalTransform a CNF formula into anequisatisfiable 3-CNF formula. That is,reduce a problem to its special case.

Page 71: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceWe need to get rid of clauses of lengthmore than 3 in an input formula

Consider such a clause:C = (ℓ1 ∨ ℓ2 ∨ A), where A is an OR ofat least two literals.Introduce a fresh variable y and replaceC with the following two clauses:(ℓ1 ∨ ℓ2 ∨ y), (y ∨ A)

The second clause is shorter than C

Repeat while there is a long clause

Page 72: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceWe need to get rid of clauses of lengthmore than 3 in an input formulaConsider such a clause:C = (ℓ1 ∨ ℓ2 ∨ A), where A is an OR ofat least two literals.

Introduce a fresh variable y and replaceC with the following two clauses:(ℓ1 ∨ ℓ2 ∨ y), (y ∨ A)

The second clause is shorter than C

Repeat while there is a long clause

Page 73: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceWe need to get rid of clauses of lengthmore than 3 in an input formulaConsider such a clause:C = (ℓ1 ∨ ℓ2 ∨ A), where A is an OR ofat least two literals.Introduce a fresh variable y and replaceC with the following two clauses:(ℓ1 ∨ ℓ2 ∨ y), (y ∨ A)

The second clause is shorter than C

Repeat while there is a long clause

Page 74: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceWe need to get rid of clauses of lengthmore than 3 in an input formulaConsider such a clause:C = (ℓ1 ∨ ℓ2 ∨ A), where A is an OR ofat least two literals.Introduce a fresh variable y and replaceC with the following two clauses:(ℓ1 ∨ ℓ2 ∨ y), (y ∨ A)

The second clause is shorter than C

Repeat while there is a long clause

Page 75: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming an InstanceWe need to get rid of clauses of lengthmore than 3 in an input formulaConsider such a clause:C = (ℓ1 ∨ ℓ2 ∨ A), where A is an OR ofat least two literals.Introduce a fresh variable y and replaceC with the following two clauses:(ℓ1 ∨ ℓ2 ∨ y), (y ∨ A)

The second clause is shorter than C

Repeat while there is a long clause

Page 76: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Running time

The running time of the transformation ispolynomial: at each iteration we replace aclause with a shorter clause and a 3-clause.Hence the total number of iterations is atmost the total number of literals of the initialformula.

Page 77: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Correctness

LemmaThe formulas F = (ℓ1 ∨ ℓ2 ∨ A) . . . andF ′ = (ℓ1 ∨ ℓ2 ∨ y)(y ∨ A) . . . areequisatisfiable.

Page 78: Алгоритмы для NP-трудных задач, осень 2016: Сведения

ProofF = (ℓ1 ∨ ℓ2 ∨ A) . . .

F ′ = (ℓ1 ∨ ℓ2 ∨ y)(y ∨ A) . . .

⇒ If either ℓ1 or ℓ2 is satisfied, set y = 0.Otherwise A must be satisfied. Then sety = 1.

⇐ If (ℓ1 ∨ ℓ2 ∨ y)(y ∨ A) are satisfied, thenso is (ℓ1 ∨ ℓ2 ∨ A).

Page 79: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Transforming a Solution

Given a satisfying assignment for F ′, justthrow away the values of all new variables(y ’s) to get a satisfying assignment of theinitial formula.

Page 80: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Outline1 Reductions

2 Showing NP-completeness

3 Independent Set → Vertex Cover

4 3-SAT → Independent Set

5 SAT → 3-SAT

6 All of NP → SAT

7 Using SAT-solvers

Page 81: Алгоритмы для NP-трудных задач, осень 2016: Сведения

GoalShow that every search problem reduces toSAT.

Instead, we show that any problem reducesto Circuit SAT problem, which, in turn,reduces to SAT.

Page 82: Алгоритмы для NP-трудных задач, осень 2016: Сведения

GoalShow that every search problem reduces toSAT.

Instead, we show that any problem reducesto Circuit SAT problem, which, in turn,reduces to SAT.

Page 83: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Circuit

x y z 1

∧ ∨

¬

∨ output

Page 84: Алгоритмы для NP-трудных задач, осень 2016: Сведения

DefinitionA circuit is a directed acyclic graph ofin-degree at most 2. Nodes of in-degree 0are called inputs and are marked by Booleanvariables and constants. Nodes of in-degree1 and 2 are called gates: gates of in-degree 1are labeled with NOT, gates of in-degree 2are labeled with AND or OR. One of thesinks is marked as output.

Page 85: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Circuit-SAT

Input: A circuit.Output: An assignment of Boolean values to

the input variables of the circuitthat makes the output true.

Page 86: Алгоритмы для NP-трудных задач, осень 2016: Сведения

SAT is a special case of Circuit-SAT as aCNF formula can be represented as a circuit:

Example: (x ∨ y ∨ z)(y ∨ x)

x y z

∨¬

∨∨∧ output

Page 87: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Circuit-SAT → SAT

To reduce Circuit-SAT to SAT, we need todesign a polynomial time algorithm that for agiven circuit outputs a CNF formula which issatisfiable, if and only if the circuit issatisfiable

Page 88: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Idea

Introduce a Boolean variable for eachgateFor each gate, write down a few clausesthat describe the relationship betweenthis gate and its direct predecessors

Page 89: Алгоритмы для NP-трудных задач, осень 2016: Сведения

NOT Gates

¬g

h

(h ∨ g)(h ∨ g)

Page 90: Алгоритмы для NP-трудных задач, осень 2016: Сведения

AND Gates

∧g

h1 h2

(h1 ∨ g)(h2 ∨ g)(h1 ∨ h2 ∨ g)

Page 91: Алгоритмы для NP-трудных задач, осень 2016: Сведения

OR Gates

∨g

h1 h2

(h1 ∨ g)(h2 ∨ g)(h1 ∨ h2 ∨ g)

Page 92: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Output Gate

g output (g)

Page 93: Алгоритмы для NP-трудных задач, осень 2016: Сведения

The resulting CNF formula is consistentwith the initial circuit: in any satisfyingassignment of the formula, the value ofg is equal to the value of the gatelabeled with g in the circuit

Therefore, the CNF formula isequisatisfiable to the circuitThe reduction takes polynomial time

Page 94: Алгоритмы для NP-трудных задач, осень 2016: Сведения

The resulting CNF formula is consistentwith the initial circuit: in any satisfyingassignment of the formula, the value ofg is equal to the value of the gatelabeled with g in the circuitTherefore, the CNF formula isequisatisfiable to the circuit

The reduction takes polynomial time

Page 95: Алгоритмы для NP-трудных задач, осень 2016: Сведения

The resulting CNF formula is consistentwith the initial circuit: in any satisfyingassignment of the formula, the value ofg is equal to the value of the gatelabeled with g in the circuitTherefore, the CNF formula isequisatisfiable to the circuitThe reduction takes polynomial time

Page 96: Алгоритмы для NP-трудных задач, осень 2016: Сведения

GoalReduce every search problem to Circuit-SAT.

Let A be a search problemWe know that there exists analgorithm 𝒞 that takes an instance Iof A and a candidate solution S andchecks whether S is a solution for I intime polynomial in |I |In particular, |S | is polynomial in |I |

Page 97: Алгоритмы для NP-трудных задач, осень 2016: Сведения

GoalReduce every search problem to Circuit-SAT.

Let A be a search problem

We know that there exists analgorithm 𝒞 that takes an instance Iof A and a candidate solution S andchecks whether S is a solution for I intime polynomial in |I |In particular, |S | is polynomial in |I |

Page 98: Алгоритмы для NP-трудных задач, осень 2016: Сведения

GoalReduce every search problem to Circuit-SAT.

Let A be a search problemWe know that there exists analgorithm 𝒞 that takes an instance Iof A and a candidate solution S andchecks whether S is a solution for I intime polynomial in |I |

In particular, |S | is polynomial in |I |

Page 99: Алгоритмы для NP-трудных задач, осень 2016: Сведения

GoalReduce every search problem to Circuit-SAT.

Let A be a search problemWe know that there exists analgorithm 𝒞 that takes an instance Iof A and a candidate solution S andchecks whether S is a solution for I intime polynomial in |I |In particular, |S | is polynomial in |I |

Page 100: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Turn an Algorithm into a CircuitNote that a computer is in fact a circuit(of constant size!) implemented on achip

Each step of the algorithm 𝒞(I , S) isperformed by this computer’s circuitThis gives a circuit of size polynomialin |I | that has input bits for I and S andoutputs whether S is a solution for I(a separate circuit for each input length)

Page 101: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Turn an Algorithm into a CircuitNote that a computer is in fact a circuit(of constant size!) implemented on achipEach step of the algorithm 𝒞(I , S) isperformed by this computer’s circuit

This gives a circuit of size polynomialin |I | that has input bits for I and S andoutputs whether S is a solution for I(a separate circuit for each input length)

Page 102: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Turn an Algorithm into a CircuitNote that a computer is in fact a circuit(of constant size!) implemented on achipEach step of the algorithm 𝒞(I , S) isperformed by this computer’s circuitThis gives a circuit of size polynomialin |I | that has input bits for I and S andoutputs whether S is a solution for I(a separate circuit for each input length)

Page 103: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction

To solve an instance I of the problem A:take a circuit corresponding to 𝒞(I , ·)

the inputs to this circuit encodecandidate solutionsuse a Circuit-SAT algorithm for thiscircuit to find a solution (if exists)

Page 104: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction

To solve an instance I of the problem A:take a circuit corresponding to 𝒞(I , ·)the inputs to this circuit encodecandidate solutions

use a Circuit-SAT algorithm for thiscircuit to find a solution (if exists)

Page 105: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Reduction

To solve an instance I of the problem A:take a circuit corresponding to 𝒞(I , ·)the inputs to this circuit encodecandidate solutionsuse a Circuit-SAT algorithm for thiscircuit to find a solution (if exists)

Page 106: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Summary

vertex cover

independent set

3-SAT

SAT

Page 107: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Outline1 Reductions

2 Showing NP-completeness

3 Independent Set → Vertex Cover

4 3-SAT → Independent Set

5 SAT → 3-SAT

6 All of NP → SAT

7 Using SAT-solvers

Page 108: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Sudoku Puzzle

This part

A simple and efficient Sudoku solver

Page 109: Алгоритмы для NP-трудных задач, осень 2016: Сведения

SAT: Theory and Practice

Theory: we have no algorithm checking thesatisfiability of a CNF formula F

with n variables in timepoly(|F |) · 1.99n

Practice: SAT-solvers routinely solveinstances with thousands ofvariables

Page 110: Алгоритмы для NP-трудных задач, осень 2016: Сведения

SAT: Theory and Practice

Theory: we have no algorithm checking thesatisfiability of a CNF formula F

with n variables in timepoly(|F |) · 1.99n

Practice: SAT-solvers routinely solveinstances with thousands ofvariables

Page 111: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Solving Hard Problems in Practice

An easy way to solve a hard combinatorialproblem in practice:

Reduce the problem to SAT (manyproblems are reduced to SAT in anatural way)

Use a SAT solver

Page 112: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Solving Hard Problems in Practice

An easy way to solve a hard combinatorialproblem in practice:

Reduce the problem to SAT (manyproblems are reduced to SAT in anatural way)Use a SAT solver

Page 113: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Sudoku Puzzle

Goal: fill in with digits the partiallycompleted 9× 9 grid so that each row, eachcolumn, and each of the nine 3× 3 subgridscontains all the digits from 1 to 9.

Page 114: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Example

Page 115: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Variables

There will be 9× 9× 9 = 729 Booleanvariables: for 1 ≤ i , j , k ≤ 9, xijk = 1, if andonly if the cell [i , j ] contains the digit k

Page 116: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Exactly One Is True

Clauses expressing the fact that exactly oneof the literals ℓ1, ℓ2, ℓ3 is equal to 1:

(ℓ1 ∨ ℓ2 ∨ ℓ3)(ℓ1 ∨ ℓ2)(ℓ1 ∨ ℓ3)(ℓ2 ∨ ℓ3)

Page 117: Алгоритмы для NP-трудных задач, осень 2016: Сведения

ConstraintsCell [i , j ] contains exactly one digit:ExactlyOneOf(xij1, xij2, . . . , xij9)

k appears exactly once in row i :ExactlyOneOf(xi1k , xi2k , . . . , xi9k)k appears exactly once in column j :ExactlyOneOf(x1jk , x2jk , . . . , x9jk)

k appears exactly once in a 3× 3 block:ExactlyOneOf(x11k , x12k , . . . , x33k)

[i , j ] already contains k : (xijk)

Page 118: Алгоритмы для NP-трудных задач, осень 2016: Сведения

ConstraintsCell [i , j ] contains exactly one digit:ExactlyOneOf(xij1, xij2, . . . , xij9)k appears exactly once in row i :ExactlyOneOf(xi1k , xi2k , . . . , xi9k)

k appears exactly once in column j :ExactlyOneOf(x1jk , x2jk , . . . , x9jk)

k appears exactly once in a 3× 3 block:ExactlyOneOf(x11k , x12k , . . . , x33k)

[i , j ] already contains k : (xijk)

Page 119: Алгоритмы для NP-трудных задач, осень 2016: Сведения

ConstraintsCell [i , j ] contains exactly one digit:ExactlyOneOf(xij1, xij2, . . . , xij9)k appears exactly once in row i :ExactlyOneOf(xi1k , xi2k , . . . , xi9k)k appears exactly once in column j :ExactlyOneOf(x1jk , x2jk , . . . , x9jk)

k appears exactly once in a 3× 3 block:ExactlyOneOf(x11k , x12k , . . . , x33k)

[i , j ] already contains k : (xijk)

Page 120: Алгоритмы для NP-трудных задач, осень 2016: Сведения

ConstraintsCell [i , j ] contains exactly one digit:ExactlyOneOf(xij1, xij2, . . . , xij9)k appears exactly once in row i :ExactlyOneOf(xi1k , xi2k , . . . , xi9k)k appears exactly once in column j :ExactlyOneOf(x1jk , x2jk , . . . , x9jk)

k appears exactly once in a 3× 3 block:ExactlyOneOf(x11k , x12k , . . . , x33k)

[i , j ] already contains k : (xijk)

Page 121: Алгоритмы для NP-трудных задач, осень 2016: Сведения

ConstraintsCell [i , j ] contains exactly one digit:ExactlyOneOf(xij1, xij2, . . . , xij9)k appears exactly once in row i :ExactlyOneOf(xi1k , xi2k , . . . , xi9k)k appears exactly once in column j :ExactlyOneOf(x1jk , x2jk , . . . , x9jk)

k appears exactly once in a 3× 3 block:ExactlyOneOf(x11k , x12k , . . . , x33k)

[i , j ] already contains k : (xijk)

Page 122: Алгоритмы для NP-трудных задач, осень 2016: Сведения

Resulting Formula

State-of-the-art SAT-solvers find a satisfyingassignment for the resulting formula in blinkof an eye, though the corresponding searchspace has size about 2729 ≈ 10220