2-SAT
computational complexity
algorithms
satisfiability problem
graph theory
Algorithm for 2-Satisfiability problem
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The 2-SAT problem asks whether a CNF formula with exactly two literals per clause is satisfiable.
Example clause: .
Unlike 3-SAT, 2-SAT is solvable in linear time using strongly connected components (SCCs).
Core Idea: Implication Graph
For each variable x, create two nodes:
x~x
For each clause , add implications:
After building the graph, compute SCCs.
Satisfiability Condition
The formula is satisfiable iff for every variable x, nodes x and ~x are in different SCCs.
If any variable and its negation fall in the same SCC, the formula is unsatisfiable.
Getting an Assignment
After SCC computation (Kosaraju or Tarjan), assign truth values by SCC topological order:
- process SCCs in reverse topological order,
- set a literal true if its SCC appears after its negation SCC.
Python Reference Implementation
Complexity
Let n be variables and m clauses:
- Vertices:
2n - Edges:
2m - Time:
O(n + m) - Space:
O(n + m)
Summary
| Topic | Result |
| Solver model | Implication graph + SCC |
| Unsat check | x and ~x in same SCC |
| Complexity | Linear in graph size |

