
A Linear Time Algorithm for 2CNFSAT
Let Fbe a CNF Boolean formula with nvariables and mclauses such that each clause has exactly two literals.
1. Construct a directed graph GFfrom Fas follows:
•Create two vertices for each variable, one for each literal of the variable: 2nvertices.
•Treat each clause as a pair of Boolean implications and add edges corresponding to the implications.
For instance, if the clause is (l1,l2) then add an edge from the vertex NOT l1to the vertex l2and an
edge from the vertex NOT l2to the vertex l1.
2. Note that if x→yis an edge in this graph then y→xis an edge in Gas well.
(Exercise: Show that if there is a directed path in Gfrom l1to l2then there is a directed path in Gfrom l2
to l1.)
3. Construct the strongly connected components of G.
4. Fis not satisfiable iff a variable occurs both as a positive literal and a negative literal in a strongly connected
component.
•Claim: If a variable occurs both as a positive literal and a negative literal in a strongly connected
component then Fis not satisfiable.
Proof: Suppose, for some variable x, both its positive literal xand its negative literal xoccur in a
strongly connected component.
–Then there is a directed path Pfrom xto x, say: x→u1→ → · · · → ur→x.
–Set xto TRUE. The literal u1must get the truth value TRUE since the edge x→u1represents
an implication. For the same reason, all the literals along the path must get the truth value TRUE.
But, the literal xappears on the path and must be set to TRUE. But, the truth value for xmust
be FALSE since xis set to TRUE. Contradiction.
–Thus, xcannot be set to TRUE.
–Similarly, xcannot be set to FALSE.
–Therefore, if, for some variable, both xand xoccur in the same strongly connected component, F
is not satisfiable.
•Conversely, if none of the strongly connected components of Gcontain both the positive literal xand
the negative literal xof any variable xthen Fis satisfiable.
Proof: The proof is constructive and gives a way to assign a satisfying truth assignment to the variables.
(All literals in the same strongly connected component get the same truth assignment.)
–Let C1, C2, . . . , Ckbe the strongly connected components of G.
–Construct the following meta-graph H= (V0, E0):
1