Processing math: 100%

2-Satisfiability solved by 252

Feb. 21, 2014, 5:41 p.m. by Rosalind Team

Topics: Graphs

Problem

In the 2SAT problem, you are given a set of clauses, where each clause is the disjunction (OR) of two literals (a literal is a Boolean variable or the negation of a Boolean variable). You are looking for a way to assign a value true or false to each of the variables so that all clauses are satisfied — that is, there is at least one true literal in each clause. For example, here’s an instance of 2SAT: (x1¯x2)(¯x1¯x3)(x1x2)(¯x3x4)(¯x1x4). This instance has a satisfying assignment: set x1, x2, x3, and x4 to true, false, false, and true, respectively.

  1. Are there other satisfying truth assignments of this 2SAT formula? If so, find them all.
  2. Give an instance of 2SAT with four variables, and with no satisfying assignment.

The purpose of this problem is to lead you to a way of solving 2SAT efficiently by reducing it to the problem of finding the strongly connected components of a directed graph. Given an instance I of 2SAT with n variables and m clauses, construct a directed graph GI=(V,E) as follows.

Note that the clause (αβ) is equivalent to either of the implications ¯αβ or ¯βα. In this sense, GI records all implications in I.

  1. Carry out this construction for the instance of 2SAT given above, and for the instance you constructed in 2.
  2. Show that if GI has a strongly connected component containing both x and ¯x for some variable x, then I has no satisfying assignment.
  3. Now show the converse of 4: namely, that if none of GI’s strongly connected components contain both a literal and its negation, then the instance I must be satisfiable. (Hint: Assign values to the variables as follows: repeatedly pick a sink strongly connected component of GI. Assign value true to all literals in the sink, assign false to their negations, and delete all of these. Show that this ends up discovering a satisfying assignment.)
  4. Conclude that there is a linear-time algorithm for solving 2SAT.

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Given: A positive integer k20 and k 2SAT formulas represented as follows. The first line gives the number of variables n103 and the number of clauses m104, each of the following m lines gives a clause of length 2 by specifying two different literals: e.g., a clause (x3¯x5) is given by 3 -5.

Return: For each formula, output 0 if it cannot be satisfied or 1 followed by a satisfying assignment otherwise.

Sample Dataset

2

2 4
1 2
-1 2
1 -2
-1 -2

3 4
1 2
2 3
-1 -2
-2 -3

Sample Output

0
1 1 -2 3

Please login to solve this problem.