Presentation is loading. Please wait.

Presentation is loading. Please wait.

Honors Track: Competitive Programming & Problem Solving 2-Satisfiability José Kuiper.

Similar presentations


Presentation on theme: "Honors Track: Competitive Programming & Problem Solving 2-Satisfiability José Kuiper."— Presentation transcript:

1 Honors Track: Competitive Programming & Problem Solving 2-Satisfiability José Kuiper

2 2-Satisfiability Contents What is the problem? How to solve it? Implementation Another solution: Limited backtracking Example problems: (line) labeling Similar problems to 2SAT

3 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?

4 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

5 Example From 2-CNF to Implication graph 2-CNF: (a ∨ b) ∧ (¬b ∨ c) INF: ¬a¬a b ¬ba c ¬c (¬a => b)∧ (¬b => a)∧ (b => c)∧ (¬c => ¬b)

6 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!

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

8 How to solve this problem? Construct implication graph Convert to implicative normal form Create vertex for each term Edges are logical implications

9 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!

10 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

11 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) A B C D E F A B C D EF Source: Fun with Graphs I, Kevin Verbeek

12 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!

13 How to solve this problem? Checking for contradictions Variables and their negations can’t be in one SCC Because transitivity would cause: x 0 => ¬x 0 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.

14 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!

15 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 A B C D EF A B C D E F

16 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!

17 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

18 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!

19 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

20 Implementation! Node-class implication graph class Node { ArrayList 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 stack = new ArrayDeque ();

21 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

22 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

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

24 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

25 Backtracking: example Example of backtracking (¬c ∨ a) ∧ (a ∨ b) ∧ (¬b ∨ c) Limited backtracking Check in parallel Abort when choice point in parallel path is found Linear time, because path length = 2*n This expression is satisfiable with:  A = true  B = true  C = true ¬a b c a a c ¬b ¬c b

26 Example: Labeling Labeling Problem: Objects need labels, can be placed right or left. Labels may not overlap Say: true=left and false=right INF: (¬a => ¬c) 2-CNF: (a ∨ ¬c) a b c labelR labelL labelR ∧ (c => a)

27 Example: Line labeling Line labeling Problem: Label can be placed above, on top or below a line. Labels may not overlap (b ∨ ¬a) ∧ (d ∨ ¬c) ∧ (a ∨ ¬c) ∧ (b ∨ ¬d) a ¬a b ¬b c d ¬c ¬d

28 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

29 Questions? Are there any questions?


Download ppt "Honors Track: Competitive Programming & Problem Solving 2-Satisfiability José Kuiper."

Similar presentations


Ads by Google