Advertisement
sweet1cris

Untitled

Feb 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. Checking of the SCC can be done in O(E+V) using the Kosaraju’s Algorithm
  2.  
  3. // C++ implementation to find if the given
  4. // expression is satisfiable using the
  5. // Kosaraju's Algorithm
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. const int MAX = 100000;
  10.  
  11. // data structures used to implement Kosaraju's
  12. // Algorithm. Please refer
  13. // http:// www.geeksforgeeks.org/strongly-connected-components/
  14. vector<int> adj[MAX];
  15. vector<int> adjInv[MAX];
  16. bool visited[MAX];
  17. bool visitedInv[MAX];
  18. stack<int> s;
  19.  
  20. // this array will store the SCC that the
  21. // particular node belongs to
  22. int scc[MAX];
  23.  
  24. // counter maintains the number of the SCC
  25. int counter = 1;
  26.  
  27. // adds edges to form the original graph
  28. void addEdges(int a, int b)
  29. {
  30.     adj[a].push_back(b);
  31. }
  32.  
  33. // add edges to form the inverse graph
  34. void addEdgesInverse(int a, int b)
  35. {
  36.     adjInv[b].push_back(a);
  37. }
  38.  
  39. // for STEP 1 of Kosaraju's Algorithm
  40. void dfsFirst(int u)
  41. {
  42.     if(visited[u])
  43.         return;
  44.  
  45.     visited[u] = 1;
  46.  
  47.     for (int i=0;i<adj[u].size();i++)
  48.         dfsFirst(adj[u][i]);
  49.  
  50.     s.push(u);
  51. }
  52.  
  53. // for STEP 2 of Kosaraju's Algorithm
  54. void dfsSecond(int u)
  55. {
  56.     if(visitedInv[u])
  57.         return;
  58.  
  59.     visitedInv[u] = 1;
  60.  
  61.     for (int i=0;i<adjInv[u].size();i++)
  62.         dfsSecond(adjInv[u][i]);
  63.  
  64.     scc[u] = counter;
  65. }
  66.  
  67. // function to check 2-Satisfiability
  68. void is2Satisfiable(int n, int m, int a[], int b[])
  69. {
  70.     // adding edges to the graph
  71.     for(int i=0;i<m;i++)
  72.     {
  73.         // variable x is mapped to x
  74.         // variable -x is mapped to n+x = n-(-x)
  75.  
  76.         // for a[i] or b[i], addEdges -a[i] -> b[i]
  77.         // AND -b[i] -> a[i]
  78.         if (a[i]>0 && b[i]>0)
  79.         {
  80.             addEdges(a[i]+n, b[i]);
  81.             addEdgesInverse(a[i]+n, b[i]);
  82.             addEdges(b[i]+n, a[i]);
  83.             addEdgesInverse(b[i]+n, a[i]);
  84.         }
  85.  
  86.         else if (a[i]>0 && b[i]<0)
  87.         {
  88.             addEdges(a[i]+n, n-b[i]);
  89.             addEdgesInverse(a[i]+n, n-b[i]);
  90.             addEdges(-b[i], a[i]);
  91.             addEdgesInverse(-b[i], a[i]);
  92.         }
  93.  
  94.         else if (a[i]<0 && b[i]>0)
  95.         {
  96.             addEdges(-a[i], b[i]);
  97.             addEdgesInverse(-a[i], b[i]);
  98.             addEdges(b[i]+n, n-a[i]);
  99.             addEdgesInverse(b[i]+n, n-a[i]);
  100.         }
  101.  
  102.         else
  103.         {
  104.             addEdges(-a[i], n-b[i]);
  105.             addEdgesInverse(-a[i], n-b[i]);
  106.             addEdges(-b[i], n-a[i]);
  107.             addEdgesInverse(-b[i], n-a[i]);
  108.         }
  109.     }
  110.  
  111.     // STEP 1 of Kosaraju's Algorithm which
  112.     // traverses the original graph
  113.     for (int i=1;i<=2*n;i++)
  114.         if (!visited[i])
  115.             dfsFirst(i);
  116.  
  117.     // STEP 2 pf Kosaraju's Algorithm which
  118.     // traverses the inverse graph. After this,
  119.     // array scc[] stores the corresponding value
  120.     while (!s.empty())
  121.     {
  122.         int n = s.top();
  123.         s.pop();
  124.  
  125.         if (!visitedInv[n])
  126.         {
  127.             dfsSecond(n);
  128.             counter++;
  129.         }
  130.     }
  131.  
  132.     for (int i=1;i<=n;i++)
  133.     {
  134.         // for any 2 vairable x and -x lie in
  135.         // same SCC
  136.         if(scc[i]==scc[i+n])
  137.         {
  138.             cout << "The given expression "
  139.                  "is unsatisfiable." << endl;
  140.             return;
  141.         }
  142.     }
  143.  
  144.     // no such variables x and -x exist which lie
  145.     // in same SCC
  146.     cout << "The given expression is satisfiable."
  147.          << endl;
  148.     return;
  149. }
  150.  
  151. //  Driver function to test above functions
  152. int main()
  153. {
  154.     // n is the number of variables
  155.     // 2n is the total number of nodes
  156.     // m is the number of clauses
  157.     int n = 5, m = 7;
  158.  
  159.     // each clause is of the form a or b
  160.     // for m clauses, we have a[m], b[m]
  161.     // representing a[i] or b[i]
  162.  
  163.     // Note:
  164.     // 1 <= x <= N for an uncomplemented variable x
  165.     // -N <= x <= -1 for a complemented variable x
  166.     // -x is the complement of a variable x
  167.  
  168.     // The CNF being handled is:
  169.     // '+' implies 'OR' and '*' implies 'AND'
  170.     // (x1+x2)*(x2’+x3)*(x1’+x2’)*(x3+x4)*(x3’+x5)*
  171.     // (x4’+x5’)*(x3’+x4)
  172.     int a[] = {1, -2, -1, 3, -3, -4, -3};
  173.     int b[] = {2, 3, -2, 4, 5, -5, 4};
  174.  
  175.     // We have considered the same example for which
  176.     // Implication Graph was made
  177.     is2Satisfiable(n, m, a, b);
  178.  
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement