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

Preview:

Citation preview

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

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

Outline

1 2-Satisfiability

2 Independent Sets in Trees

This part

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

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).

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

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

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

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

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

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

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

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.

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.

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

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

z

z

x

x

y

y

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

z

z

x

x

y

y

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

z

z

x

x

y

y

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

z

z

x

x

y

y

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

z

z

x

x

y

y

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

z

z

x

x

y

y

x = 1, y = 1, z = 1

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

z

z

x

x

y

y

x = 0, y = 0, z = 0

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.

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

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

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

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

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

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

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

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!

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!

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!

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 |)

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 |)

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).

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).

Outline

1 2-Satisfiability

2 Independent Sets in Trees

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.

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.

Example

Example

Example

Example

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

It is safe to take all the leaves.

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

It is safe to take all the leaves.

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).

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).

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.

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.

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

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

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

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)

⎫⎪⎪⎬⎪⎪⎭ .

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)

⎫⎪⎪⎬⎪⎪⎭ .

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)

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)

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)

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)

Example

318

55 13 610

22 33 77 22

11 22 11

Recommended