CNF 合取范式

CNF 是合取范式的简称,是 SAT 问题中比较重要的概念,我在第 1, 2 节部分结合网上信息 1 对其背景相关知识进行介绍,在第 3 节部分针对 CNF 具体用法进行阐述。

1. Introduction

CNF is a data directory which contains examples of files stored using the DIMACS CNF file format. This format is used to define a Boolean expression, written in conjunctive normal form, that may be used as an example of the satisfiability problem.

CNF 是合取范式(Conjunctive Normal Form)的简称,它由一系列析取子句用或连接符(AND)连接而来。CNF 文件一般以 .dimacs 为后缀名。

The satisfiability problem considers the case in which N boolean variables are used to form a Boolean expression involving negation (NOT), conjunction (AND) and disjunction (OR). The problem is to determine whether there is any assignment of values to the Boolean variables which makes the formula true. It’s something like trying to flip a bunch of switches to find the setting that makes a light bulb turn on.

可满足问题(Satisifiability problem)指的是给一些列布尔变量赋值使得由这些布尔变量组合起来的布尔表达式结果为真。注意,布尔表达式组合方式仅限于 NOT,AND,OR。

2. CNF file

For simplicity, it is common to require that the boolean expression be written in conjunction normal form or “CNF”. A formula in conjunctive normal form consists:

  1. clauses joined by AND;
  2. each clause, in turn, consists of literals joined by OR;
  3. each literal is either the name of a variable (a positive literal, or the name of a variable preceded by NOT (a negative literal.

为了方便起见,布尔表达式(或公式)一般以 CNF 的格式进行书写,一个公式包含以下 3 个部分:

  1. 子句之间用 AND 连接;
  2. 子句中的布尔变量之间用 OR 连接;
  3. 每个布尔变量前面可加上 + 或者 - 号,表示逻辑上的“是”和“非”。

以下是一个简单的布尔表达式的例子,其中包含了 x1,x2,x3 3个变元和 2 个子句。

( x1 OR ( NOT x3 ) )
AND
( x2 OR x3 OR ( NOT x1 ) )

The CNF file format is an ASCII file format.

The file may begin with comment lines. The first character of each comment line must be a lower case letter “c”. Comment lines typically occur in one section at the beginning of the file, but are allowed to appear throughout the file.

CNF 文件的注释行以字符“c”开头,且一般位于文件的开头部分。

The comment lines are followed by the “problem” line. This begins with a lower case “p” followed by a space, followed by the problem type, which for CNF files is “cnf”, followed by the number of variables followed by the number of clauses.

CNF 文件用标记行“p cnf A B”来区分注释行和子句行,即这行之后的每一行都表示一个布尔子句,所有的子句用 AND 连接,组成一个完整的布尔表达式。这里的 A 表示变量的个数,B 表示子句的个数。

The remainder of the file contains lines defining the clauses, one by one.
A clause is defined by listing the index of each positive literal, and the negative index of each negative literal. Indices are 1-based, and for obvious reasons the index 0 is not allowed.

每个子句中的变量都是以数字序号表示的,如 1-100 表示了 100 个变量。注意在标号的时候不能出现 0,第一个变量必须用 1 来表示。

The definition of a clause may extend beyond a single line of text.
The definition of a clause is terminated by a final value of “0”.
The file terminates after the last clause is defined.
Some odd facts include:

由于每一行代表一个子句,而不同行是由 AND 来连接的,因此有必要在每一行的最后添上一个标记符,即“0”。这里的“0”代表这条子句的终结。上述的例子可以写成

c 1 x1
c 2 x2
c 3 x3
p cnf 3 2 
1 -3 0
2 3 -1 0

The definition of the next clause normally begins on a new line, but may follow, on the same line, the “0” that marks the end of the previous clause.
In some examples of CNF files, the definition of the last clause is not terminated by a final ‘0’;
In some examples of CNF files, the rule that the variables are numbered from 1 to N is not followed. The file might declare that there are 10 variables, for instance, but allow them to be numbered 2 through 11.

当然了,之前讲的注释规则,每条子句表示规则都是一般性规则。在某些特定的场合下,我们也可以对其进行修改,例如每一行不再以 0 结尾,变量的序号不从 1 开始等。

3. Usage

CNF 除了在构建 SAT 问题上有用,它在软件产品线(SPL)领域中构建特征模型(Feature model)时也十分有效,见论文 Evolving feature model configurations in software product lines 2

这里简单举一个例子,某软件有着如下的特征模型。

我们可以尝试写出其 CNF 文件如下,他们存在以下约束

  • B,C,D 都是 A 的子特征;
  • B,C 和 D 只能选择一个;
  • B 是必选特征,C 和 D 是可选特征;
  • C 和 D 不能同时选中。
c 1 A
c 2 B
c 3 C
c 4 D
p cnf 4 7
1 0
1 -2 0
1 -3 0
1 -4 0
2 -1 0
-2 -3 -4 0
2 3 4 -1 0

  1. CNF files. http://people.sc.fsu.edu/~jburkardt/data/cnf/cnf.html. ↩︎

  2. White, Jules, et al. “Evolving feature model configurations in software product lines.” Journal of Systems & Software 87.1(2014):119-136. ↩︎

发布了14 篇原创文章 · 获赞 29 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/chikily_yongfeng/article/details/82904522