Card Constraints
Card Constraints
Carsten Sinz
1 Introduction
Cardinality constraints—expressing numerical bounds on discrete quantities—arise fre-
quently out of the encoding of real-world problems: in product configuration, e.g., en-
gineers want to express that at least n parts of a certain kind are needed for a fully
functional product [1, 2]; in scheduling radio frequencies transmitters have to obey cer-
tain non-interference distance rules [3]; or in reconstructing images form computer to-
mographs different projections impose different numerical constraints on the original
picture [4]. Moreover, the general problem of re-encoding problems of a finite variable
logic in ordinary propositional logic usually requires setting up cardinality constraints
expressing that each variable has to take exactly one of its possible values at any time.
With considerable progress made over the last years in solving propositional satis-
fiability (SAT) instances, interest increased in tackling problems that include cardinal-
ity constraints using SAT-solvers. This requires, however, a re-formulation of cardinal-
ity constraints in the language of purely propositional logic, or, even more restrictive,
in conjunctive normal form (CNF), the predominant input language of modern SAT-
solvers. The SAT-approach also offers the advantage that problem instances consisting
of a mixture of ordinary Boolean constraints and cardinality constraints (occurring fre-
quently in practical applications like product configuration), can be handled with only
one unified technique, thus making the use of hybrid solvers dispensable.
Boolean cardinality constraints are a special—but common—form of cardinality
constraints. They put numerical restrictions on the number of propositional variables
2
that are allowed to be true at the same time. A typical construct like ≤k (x1 , . . . , xn )
means that not more than k out of the n variables x1 , . . . , xn are allowed to be true. The
traditional way of converting a constraint like ≤k (x1 , . . . , xn ) to purely propositional
logic is by explicitly excluding all possible combinations of k + 1 variables being true.
So, exemplarily for the case k = 1, we have to exclude all combinations of two variables
being simultaneously true, thus obtaining
^
(¬xi ∨ ¬xj ) .
1≤i<j≤n
This formula, however, consists of n2 , i.e. O(n2 ), clauses. In general, using the given
n
as an encoding of ≤ k (x1 , . . . , xn ). Unfortunately, this encoding requires k+1 clauses
n
p
of length k + 1, which in the worst case of k = dn/2e − 1 amounts to O(2 / n/2)
clauses.1
Better encodings are known. Bailleux and Boufkhad [4], e.g., presented an encoding
requiring only a quadratic number of clauses. Another translation, given by Warners [6]
for the more general case of discrete linear inequalities with integer coefficients, results
in an encoding with 8n clauses and 2n additional variables.
x1 s0
x2
ok?
∑ ≤k
sm
xn
Fig. 1. General approach to compute ≤ k (x1 , . . . , xn ): First, sum up the input bits xi with a
unary or binary counter; then, compare the resulting sum with the bound k.
1
The complexity bound is by Stirling’s approximation of n!. A more elaborate result can be
found in [5].
3
Obviously, there are different possibilities on how to construct such a circuit, but in
general they consist of two subsequent stages: in the first stage, the counter stage, the
number of inputs that are set to true is counted. Then, in a subsequent comparator stage,
it is checked whether the counter result lies within the bound given by the cardinality
constraint. The general approach is displayed in Fig. 1 for the cardinality constraint
≤ k (x1 , . . . , xn ). Two criteria allow a coarse distinction of different counter designs:
(i) Does the circuit count in unary or in binary, i.e., is the counter result encoded as a
unary or binary number?2 And (ii), is the counter realized as a sequential or as a parallel
implementation?
In the literature we have found two counter-based realizations of CNF encodings:
Bailleux and Boufkhad’s solution [4] makes use of a parallel, unary counter; the ap-
proach by Warners [6] resembles a parallel, binary counter, but with some differences
in the encoding and without explicit reference to a hardware design.
The rest of this paper is organized as follows: In the next section we give basic
definitions. Then we present two circuit designs resulting in novel CNF encodings:
one based on a sequential, unary counter, the other based on a parallel, binary counter.
Thereafter, we prove a linear lower bound on the number of clauses for any CNF en-
coding. Finally, we point to possible future directions of research and conclude.
2 Basic Definitions
φ, ψ ::= ⊥ | > | xi | ¬φ | (φ ∨ ψ) | (φ ∧ ψ) | (φ ⇒ ψ) | (φ ⇔ ψ) | (φ ⊕ ψ) .
Here, ⊥ denotes falsity, > denotes truth, ⊕ stands for exclusive OR, the other symbols
have the usual meaning. The set of all formulae built upon V we will denote by F (V )
or simply F . We further assume the customary definition of the semantics of formulae
using variable assignments α : V → B and corresponding evaluation functions α̂ :
F (V ) → B for formulae, where B = {0, 1} denotes the set of Boolean constants.
We extend our language to additionally include cardinality constaints, i.e. expres-
sions of the form ≤k (φ1 , . . . , φn ), ≥k (φ1 , . . . , φn ), and =k (φ1 , . . . , φn ), with k ∈ Z,
n ≥ 0, and φi ∈ F for 1 ≤ i ≤ n. The intended meaning is that at most (at least,
exactly) k among the formulae φi (1 ≤ i ≤ n) are true. A formal definition of their
semantics is given by the following definition.
2
In a unary number representation, a value n between 0 and k is represented as a vector of k
bits (nk nk−1 . . . n1 ), with the first n bits set to true and the others to false.
4
We can separate the problem of CNF conversion for cardinality constraints and that
of CNF conversion for the ordinary propositional logical operators by the following
lemma.
SAT
Lemma 1. We denote by φ ≡ ψ that φ and ψ are satisfiability equivalent, i.e. that φ
is satisfiable iff ψ is. Then the following holds:
SAT ^
≤k (φ1 , . . . , φn ) ≡ ≤k (y1 , . . . , yn ) ∧ (φi ⇒ yi )
1≤i≤n
SAT ^
≥k (φ1 , . . . , φn ) ≡ ≥k (y1 , . . . , yn ) ∧ (yi ⇒ φi ) ,
1≤i≤n
where the yi ’s are new propositional variables not occurring in any of the φi ’s.
Proof. With equivalences instead of implications (on the right), the given formulae are
obvious. By the usual polarity argument [8] of CNF conversion, the respective direc-
tions of the equivalences can be dropped.
Proof. Trivial.
We now give a CNF encoding for cardinality constraints of the form ≤k (x1 , . . . , xn )
that is based on a sequential
Pi counter circuit. The circuit is shown in Fig. 2 and computes
partial sums si = j=1 xj for increasing values of i upto the final i = n. The values
of all si ’s are represented as unary numbers. The overflow bits vi are set to true if the
partial sum si is greater than k.
xi
x1 x2 xn si-1,1 ≥1 si,1
&
si-1,2 ≥1 si,2
s1,1 s2,1 sn-1,1 si-1,k-1
si-1,k-1
&
s1,k s2,k sn-1,k si-1,k ≥1 si,k
&
v1 v2 vn vi
Fig. 2. Left: Circuit for computing ≤ k (x1 , . . . , xn ). si,j denotes the j-th digit of the i-th partial
sum si in unary representation; variables vi are overflow bits, indicating that the i-th partial sum
is greater than k. Right: Sub-circuit for computing a partial sum si in unary representation.
3
A model for a formula φ is the same as a satisfying assignment of φ, i.e. an assignment to the
variables of φ under which φ evaluates to true.
6
In the following, to avoid special treatment, we tacitly skip the cases k = 0 as well
as n > 1 and always assume k > 0 and n > 1.4
The first partial sum s1 of the sequential counter is easy to compute, it is just equiv-
alent to x1 , thus s1,1 ⇔ x1 and all other digits of s1 are zero. We therefore obtain
s1,j ⇔ ⊥, or simply ¬s1,j , for 1 < j ≤ k. Moreover, overflow bit v1 is never set
(as k > 0), and therefore v1 ⇔ ⊥. At the other end, the last (n-th) partial sum does
not have to be computed, we only need the overflow bit, for which vn ⇔ xn ∧ sn−1,k
holds. For all intermediate values of i, i.e. i ∈ {2, . . . , n − 1}, we have to add xi to the
already computed partial sum si−1 to obtain si . As the partial sums are given in unary
representation, we just have to set the lowest digit that is still zero in si−1 to one in si
if xi is true. Otherwise, i.e. if xi is false, si = si−1 . In Fig. 2, on the right hand side,
we have depicted the sub-circuit that accomplishes this task. It is easily seen that for
xi = 0 the input bits si−1,j are just copied to the next partial sum si . Otherwise the
lowest 0-digit is switched to 1 in si . The sub-circuit to compute si is characterized by
the following set of equivalences:
si,1 ⇔ xi ∨ si−1,1
si,j ⇔ xi ∧ si−1,j−1 ∨ si−1,j for 1 < j ≤ k
vi ⇔ xi ∧ si−1,k
Now, for the cardinality constraint to be fulfilled, no overflows are allowed, and thus
all vi have to be false. This property conceptionally already belongs to the comparator
part of the whole circuit computing the cardinality constraint. We can use the fact that
¬vi for 1 ≤ i ≤ n has to hold to simplify the equivalences defining the counter circuit.
We thus arrive at a set of clauses, call it LTn,kSEQ , defining the cardinality constraint
≤ k (x1 , . . . , xn ) based on the sequential counter with the additional property that no
4
The cardinality constraint for the case k = 0 is easily seen to be equivalent to ¬x1 ∧· · ·∧¬xn ,
which is already in CNF. For n = 0, the constraint is always true. For n = 1 it is true if k > 0,
and otherwise equivalent to (¬x1 ).
7
(¬xn ∨ ¬sn−1,k )
The first two lines represent the case i = 1, the last line the case i = n. Counting the
number of clauses nCL in this set, we arrive at k + 1 clauses altogether for the cases
i = 0 and i = n, and (n − 2) · (2(k − 1) + 3) clauses for the remaining cases, summing
up to nCL = 2nk + n − 3k − 1 clauses. Moreover, apart from the essential xi ’s, the
clause set uses nV = (n − 1) · k auxiliary variables.
Due to its practical importance, we give in brief the clause set LTn,1
SEQ for the case
k = 1 explicitly (as a formula):
^
(¬x1 ∨s1,1 )∧(¬xn ∨¬sn−1,1 )∧ (¬xi ∨si,1 )∧(¬si−1,1 ∨si,1 )∧(¬xi ∨¬si−1,1 )
1<i<n
This clause set consists of 3n − 4 clauses (and n − 1 additional encoding variables) and
is thus—with regard to the number of clauses—superior to the naı̈ve encoding for all
n > 5. The following theorem summarizes our results.
Theorem 1. LTn,k
SEQ is a clausal encoding of ≤ k (x1 , . . . , xn ) requiring O(n · k)
clauses and O(n · k) auxiliary variables.
Proof. We just give an outline of the proof that LTn,k SEQ is a clausal encoding of ≤
k (x1 , . . . , xn ). By induction we show two parts: first, that setting k + 1 variables of the
xi ’s to true leads to a contradiction and, second, that by setting exactly k of the variables
xi to true all auxiliary encoding variables si,j get fixed values. From the second part it
follows that fixing k variables to true does not lead to a contradiction, and thus setting
less than k variables does so, too. The upper bounds on the number of clauses and
variables can be read off directly from the encoding given in this section.
All details of the proof can be found on the Internet under http://www-sr.informatik.
uni-tuebingen.de/˜sinz/CardConstraints.
The encoding LTn,k SEQ also fulfills the efficiency condition given by Bailleux and
Boufkhad [4]. If more than k variables are set to true (which violates the cardinality
constraint ≤ k (x1 , . . . , xn )), this can be detected by unit propagation alone, i.e. by a
linear time decision procedure. This can be seen immediately from the fact that LTn,k SEQ
consists solely of Horn clauses, and sets of Horn clauses are well known to be decidable
by unit propagation. Therefore LTn,k SEQ is decidable in linear time. Another requirement
8
of efficiency as defined by Bailleux and Boufkhad is that, for a partial assignment that
sets k of the variables xi to true, the value of all other xi ’s must be derivable by unit
propagation. That LTn,k
SEQ fulfills this condition, too, can be seen by the argument given
in the proof of Theorem 1: if k of the variables xi are set to true, all auxiliary variables
si,j have fixed values, and thus the remaining xi ’s are forced to be set to false.
We have observed that many cardinality constraints occurring in practice require
only small values of k (compared to n). Take the following as a typical representative
for such a constraint: in product configuration products are typically equipped with
a set of Boolean variables representing the denomination country of the product. Out
of those, exactly one variable has to be selected, making up a cardinality constraint
with k = 1. Thus, LTn,k SEQ can deliver compact encodings for many cases of practical
relevance.
Besides, note that the clause set LTn,k
SEQ can be further simplified by propagating
and thus removing the unit clauses (¬s1,j ) for 1 < j ≤ k. This would result in a further
reduction in the number of clauses and variables that we do not want to elaborate on
here.
∑2m-1 ∑2m-1
ym-1 y1 y0 zm-1 z1 z0
Am-1 A1 A0
∑n
sm sm-1 s1 s0
Fig. 3. Parallel counter according to Muller and Preparata [12] for recursively computing the
number of inputs xi that are set to true (represented as a binary number). The sub-circuits Aj are
1-bit-(full-)adders.
ordinary m-bit adder. This m-bit adder is built from m 1-bit adders. In the case shown
in Fig. 3, where the second half also consists of 2m − 1 inputs, all 1-bit-adders have to
be full-adders. In other cases, where n is not of the form 2m+1 − 1, some of them may
be replaced by half-adders (with only two inputs), as some of the counter’s inputs must
be 0. More exactly, this happens for each 0 in the binary representation of n.
Before we give the defining equations for the basic constituents of the parallel
counter—the 1-bit (full and half) adders—we want to derive upper bounds on the num-
ber of those basic building blocks that are needed for the complete circuit. To estimate
the number of full-adders, Muller and Preparata observe that each full-adder has one
output less than it has inputs, and thus reduces the number of “wires” by one (the half-
adders do not change this number). As we start with n inputs and end with m + 1
outputs, we know that our circuit consists of n − (m + 1) full adders. As noted above,
the number of half adders equals the number of zeros in the binary representation of
n and therefore must be less than or equal to m. Thus our parallel counter possesses
exactly n − blog nc − 1 full-adders and at most blog nc half-adders.
Each half-adder is determined by the following set of equivalences
sout ⇔ a ⊕ b
cout ⇔ a ∧ b ,
where we assume that the half-adder computes a + b and that sout and cout are the
sum resp. carry output bit of the half-adder. The equivalences describing the full-adder,
computing a + b + c, are:
sout ⇔ a ⊕ b ⊕ c
cout ⇔ a ∧ b ∨ c ∧ (a ⊕ b) ,
again with the same meaning of the output bits sout and cout . Dropping once more one
direction of the equivalences, and converting the remaining implications (form “right”
10
for each full-adder. Combining this with the result of the last paragraph, we obtain
as an upper bound on the number of clauses required for the parallel binary counter.
We may further note that the additionally required variables for the parallel counter
are exactly the sum and overflow bits of the half- and full-adders. We therefore need at
most 2 · (n − 1) additional variables.
We still need a comparator circuit as a companion to the parallel counter of the last
section in order to encode a cardinality constraint ≤ k (x1 , . . . , xn ). This comparator
circuit has to make sure that the result sm sm−1 . . . s0 of the binary counter is not greater
than k. For building this binary comparator we assume that the constraint’s limit k is
given as an (m + 1)-bit binary number, say k = km . . . k0 . We can then easily give
recursive equations to generate the clauses for the (m + 1)-bit comparator:
(
{{¬s0 }} if k0 = 0
L(k0 ) =
∅ if k0 = 1
(
{{¬si }} ∪ L(ki−1 . . . k0 ) if ki = 0
L(ki . . . k0 ) =
{{¬si }} ⊗ L(ki−1 . . . k0 ) if ki = 1
L(k0 ) = {{¬s0 }}
L(k1 k0 ) = {{¬s1 }, {¬s0 }}
..
.
L(k4 k3 k2 k1 k0 ) = {{¬s4 , ¬s3 }, {¬s4 , ¬s2 , ¬s1 }, {¬s4 , ¬s2 , ¬s0 }}
It is easily seen that the (m + 1)-bit comparator generates as many clauses as there
are zeros in the (m + 1)-bit binary representation of k. As we may assume k to be
not greater than n (see Lemma 2), we obtain a blog nc upper bound on the number of
clauses required for the comparator.
Combining the results of this paragraph with that of the last one and doing the same
for the clause sets of the parallel counter and the comparator (resulting in the clause set
LTn,k
PAR ), we arrive at a theorem characterizing the encoding of cardinality constraints
with a parallel binary counter:
Proof. Along the lines of the proof for Theorem 1. For details see http://www-sr.infor-
matik.uni-tuebingen.de/˜sinz/CardConstraints.
5 Comparison
In this section we want to compare the two encodings given in this paper with other en-
codings that can be found in the literature. Criteria for assessing the different encodings
are (i) the number of clauses required for the encoding; (ii) the number of additional
propositional variables required for the encoding; and (iii) the time needed to decide
the encoding (as defined in Def. 3). Besides our two encodings, we have included the
naı̈ve encoding (given in the introduction) and the encodings of Bailleux&Boufkhad
and Warners into our comparison.
Encoding #clauses
` n ´ #add. vars decided
Naı̈ve k+1
0 immediate
Sequential unary counter (LTn,k
SEQ ) O(n · k) O(n · k) by unit prop.
Parallel binary counter (LTn,k
PAR ) 7n − 3blog nc − 6 2n − 2 by search
Bailleux & Boufkhad [4] O(n2 ) O(n · log n) by unit prop.
Warners [6] 8n 2n by search
Table 1 shows the results. With respect to the number of clauses required, our en-
coding LTn,k
PAR is the best with a little less than 7n clauses; however, it requires search
12
to check whether the constraint is fulfilled or not. Among the encodings requiring no
search, which are thus decidable in linear time, is that of Bailleux&Boufkhad and our
LTn,k
SEQ encoding. The latter performs better for small values of k, whereas the former
is better for large bounds.
6 Lower Bound
Considering the different encodings given in the last section, the question arises in how
far these results can still be improved. To handle this question, we prove a linear lower
bound on the number of clauses required for any clausal encoding of cardinality con-
straints. This means that no sub-linear (e.g. logarithmic) encoding is possible. So, the
formulae derived in the last section are asymptotically optimal with respect to the num-
ber of clauses (i.e. the encoding problem needs Ω(n) clauses) . However, there is still a
gap remaining, in the sense that our lower bound can only show that at least n clauses
are required, whereas the best encoding still uses almost 7n clauses. We believe, how-
ever, that the upper bound is closer to the exact complexity bound.
We want to show now that every encoding of ≤ k (x1 , . . . , xn ) requires at least n
clauses:
Theorem 3. For all n ∈ N and all k with 0 ≤ k < n − 1, each clausal (CNF) encoding
of ≤ k (x1 , . . . , xn ) requires at least n clauses.
Proof. Our proof runs in two parts: First, we show that each literal ¬xi has to occur
at least once in the clause set. Then, we prove that multiple negative literals ¬xi in the
same clause make additional clauses unavoidable.
Let E be any CNF-encoding of ≤ k (x1 , . . . , xn ). To prove the first part, assume
that for some p with 1 ≤ p ≤ n, literal ¬xp does not occur in the clause set E. We
know that, by definition, every assignment α : {x1 , . . . , xn } → B, in which exactly
k variables are set to 1, can be extended to a satisfying assignment α∗ of E by assign-
ing appropriate values to the auxiliary variables. Take any such assignment α with the
additional property that α(xp ) = 0. Such an α always exists, as k < n. Now, let β
be α altered at point xp (i.e. β(xi ) = α(xi ) for all i 6= p and β(xp ) = 1) and β ∗ the
respectively altered extension. As, by assumption, there is no clause containing ¬xp , all
clauses of E remain satisfied. Thus, β ∗ is a satisfying assignment of E in which k + 1
variables are set to 1. This, however, is a contradiction to our assumption that E is a
clausal encoding of ≤ k (x1 , . . . , xn ).
Now, for the second part of our proof, assume that there is a clause c ∈ E containing
some literals ¬xp and ¬xq (with p 6= q) simultaneously. If there is no such clause, E
must contain at least n clauses, one for each literal ¬xp , and we are done.
Consider an assignment α with α(xp ) = α(xq ) = 0 and exactly k literals set to 1.
As k < n − 1 such an assignment always exists. By definition, α can be extended to a
model α∗ of E. Changing α to β by setting β(xq ) = 1, we arrive at an assignment with
k + 1 variables set to 1 and its extension β ∗ still satisfying clause c. As no extension
of β, and thus neither β ∗ , is a model of E, there must be another clause d ∈ E which
is not satisfied by β ∗ , and thus both ¬xp ∈/ d and ¬xq ∈ d must hold. Thus, we have
shown that for all p, q with 1 ≤ p, q ≤ n and p 6= q there must be at least one clause
13
cq,p that contains ¬xq , but does not contain ¬xp . This requirement can only be met by
at least n clauses.
We want to close this section with two notes: First, note that the case k = n − 1
can be accomplished with less than n clauses. One clause, namely (¬x1 ∨ · · · ∨ ¬xn )
is sufficient. For k = n no restictions apply, and thus no clauses at all are required.
Second, if k 6= 0, it turns out that all clauses in an optimal CNF-encoding of E must be
non-unit clauses. To see this, assume that there exists a unit-clause that is essential, i.e.
contains exactly one of the variables xi and thus cannot be removed. Such a unit-clause
must contain a negative literal (as can be seen from the prove above), thus it must be of
the form (¬xi ). This, however, is a contradiction, as setting xi to true must be allowed
if k > 0.
References
1. Sabin, D., Weigel, R.: Product configuration frameworks – a survey. IEEE Intelligent Sys-
tems 13 (1998) 42–49
2. Küchlin, W., Sinz, C.: Proving consistency assertions for automotive product data manage-
ment. J. Automated Reasoning 24 (2000) 145–163
3. Cabon, B., de Givry, S., Lobjois, L., Schiex, T., Warners, J.P.: Radio link frequency assign-
ment. Constraints 4 (1999) 79–89
4. Bailleux, O., Boufkhad, Y.: Efficient CNF encoding of boolean cardinality constraints. In
Rossi, F., ed.: 9th Intl. Conf. on Principles and Practice of Constraint Programming (CP
2003). Volume 2833 of Lecture Notes in Computer Science., Springer (2003) 108–122
5. Stanica, P.: Good lower and upper bounds on binomial coefficients. Journal of Inequalities
in Pure and Applied Mathematics 2 (2001)
6. Warners, J.P.: A linear-time transformation of linear inequalities into conjunctive normal
form. Inf. Process. Lett. 68 (1998) 63–69
14
7. Nonnengart, A., Rock, G., Weidenbach, C.: On generating small clause normal forms. In
Kirchner, C., Kirchner, H., eds.: 15th Intl. Conf. on Automated Deduction (CADE-15). Vol-
ume 1421 of Lecture Notes in Computer Science., Springer (1998) 397–411
8. Jackson, P., Sheridan, D.: The optimality of a fast CNF conversion and its use with
SAT. Technical Report APES-82-2004, APES Research Group (2004) Available from
http://www.dcs.st-and.ac.uk/ apes/apesreports.html.
9. Tseitin, G.S.: On the complexity of derivation in propositional calculus. In Silenko, A.O.,
ed.: Studies in Constructive Mathematics and Mathematical Logic. (1970) 115–125
10. Murray, N.V.: Completely non-clausal theorem proving. Artif. Intell. 18 (1982) 67–85
11. Plaisted, D.A., Greenbaum, S.: A structure-preserving clause form translation. J. Symb.
Comput. 2 (1986) 293–304
12. Muller, D.E., Preparata, F.P.: Bounds to complexities of networks for sorting and for switch-
ing. J. ACM 22 (1975) 195–201
13. Wegener, I.: The Complexity of Boolean Functions. Wiley-Teubner (1987)
14. Paterson, M., ed.: Boolean Function Complexity. Number 169 in London Mathematical
Society Lecture Note Series. Cambridge University Press (1992)