On this page
Picture a list of rules where each one says at least one of two things must hold, such as Physics or Chemistry in the first timetable slot. Can every rule hold at once? When each rule mentions only two variables, the question is called 2-satisfiability, or 2-SAT, and it can be answered in linear time, meaning time proportional to the size of the formula (2-satisfiability, Wikipedia). The same article notes that the more general satisfiability problems, which can involve constraints on more than two variables, are NP-complete, which is why the two-variable case is worth knowing well.
How does a 2-SAT clause become an implication?
A clause (a ∨ b) says a, or b, or both. Read it as a promise: if a is false, b must be true, and if b is false, a must be true. So one clause becomes two implications, ¬a → b and ¬b → a. Each is the contrapositive of the other; keeping both makes the graph symmetric.
The implication graph has one vertex for every literal (each variable and its negation) and one directed edge for every implication. With n variables and m clauses, that is 2n vertices and 2m edges.
A worked 4-variable 2-SAT example
Take the variables a, b, c and d, and five clauses:
(a ∨ b) gives ¬a → b and ¬b → a
(¬a ∨ c) gives a → c and ¬c → ¬a
(¬b ∨ c) gives b → c and ¬c → ¬b
(¬c ∨ d) gives c → d and ¬d → ¬c
(¬d ∨ a) gives d → a and ¬a → ¬d
That makes 8 vertices and 10 edges, the graph in the picture at the top of this post.
From a you go a → c → d → a, a cycle, so a, c and d can all reach one another. On the negative side, ¬a → ¬d → ¬c → ¬a is the mirror-image cycle. Neither b nor ¬b lies on a cycle. So the strongly connected components are:
P = {a, c, d}
N = {¬a, ¬c, ¬d}
{b}
{¬b}
Bigger graphs need an algorithm. Our post on strongly connected components with Kosaraju and Tarjan works both methods by hand. Robert Tarjan's 1972 paper gave a depth-first search algorithm whose time and space grow at most linearly with the number of vertices and edges (Tarjan, SIAM Journal on Computing).
When is a 2-SAT formula unsatisfiable?
A 2-SAT formula is satisfiable if and only if no variable lies in the same strongly connected component as its negation, as Bengt Aspvall, Michael Plass and Robert Tarjan showed in 1979 (Aspvall, Plass and Tarjan; statement as summarised in 2-satisfiability, Wikipedia). The reason: if x and ¬x share a component, x forces ¬x and ¬x forces x, so either choice contradicts itself.
In our example, no variable shares a component with its negation, so the formula is satisfiable.
Now watch it break. Add a sixth clause (¬a ∨ ¬c), which gives a → ¬c and c → ¬a. The path a → ¬c → ¬a → b → c → d → a is now one loop, so a and ¬a share a component. Check by hand: if a is true, clause 2 forces c true and clause 6 fails; if a is false, clause 1 forces b, then clause 3 forces c, clause 4 forces d, and clause 5 forces a true. No assignment works.
How do you read off a satisfying assignment?
Shrink each component to a single node. The result, called the condensation, has no cycles, so it can be put in topological order, as in our guide to topological sort on a DAG. For the five-clause formula, N comes first, then {b} and {¬b} in either order, then P.
Then go through the components in reverse topological order and set every literal in a component true unless it already has a value, which sets its negation false (2-satisfiability, Wikipedia). An equivalent shortcut: x is true when the component of x comes after the component of ¬x.
P comes last, so a, c and d are true.
No arrow joins b and ¬b, so b is free to take either value.
Check a = c = d = true with b = false: (a ∨ b) holds through a, (¬a ∨ c) through c, (¬b ∨ c) through c, (¬c ∨ d) through d and (¬d ∨ a) through a. Setting b true works too.
Why reverse order? When you set a late component true, everything it implies has already been set true, so no arrow runs from a true literal to a false one.
A checklist for any 2-SAT question
Rewrite each clause as two implications, and check you have 2m edges.
Find the strongly connected components with Kosaraju or Tarjan.
If some x and ¬x share a component, stop: the formula is unsatisfiable.
Otherwise order the condensation topologically and set x true when its component comes after that of ¬x.
Substitute the assignment back into every clause.
The Fermi Concept Library gives every concept in mathematics and computing its own lesson, and the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.
Try it now: add the clause (b ∨ ¬d) to the five-clause formula, redraw the graph, and decide whether b is still free.



