Encoding cardinality constraints using multiway merge selection networks
 74 Downloads
Abstract
Boolean cardinality constraints (CCs) state that at most (at least, or exactly) k out of n propositional literals can be true. We propose a new, arcconsistent, easy to implement and efficient encoding of CCs based on a new class of selection networks. Several comparator networks have been recently proposed for encoding CCs and experiments have proved their efficiency (Abío et al. 2013, Asín et al. Constraints 12(2): 195–221, 2011, Codish and ZazonIvry 2010, Eén and Sörensson Boolean Modeling and Computation 2: 1–26, 2006). In our construction we use the idea of the multiway merge sorting networks by Lee and Batcher (1995) that generalizes the technique of oddeven sorting ones by merging simultaneously more than two subsequences. The new selection network merges 4 subsequences in that way. Based on this construction, we can encode more efficiently comparators in the combine phase of the network: instead of encoding each comparator separately by 3 clauses and 2 additional variables, we propose an encoding scheme that requires 5 clauses and 2 variables on average for each pair of comparators. We also extend the model of comparator networks so that the basic components are not only comparators (2sorters) but more general msorters, for m ∈ {2, 3, 4}, that can also be encoded efficiently. We show that with small overhead (regarding implementation complexity) we can achieve a significant improvement in SATsolver runtime for many test cases. We prove that the new encoding is competitive to the other stateoftheart encodings.
Keywords
Cardinality constraint Selection network SATsolver SAT encoding SAT translation1 Introduction
Several hard decision problems can be efficiently reduced to the Boolean satisfiability (SAT) problem and tried to be solved by recentlydeveloped SATsolvers. Some of them are formulated with the help of different highlevel constraints, which should be either encoded into CNF formulas or solved inside a SATsolver by a specialized extension. There has been much research on both of these approaches.
In this paper we consider encodings of Boolean cardinality constraints that take the form x_{1} + x_{2} + … + x_{n} ∼ k, where x_{1}, x_{2}, … , x_{n} are Boolean literals (that is, variables or their negations), ∼ is a relation from the set {<, ≤, =, ≥, >} and k ∈ ℕ. Such cardinality constraints appear naturally in formulations of different realworld problems including cumulative scheduling [20], timetabling [3] or formal hardware verification [8].
In a direct encoding of a cardinality constraint x_{1} + x_{2} + … + x_{n} < k one can take all subsets of X = {x_{1}, … , x_{n}} of size k and for each of them construct a CNF formula that states that at least one of the literals in the subset must be false. The direct encoding is quite efficient for very small values of k and n, but for larger parameters another approach should be used.
1.1 Related work
In the last years several selection networks were proposed for encoding cardinality constraints and experiments proved their efficiency. They were based mainly on the oddeven or pairwise comparator networks. Codish and ZazonIvry [10] introduced pairwise selection networks that used the concept of Parberry’s pairwise sorting network [18]. Their construction was later improved by Karpiński and Piotrów [15]. Abío, Asín, Nieuwenhuis, Oliveras and RodríguezCarbonell [1, 2] defined encodings that implemented selection networks based on the oddeven sorting networks by Batcher [6]. In [1] the authors proposed a mixed parametric approach to the encodings, where the direct encoding is chosen for small subproblems and the splitting point is optimized when large problems are divided into two smaller ones. They proposed to minimize the function λ ⋅ num_vars + num_clauses in the encodings. The constructed encodings are small and efficient.
1.2 Our contribution
To improve the existing constructions, we started looking for selection networks that can be easily implemented and encoded with a smaller number of auxiliary variables and, if possible, not much larger number of clauses. In addition, we investigate the influence of our encodings on the execution times of SATsolvers to be sure that the new algorithms can be used in practice. The obtained construction is presented in this paper. The main idea is to split the problem into 4 subproblems, recursively select elements in them and then merge the selected subsequences using an idea of multiway merging. In such a construction, we can encode more efficiently comparators in the combine phase of the merger: instead of encoding each comparator separately by 3 clauses and 2 additional variables. we propose an encoding scheme that requires 5 clauses and 2 variables on average for each pair of comparators. Moreover, in the network we can use not only comparators (2sorters) but also msorters (for m ≤ 4), which can be encoded directly. It should be noted here that the value of m must be small, because the direct encoding of an msorter requires 2^{m} − 1 clauses.
Using this generalized version of comparators we have created a novel class of networks which we call 4OddEven Selection Networks, where the multiway merge sorting networks by Batcher and Lee [7] are generalized in a way that we can recursively select k largest elements from each of the 4 subproblems.
Our algorithm is presented using divideandconquer paradigm. The key to achieve efficient algorithms lies in the construction of networks that combine the results obtained from the recursive calls. The construction of those mergers is one of the main results of this paper. We give a detailed construction for 4OddEven Merging Network. We compare the numbers of variables and clauses of the encoding and its counterpart: the 2OddEven Merging Networks [10]. The calculations show that encodings based on our network use fewer variables and clauses, when k < n.
The construction is parametrized by any values of k and n, so they can be further optimized by mixing them with other constructions. For example, in our experiments we mixed them with the direct encoding for small values of parameters. We used the standard encoding of msorters, therefore arcconsistency is preserved [13]. Finally, we present results of our experiments. We show that multicolumn selection networks are superior to standard selection networks previously proposed in the literature, in context of translating cardinality constraints into propositional formulas.
We also empirically compare our encodings with other stateoftheart encodings, not only based on comparator networks, but also on binary adders and binary decision diagrams. Those are mainly used in encodings of PseudoBoolean constraints, but it is informative to see how well they perform when encoding cardinality constraints.
Developing new methods to encode cardinality constraints based on comparator networks is important from the practical point of view. Using such encodings gives an extra edge in solving optimization problems for which we need to solve a series of problems that differ only in that a bound on cardinality constraint x_{1} + … + x_{n} ≤ k becomes tighter, i.e., by decreasing k to k′. In this setting we only need to add one more clause to assert the constraint < k′, and the computation can be resumed while keeping all the previous clauses untouched. This operation is allowed because if a comparator network is a kselection network, then it is also a k′selection network, for any k′ < k. This property is called incremental strengthening and most stateofart SATsolvers provide an interface for doing this.
1.3 Structure of the paper
The rest of the paper is organized as follows: Section 2 contains definitions and notations used in the paper. In Section 3 the construction of the 4OddEven Selection Network is given. In Section 4 we compare the encoding produced by our constructions with 2OddEven Selection Networks in terms of number of variables and clauses. Our experimental evaluation is presented in Section 5 followed by conclusions in Section 6.
2 Preliminaries
In this section we introduce definitions and notations used in the rest of the paper. Let X denote a totally ordered set, for example the set of natural numbers \(\mathbb {N}\) or the set of binary values {0,1}.
Definition 1 (sequences)
A sequence of length n, say \(\bar {x} = \langle x_{1}, \dots , x_{n} \rangle \), is an element of X^{n}. In particular, an element of {0, 1}^{n} is called a binary sequence. We say that a sequence \(\bar {x} \in X^{n}\) is sorted if x_{i} ≥ x_{i+ 1}, 1 ≤ i < n. Given two sequences \(\bar {x} = \langle {x_{1}, \dots , x_{n}}\rangle \) and \(\bar {y} = \langle y_{1}, \dots , y_{m} \rangle \) we define concatenation as \(\bar {x} :: \bar {y} = \langle x_{1}, \dots , x_{n}, y_{1}, \dots , y_{m} \rangle \).
We use also the following notation: \(\bar {x}_{\textit {odd}} = \langle x_{1}, x_{3}, {\dots } \rangle \), \(\bar {x}_{\textit {even}} = \langle x_{2}, x_{4}, {\dots } \rangle \), \(\bar {x}_{a,\dots ,b} = \langle x_{a}, \dots , x_{b} \rangle \), 1 ≤ a ≤ b ≤ n, and the prefix/suffix operators: \(\text {pref}(i,\bar {x}) = \bar {x}_{1,\dots ,i}\) and \(\text {suff}(i,\bar {x})\) = \(\bar {x}_{i,\dots ,n}\), 1 ≤ i ≤ n. The length of \(\bar {x}\) is denoted by \(\bar {x}\). The number of occurrences of a given value b in \(\bar {x}\) is denoted by \(\bar {x}_{b}\).
A sequence \(\bar {x} \in X^{n}\) is top k sorted, for k ≤ n, if 〈x_{1}, … , x_{k}〉 is sorted and x_{k} ≥ x_{i}, for each i > k.
2.1 Comparator networks
We construct and use comparator networks in this paper. Traditionally comparator networks are presented as circuits that receive n inputs and permute them using comparators (2sorters) connected by ”wires”. Each comparator has two inputs and two outputs. The ”upper” output is the maximum of inputs, and ”lower” one is the minimum. The standard definitions and properties of them can be found, for example, in [16]. The only difference is that we assume that the output of any sorting operation or comparator is in a nonincreasing order.
In the definitions of this section we assume that comparators are functions and comparator networks are composition of comparators. This makes the presentation clear.
Example 1
Definition 2 (selection network, msorter)
A comparator network \({f^{n}_{k}}\) (where k ≤ n) is a kselection network (or kselector of order n), if for each \(\bar {x} \in X^{n}\), \({f^{n}_{k}}(\bar {x})\) is top k sorted and is a permutation of \(\bar {x}\). An mselector of order m is called a sorting network (or an msorter).
The main building blocks of our networks are mergers – networks that output a sorted sequence (or a top k sorted sequence) given outputs of recursive calls.
Definition 3 (mmerger)
A comparator network \({f^{s}_{k}}\) is an mmerger of orderk, if for each tuple \(T = \langle \bar {x}^{1}, \dots , \bar {x}^{m} \rangle \), where each \(\bar {x}^{i}\) is top k sorted and \(s = {\sum }_{i = 1}^{m} \bar {x}^{i}\), \({f^{s}_{k}}(T)\) is top k sorted and is a permutation of \(\bar {x}^{1} :: {\ldots } :: \bar {x}^{m}\).
2.2 Standard encoding of cardinality constraints
A clause is a disjunction of literals (Boolean variables x or their negation ¬x). A CNF formula is a conjunction of clauses. Cardinality constraints are of the form x_{1} + … + x_{n} ∼ k, where \(k \in \mathbb {N}\) and ∼ belongs to {<, ≤, =, ≥, >}. We will focus on cardinality constraints with lessthan relation, i.e. x_{1} + … + x_{n} < k. The other can be easily translated to such form (see [2]).
In [1, 2, 10, 11] authors are using sorting networks to encode cardinality constraints, where inputs and outputs of a comparator are Boolean variables and comparators are encoded as a CNF formula. In addition, the kth greatest output variable y_{k} of the network is forced to be 0 by adding ¬y_{k} as a clause to the formula that encodes x_{1} + … + x_{n} < k. We use similar approach, but rather than using simple comparators (2sorters), we also use comparators of higher order as building blocks. The mselector of order n can be encoded as follows: for n input variables \(\bar {x}\) and m output variables \(\bar {y}\), we add the set of clauses \(\{x_{i_{1}} \wedge {\dots } \wedge x_{i_{p}} \Rightarrow y_{p} : 1 \leq p \leq m, 1 \leq i_{1} < {\dots } < i_{p} \leq n\}\). The msorter is an mselector of order m, therefore we need m auxiliary variables and 2^{m} − 1 clauses to encode it.
Example 2
Assume, that we would like to encode 1selector of order 4 using the network given in Fig. 1. If we name the input variables of the longer comparator as {z_{1}, z_{2}}, then the entire network can be encoded by encoding each 2sorter separately. This produces the clause set {x_{1} ⇒ z_{1}, x_{2} ⇒ z_{1}, x_{1}∧x_{2} ⇒ y_{2}}∪{x_{3} ⇒ z_{2}, x_{4} ⇒ z_{2}, x_{3}∧x_{4} ⇒ y_{4}}∪{z_{1} ⇒ y_{1}, z_{2} ⇒ y_{1}, z_{1}∧z_{2} ⇒ y_{3}}. This approach uses 6 auxiliary variables (not counting x_{i}’s) and 9 clauses. Another way to encode the same network is to simply use a single 1selector of order 4. This gives the clause set {x_{1} ⇒ y_{1}, x_{2} ⇒ y_{1}, x_{3} ⇒ y_{1}, x_{4} ⇒ y_{1}}, where we only need 1 additional variable and 4 clauses. Notice that to achieve y_{1} = max{x_{1}, x_{2}, x_{3}, x_{4}} we are only interested in the value of the top output variable, therefore we do not need to assert other output variables.
2.3 Arcconsistency
Unit Propagation (UP) is a process, that for given CNF formula, clauses are sought in which all literals but one are false (say l) and l is undefined (initially only clauses of size one satisfy this condition). This literal l is set to true and the process is iterated until reaching a fix point.
In the case of cardinality constraints and SATsolvers, arcconsistency states that: for a constraint x_{1} + … + x_{n} < k, as soon as k − 1 variables among the x_{i}’s become true, unit propagation sets all other x_{i}’s to false. This has a positive impact on the practical efficiency of SATsolvers, which is an important factor for the Constraint Programming community.
Encodings using selection networks where each msorter is encoded as described in the previous subsection and additional clause ¬y_{k} is added are said to be encoded in a standard way, and it is already known that such encodings are arcconsistent [13].
3 New selection network
Here we present a novel construction of selection network which uses sorters up to size 4 as components. We want to apply our algorithms for CNF encoding, therefore the only nontrivial operation that we are allowed to use in generalized comparator networks is \({\textit {select}^{m}_{k}} : X^{m} \rightarrow X^{m}\), which is an kselector of order m (for k ≤ m ≤ 4 or k = 1). For the purpose of presentation we use it as a black box, but keep in mind that in the actual implementation one should encode each kselector of order m using the standard procedure explained in Section 2. We would also like to note that only the first k outputs of the selection network is of interest, but to stay consistent with the definitions of Section 2, we write our algorithms so that the output sequence is a permutation of the given input one. To this end we introduce the variable \(\overline {out}\) in which we store all throwaway variables in an arbitrary order. The sequence \(\overline {out}\) is then appended to the output of the algorithm.
3.1 4oddeven selection network
We begin with the toplevel algorithm for constructing the 4OddEven Selection Network (Network 1) where we use \(oe\_4merg{e^{s}_{k}}\) as a black box. It is a 4merger of order k. We give detailed construction of a 4merger called 4OddEven Merger in the next subsection.
The idea we use is the generalization of the one used in 2OddEven Selection Network from [10], which is based on the OddEven Sorting Network by Batcher [6], but we replace the last network with Multiway Merge Sorting Network by Batcher and Lee [7]. We arrange the input sequence into 4 columns of nonincreasing sizes (lines 3–6) and then recursively run the selection algorithm on each column (lines 9–11), where at most top k items are selected from each column. Notice that each column is represented by ranges derived from the increasing value of variable offset. Notice further, that sizes of the columns are selected in such a way that in most cases all but first columns are of equal length and the length is a power of two (lines 3–5) that is close to the value of k/4 (observe that [k/6, k/3) is the smallest symmetric interval around k/4 that contains a power of 2). Such a choice produces much longer propagation paths for small values of k with respect to n. In the recursive calls selected items are sorted and form prefixes of the columns, which are then the input to the merging procedure (line 13). The base case, when k = 1 (line 2), is handled by the auxiliary network \({\textit {select}^{n}_{1}}\), which outputs the maximum of n elements and can be encoded with n clauses.
Example 3
Theorem 1
Let\(n,k \in \mathbb {N}\), such thatk ≤ n. Then\(oe\_4se{l^{n}_{k}}\)isakselection network.
Proof
Finally, using \(oe{\_merge^{s}_{k}}\) the algorithm returns k largest elements from \(\bar {x}\), which completes the proof. □
3.2 4oddeven merging network
The network is presented in Network 3. The input to the procedure is \(\langle \text {pref}(k_{1},\bar {y}^{1})\), …, \(\text {pref}(k_{4},\bar {y}^{4})\rangle \), where each \(\bar {y}^{i}\) is the output of the recursive call in Network 1. The goal is to return the k largest (and sorted) elements. It is done by splitting each input sequence into two parts, one containing elements of odd index, the other containing elements of even index. Odd sequences and even sequences are then recursively merged (lines 5–6) into two sequences \(\bar {a}\) and \(\bar {b}\) that are top k sorted. The sorted prefixes are then combined by oe_4combine into a sorted sequence to which the suffixes of \(\bar {a}\) and \(\bar {b}\) are appended. The result is top k sorted.
Our network is the generalization of the classic Multiway Merge Sorting Network by Batcher and Lee [7], where we use 4way mergers and each merger consists of two submergers and a combine subnetwork. The goal of our network is to select and sort the k largest items of four sorted input sequences. The combine networks are described and analyzed in [7]. The goal of them is to correct a small, unordered part that can appear after zipping the two input sequences \(\bar {x} = \text {pref}(k_{a}, \bar {a})\) and \(\bar {y} = \text {pref}(k_{b},\bar {b})\) (by ”zipping” we mean producing a sequence 〈x_{1}, y_{1}, x_{2}, y_{2}, …〉 – this operation is also implicitly done in the standard 2OddEven Merging Network before the final set of comparators is applied). Since \(\bar {x}\) can contain up to 4 more 1’s than \(\bar {y}\), it is enough to apply two sets of comparators: in the first set compareandexchange y_{i} with x_{i+ 2}, i = 1, 2, … , and in the second one compareandexchange even with odd items in the output of the first set. For example, if \(\bar {x} = 1^{m + 3}0^{*}\) and \(\bar {y} = 1^{m}0^{*}\) for some \(m\in \mathbb {N}\), then after the zip operation, the resulting sequence looks like 1^{2m+ 1}01010^{∗}, therefore we need just one comparator y_{m+ 1} : x_{m+ 3} to fix the order and make the sequence sorted.
Let \(\bar {a} = oe\_4combine^{k_{a}+k_{b}}_{k}(\bar {x}, \bar {y})\), that is, a_{2i} (a_{2i− 1}), i = 1,… is defined by the equation in line 4 (line 5, respectively) of Network 2. They correspond to the two describedabove sets of comparators. In our implementation the equations are encoded into clauses, in such a way, that in average we use 2 new variables and 5 clauses for each pair of comparators: if 1’s should be propagated from inputs to outputs then (1) y_{i} ⇒ a_{2i}, (2) x_{i+ 2} ⇒ a_{2i}, (3) y_{i− 1} ∧ x_{i+ 1} ⇒ a_{2i}, (4) y_{i− 1} ∧ x_{i} ⇒ a_{2i− 1} and (5) y_{i− 2} ∧ x_{i+ 1} ⇒ a_{2i− 1} or, otherwise: (1) a_{2i} ⇒ y_{i− 1} ∨ x_{i+ 2}, (2) a_{2i} ⇒ y_{i} ∨ x_{i+ 1}, (3) a_{2i− 1} ⇒ x_{i}, (4) a_{2i− 1} ⇒ y_{i− 2} and (5) a_{2i− 1} ⇒ y_{i− 1} ∨ x_{i+ 1}. If each comparator is encoded separately, we need 4 new variables and 6 clauses for a pair of comparators. Therefore, we can save about k new variables and k/2 clauses for each \(oe\_4combine^{4k}_{k}\). This is the main advantage of using 4way mergers instead of oddeven mergers.
Example 4
In Fig. 2, in dashed lines, a schema of 4OddEven merger is presented with s = 9, k = 3, k_{1} = 3 and k_{2} = k_{3} = k_{4} = 2. First, the input columns are split into two by odd and even indexes, and the recursive calls are made. After that, a combine operation fixes the order of elements, to output the 3 largest ones. For more detailed example of Network 3, assume that k = 6 and \(\bar {w}= 100000\), \(\bar {x}= 111000\), \(\bar {y}= 100000\), \(\bar {z}= 100000\). Then we have \(\bar {a} = oe\_4merge^{12}_{5}(100,110,100,100)= 111110000000\), \(\bar {b} = oe\_4merge^{12}_{3}(000,100,000,000)\) = 100000000000. The combine operation gets \(\bar {x} = \text {pref}(5, \bar {a}) = 11111\) and \(\bar {y} = \text {pref}(3,\bar {b}) = 100\). Notice that \(\bar {x}_{1}  \bar {y}_{1}= 4\) and after zipping we get 11101011. Thus, two comparators from the first set are needed to fix the order.
Theorem 2
The output of Network 3 is top k sorted.
We start with proving a lemma stating that the result of applying network oe_4combine to any two sequences that satisfy the requrements of the network is sorted and is a permutation of inputs. Then we prove the theorem.
Lemma 1
Letk ≥ 1 and\(\bar {x}, \bar {y} \in \{0,1\}^{*}\)bea pair of sorted sequences such that\(k \leq s = \bar {x} + \bar {y}\), \(\bar {y} \le \left \lfloor k/2 \right \rfloor \), \(\bar {x} \le \left \lfloor k/2 \right \rfloor + 2\)and\(\bar {y}_{1} \le \bar {x}_{1} \le \bar {y}_{1}+ 4\). Let\(\bar {a}\)bethe output sequence of\(oe\_4combin{e^{s}_{k}}(\bar {x}, \bar {y})\). Then for any j, 1 ≤ j < swe havea_{j} ≥ a_{j+ 1}. Moreover,\(\bar {a}\)isa permutation of\(\bar {x} :: \bar {y}\).
Proof
Note first that the notations x(i) and y(i) (introduced in the network) defines monotone sequences that extend the given input sequences \(\bar {x}\) and \(\bar {y}\) (which are sorted). Notice that the inequality is obvious for an even j = 2i, because a_{2i} = max(max(x(i + 2), y(i)), min(x(i+ 1), y(i− 1))) ≥ min(max(x(i+ 2), y(i)), min(x(i+ 1), y(i− 1))) = a_{2j+ 1}. Consider now an odd j = 2i − 1 for which a_{2i− 1} = min(max(x(i + 1), y(i − 1)), min(x(i), y(i − 2))). We would like to show that all three values: (1) max(x(i + 1), y(i − 1)), (2) x(i) and (3) y(i − 2) are upper bounds on a_{2j}. Then the minimum of them will be also an upper bound on a_{2j}.
 (1)
max(x(i+ 1), y(i− 1)) ≥ max(max(x(i+ 2), y(i)), min(x(i+ 1), y(i− 1))) = a_{2j},
 (2)
x(i) ≥ max(max(x(i + 2), y(i)), min(x(i + 1), y(i − 1))) = a_{2j} and
 (3)
y(i − 2) ≥ max(max(x(i + 2), y(i)), min(x(i + 1), y(i − 1))) = a_{2j}.
To prove the second part of the lemma let us introduce an intermediate sequence b_{j}, 1 ≤ j ≤ s + 1 such that b_{2i} = max(x(i + 2), y(i)) and b_{2i− 1} = min(x(i), y(i − 2) and observe that it is a permutation of \(\bar {x} :: \bar {y} :: 0\), since a pair b_{2i} and b_{2i+ 3} = min(x(i + 2), y(i)) is a permutation of the pair x(i + 2) and y(i). Now we can write a_{2i} as max(b_{2j}, b_{2j+ 1}) and a_{2i+ 1} as min(b_{2j}, b_{2j+ 1}), thus the sequence \(\bar {a} :: 0\) is a permutation of \(\bar {b}\) and we are done. □
Proof
of Theorem 2 Let k ≥ 1 and \(\bar {w}\), \(\bar {x}\), \(\bar {y}\) and \(\bar {z}\) be sorted binary sequences such that \(k \le s = \bar {w} + \bar {x} + \bar {y} + \bar {z}\) and \(k \ge \bar {w} \ge \bar {x} \ge \bar {y} \ge \bar {z}\). Assume that they are the inputs to the network \(oe\_4merg{e^{s}_{k}}\), so we can use in the following the variables and sequences defined in it. The two base cases are: (1) all but first sequences are empty, and (2) all sequences contain at most one item. In both of them the network trivially select the top k items. In the other cases the construction of \(oe\_4merg{e^{s}_{k}}\) is recursive, so we proceed by induction on s. Observe then that s_{a}, s_{b} < s, since \(\bar {w} \geq 2\) and \(\bar {x} \geq 1\). By induction hypothesis, \(\bar {a}\) is top k_{a} sorted and \(\bar {b}\) is top k_{b} and \(\bar {a} :: \bar {b}\) is an permutation of the inputs. Let \(\bar {c} = oe\_4combine_{k}^{k_{a} + k_{b}}(\text {pref}(k_{a}, \bar {a}), \text {pref}(k_{b}, \bar {b})\). By previous lemma \(\bar {c}\) is sorted and is a permutation of \(\text {pref}(k_{a}, \bar {a}) :: \text {pref}(k_{b}, \bar {b})\). Thus the output sequence \(\bar {c} :: \text {suff}(k_{a}+ 1, \bar {a}) :: \text {suff}(k_{b}+ 1, \bar {b})\) is a permutation of \(\bar {w} ::\bar {x} :: \bar {y} ::\bar {z}\) and it remains only to prove that the output is top k sorted.
If \(\text {suff}(k_{a}+ 1, \bar {a}) :: \text {suff}(k_{b}+ 1, \bar {b})\) contains just zeroes, there is nothing to prove. Assume then that it contains at least one 1’s. In this case we would like to prove that \(\text {pref}(k_{a}, \bar {a}) :: \text {pref}(k_{b}, \bar {b})\) contains at least k 1’s, thus c_{k} will be 1, and the output will be top k sorted. Observe that k_{a} + k_{b} ≥ k, because s_{b} ≤ s_{a} ≤ s_{b} + 4 and s_{a} + s_{b} = s ≥ k so s_{a} ≥ ⌈s/2⌉ and s_{b} ≥ ⌈s/2⌉ − 2.
It is also true that \(\bar {b}_{1} \leq \bar {a}_{1} \leq \bar {b}_{1} + 4\), because in each sorted input at odd positions there is the same number of 1’s or one more as at even positions. Assume first that \(\text {suff}(k_{a}+ 1, \bar {a})_{1} > 0\). Then the suffix in nonempty, so k_{a} = ⌊k/2⌋ + 2 and \(\text {pref}(k_{a},\bar {a})\) must contain only 1’s, thus \(\bar {a}_{1} \geq k_{a} + 1 = \left \lfloor k/2 \right \rfloor + 3\). It follows that \(\bar {b}_{1} \geq \left \lfloor k/2 \right \rfloor  1\). If \(\text {pref}(k_{b}, \bar {b})\) contains only 1’s then \(\bar {c}\) also consists only of 1’s. Otherwise, the prefix must contain ⌊k/2⌋ − 1 1’s and thus the total number of 1’s in \(\bar {c}\) is at least ⌊k/2⌋ + 2 + ⌊k/2⌋ − 1 ≥ k.
Assume next that \(\text {suff}(k_{b}+ 1, \bar {b})_{1} > 0\). Then \(\text {pref}(k_{b},\bar {b})\) must contain only 1’s and \(\bar {b}_{1} \geq k_{b} + 1 \geq \left \lfloor k/2 \right \rfloor + 1\). Since k_{a} ≥ ⌈k/2⌉ and \(\bar {a}_{1} \geq \bar {b}_{1} \ge \left \lfloor k/2 \right \rfloor + 1\), we get \(\text {pref}(k_{a},\bar {a})_{1} \geq \left \lceil k/2 \right \rceil \) and finaly \(\bar {c}_{1} \geq \left \lfloor k/2 \right \rfloor + \left \lceil k/2 \right \rceil \geq k\). □
4 Comparison of oddeven selection networks
Comparison of different encodings for cardinality constraints
Method  Origin  New vars.  Clauses  AC 

Binomial  folklore  0  \(\binom {n}{k + 1}\)  yes 
Binary  Firsch & Giannaros [12]  O(kn)  \(O(kn \log n)\)  no 
Commander  kn/2  \(\left (\binom {2k + 2}{k + 1}+\binom {2k + 2}{k1}\cdot n/2\right )\)  yes  
Product  \((k + 1)O(\sqrt [k]{n})\)  \((k + 1)(n + O(k\sqrt [k]{n}))\)  yes  
Adders  Warners [22]  2n  8n  no 
Totalizers  Büttner & Rintanen [9]  O(kn)  O(k^{2}n)  yes 
Seq. counter  Sinz [21]  k(n − 1)  2nk + n − 3k − 1  yes 
Par. counter  2n − 2  \(7n3\left \lfloor \log n \right \rfloor 6\)  no  
BDDs  Bailleux et al. [5]  O(n^{2})  O(n^{2})  yes 
Sort. net.  Eén & Sörensson [11]  \(O(n\log ^{2} n)\)  \(O(n\log ^{2} n)\)  yes 
Sel. net.  \(O(n\log ^{2} k)\)  \(O(n\log ^{2} k)\)  yes  
4oe sel. net.  this contribution  \(O(n\log ^{2} k)\)  \(O(n\log ^{2} k)\)  yes 
To simplify the presentation we assume that k ≤ n/4 and both k and n are the powers of 4. We also omit the ceiling and floor function in the calculations, when it is convenient for us.
Definition 4
Let \(n,k \in \mathbb {N}\). For given (selection) network \({f^{n}_{k}}\) let \(V({f^{n}_{k}})\) and \(C({f^{n}_{k}})\) denote the number of variables and clauses used in the standard CNF encoding of \({f^{n}_{k}}\).
We remind the reader that a single 2comparator uses 2 auxiliary variables and 3 clauses. In case of a 4comparator the numbers are 4 and 15.
 1.
Split the input \(\bar {x} \in \{0,1\}^{n}\) into two sequences \(\bar {x}^{1} = \bar {x}_{\textit {odd}}\) and \(\bar {x}^{2} = \bar {x}_{\textit {even}}\).
 2.
Recursively select top k sorted elements from \(\bar {x}^{1}\) and top k sorted elements from \(\bar {x}^{2}\).
 3.
Merge the outputs of the previous step using an 2OddEven Merging Network of order (2k, k) and output the top k from 2k elements.
If we treat the merging step as a network \(oe\_2merge^{2k}_{k}\), then the number of 2comparators used in the 2OddEven Selection Network of order (n, k) can be written as:
One can check that Step 3 requires \(oe\_2merge^{2k}_{k}=k\log k + 1\) 2comparators (see [10]), which leads to the simple lemma.
Lemma 2
\(V(oe\_2merge^{2k}_{k}) = 2 k \log k + 2\), \(C(oe\_2merge^{2k}_{k}) = 3 k \log k + 3\).
Now we do the counting for our 4way merging network based on Network 3.
Lemma 3
Let\(k \in \mathbb {N}\), then:\(V(oe\_4merge^{4k}_{k}) \leq (k  2)\log k + 5k  1\);\(C(oe\_4merge^{4k}_{k})\) ≤ \((\frac {5}{2}k  5)\log k + 21k  6\).
Proof
We separately count the number variables and clauses used.
In the base case (line 2) we can assume – for the sake of the upper bound – that we always use 4comparators. Notice, that the number of 4comparators is only dependent on the variable s. The solution to the following recurrence gives the sought number: {A(4) = 1;A(s) = 2A(s/2),fors > 4}, which is equal to s/4. Therefore we use s auxiliary variables and (15/4)s clauses. We treat the recursive case separately below.
The upper bound of the number of clauses can now be easily computed noticing that in the combine we require either 2 or 3 clauses for each new variable (depending on the parity of the index), therefore the number of clauses in the combiner is bounded by 2.5 × #vars + 3.5. Constant factor 3 is added because additional clauses can be added for values a_{k+ 1} and a_{k+ 2} (see equations in Section 3.2). The overall number of clauses in the merger (omitting base cases) is then at most 2.5 ⋅ B(s, k) + 3.5(k − 1), where factor (k − 1) is the upper bound on the number of combines used in the recursive tree of the merger. Elementary calculations give the desired result. □
Combining Lemmas 2 and 3 gives the following corollary.
Corollary 1
Let\(k \in \mathbb {N}\). Then\(3V(oe\_2merge^{2k}_{k})  V(oe\_4merge^{4k}_{k}) \geq (5k + 2)\log (\frac {k}{2}) + 9 \geq 0\), and fork ≥ 8, \(3C(oe\_2merge^{2k}_{k})  C(oe\_4merge^{4k}_{k}) \geq (\frac {13}{2}k + 5)\log (\frac {k}{8})\frac {3}{2}k\) + 30 ≥ 0.
This shows that using our merging procedure gives a smaller encoding than its 2column counterpart and the differences in the number variables and clauses used is significant.
The main result of this section is as follows.
Theorem 3
Proof
The solution to the above recurrence is \(dsV_{k}(n) \geq \frac {1}{3}(\frac {n}{k}1)dV_{k}\). Therefore:
□
Similar theorem can be proved for the number of clauses (when k ≥ 8).
5 Experimental evaluation
As it was observed in [1], having a smaller encoding in terms of number of variables or clauses is not always beneficial in practice, as it should also be accompanied with a reduction of SATsolver runtime. In this section we assess how our encoding based on the new family of selection networks affect the performance of a SATsolver.
5.1 Methodology

minicard  the core MiniCard solver with native AtMost constraints,

minicard_encodings  a cardinality solver using CNF encodings for AtMost constraints,

minicard_simp_encodings  the above solver with simplification / preprocessing.
The main program in minicard_encodings has an option to generate a CNF formula, given a CNFP instance (CNF with the set of cardinality constraints) and to select a type of encoding applied to cardinality constraints. Program run with this option outputs a CNF instance that consists of collection of the original clauses with the conjunction of CNFs generated by given method for each cardinality constraint. No additional preprocessing and/or simplifications are made. Authors of minicard_encodings have implemented six methods to encode cardinality constraints and arranged them in one library called Encodings.h. Our modification of MiniCard is that we added implementation of the encoding presented in this paper and put it in the library Encodings_MW.h. Then, for each CNFP instance and each encoding method, we used MiniCard to generate CNF instances. After preparing, the benchmarks were run on a different SATsolver. Our extension of MiniCard, which we call KPMiniCard, is available online.^{2}
In our evaluation we use the stateoftheart SATsolver COMiniSatPS by Chanseok Oh^{3} [17], which have collectively won six medals in SAT Competition 2014 and Configurable SAT Solver Challenge 2014. Moreover, the modification of this solver called MapleCOMSPS won the Main Track category of SAT Competition 2016.^{4} All experiments were carried out on the machines with Intel(R) Core(TM) i72600 CPU @ 3.40GHz.
Detailed results are available online.^{5} We publish spreadsheets showing running time for each instance, speedup/slowdown tables for our encodings, number of timeouts met and total running time.
5.2 Encodings
We use our multicolumn selection network for evaluation – the 4OddEven Selection Network (4OE) based on Networks 1, 2 and 3. We compare our encoding to some others found in the literature. We consider the Pairwise Cardinality Networks [10]. We also consider a solver called MiniSat+^{6} which implements techniques to encode PseudoBoolean constraints to propositional formulas [11]. Since cardinality constraints are a subclass of PseudoBoolean constraints, we can measure how well the encodings used in MiniSat+ perform, compared with our methods. The solver chooses between three techniques to generate SAT encodings for PseudoBoolean constraints. These convert the constraint to: a BDD structure, a network of binary adders, a network of sorters. The network of adders is the most concise encoding, but it can have poor propagation properties and often leads to longer computations than the BDD based encoding. The network of sorters is the implementation of classic oddeven (2column) sorting network by Batcher [6]. Calling the solver we can choose the encoding with one of the parameters: ca, cb, cs. By default, MiniSat+ uses the so called Mixed strategy, where program chooses which method (adders, BDDs or sorters) to use in the encodings. We don’t include Mixed strategy in the results, as the evaluation showed that it performs almost the same as cb option. The generated CNFs were written to files with the option cnf=<file>. Solver MiniSat+ have been slightly modified, namely, we fixed a pair of bugs such as the one reported in the experiments section of [4].

PCN  the Pairwise Cardinality Networks (our implementation),

CA  encodings based on Binary Adders (from MiniSat+),

CB  encodings based on Binary Decision Diagrams (from MiniSat+),

CS  the 2OddEven Sorting Networks (from MiniSat+).
Encodings 4OE and PCN were extended, following the idea presented in [1], where authors use Direct Cardinality Networks in their encodings for sufficiently small values of n and k. Values of n and k for which we substitute the recursive calls with Direct Cardinality Network were selected based on the optimization idea in [1]. We minimize the function λ ⋅ V + C, where V is the number of variables and C the number of clauses to determine when to switch to direct networks, and following authors’ experimental findings, we set λ = 5.
Additionally, we compare our encodings with two stateoftheart general purpose constraint solvers. First is the PBLib ver. 1.2.1, by Tobias Philipp and Peter Steinke [19]. This solver implements a plethora of encodings for three types of constraints: atmostone, atmostk (cardinality constraints) and PseudoBoolean constraints. The PBLib automatically normalizes the input constraints and decides which encoder provides the most effective translation. One of the implemented encodings for atmostk constraints is based on the sorting network from the paper by Abío et al. [1]. One part of the PBLib library is the program called PBEncoder which takes an input file and translates it into CNF using the PBLib. We have generated CNF formulas from all benchmark instances using this program, then we have run COMiniSatPS on those CNFs. Results for this method are labeled PBE in our evaluation.
The second solver is the npSolver by Norbert Manthey and Peter Steinke,^{7} which is a PseudoBoolean solver that translates PseudoBoolean constraints to SAT similar to MiniSat+, but which incorporates novel techniques. We have exchanged the SATsolver used by default in npSolver to COMiniSatPS because the results were better with this one. Results for this method are labeled NPS in our evaluation.
5.3 Benchmarks
The set of benchmarks we used is PB15 suite, which is a set of instances from the PseudoBoolean Evaluation 2015.^{8} One of the categories of the competition was DECLIN32CARD, which contains 2289 instances – we use these in our evaluation. Every instance is a collection of cardinality constraints. The motivation for using this set of instances is as follows: PseudoBoolean Evaluation assess the stateoftheart in the field of PseudoBoolean solvers. Benchmarks of this competition are widely used in academia as they consist of many diverse instances (artificial and industrial). The 2015 edition was the last one that held a category dedicated specifically to cardinality constraints.
5.4 Results
The timeout limit in the SATsolver was set to 1800 seconds. When comparing two encodings we only considered instances for which at least one achieved the SATsolver runtime of at least 10% of the timeout limit. All other instances were considered trivial, and therefore were not included in the speedup/slowdown results. We also filtered out instances for which relative percentage deviation of the running time of encoding A w.r.t. the running time of encoding B was less than 10% (and viceversa).
Comparison of encodings in terms of SATsolver runtime on PB15 suite
4OE speedup  4OE slowdown  

TO  4.0  2.0  1.5  1.1  Total  TO  4.0  2.0  1.5  1.1  Total  Time dif.  #s dif.  
PCN  9  11  5  5  5  35  1  2  1  3  3  10  + 02:55  −8 
CA  22  15  28  21  21  107  5  3  5  4  11  28  + 10:54  −17 
CB  18  11  15  8  24  76  7  2  6  3  28  46  + 04:54  −11 
CS  27  13  14  13  18  85  3  0  18  14  13  48  + 06:55  −24 
PBE  15  13  10  10  20  68  6  16  5  6  27  60  + 02:48  −9 
NPS  17  15  7  11  29  67  5  16  6  5  26  58  + 03:51  −12 
6 Conclusions
In this paper we presented a multicolumn selection network based on oddeven approach, that can be used to encode cardinality constraints. We showed that its CNF encoding is smaller than the 2column version. We extended the encoding by applying the Direct Cardinality Networks of [1] for sufficiently small input. The new encoding was compared with the selected stateoftheart encodings based on comparator networks, adders and binary decision diagrams as well as with two popular general constraints solvers. The experimental evaluation shows that the new encoding yields better speedup and overall runtime in the SATsolver performance.
On a final note, we would like to point out that the construction can be used to encode PseudoBoolean constraints, which are more expressive than cardinality constraints. Application of sorting networks in this setup was already reported by Eén and Sörensson [11] and others. Applying our algorithm instead of standard OddEven or Pairwise Sorting Networks can lead to increase in the number of solved instances. In fact, we have developed a PBsolver which we call KPMiniSat+ [14], which uses the algorithm presented in this paper. The code is available online^{9} as well as results of the experimental evaluation.^{10}
Footnotes
Notes
References
 1.Abío, I., Nieuwenhuis, R., Oliveras, A., RodríguezCarbonell, E. (2013). A Parametric Approach for Smaller and Better Encodings of Cardinality Constraints. In Schulte, C. (Ed.) Principles and practice of constraint programming  CP 2013, LNCS, (Vol. 8124 pp. 80–96). Heidelberg: Springer.Google Scholar
 2.Asín, R., Nieuwenhuis, R., Oliveras, A., RodríguezCarbonell, E. (2011). Cardinality networks: a theoretical and empirical study. Constraints, 16(2), 195–221.MathSciNetCrossRefzbMATHGoogle Scholar
 3.Asín, R., & Nieuwenhuis, R. (2014). Curriculumbased course timetabling with SAT and MaxSAT. Annals of Operations Research, 218(1), 71–91.MathSciNetCrossRefzbMATHGoogle Scholar
 4.Aavani, A., Mitchell, D.G., Ternovska, E. (2013). New encoding for translating pseudoBoolean constraints into SAT. In Frisch, A.M., Gregory, P. (eds.) SARA, AAAI.Google Scholar
 5.Bailleux, O., Boufkhad, Y., Roussel, O. (2006). A translation of pseudoboolean constraints to SAT, Journal on Satisfiability. Boolean Modeling and Computation, 2, 191–200.zbMATHGoogle Scholar
 6.Batcher, K. E. (1968). Sorting networks and their applications. In Proceedings of the April 30–May 2, 1968, spring joint computer conference, AFIPS ’68 (Spring) (pp. 307–314). New York: ACM.Google Scholar
 7.Batcher, K. E., & Lee, D. (1995). A multiway merge sorting network. In: IEEE transactions on parallel and distributed systems, Vol. 6, No. 2, pp. 211–215.Google Scholar
 8.Biere, A., Cimatti, A., Clarke, E., Zhu, Y. (1999). Symbolic model checking without BDDs. In Proceedings of 5th international conference on tools and algorithms for the construction and analysis of systems (TACAS’99), LNCS, (Vol. 1579 pp. 193–207). Heidelberg: Springer.Google Scholar
 9.Büttner, M., & Rintanen, J. (2005). Satisfiability planning with constraints on the number of actions. In Proceedings of 15th International Conference on Automated Planning and Scheduling, pp. 292–299.Google Scholar
 10.Codish, M., & ZazonIvry, M. (2010). Pairwise cardinality networks. In Clarke, E., & Voronkov, A. (Eds.) Logic for programming, artificial intelligence, and reasoning, LNCS, (Vol. 6355 pp. 154–172). Heidelberg: Springer.Google Scholar
 11.Eén, N., & Sörensson, N. (2006). Translating PseudoBoolean Constraints into SAT. Journal on Satisfiability. Boolean Modeling and Computation, 2, 1–26.zbMATHGoogle Scholar
 12.Frisch, A.M., & Giannoros, P.A. (2010). SAT encodings of the AtMostk constraint. Some Old, Some New, Some Fast, Some Slow. In Proc. of the 9th Int. workshop of constraint modelling and reformulation.Google Scholar
 13.Karpiński, M. (2018). Encoding cardinality constraints using standard encoding of generalized selection networks preserves arcconsistency. Theoretical Computer Science, 707, 77–81.MathSciNetCrossRefzbMATHGoogle Scholar
 14.Karpiński, M., & Piotrów, M. (2018). Competitive sorterbased encoding of PBconstraints into SAT. To appear in the proceedings of Pragmatics of SAT 2018 Workshop.Google Scholar
 15.Karpiński, M., & Piotrów, M. (2015). Smaller selection networks for cardinality constraints encoding. In Pesant, G. (Ed.) Principles and practice of constraint programming  CP 2015, LNCS, (Vol. 9255 pp. 210–225): Springer International Publishing.Google Scholar
 16.Knuth, D. E. (1998). The art of computer programming, Volume 3: (2Nd Ed.) sorting and searching. Redwood City: Addison Wesley Longman Publishing Co., Inc.Google Scholar
 17.Oh, C.H. (2016). Improving SAT solvers by exploiting empirical characteristics of CDCL. PHD thesis, New York University.Google Scholar
 18.Parberry, I. (1992). The pairwise sorting network. Parallel Processing Letters, 2, 205–211.MathSciNetCrossRefGoogle Scholar
 19.Philipp, T., & Steinke, P. (2015). PBLib – A library for encoding pseudoboolean constraints into CNF. Theory and applications of satisfiability testing – SAT 2015, LNCS, vol. 9340, pp. 9–16. Berlin: Springer International Publishing.Google Scholar
 20.Schutt, A., Feydy, T., Stuckey, P., Wallace, M. (2009). Why cumulative decomposition is not as bad as it sounds. In Gent, I. (Ed.) Principles and practice of constraint programming  CP 2009, LNCS, (Vol. 5732 pp. 746–761). Heidelberg: Springer.Google Scholar
 21.Sinz, C. (2005). Towards an optimal CNF encoding of boolean cardinality constraints. In: Proc. of 11th International Conference on Principles and Practice of Constraint Programming, pp. 827–831. Springer.Google Scholar
 22.Warners, J. P. (1998). A lineartime transformation of linear inequalities into conjunctive normal form. Information Processing Letters, 68, 63–69.MathSciNetCrossRefzbMATHGoogle Scholar
Copyright information
Open Access This article is distributed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits unrestricted use, distribution, and reproduction in any medium, provided you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license, and indicate if changes were made.