60
Coping with NP-completeness: Special Cases 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: Частные случаи

Coping withNP-completeness:

Special Cases

Alexander S. KulikovSteklov Institute of Mathematics at St. Petersburg

Russian Academy of Sciences

Advanced Algorithms and ComplexityData Structures and Algorithms

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

The fact that a problem is NP-completedoes not exclude an efficient algorithm forspecial cases of the problem.

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

Outline

1 2-Satisfiability

2 Independent Sets in Trees

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

This part

Striking connection between stronglyconnected components of a graph andformulas in 2-CNFA linear time algorithm for 2-SAT

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

2-Satisfiability (2-SAT)

Input: A set of clauses, each containing atmost two literals (that is, a 2-CNFformula).

Output: Find a satisfying assignment(if exists).

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

Example

(x ∨ y)(z)(z ∨ x) is satisfied byx = 0, y = 1, z = 0

(x ∨ y)(z)(z ∨ x)(y) is unsatisfiable(x ∨ y)(x ∨ y)(x ∨ y)(x ∨ y) isunsatisfiable

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

Example

(x ∨ y)(z)(z ∨ x) is satisfied byx = 0, y = 1, z = 0(x ∨ y)(z)(z ∨ x)(y) is unsatisfiable

(x ∨ y)(x ∨ y)(x ∨ y)(x ∨ y) isunsatisfiable

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

Example

(x ∨ y)(z)(z ∨ x) is satisfied byx = 0, y = 1, z = 0(x ∨ y)(z)(z ∨ x)(y) is unsatisfiable(x ∨ y)(x ∨ y)(x ∨ y)(x ∨ y) isunsatisfiable

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

Consider a clause (ℓ1 ∨ ℓ2)

Essentially, it says that ℓ1 and ℓ2 cannotbe both equal to 0In other words, if ℓ1 = 0, then ℓ2 = 1and if ℓ2 = 0, then ℓ1 = 1

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

Consider a clause (ℓ1 ∨ ℓ2)

Essentially, it says that ℓ1 and ℓ2 cannotbe both equal to 0

In other words, if ℓ1 = 0, then ℓ2 = 1and if ℓ2 = 0, then ℓ1 = 1

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

Consider a clause (ℓ1 ∨ ℓ2)

Essentially, it says that ℓ1 and ℓ2 cannotbe both equal to 0In other words, if ℓ1 = 0, then ℓ2 = 1and if ℓ2 = 0, then ℓ1 = 1

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

DefinitionImplication is a binary logical operationdenoted by ⇒ and defined by the followingtruth table:

x y x ⇒ y

0 0 10 1 11 0 01 1 1

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

DefinitionFor a 2-CNF formula, its implication graph isconstructed as follows:

for each variable x , introduce twovertices labeled by x and x ;for each 2-clause (ℓ1 ∨ ℓ2), introducetwo directed edges ℓ1 → ℓ2 and ℓ2 → ℓ1

for each 1-clause (ℓ), introduce an edgeℓ→ ℓ

Encodes all implications imposed by theformula.

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

DefinitionFor a 2-CNF formula, its implication graph isconstructed as follows:

for each variable x , introduce twovertices labeled by x and x ;for each 2-clause (ℓ1 ∨ ℓ2), introducetwo directed edges ℓ1 → ℓ2 and ℓ2 → ℓ1

for each 1-clause (ℓ), introduce an edgeℓ→ ℓ

Encodes all implications imposed by theformula.

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

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

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

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

z

z

x

x

y

y

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

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

z

z

x

x

y

y

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

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

z

z

x

x

y

y

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

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

z

z

x

x

y

y

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

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

z

z

x

x

y

y

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

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

z

z

x

x

y

y

x = 1, y = 1, z = 1

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

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

z

z

x

x

y

y

x = 0, y = 0, z = 0

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

Thus, our goal is to assign truth values tothe variables so that each edge in theimplication graph is “satisfied”, that is, thereis no edge from 1 to 0.

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

Skew-Symmetry

The graph is skew-symmetric: if there isan edge ℓ1 → ℓ2, then there is an edgeℓ2 → ℓ1

This generalizes to paths: if there is apath from ℓ1 to ℓ2, then there is a pathfrom ℓ2 to ℓ1

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

Skew-Symmetry

The graph is skew-symmetric: if there isan edge ℓ1 → ℓ2, then there is an edgeℓ2 → ℓ1

This generalizes to paths: if there is apath from ℓ1 to ℓ2, then there is a pathfrom ℓ2 to ℓ1

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

TransitivityLemmaIf all the edges are satisfied by an assignmentand there is a path from ℓ1 to ℓ2, then itcannot be the case that ℓ1 = 1 and ℓ2 = 0.

Proof

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

TransitivityLemmaIf all the edges are satisfied by an assignmentand there is a path from ℓ1 to ℓ2, then itcannot be the case that ℓ1 = 1 and ℓ2 = 0.

Proof

ℓ1

1ℓ2

0

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

TransitivityLemmaIf all the edges are satisfied by an assignmentand there is a path from ℓ1 to ℓ2, then itcannot be the case that ℓ1 = 1 and ℓ2 = 0.

Proof

ℓ1

1ℓ2

0

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

TransitivityLemmaIf all the edges are satisfied by an assignmentand there is a path from ℓ1 to ℓ2, then itcannot be the case that ℓ1 = 1 and ℓ2 = 0.

Proof

ℓ1

1ℓ2

01 0

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

TransitivityLemmaIf all the edges are satisfied by an assignmentand there is a path from ℓ1 to ℓ2, then itcannot be the case that ℓ1 = 1 and ℓ2 = 0.

Proof

ℓ1

1ℓ2

01 0

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

Strongly Connected Components

All variables lying in the same SCC ofthe implication graph should be assignedthe same value

In particular, if a SCC contains avariable together with its negation, thenthe formula is unsatisfiableIt turns out that otherwise the formulais satisfiable!

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

Strongly Connected Components

All variables lying in the same SCC ofthe implication graph should be assignedthe same valueIn particular, if a SCC contains avariable together with its negation, thenthe formula is unsatisfiable

It turns out that otherwise the formulais satisfiable!

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

Strongly Connected Components

All variables lying in the same SCC ofthe implication graph should be assignedthe same valueIn particular, if a SCC contains avariable together with its negation, thenthe formula is unsatisfiableIt turns out that otherwise the formulais satisfiable!

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

2SAT(2-CNF F)construct the implication graph Gfind SCC’s of Gfor all variables x:

if x and x lie in the same SCC of G:return “unsatisfiable”

find a topological ordering of SCC’sfor all SCC’s C in reverse order:

if literals of C are not assigned yet:set all of them to 1set their negations to 0

return the satisfying assignment

Running time: O(|F |)

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

2SAT(2-CNF F)construct the implication graph Gfind SCC’s of Gfor all variables x:

if x and x lie in the same SCC of G:return “unsatisfiable”

find a topological ordering of SCC’sfor all SCC’s C in reverse order:

if literals of C are not assigned yet:set all of them to 1set their negations to 0

return the satisfying assignment

Running time: O(|F |)

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

LemmaThe algorithm 2SAT is correct.

ProofWhen a literal is set to 1, all the literalsthat are reachable from it have alreadybeen set to 1 (since we process SCC’s inreverse topological order).

When a literal is set to 0, all the literalsit is reachable from have already beenset to 0 (by skew-symmetry).

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

LemmaThe algorithm 2SAT is correct.

ProofWhen a literal is set to 1, all the literalsthat are reachable from it have alreadybeen set to 1 (since we process SCC’s inreverse topological order).When a literal is set to 0, all the literalsit is reachable from have already beenset to 0 (by skew-symmetry).

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

Outline

1 2-Satisfiability

2 Independent Sets in Trees

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

Planning a company party

You are organizing a company party. Youwould like to invite as many people aspossible with a single constraint: no personshould attend a party with his or her directboss.

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

Maximum independent set in a tree

Input: A tree.Output: An independent set (i.e., a subset

of vertices no two of which areadjacent) of maximum size.

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

Example

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

Example

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

Example

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

Example

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

Safe moveFor any leaf, there exists an optimal solutionincluding this leaf.

It is safe to take all the leaves.

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

Safe moveFor any leaf, there exists an optimal solutionincluding this leaf.

It is safe to take all the leaves.

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

PartyGreedy(T )

while T is not empty:take all the leaves to the solutionremove them and their parents from T

return the constructed solution

Running time: O(|T |) (for each vertex, maintainthe number of its children).

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

PartyGreedy(T )

while T is not empty:take all the leaves to the solutionremove them and their parents from T

return the constructed solution

Running time: O(|T |) (for each vertex, maintainthe number of its children).

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

Planning a company party

You are organizing a company party again.However this time, instead of maximizing thenumber of attendees, you would like tomaximize the total fun factor.

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

Maximum weighted independent setin trees

Input: A tree T with weights on vertices.Output: An independent set (i.e., a subset

of vertices no two of which areadjacent) of maximum total weight.

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

Example

3

5 1 6

2 3 7 2

1 2 1

3

5 1 6

2 3 7 2

1 2 1total weight: 17

3

5 1 6

2 3 7 2

1 2 1total weight: 18

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

Example

3

5 1 6

2 3 7 2

1 2 1

3

5 1 6

2 3 7 2

1 2 1total weight: 17

3

5 1 6

2 3 7 2

1 2 1total weight: 18

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

Example

3

5 1 6

2 3 7 2

1 2 1

3

5 1 6

2 3 7 2

1 2 1total weight: 17

3

5 1 6

2 3 7 2

1 2 1total weight: 18

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

Subproblems

D(v) is the maximum weight of anindependent set in a subtree rooted at v

Recurrence relation: D(v) is

max

⎧⎪⎪⎨⎪⎪⎩w(v) +∑︁

grandchildrenw of v

D(w),∑︁

childrenw of v

D(w)

⎫⎪⎪⎬⎪⎪⎭ .

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

Subproblems

D(v) is the maximum weight of anindependent set in a subtree rooted at vRecurrence relation: D(v) is

max

⎧⎪⎪⎨⎪⎪⎩w(v) +∑︁

grandchildrenw of v

D(w),∑︁

childrenw of v

D(w)

⎫⎪⎪⎬⎪⎪⎭ .

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

Function FunParty(v)if D(v) =∞:

if v has no children:D(v)← w(v)

else:m1 ← w(v)for all children u of v:

for all children w of u:m1 ← m1 + FunParty(w)

m0 ← 0for all children u of v:

m0 ← m0 + FunParty(u)D(v)← max(m1,m0)

return D(v)

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

Function FunParty(v)if D(v) =∞:

if v has no children:D(v)← w(v)

else:m1 ← w(v)for all children u of v:

for all children w of u:m1 ← m1 + FunParty(w)

m0 ← 0for all children u of v:

m0 ← m0 + FunParty(u)D(v)← max(m1,m0)

return D(v)

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

Function FunParty(v)if D(v) =∞:

if v has no children:D(v)← w(v)

else:m1 ← w(v)for all children u of v:

for all children w of u:m1 ← m1 + FunParty(w)

m0 ← 0for all children u of v:

m0 ← m0 + FunParty(u)

D(v)← max(m1,m0)return D(v)

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

Function FunParty(v)if D(v) =∞:

if v has no children:D(v)← w(v)

else:m1 ← w(v)for all children u of v:

for all children w of u:m1 ← m1 + FunParty(w)

m0 ← 0for all children u of v:

m0 ← m0 + FunParty(u)D(v)← max(m1,m0)

return D(v)

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

Example

318

55 13 610

22 33 77 22

11 22 11