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 or 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: This instance has a satisfying assignment: set , , , and to , , , and , respectively.
- Are there other satisfying truth assignments of this 2SAT formula? If so, find them all.
- 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 of 2SAT with variables and clauses, construct a directed graph as follows.
- has nodes, one for each variable and its negation.
- has edges: for each clause of (where are literals), has an edge from the negation of to , and one from the negation of to .
Note that the clause is equivalent to either of the implications or . In this sense, records all implications in .
- Carry out this construction for the instance of 2SAT given above, and for the instance you constructed in 2.
- Show that if has a strongly connected component containing both and for some variable , then has no satisfying assignment.
- Now show the converse of 4: namely, that if none of ’s strongly connected components contain both a literal and its negation, then the instance must be satisfiable. (Hint: Assign values to the variables as follows: repeatedly pick a sink strongly connected component of . Assign value to all literals in the sink, assign to their negations, and delete all of these. Show that this ends up discovering a satisfying assignment.)
- Conclude that there is a linear-time algorithm for solving 2SAT.
Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.
Given: A positive integer and 2SAT formulas represented as follows. The first line gives the number of variables and the number of clauses , each of the following lines gives a clause of length by specifying two different literals: e.g., a clause is given by 3 -5
.
Return: For each formula, output if it cannot be satisfied or followed by a satisfying assignment otherwise.
在2SAT问题中,为您提供了一组子句,其中每个子句是两个文字的析取(OR)(一个文字是一个布尔变量或一个布尔变量的取反)。您正在寻找一种分配值的方法 要么 到每个变量,以便所有子句都得到满足-也就是说,每个子句中至少有一个真实的文字。例如,这是2SAT的一个实例: 此实例的分配令人满意:set , , 和 至 , , 和 , 分别。
- 这个2SAT公式还有其他令人满意的真值分配吗?如果是这样,请全部找到。
- 给出一个2SAT实例,该实例具有四个变量,并且没有令人满意的分配。
此问题的目的是通过将2SAT简化为查找有向图的强连接组件的问题,从而引导您有效地解决2SAT 。给定一个实例 的2SAT与 变量和 子句,构造有向图 如下。
- 具有 节点,每个变量及其取反。
- 具有 边:对于每个子句 的 (哪里 是文字), 从否定中有优势 至 和一个来自否定的 至 。
注意该条款 等同于任何一种含义 要么 。在这个意义上, 记录所有含义 。
- 对上面给出的2SAT实例以及您在2中构造的实例进行此构造。
- 证明是否 具有紧密连接的组件,其中包含 和 对于一些变量 , 然后 没有令人满意的任务。
- 现在显示4的反面:即,如果没有 的高度连接的组件包含文字及其取反,然后是实例 必须令人满意。(提示:按如下所示为变量分配值:反复选择一个接收器的强连接组件。赋值 接收器中的所有文字,分配 他们的否定,并删除所有这些。证明这最终找到了令人满意的任务。)
- 结论存在一种用于求解2SAT的线性时间算法。
资料来源:Dasgupta,Papadimitriou,Vazirani的算法。麦格劳-希尔。2006年。
给定:正整数 和 2SAT公式表示如下。第一行给出变量的数量 和子句数 ,以下每个 行给出了一个长度子句 通过指定两个不同的文字:例如,子句 由给出3 -5
。
返回值:对于每个公式,输出 如果无法满足或者 后面是令人满意的作业。
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