2-Sat
Created: 2019-12-05 Thu 16:31
Some definitions from Logic
An “and” expression is called a conjunction.
E.g. \( x_1 \wedge x_2 \)
An “or” is called a disjunction.
E.g. \( x_1 \vee x_2 \)
An expression of the form
\( (x_1 \vee x_2 \vee \cdots \vee x_n) \wedge (y_1 \vee y_2 \vee \cdots \vee y_n) \cdots \)
is said to be in conjunctive normal form (CNF).
2-Sat
- If each of the disjucts have only 2 elements, then we have (2-CNF).
- Solving CNF is NP-Complete.
- Solving 2-CNF is much easier!
- We will convert the 2-CNF into a graph.
- Each variable \(x\) become two nodes: one for \(x\) and one for \(\neg x\).
- Implications become directed edges.
- Find strongly connected components, and check that \(x\) and \(\neg x\)
are never in the same component.
- Where to get implications? Basic idea:
- Remember that \( a \rightarrow b \equiv a \vee \neg b \)
- So.. we convert every \(x \vee y\) into \( \neg x \rightarrow y \)
Tips
- To keep track of nodes, you need \(2n\) entries.
- Let \(x\) and \(\neg x\) be adjacent:
- E.g. \(x_2\) in position 4 and \(\neg x_2\) in position 5.
- Then you can get the index of the negation by xor with 1.
- To find strongly connected components, you can use the DFS method presented.
- Or use Kosaraju’s Algorithm
- DFS the original graph, push nodes visited to a stack \(s\).
- DFS the inverse graph, starting with the top of stack.
- Each time you have to get a new element from the stack, increment the SCC counter.