![]() |
VOOZH | about |
Boolean Satisfiability or simply SAT is the problem of determining if a Boolean formula is satisfiable or unsatisfiable.
Examples:
| TRUE | FALSE | FALSE |
|---|---|---|
| FALSE | TRUE | FALSE |
Note : Boolean satisfiability problem is NP-complete (For proof, refer Cook's Theorem).
👁 Boolean Satisfiability 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)
For the CNF value to come TRUE, value of every clause should be TRUE. Let one of the clause be .= TRUE
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:
Let’s see one example of Implication Graph.
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:
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.