# Homework 4 Optional Problems

In the 2SAT problem, you are given a set of clauses, where each clause is the disjunction 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 this problem, design an algorithm that determines whether or not a given 2SAT instance has a satisfying assignment. (Your algorithm does not need to exhibit a satisfying assignment, just decide whether or not one exists.) Your algorithm should run in time, where and are the number of clauses and variables, respectively. [Hint: strongly connected components.]

**ANSWER:** Quoting Wikipedia:

In computer science, 2-satisfiability, 2-SAT or just 2SAT is a computational problem of assigning values to variables, each of which has two possible values, in order to satisfy a system of constraints on pairs of variables. It is a special case of the general Boolean satisfiability problem, which can involve constraints on more than two variables, and of constraint satisfaction problems, which can allow more than two choices for the value of each variable. But in contrast to those more general problems, which are NP-complete, 2-satisfiability can be solved in polynomial time.

From the given formula, we construct a digraph where each vertex is a literal and each edge an implication between two literals. Each clause can be equivalently expressed as two implications:

The above implications should be intuitive; Given is (not ), if the clause is to be , must be (since the clause is a Boolean OR). Similarly, given is , must be . Of course, we can also deduce the same from a truth table.

So, whenever there is a clause is the formula, we add two edges to corresponding to the equivalent implications. Then we run Kosaraju’s algorithm for finding strongly connected components (SCC). The algorithms runs in time. If a literal and its negation are in the same SCC, there is no possible assignment that can make both , thus, the formula is not satisfiable. We need to do a linear scan of each SCC to see if a literal and its negation are both in it; this can be done in time. Thus, the overall time complexity for determining satisfiability is .

Having determined satisfiability, we can go further and find assignments that satisfy the formula. In order to do that, we take advantage of a property of Kosaraju’s algorithm: the SCCs are returned in topological order. That is, if a node is in the ith SCC, is in the jth one, with , appears before .

That tells us that, if we take those and , the original formula can have the implication , but not in the other direction, otherwise, they wouldn’t be in separate SCCs.

Following the truth table for the implication operation, we want to avoid the case where ; the only case where the implication evaluates to .

Therefore, if comes before , we choose , thus making . On the other hand, if comes before , we choose , thus making .