CS 113: Data Structures and Algorithms: Abhiram G. Ranade 2 Sat
CS 113: Data Structures and Algorithms: Abhiram G. Ranade 2 Sat
CS 113: Data Structures and Algorithms: Abhiram G. Ranade 2 Sat
Algorithms
Abhiram G. Ranade
2 SAT
Systems of equations
• You have already seen problems of the type “Find x, y, z s.t. x 2 + yz =
5, xy sin z = 34, x+z=7”
• If the variables x, y, z are logical (Boolean) variables, and the
conditions are expressed using logical operators (and, or, not), then
we will have a Boolean system of equations.
• Notation in what follows: + means or, multiplication means and,
single quote: ‘ means not in what follows. Also we write 0, 1 for T, F.
• Many interesting problems can be expressed as Boolean systems of
equations.
• 2 satisfiability problems (2 SAT) are a small subclass of Boolean
systems of equations. They can be solved in linear time using
algorithms for finding strongly connected components.
Graph colouring as a Boolean system of
equations
• Graph k-Colouring: Assign one of k colours to each vertex in an
undirected graph s.t. neighbouring vertices have distinct colours.
Formulating as Boolean system of equations:
• Boolean variables:
– xuc where u is a vertex and c is a colour.
• Constraints:
For all u: xu1 + xu2 + ... + xuk = 1 u must receive ≥ 1 colours.
For all c ≠ d, u : xuc’ + xud’ = 1 u must not receive both.
For all edges (u,v), c: xuc’+xvc’ = 1 u, v must not both get colour c
• If we can (solve) find values for all variables s.t. all constraints
(equations) are satisfied, we will know how to colour the graph.
Boolean Satisfiability problems
• Same logical expression can be written in many ways.
– e.g. x + yz’ = (x + y)(x + z’)
(Use: * Distributes over +, x*x = x+x = x, 1+x = 1)
• Conjunctive Normal Form (CNF): Expression = conjunction (And) of clauses
• Clause = disjunction (Or) of variables or their negations.
– e.g. (x + y)(x + z’)
– Non example: x + yz’
• CNF Satisfiability (SAT) problem: Is there an assignment of values to variables
such that the given CNF expression (“formula”) has the value 1?
• k-CNF SAT: Each disjunction contains at most k literals.
• Graph k-colouring:
– Each rule has either k or 2 literals.
– Formula to be satisfied: conjunction of lhs of all rules.
– k-CNF SAT problem
• We often drop “CNF” from the name.
2 SAT
• Product of disjunctions, each a disjunction of at most
2 literals.
• Satisfying assignment can be found in O(m+n) time
where n = number of variables, m = number of
disjunctions.
• No fast algorithm is known for k-CNF, for k > 2.
• Our graph colouring problem is expressed as k-SAT,
so our algorithm is useful only for 2-colouring.
• In fact just like 3-CNF, 3-colouring a graph is
considered to be a very hard problem.
Logic preliminaries
• The ⇒ Boolean operator: (“Implies”)
x ⇒ y will be said to be false if x is true and y is false, and true otherwise.
• True when
– x=1, y=1
– x=0, y=0
– x=0, y=1
• False when
– x=1, y=0
• Alternate definition 1: x ⇒ y = y + x’
• By the way: x ⇒ y = y’ ⇒ x’ (contra-positive)
• Alternate definition 2:
x⇒y = x ≤y (In LHS, we think of 1, 0 as numbers)
2 SAT Algorithm
1. For every variable x, have vertices x, x’.
2. For every disjunction x + y, have directed edges (y’, x)
and (x’, y)
Note: x + y is equivalent to y’ ⇒ x and x’ ⇒ y
3. Find scc of this graph.
4. If any scc contains vertices x, x’ for any x, then
declare failure.
5. Otherwise: Assign value T for all literals in a sink scc.
6. Repeat till all variables get assigned values.
Example
• (a+b’)(b+c)(c’+a’)(c+d’)(d+e)(e’+d’)
• Convert to implications:
b ⇒ a, a’⇒b’, c’ ⇒ b, b’ ⇒ c, c ⇒ a’, a ⇒ c’
d ⇒ c, c’ ⇒ d’
e’ ⇒ d, d ⇒ e’, d’ ⇒ e, e ⇒ d’
• Strongly connected components:
– 1: b, a, c’ 2: a’,b’,c, 3: e’, d, 4: d’, e
– Edges from 3 to 2, 1 to 4
– No scc contains x, x’. So solution exists.
• Set a’=b’=c=1, and a=b=c’=0,
• Next set e’=d=0, e=d’=1
Correctness
• Using alternate definition 2, each edge encodes ≤ relationship between its
endpoint literals.
• All literals in a scc must get the same value because of the cyclic relationship.
• If x, x’ appear in same scc, we require them to have same value, which is a
constraint we cannot satisfy. So we declare failure.
• Assume no x,x’ are in same component.
• For each scc C, we will have a scc C’ which will contain all literals of C
complemented. (Because for every x ⇒ y we also have y’ ⇒ x’)
• If we assign T values to literals in (sink) C, automatically we assign F values to
literals in (source) C’.
• Assigning 1 to y in x ≤ y does not constrain x.
• Assigning 0 to x in x ≤ y does not constrain y.
• So we can repeat, without worrying that new assignments will not be
consistent with previous assignments.
Remarks
• There is a simpler algorithm to 2 colour a
graph.
– Use dfs or bfs
• The discussion here shows the following ideas
– Transforming one problem to another problem
– Application of finding strongly connected
components.