Sub topic 15
Solving 2-SAT with Strongly Connected Components
This chapter assumes you know what the condensation DAG is, how Kosaraju's algorithm labels components, and that Kosaraju numbers components in topological order. It also assumes you can read basic Boolean logic: OR (∨), NOT (¬), and implication (⇒).
The 2-SAT problem
A 2-SAT instance is a list of clauses over Boolean variables. Each clause is the OR of exactly two literals, and a literal is a variable or its negation. The task is to find a true/false value for every variable that makes all clauses true, or to report that no such assignment exists.
General SAT is NP-complete. 2-SAT can be solved in O(V + E) time by reducing it to an SCC problem.
From clauses to an implication graph
The clause (a ∨ b) is logically equivalent to two implications:
- ¬a ⇒ b (if a is false, b must be true)
- ¬b ⇒ a (if b is false, a must be true)
Build a directed graph with one vertex per literal, so 2n vertices for n variables. Each clause adds the two edges above. This graph is the implication graph. Every path in it is a chain of forced conclusions.
For the clauses (x0 ∨ x1), (¬x0 ∨ x1), (¬x1 ∨ ¬x2), the graph looks like this:
The SCC test
Literals in the same SCC imply each other, so they must all have the same truth value.
- Unsatisfiable if, for some variable x, the literals x and ¬x lie in the same SCC. Then x ⇒ ¬x and ¬x ⇒ x, which cannot both hold.
- Satisfiable otherwise. To build an assignment, set x true when comp[x] comes after comp[¬x] in the topological order of the condensation.
The rule works because it makes literals near the sinks of the condensation true. An edge u ⇒ v never points from a true literal to a false one, since v sits at or after u in topological order. The graph has a built-in symmetry: u ⇒ v exists exactly when ¬v ⇒ ¬u does. That symmetry makes the choices for different variables consistent with each other.
Implementation
Literal xi is vertex 2i and ¬xi is vertex 2i + 1, so negating a literal is lit ^ 1. The Kosaraju function from the earlier chapter is repeated here so the block runs on its own.
def kosaraju(n, adj):
visited = [False] * n
order = []
def dfs1(u):
visited[u] = True
for v in adj[u]:
if not visited[v]:
dfs1(v)
order.append(u)
for u in range(n):
if not visited[u]:
dfs1(u)
radj = [[] for _ in range(n)]
for u in range(n):
for v in adj[u]:
radj[v].append(u)
comp = [-1] * n
def dfs2(u, c):
comp[u] = c
for v in radj[u]:
if comp[v] == -1:
dfs2(v, c)
c = 0
for u in reversed(order):
if comp[u] == -1:
dfs2(u, c)
c += 1
return comp
def lit(i, positive=True):
return 2 * i + (0 if positive else 1)
def two_sat(num_vars, clauses):
n = 2 * num_vars
adj = [[] for _ in range(n)]
for a, b in clauses: # clause (a OR b)
adj[a ^ 1].append(b) # not a => b
adj[b ^ 1].append(a) # not b => a
comp = kosaraju(n, adj)
result = []
for i in range(num_vars):
if comp[2 * i] == comp[2 * i + 1]:
return None # x and not x in the same SCC
result.append(comp[2 * i] > comp[2 * i + 1])
return result
clauses = [
(lit(0), lit(1)),
(lit(0, False), lit(1)),
(lit(1, False), lit(2, False)),
]
print(two_sat(3, clauses)) # [True, True, False]
print(two_sat(1, [(lit(0), lit(0)), (lit(0, False), lit(0, False))])) # NoneThe second call encodes "x0 must be true" and "x0 must be false". It adds the edges ¬x0 ⇒ x0 and x0 ⇒ ¬x0, which puts both literals in one SCC.
Common errors
- Adding only one of the two implications per clause. Without both edges the graph loses its symmetry and the assignment rule can produce wrong answers.
- Comparing component numbers in the wrong direction for your SCC algorithm.
- Encoding a unit clause "x must be true" incorrectly. Write it as (x ∨ x), which adds the edge ¬x ⇒ x.
Exercise
Is (x0 ∨ x1), (¬x0 ∨ x1), (x0 ∨ ¬x1), (¬x0 ∨ ¬x1) satisfiable? Justify using the implication graph.
Show answer
No. The edges include ¬x0 ⇒ x1 and x1 ⇒ x0, from clauses 1 and 4, so ¬x0 reaches x0. They also include x0 ⇒ x1 and x1 ⇒ ¬x0, from clauses 2 and 4, so x0 reaches ¬x0. Therefore x0 and ¬x0 are in the same SCC, and the formula is unsatisfiable.
Exercise
You switch two_sat to use Tarjan's algorithm and set comp[v] to the position of v's component in Tarjan's output list. What must change?
Show answer
Tarjan emits components in reverse topological order, so the assignment test becomes comp[2i] < comp[2i + 1]. The test for unsatisfiability, where both literals share a component, stays the same.