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:Â
*** QuickLaTeX cannot compile formula:
F = A \wedge \bar{B}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
, is satisfiable, because A = TRUE and B = FALSE makes F = TRUE.*** QuickLaTeX cannot compile formula:
G = A \wedge \bar{A}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
, is unsatisfiable, because:Â
Â
Â
Note : Boolean satisfiability problem is NP-complete (For proof, refer 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:Â
*** QuickLaTeX cannot compile formula:
F = (A_1 \vee B_1) \wedge (A_2 \vee B_2) \wedge (A_3 \vee B_3) \wedge ....... \wedge (A_m \vee B_m)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
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 : *** QuickLaTeX cannot compile formula:
F = (x1 \vee x2) \wedge (x2 \vee \bar{x1}) \wedge (\bar{x1} \vee \bar{x2})
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
Output : The given expression is satisfiable. (for x1 = FALSE, x2 = TRUE)Input : *** QuickLaTeX cannot compile formula:
F = (x1 \vee x2) \wedge (x2 \vee \bar{x1}) \wedge (x1 \vee \bar{x2}) \wedge (\bar{x1} \vee \bar{x2})
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
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Â
*** QuickLaTeX cannot compile formula:
(A \vee B)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
.*** QuickLaTeX cannot compile formula:
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
= TRUEÂ
- If A = 0, B must be 1 i.e.Â
*** QuickLaTeX cannot compile formula:
(\bar{A} \Rightarrow B)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
- If B = 0, A must be 1 i.e.Â
*** QuickLaTeX cannot compile formula:
(\bar{B} \Rightarrow A)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
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.Â
*** QuickLaTeX cannot compile formula:
(A \vee B)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
is expressed in Implication Graph as *** QuickLaTeX cannot compile formula:
edge(\bar{A} \rightarrow B) \ & edge(\bar{B} \rightarrow A)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
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.Â
*** QuickLaTeX cannot compile formula:
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
Note: The implication (if A then B) is equivalent to its contrapositive (ifÂ
*** QuickLaTeX cannot compile formula:
\bar{B}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
then *** QuickLaTeX cannot compile formula:
\bar{A}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
).Now, consider the following cases:
CASE 1: If *** QuickLaTeX cannot compile formula:
edge(X \rightarrow \bar{X})
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
[Tex]exists in the graph
[/Tex]This means *** QuickLaTeX cannot compile formula:
(X \Rightarrow \bar{X})
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
If X = TRUE, *** QuickLaTeX cannot compile formula:
\bar{X}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
= TRUE, which is a contradiction.But if X = FALSE, there are no implication constraints.Thus, X = FALSE
CASE 2: If *** QuickLaTeX cannot compile formula:
edge(\bar{X} \rightarrow X)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
[Tex]exists in the graph
[/Tex]This means *** QuickLaTeX cannot compile formula:
(\bar{X} \Rightarrow X)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
If *** QuickLaTeX cannot compile formula:
\bar{X}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
= TRUE, X = TRUE, which is a contradiction.But if *** QuickLaTeX cannot compile formula:
\bar{X}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
= FALSE, there are no implication constraints.Thus, *** QuickLaTeX cannot compile formula:
\bar{X}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
= FALSE i.e. X = TRUE
CASE 3: If *** QuickLaTeX cannot compile formula:
edge(X \rightarrow \bar{X}) \& edge(\bar{X} \rightarrow X)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
[Tex]both exist in the graph
[/Tex]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Â
*** QuickLaTeX cannot compile formula:
X
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
and *** QuickLaTeX cannot compile formula:
\bar{X}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
are on a cycle i.e. *** QuickLaTeX cannot compile formula:
path(\bar{A} \rightarrow B) \& path({B} \rightarrow A)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
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 *** QuickLaTeX cannot compile formula:
(A \Rightarrow B) \& (B \Rightarrow C), then A \Rightarrow C
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
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Â
*** QuickLaTeX cannot compile formula:
\bar{X}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
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 *** QuickLaTeX cannot compile formula:
\bar{X}
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10001 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
lie on the same SCC, we will definitely have *** QuickLaTeX cannot compile formula:
path(\bar{A} \rightarrow B) \& path({B} \rightarrow A)
*** Error message:
Cannot connect to QuickLaTeX server: cURL error 28: Connection timed out after 10000 milliseconds
Please make sure your server/PHP settings allow HTTP requests to external resources ("allow_url_fopen", etc.)
These links might help in finding solution:
http://wordpress.org/extend/plugins/core-control/
http://wordpress.org/support/topic/an-unexpected-http-error-occurred-during-the-api-request-on-wordpress-3?replies=37
present and hence the conclusion.Checking of the SCC can be done in O(E+V) using the Kosaraju’s Algorithm
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000;
vector< int > adj[MAX];
vector< int > adjInv[MAX];
bool visited[MAX];
bool visitedInv[MAX];
stack< int > s;
int scc[MAX];
int counter = 1;
void addEdges( int a, int b)
{
adj[a].push_back(b);
}
void addEdgesInverse( int a, int b)
{
adjInv[b].push_back(a);
}
void dfsFirst( int u)
{
if (visited[u])
return ;
visited[u] = 1;
for ( int i=0;i<adj[u].size();i++)
dfsFirst(adj[u][i]);
s.push(u);
}
void dfsSecond( int u)
{
if (visitedInv[u])
return ;
visitedInv[u] = 1;
for ( int i=0;i<adjInv[u].size();i++)
dfsSecond(adjInv[u][i]);
scc[u] = counter;
}
void is2Satisfiable( int n, int m, int a[], int b[])
{
for ( int i=0;i<m;i++)
{
if (a[i]>0 && b[i]>0)
{
addEdges(a[i]+n, b[i]);
addEdgesInverse(a[i]+n, b[i]);
addEdges(b[i]+n, a[i]);
addEdgesInverse(b[i]+n, a[i]);
}
else if (a[i]>0 && b[i]<0)
{
addEdges(a[i]+n, n-b[i]);
addEdgesInverse(a[i]+n, n-b[i]);
addEdges(-b[i], a[i]);
addEdgesInverse(-b[i], a[i]);
}
else if (a[i]<0 && b[i]>0)
{
addEdges(-a[i], b[i]);
addEdgesInverse(-a[i], b[i]);
addEdges(b[i]+n, n-a[i]);
addEdgesInverse(b[i]+n, n-a[i]);
}
else
{
addEdges(-a[i], n-b[i]);
addEdgesInverse(-a[i], n-b[i]);
addEdges(-b[i], n-a[i]);
addEdgesInverse(-b[i], n-a[i]);
}
}
for ( int i=1;i<=2*n;i++)
if (!visited[i])
dfsFirst(i);
while (!s.empty())
{
int n = s.top();
s.pop();
if (!visitedInv[n])
{
dfsSecond(n);
counter++;
}
}
for ( int i=1;i<=n;i++)
{
if (scc[i]==scc[i+n])
{
cout << "The given expression "
"is unsatisfiable." << endl;
return ;
}
}
cout << "The given expression is satisfiable."
<< endl;
return ;
}
int main()
{
int n = 5, m = 7;
int a[] = {1, -2, -1, 3, -3, -4, -3};
int b[] = {2, 3, -2, 4, 5, -5, 4};
is2Satisfiable(n, m, a, b);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static final int MAX = 100000 ;
@SuppressWarnings ( "unchecked" )
static List<List<Integer> > adj = new ArrayList();
@SuppressWarnings ( "unchecked" )
static List<List<Integer> > adjInv = new ArrayList();
static boolean [] visited = new boolean [MAX];
static boolean [] visitedInv = new boolean [MAX];
static Stack<Integer> s = new Stack<Integer>();
static int [] scc = new int [MAX];
static int counter = 1 ;
static void addEdges( int a, int b)
{
adj.get(a).add(b);
}
static void addEdgesInverse( int a, int b)
{
adjInv.get(b).add(a);
}
static void dfsFirst( int u)
{
if (visited[u])
return ;
visited[u] = true ;
for ( int i = 0 ; i < adj.get(u).size(); i++)
dfsFirst(adj.get(u).get(i));
s.push(u);
}
static void dfsSecond( int u)
{
if (visitedInv[u])
return ;
visitedInv[u] = true ;
for ( int i = 0 ; i < adjInv.get(u).size(); i++)
dfsSecond(adjInv.get(u).get(i));
scc[u] = counter;
}
static void is2Satisfiable( int n, int m,
int a[], int b[])
{
for ( int i = 0 ; i < m; i++)
{
if (a[i] > 0 && b[i] > 0 )
{
addEdges(a[i] + n, b[i]);
addEdgesInverse(a[i] + n, b[i]);
addEdges(b[i] + n, a[i]);
addEdgesInverse(b[i] + n, a[i]);
}
else if (a[i] > 0 && b[i] < 0 )
{
addEdges(a[i] + n, n - b[i]);
addEdgesInverse(a[i] + n, n - b[i]);
addEdges(-b[i], a[i]);
addEdgesInverse(-b[i], a[i]);
}
else if (a[i] < 0 && b[i] > 0 )
{
addEdges(-a[i], b[i]);
addEdgesInverse(-a[i], b[i]);
addEdges(b[i] + n, n - a[i]);
addEdgesInverse(b[i] + n, n - a[i]);
}
else
{
addEdges(-a[i], n - b[i]);
addEdgesInverse(-a[i], n - b[i]);
addEdges(-b[i], n - a[i]);
addEdgesInverse(-b[i], n - a[i]);
}
}
for ( int i = 1 ; i <= 2 * n; i++)
if (!visited[i])
dfsFirst(i);
while (!s.isEmpty())
{
int top = s.peek();
s.pop();
if (!visitedInv[top])
{
dfsSecond(top);
counter++;
}
}
for ( int i = 1 ; i <= n; i++)
{
if (scc[i] == scc[i + n])
{
System.out.println( "The given expression" +
"is unsatisfiable." );
return ;
}
}
System.out.println( "The given expression " +
"is satisfiable." );
}
public static void main(String[] args)
{
int n = 5 , m = 7 ;
for ( int i = 0 ; i < MAX; i++)
{
adj.add( new ArrayList<Integer>());
adjInv.add( new ArrayList<Integer>());
}
int a[] = { 1 , - 2 , - 1 , 3 , - 3 , - 4 , - 3 };
int b[] = { 2 , 3 , - 2 , 4 , 5 , - 5 , 4 };
is2Satisfiable(n, m, a, b);
}
}
|
Python3
from collections import defaultdict
MAX = 100000
adj = defaultdict( list )
adj_inv = defaultdict( list )
visited = [ False ] * ( MAX + 1 )
visited_inv = [ False ] * ( MAX + 1 )
s = []
scc = [ 0 ] * ( MAX + 1 )
counter = 1
def add_edges(a, b):
adj[a].append(b)
def add_edges_inverse(a, b):
adj_inv[b].append(a)
def dfs_first(u):
if visited[u]:
return
visited[u] = True
for neighbor in adj[u]:
dfs_first(neighbor)
s.append(u)
def dfs_second(u):
if visited_inv[u]:
return
visited_inv[u] = True
for neighbor in adj_inv[u]:
dfs_second(neighbor)
scc[u] = counter
def is_2_satisfiable(n, m, a, b):
global counter
for i in range (m):
if a[i] > 0 and b[i] > 0 :
add_edges(a[i] + n, b[i])
add_edges_inverse(a[i] + n, b[i])
add_edges(b[i] + n, a[i])
add_edges_inverse(b[i] + n, a[i])
elif a[i] > 0 and b[i] < 0 :
add_edges(a[i] + n, n - b[i])
add_edges_inverse(a[i] + n, n - b[i])
add_edges( - b[i], a[i])
add_edges_inverse( - b[i], a[i])
elif a[i] < 0 and b[i] > 0 :
add_edges( - a[i], b[i])
add_edges_inverse( - a[i], b[i])
add_edges(b[i] + n, n - a[i])
add_edges_inverse(b[i] + n, n - a[i])
else :
add_edges( - a[i], n - b[i])
add_edges_inverse( - a[i], n - b[i])
add_edges( - b[i], n - a[i])
add_edges_inverse( - b[i], n - a[i])
for i in range ( 1 , 2 * n + 1 ):
if not visited[i]:
dfs_first(i)
while s:
node = s.pop()
if not visited_inv[node]:
dfs_second(node)
counter + = 1
for i in range ( 1 , n + 1 ):
if scc[i] = = scc[i + n]:
print ( "The given expression is unsatisfiable." )
return
print ( "The given expression is satisfiable." )
def main():
n, m = 5 , 7
a = [ 1 , - 2 , - 1 , 3 , - 3 , - 4 , - 3 ]
b = [ 2 , 3 , - 2 , 4 , 5 , - 5 , 4 ]
is_2_satisfiable(n, m, a, b)
if __name__ = = "__main__" :
main()
|
C#
using System;
using System.Collections.Generic;
class TwoSatisfiability
{
const int MAX = 100000;
static List< int >[] adj = new List< int >[MAX];
static List< int >[] adjInv = new List< int >[MAX];
static bool [] visited = new bool [MAX];
static bool [] visitedInv = new bool [MAX];
static Stack< int > s = new Stack< int >();
static int [] scc = new int [MAX];
static int counter = 1;
static void AddEdges( int a, int b)
{
adj[a].Add(b);
}
static void AddEdgesInverse( int a, int b)
{
adjInv[b].Add(a);
}
static void DfsFirst( int u)
{
if (visited[u])
return ;
visited[u] = true ;
foreach ( var v in adj[u])
DfsFirst(v);
s.Push(u);
}
static void DfsSecond( int u)
{
if (visitedInv[u])
return ;
visitedInv[u] = true ;
foreach ( var v in adjInv[u])
DfsSecond(v);
scc[u] = counter;
}
static void Is2Satisfiable( int n, int m, int [] a, int [] b)
{
for ( int i = 0; i < MAX; i++)
{
adj[i] = new List< int >();
adjInv[i] = new List< int >();
}
for ( int i = 0; i < m; i++)
{
if (a[i] > 0 && b[i] > 0)
{
AddEdges(a[i] + n, b[i]);
AddEdgesInverse(a[i] + n, b[i]);
AddEdges(b[i] + n, a[i]);
AddEdgesInverse(b[i] + n, a[i]);
}
else if (a[i] > 0 && b[i] < 0)
{
AddEdges(a[i] + n, n - b[i]);
AddEdgesInverse(a[i] + n, n - b[i]);
AddEdges(-b[i], a[i]);
AddEdgesInverse(-b[i], a[i]);
}
else if (a[i] < 0 && b[i] > 0)
{
AddEdges(-a[i], b[i]);
AddEdgesInverse(-a[i], b[i]);
AddEdges(b[i] + n, n - a[i]);
AddEdgesInverse(b[i] + n, n - a[i]);
}
else
{
AddEdges(-a[i], n - b[i]);
AddEdgesInverse(-a[i], n - b[i]);
AddEdges(-b[i], n - a[i]);
AddEdgesInverse(-b[i], n - a[i]);
}
}
for ( int i = 1; i <= 2 * n; i++)
if (!visited[i])
DfsFirst(i);
while (s.Count > 0)
{
int node = s.Pop();
if (!visitedInv[node])
{
DfsSecond(node);
counter++;
}
}
for ( int i = 1; i <= n; i++)
{
if (scc[i] == scc[i + n])
{
Console.WriteLine( "The given expression is unsatisfiable." );
return ;
}
}
Console.WriteLine( "The given expression is satisfiable." );
}
static void Main()
{
int n = 5, m = 7;
int [] a = { 1, -2, -1, 3, -3, -4, -3 };
int [] b = { 2, 3, -2, 4, 5, -5, 4 };
Is2Satisfiable(n, m, a, b);
}
}
|
Javascript
const MAX = 100000;
const adj = new Array(MAX).fill(0).map(() => []);
const adjInv = new Array(MAX).fill(0).map(() => []);
const visited = new Array(MAX).fill( false );
const visitedInv = new Array(MAX).fill( false );
const s = [];
const scc = new Array(MAX).fill(0);
let counter = 1;
function addEdges(a, b) {
adj[a].push(b);
}
function addEdgesInverse(a, b) {
adjInv[b].push(a);
}
function dfsFirst(u) {
if (visited[u]) return ;
visited[u] = true ;
for (let i = 0; i < adj[u].length; i++)
dfsFirst(adj[u][i]);
s.push(u);
}
function dfsSecond(u) {
if (visitedInv[u]) return ;
visitedInv[u] = true ;
for (let i = 0; i < adjInv[u].length; i++)
dfsSecond(adjInv[u][i]);
scc[u] = counter;
}
function is2Satisfiable(n, m, a, b) {
for (let i = 0; i < m; i++) {
if (a[i] > 0 && b[i] > 0) {
addEdges(a[i] + n, b[i]);
addEdgesInverse(a[i] + n, b[i]);
addEdges(b[i] + n, a[i]);
addEdgesInverse(b[i] + n, a[i]);
} else if (a[i] > 0 && b[i] < 0) {
addEdges(a[i] + n, n - b[i]);
addEdgesInverse(a[i] + n, n - b[i]);
addEdges(-b[i], a[i]);
addEdgesInverse(-b[i], a[i]);
} else if (a[i] < 0 && b[i] > 0) {
addEdges(-a[i], b[i]);
addEdgesInverse(-a[i], b[i]);
addEdges(b[i] + n, n - a[i]);
addEdgesInverse(b[i] + n, n - a[i]);
} else {
addEdges(-a[i], n - b[i]);
addEdgesInverse(-a[i], n - b[i]);
addEdges(-b[i], n - a[i]);
addEdgesInverse(-b[i], n - a[i]);
}
}
for (let i = 1; i <= 2 * n; i++)
if (!visited[i])
dfsFirst(i);
while (s.length > 0) {
const node = s.pop();
if (!visitedInv[node]) {
dfsSecond(node);
counter++;
}
}
for (let i = 1; i <= n; i++) {
if (scc[i] === scc[i + n]) {
console.log( "The given expression is unsatisfiable." );
return ;
}
}
console.log( "The given expression is satisfiable." );
}
const n = 5, m = 7;
const a = [1, -2, -1, 3, -3, -4, -3];
const b = [2, 3, -2, 4, 5, -5, 4];
is2Satisfiable(n, m, a, b);
|
OutputThe 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.
Last Updated :
02 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...