# 2-Satisfiability (2-SAT) Problem

## 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:
TRUE FALSE FALSE
FALSE TRUE FALSE

Note : Boolean satisfiability problem is NP-complete (For proof, refer <a href="https://en.wikipedia.org/wiki/Cook%E2%80%93Levin_theorem" target="_blank”>Cook’s Theorem).

## 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-Satisfiabilty 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.

Note: The implication (if A then B) is equivalent to its contrapositive (if then ).

Now, consider the following cases:

CASE 1: If  exists in the graph
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  exists in the graph
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  both exist in the graph
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

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.

This article is contributed by Aanya Jindal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : danielagfavero

Article Tags :
Practice Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.