What is the problem?

advertisement
Honors Track:
Competitive Programming
& Problem Solving
2-Satisfiability
José Kuiper
2-Satisfiability
Contents
 What is the problem?
 How to solve it?
 Implementation
 Another solution: Limited backtracking
 Example problems: (line) labeling
 Similar problems to 2SAT
What is the problem?
2-Satisfiability
 Mostly expressed in Conjunctive Normal Form (CNF)
 Conjunction of disjunctions:
 Terms:
variables, possibly negated.
 Clauses: disjunction of terms, for example:
 CNF-expression: clause and clause and clause and…
The problem
 Does assignment to values exist such that the expression holds?
Other representations
Implicative normal form
 Rewriting the clauses of a 2-CNF formula to implications:
Implication graph
 A directed graph
 A vertex for each term
 An arrow for each
implication of the implicative
normal form
Example
From 2-CNF to Implication graph
 2-CNF: (a ∨ b) ∧ (¬b ∨ c)
 INF: (¬a => b) ∧ (¬b => a) ∧ (b => c) ∧ (¬c => ¬b)
c
¬a
b
¬b
a
¬c
How to solve this problem?
Solution
 Linear time
Procedure
 Construct implication graph
 Find strongly connected components
 Check for contradictions within those
 Construct condensation of the graph
 Topologically order it
 Set the variables!
How to solve this problem?
Solution
 Linear time
Procedure
 Construct implication graph
 Find strongly connected components
 Check for contradictions within those
 Construct condensation of the graph
 Topologically order it
 Set the variables!
How to solve this problem?
Construct implication graph
 Convert to implicative normal form
 Create vertex for each term
 Edges are logical implications
How to solve this problem?
Solution
 Linear time
Procedure
 Construct implication graph
 Find strongly connected components
 Check for contradictions within those
 Construct condensation of the graph
 Topologically order it
 Set the variables!
How to solve this problem?
Find strongly connected components
 Strongly connected component (SCC):
Part of the graph in which there is a directed path from
every vertex, to all other vertices of that part of the
graph.
 See Fun with Graphs I
Tarjan’s algorithm
Tarjan’s algorithm
 Perform a DFS
 Assign number to each node based on DFS order
 For each node v keep track of lowest index node reachable from v
 Keep stack of vertices not in SCC yet
 If v.index = v.low then all nodes on stack until v are SCC
 Running time O(V + E)
E
B
A
B
E
A
F
C
D
C
Source: Fun with Graphs I, Kevin Verbeek
D
F
How to solve this problem?
Solution
 Linear time
Procedure
 Construct implication graph
 Find strongly connected components
 Check for contradictions within those
 Construct condensation of the graph
 Topologically order it
 Set the variables!
How to solve this problem?
Checking for contradictions
 Variables and their negations can’t be in one SCC
 Because transitivity would cause: x0 => ¬x0
 Meaning the expression is not satisfiable
Works both ways
 A 2-CNF formula is satisfiable if and only if there is no variable that
belongs to the same strongly connected component as its negation.
How to solve this problem?
Solution
 Linear time
Procedure
 Construct implication graph
 Find strongly connected components
 Check for contradictions within those
 Construct condensation of the graph
 Topologically order it
 Set the variables!
How to solve this problem?
Construct condensation
 One vertex for each strongly connected component
 Edges between components as they are in implicative graph
 Results in directed acyclic graph
E
B
A
B
E
A
F
C
D
C
D
F
How to solve this problem?
Solution
 Linear time
Procedure
 Construct implication graph
 Find strongly connected components
 Check for contradictions within those
 Construct condensation of the graph
 Topologically order it
 Set the variables!
How to solve this problem?
Topological ordering
 A linear ordering of vertices such that every edge moves forwards
 Example: If there’s an edge going from u to v, then u appears before
v in the topological ordering.
Already done
 Kosaraju’s already sorts components topologically
 Tarjan’s algorithm in a reverse topological order
How to solve this problem?
Solution
 Linear time
Procedure
 Construct implication graph
 Find strongly connected components
 Check for contradictions within those
 Construct condensation of the graph
 Topologically order it
 Set the variables!
How to solve this problem?
Setting the variables
 All terms in a SCC have the same value
 Implication only false if: True  False
 Start at the back of topological order
 Make last component true
 Make complementary component false
 Repeat until done
Complementary components
 Skew-symmetry
 If edge from a to b ➨ edge from ¬b to ¬a
Implementation!
Node-class implication graph
class Node {
ArrayList<Edge> adj;
String name;
boolean sign;
boolean value = true;
Node counter; //complementary term
int index; // replaces visited, initially -1
int low;
int comp; // initially -1, can be used to check if on stack
}
Running time
 O(n+m)
Finding SCCs (Tarjan)
int index = 0, C = 0;
ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
Implementation: Tarjan
void tarjan(int i) { // run for every i with V[i].index == -1
V[i].index = index++;
V[i].low = V[i].index;
stack.push(i);
for (Edge e: V[i].adj) {
if (V[e.target].index == -1) {
tarjan(e.target);
V[i].low = Math.min(V[i].low, V[e.target].low);
}
else if (V[e.target].comp == -1) { // e.target must be on stack
V[i].low = Math.min(V[i].low, V[e.target].index);
}
}
if (V[i].index == V[i].low) { // SCC is found
int j;
do {
j = stack.pop();
V[j].comp = C;
} while(j != i);
C++;
}
}
Source: Fun with Graphs I, Kevin Verbeek
Implementation: contradictions&condensation
Contradiction checking
 For each vertex v:
If (v.comp==v.counter.comp) {
break; //Expression is not satisfiable
}
Running time
 Constant time for each vertex
 O(v)
Condensation graph
 Not needed when using Tarjan’s
 Already in reverse topological order
Implementation: setting variables
Setting the variables
 For each component c
 Reverse topological order
For (Node n: c.list) {
if (n.value != false) {
n.counter.value = false;
}
}
Running time
 Constant time for each vertex
 O(v)
 V = 2*n  O(n)
Another solution: backtracking
Backtracking
 Uses 2-CNF expression
 Give a variable a value (starting choice point)
 Add values to variables this choice determined
 If contradiction ➨ go back to last choice point
 If no contradiction ➨ you reach a new choice point
 Repeat until
 All variables have a value, OR
 Both choices at a choice point yield a contradiction
Backtracking: example
Example of backtracking
 (¬c ∨ a) ∧ (a ∨ b) ∧ (¬b ∨ c)
¬a
a
c
b
c
b
¬c
¬b
 This expression is satisfiable with:
 A = true
 B = true
 C = true
a
Limited backtracking
 Check in parallel
 Abort when choice point in parallel path is found
 Linear time, because path length = 2*n
Example: Labeling
Labeling
 Problem: Objects need labels, can be placed right or left.
 Labels may not overlap
labelL
a
labelR
labelL
labelL
b
c
labelR
labelR
 Say: true=left and false=right
 INF: (¬a => ¬c) ∧ (c => a)
 2-CNF: (a ∨ ¬c)
Example: Line labeling
Line labeling
 Problem: Label can be placed
above, on top or below a line.
 Labels may not overlap
a
 (b ∨ ¬a)
b
 ∧ (d ∨ ¬c)
 ∧ (a ∨ ¬c)
¬a
¬b
c
d
¬c
¬d
 ∧ (b ∨ ¬d)
Similar problems
Similar problems
 MAX-2-SAT
 Given: 2-CNF-expression
 Problem: Maximum number of clauses simultaneously true
 Complexity: NP-hard
 W2SAT
 Given: 2-SAT instance and integer k
 Problem: Satisfying assignment with at most k true variables
 Complexity: NP-complete
Questions?
Are there any questions?
Download