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
The above implications should be intuitive; Given
So, whenever there is a clause
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
That tells us that, if we take those
Following the truth table for the implication operation, we want to avoid the case where
Therefore, if
Leave a comment