VOOZH about

URL: https://www.geeksforgeeks.org/dsa/2-satisfiability-2-sat-problem/

⇱ 2-Satisfiability (2-SAT) Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

2-Satisfiability (2-SAT) Problem

Last Updated : 23 Jul, 2025

Boolean Satisfiability Problem

Boolean Satisfiability or simply SAT is the problem of determining if a Boolean formula is satisfiable or unsatisfiable. 

  • Satisfiable : If the Boolean variables can be assigned values such that the formula turns out to be TRUE, then we say that the formula is satisfiable.
  • Unsatisfiable : If it is not possible to assign such values, then we say that the formula is unsatisfiable.

Examples:

  • , is satisfiable, because A = TRUE and B = FALSE makes F = TRUE.
  • , is unsatisfiable, because: 
TRUEFALSEFALSE
FALSETRUEFALSE


Note : Boolean satisfiability problem is NP-complete (For proof, refer Cook's Theorem).

👁 Boolean Satisfiability Problem

What is 2-SAT Problem

2-SAT is a special case of Boolean Satisfiability Problem and can be solved 
in polynomial time.

To understand this better, first let us see what is Conjunctive Normal Form (CNF) or also known as Product of Sums (POS). 

CNF : CNF is a conjunction (AND) of clauses, where every clause is a disjunction (OR).
Now, 2-SAT limits the problem of SAT to only those Boolean formula which are expressed as a CNF with every clause having only 2 terms(also called 2-CNF).

Example:

Thus, Problem of 2-Satisfiability can be stated as:

Given CNF with each clause having only 2 terms, is it possible to assign such values to the variables so that the CNF is TRUE?

Examples: 

Input : Output : The given expression is satisfiable. (for x1 = FALSE, x2 = TRUE)Input : Output : The given expression is unsatisfiable. (for all possible combinations of x1 and x2)

Approach for 2-SAT Problem


For the CNF value to come TRUE, value of every clause should be TRUE. Let one of the clause be .= TRUE

  • If A = 0, B must be 1 i.e. 
  • If B = 0, A must be 1 i.e. 

Thus, 

 = TRUE is equivalent to 

Now, we can express the CNF as an Implication. So, we create an Implication Graph which has 2 edges for every clause of the CNF. 
is expressed in Implication Graph as 

Thus, for a Boolean formula with ‘m’ clauses, we make an Implication Graph with: 

  • 2 edges for every clause i.e. ‘2m’ edges.
  • 1 node for every Boolean variable involved in the Boolean formula.

Let’s see one example of Implication Graph. 

👁 Approach for 2-SAT Problem

Note: The implication (if A then B) is equivalent to its contrapositive (if then ).
Now, consider the following cases:

CASE 1: If This means If X = TRUE, = TRUE, which is a contradiction.But if X = FALSE, there are no implication constraints.Thus, X = FALSE
CASE 2: If This means If = TRUE, X = TRUE, which is a contradiction.But if = FALSE, there are no implication constraints.Thus, = FALSE i.e. X = TRUE
CASE 3: If One edge requires X to be TRUE and the other one requires X to be FALSE.Thus, there is no possible assignment in such a case.


CONCLUSION:

If any two variables and are on a cycle i.e. both exists, then the CNF is unsatisfiable. Otherwise, there is a possible assignment and the CNF is satisfiable. 
Note here that, we use path due to the following property of implication: 
If we have 
Thus, if we have a path in the Implication Graph, that is pretty much same as having a direct edge.

CONCLUSION FROM IMPLEMENTATION POINT OF VIEW:

If both X and lie in the same SCC (Strongly Connected Component), the CNF is unsatisfiable. 
A Strongly Connected Component of a directed graph has nodes such that every node can be reach from every another node in that SCC. 
Now, if X and lie on the same SCC, we will definitely have present and hence the conclusion.
Checking of the SCC can be done in O(E+V) using the Kosaraju’s Algorithm

Implementation:


Output
The given expression is satisfiable.

More Test Cases:

Input : n = 2, m = 3
a[] = {1, 2, -1}
b[] = {2, -1, -2}
Output : The given expression is satisfiable.
Input : n = 2, m = 4
a[] = {1, -1, 1, -1}
b[] = {2, 2, -2, -2}
Output : The given expression is unsatisfiable.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Comment
Article Tags:
Article Tags: