Encoding cardinality constraints using multiway merge selection networks

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, arc-consistent, 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 Zazon-Ivry 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 odd-even 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 (2-sorters) but more general m-sorters, 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 SAT-solver runtime for many test cases. We prove that the new encoding is competitive to the other state-of-the-art encodings.


Introduction
Several hard decision problems can be efficiently reduced to the Boolean satisfiability (SAT) problem and tried to be solved by recently-developed SAT-solvers. Some of them are for- Michał Karpiński karp@cs.uni.wroc.pl Marek Piotrów mpi@cs.uni.wroc.pl 1 Institute of Computer Science, University of Wrocław, Joliot-Curie 15, 50-383 Wrocław, Poland mulated with the help of different high-level constraints, which should be either encoded into CNF formulas or solved inside a SAT-solver 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 ∈ N. Such cardinality constraints appear naturally in formulations of different real-world 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.

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 odd-even or pairwise comparator networks. Codish and Zazon-Ivry [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íguez-Carbonell [1,2] defined encodings that implemented selection networks based on the odd-even 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.

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 SAT-solvers 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 sub-problems, recursively select elements in them and then merge the selected subsequences using an idea of multi-way 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 (2-sorters) but also m-sorters (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 m-sorter requires 2 m − 1 clauses.
Using this generalized version of comparators we have created a novel class of networks which we call 4-Odd-Even Selection Networks, where the multi-way 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 sub-problems.
Our algorithm is presented using divide-and-conquer 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 4-Odd-Even Merging Network. We compare the numbers of variables and clauses of the encoding and its counterpart: the 2-Odd-Even 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 m-sorters, therefore arc-consistency is preserved [13]. Finally, we present results of our experiments. We show that multi-column 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 state-of-the-art encodings, not only based on comparator networks, but also on binary adders and binary decision diagrams. Those are mainly used in encodings of Pseudo-Boolean 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 k-selection network, then it is also a k -selection network, for any k < k. This property is called incremental strengthening and most state-of-art SAT-solvers provide an interface for doing this.

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 4-Odd-Even Selection Network is given. In Section 4 we compare the encoding produced by our constructions with 2-Odd-Even Selection Networks in terms of number of variables and clauses. Our experimental evaluation is presented in Section 5 followed by conclusions in Section 6.

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 N or the set of binary values {0, 1}. Definition 1 (sequences) A sequence of length n, sayx = x 1 , . . . , x n , is an element of X n . In particular, an element of {0, 1} n is called a binary sequence. We say that a sequencē x ∈ X n is sorted if x i ≥ x i+1 , 1 ≤ i < n. Given two sequencesx = x 1 , . . . , x n and y = y 1 , . . . , y m we define concatenation asx ::ȳ = x 1 , . . . , x n , y 1 , . . . , y m .
A sequencex ∈ 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.

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 (2-sorters) 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 non-increasing 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. Figure 1 is an example of a simple comparator network consisting of 3 comparators. It outputs the maximum from 4 inputs on the top horizontal line, namely,

Example 1
is top k sorted and is a permutation ofx. An m-selector of order m is called a sorting network (or an m-sorter).
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.

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 ∈ N and ∼ belongs to {<, ≤, =, ≥, >}. We will focus on cardinality constraints with less-than 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 k-th 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 (2-sorters), we also use comparators of higher order as building blocks. The m-selector of order n can be encoded as follows: for n input variablesx and m output variablesȳ, we add the set of clauses The m-sorter is an m-selector 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 1-selector 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 2-sorter separately. This produces the clause set 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 1-selector of order 4. This gives the clause set 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.

Arc-consistency
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 SAT-solvers, arc-consistency 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 SAT-solvers, which is an important factor for the Constraint Programming community.
Encodings using selection networks where each m-sorter is encoded as described in the previous sub-section 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 arc-consistent [13].

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 select m k : X m → X m , which is an k-selector 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 k-selector 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 out in which we store all throw-away variables in an arbitrary order. The sequence out is then appended to the output of the algorithm.

4-odd-even selection network
We begin with the top-level algorithm for constructing the 4-Odd-Even Selection Network (Network 1) where we use oe 4merge s k as a black box. It is a 4-merger of order k. We give detailed construction of a 4-merger called 4-Odd-Even Merger in the next sub-section.
The idea we use is the generalization of the one used in 2-Odd-Even Selection Network from [10], which is based on the Odd-Even 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 non-increasing 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 select n 1 , which outputs the maximum of n elements and can be encoded with n clauses.
Example 3 In Fig. 2 we present a schema of 4-Odd-Even Selection Network, which selects 3 largest elements from the input 01100000001. In this example, n = 11, k = 3, n 1 = 5, n 2 = n 3 = n 4 = 2. First, the input is passed to the recursive calls, then the procedure oe 4merge 9 3 is applied (Network 3). Theorem 1 Let n, k ∈ N, such that k ≤ n. Then oe 4sel n k is a k-selection network.
Proof Observe thatȳ =ȳ 1 :: · · · ::ȳ 4 is a permutation of the input sequencex. We prove by induction that for each n, k ∈ N such that 1 ≤ k ≤ n and eachx ∈ {0, 1} n : oe sel n k (x) is top k sorted. If 1 = k ≤ n then oe sel n k = max n , so the theorem is true. For the induction step assume that n ≥ k ≥ 2 and for each (n * , k * ) ≺ (n, k) (in lexicographical order) the theorem holds. We have to prove that the sequencew = pref(k 1 ,ȳ 1 ) :: · · · :: pref(k 4 ,ȳ 4 ) contains k largest elements fromx. If all 1's fromȳ are inw, we are done. So assume that there exists y i j = 1 for some 1 ≤ i ≤ 4, k i < j ≤ n i . We will show that |w| 1 ≥ k. Notice that k i = k, otherwise j > k i = n i -a contradiction. Since |ȳ i | = n i ≤ n 1 < n, from the induction hypothesis we get thatȳ i is top k i sorted. In consequence, each element of pref(k i ,ȳ i ) is greater or equal to y i j , which implies that |pref(k i , y i )| 1 = k i = k. We conclude that |w| 1 ≥ |pref(k i , y i )| 1 = k. Note also that in the case n = k we have all k i = min(n i , k) < k, so the case is correctly reduced.
Finally, using oe merge s k the algorithm returns k largest elements fromx, which completes the proof.

4-odd-even merging network
In this section we give the detailed construction of the network oe 4merge -the 4-Odd-Even Merger -that merges four sequences (columns) obtained from the recursive calls in Network 1. We can assume that input columns are sorted and of length at most k.
The network is presented in Network 3. The input to the procedure is pref(k 1 ,ȳ 1 ), . . ., pref(k 4 ,ȳ 4 ) , where eachȳ 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ā andb that are top k sorted. The sorted prefixes are then combined by oe 4combine into a sorted sequence to which the suffixes ofā andb 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 4-way mergers and each merger consists of two submergers and a combine sub-network. 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 sequencesx = pref(k a ,ā) andȳ = pref(k b ,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 2-Odd-Even Merging Network before the final set of comparators is applied). Sincex can contain up to 4 more 1's thanȳ, it is enough to apply two sets of compara-tors: in the first set compare-and-exchange y i with x i+2 , i = 1, 2, . . . , and in the second one compare-and-exchange even with odd items in the output of the first set. For example, ifx = 1 m+3 0 * andȳ = 1 m 0 * for some m ∈ 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ā = oe 4combine k a +k b k (x,ȳ), 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) 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 4-way mergers instead of odd-even mergers. Fig. 2, in dashed lines, a schema of 4-Odd-Even 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 andw = 100000,x = 111000,ȳ = 100000, z = 100000. Then we haveā = oe 4merge 12 5 (100, 110, 100, 100) = 111110000000, b = oe 4merge 12 3 (000, 100, 000, 000) = 100000000000. The combine operation getsx = pref(5,ā) = 11111 andȳ = pref(3,b) = 100. Notice that |x| 1 − |ȳ| 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 Let k ≥ 1 andx,ȳ ∈ {0, 1} * be a pair of sorted sequences such that k ≤ s = |x| + |ȳ|, |ȳ| ≤ k/2 , |x| ≤ k/2 + 2 and |ȳ| 1 ≤ |x| 1 ≤ |ȳ| 1 + 4. Letā be the output sequence of oe 4combine s k (x,ȳ). Then for any j , 1 ≤ j < s we have a j ≥ a j +1 . Moreover, a is a permutation ofx ::ȳ.
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 ofx ::ȳ :: 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ā :: 0 is a permutation ofb and we are done.
Proof of Theorem 2 Let k ≥ 1 andw,x,ȳ andz be sorted binary sequences such that k ≤ s = |w| + |x| + |ȳ| + |z| and k ≥ |w| ≥ |x| ≥ |ȳ| ≥ |z|. Assume that they are the inputs to the network oe 4merge 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 4merge s k is recursive, so we proceed by induction on s. Observe then that s a , s b < s, since |w| ≥ 2 and |x| ≥ 1. By induction hypothesis,ā is top k a sorted andb is top k b andā ::b is an permutation of the inputs. Letc = oe 4combine k a +k b k (pref(k a ,ā), pref(k b ,b). By previous lemmac is sorted and is a permutation of pref(k a ,ā) :: pref(k b ,b). Thus the output sequencec :: suff(k a + 1,ā) :: suff(k b + 1,b) is a permutation ofw ::x ::ȳ ::z and it remains only to prove that the output is top k sorted.
If suff(k a + 1,ā) :: suff(k b + 1,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 pref(k a ,ā) :: pref(k b ,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.

Comparison of odd-even selection networks
In this section we would like to estimate and compare the number of variables and clauses in encodings based on our algorithm to other encoding based on odd-even selection. Such encoding -which we call 2-Odd-Even Selection Network -was already analyzed by Codish and Zazon-Ivry [10]. We start by counting how many variables and clauses are needed in order to merge 4 sorted sequences returned by recursive calls of 4-Odd-Even Selection Network and 2-Odd-Even Selection Network. Then, based on those values we prove that the overall number of variables and clauses is almost always smaller when using 4-column encoding rather than 2-column encoding. In the next section we show that the new encoding is not just smaller, but also have better solving times in many benchmark instances. For overview of the sizes of other encodings found in the literature see Table 1. 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 ∈ 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 2-comparator uses 2 auxiliary variables and 3 clauses. In case of a 4-comparator the numbers are 4 and 15.
We count how many variables and clauses are needed in order to merge 4 sorted sequences returned by recursive calls of 2-Odd-Even Selection Network and 4-Odd-Even Selection Network, respectively. Two-column selection network using odd-even approach is presented in [10]. We briefly introduce this network with the following three-step recursive procedure (omitting the base case): 1. Split the inputx ∈ {0, 1} n into two sequencesx 1 =x odd andx 2 =x even . 2. Recursively select top k sorted elements fromx 1 and top k sorted elements fromx 2 . 3. Merge the outputs of the previous step using an 2-Odd-Even 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 2-comparators used in the 2-Odd-Even 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 2-comparators (see [10]), which leads to the simple lemma.  We report on the number of new variables that needs to be introduced, the number of generated clauses and whether an encoding achieves some form of arc-consistency Lemma 2 V (oe 2merge 2k k ) = 2k log k + 2, C(oe 2merge 2k k ) = 3k log k + 3.
The schema of this network is presented in Fig. 3. In order to count the number of comparators used in merging 4 sorted sequences we need to expand the recursive step by one level (see Fig. 3b). Now we do the counting for our 4-way merging network based on Network 3.
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 4-comparators. Notice, that the number of 4-comparators 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 number of variables used in the combine network is at most k − 1, because a new variable is not needed for a i , where i > k, because such a i can be replaced by a zero in clauses containing it, and not for a 1 = x 1 . Therefore, the total number of variables is bounded by solution to the following recurrence: where we have k ≤ s = s a + s b ≤ 4k, s b ≤ s a ≤ s b + 4 and k a = min(s a , k/2 + 2) and k b = min(s b , k/2 ). Therefore s/2 ≤ s a ≤ s/2 + 2, s/2 − 2 ≤ s b ≤ s/2, k a ≤ k/2 + 2 and k b ≤ k/2. We claim that B(s, k) ≤ (k − 2)(log s − 2) + 1 4 s − 1. This can be easily verified by induction.
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 + Fig. 3 2-Odd-Even Selection Network 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.
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 Let n, k ∈ N such that 1 ≤ k ≤ n/4 and n and k are both powers of 4. Then: Proof Let dV k = 3V (oe 2merge 2k k ) − V (oe 4merge 4k k ) (from Corollary 1 ), then: The solution to the above recurrence is dsV k (n) ≥ 1 3 ( n k − 1)dV k . Therefore: Similar theorem can be proved for the number of clauses (when k ≥ 8).

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 SAT-solver runtime. In this section we assess how our encoding based on the new family of selection networks affect the performance of a SAT-solver.

Methodology
Our algorithms that encode CNF instances with cardinality constraints into CNFs were implemented as an extension of MINICARD ver. 1.1, created by Mark Liffiton and Jordyn Maglalang. 1

MINICARD uses three types of solvers:
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 / pre-processing.
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 pre-processing 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 SAT-solver. Our extension of MINICARD, which we call KP-MINICARD, is available online. 2 In our evaluation we use the state-of-the-art SAT-solver 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) i7-2600 CPU @ 3.40GHz.
Detailed results are available online. 5 We publish spreadsheets showing running time for each instance, speed-up/slow-down tables for our encodings, number of time-outs met and total running time.

Encodings
We use our multi-column selection network for evaluation -the 4-Odd-Even 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 Pseudo-Boolean constraints to propositional formulas [11]. Since cardinality constraints are a subclass of Pseudo-Boolean 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 Pseudo-Boolean 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 odd-even (2-column) 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].
To sum up, here are the competitors' encodings used in this evaluation: 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 state-of-the-art 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: at-most-one, atmost-k (cardinality constraints) and Pseudo-Boolean constraints. The PBLIB automatically normalizes the input constraints and decides which encoder provides the most effective translation. One of the implemented encodings for at-most-k 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 Pseudo-Boolean solver that translates Pseudo-Boolean constraints to SAT similar to MINISAT+, but which incorporates novel techniques. We have exchanged the SAT-solver 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.

Benchmarks
The set of benchmarks we used is PB15 suite, which is a set of instances from the Pseudo-Boolean Evaluation 2015. 8 One of the categories of the competition was DEC-LIN-32-CARD, 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: Pseudo-Boolean Evaluation assess the state-of-the-art in the field of Pseudo-Boolean solvers. Benchmarks of this competition are widely used in academia as they consist of Fig. 4 The number of solved instances of PB15 suite in given time many diverse instances (artificial and industrial). The 2015 edition was the last one that held a category dedicated specifically to cardinality constraints.

Results
The time-out limit in the SAT-solver was set to 1800 seconds. When comparing two encodings we only considered instances for which at least one achieved the SAT-solver runtime of at least 10% of the time-out limit. All other instances were considered trivial, and therefore were not included in the speed-up/slow-down 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 vice-versa).
In Fig. 4 we present a cactus plot, where x-axis gives the number of solved instances of PB15 suite and the y-axis the time needed to solve them (in seconds) using given encoding. From the plot we can see that the 4OE encoding outperforms all other encodings. Table 2 presents speed-up and slow-down factors for encoding 4OE w.r.t. all other encodings. From the evaluation we can conclude that the best performing encoding is 4OE. From the data presented in Table 2 our encoding achieve better speed-up factor w.r.t. all other encodings. Total running time for 4OE is 629.78 hours on all 2289 instances. All other encodings required more time to finish the computation. Also, 4OE solved the most number of instances -1095. The second to last column of Table 2 shows the difference in total running time of all encodings w.r.t. 4OE (in HH:MM format -hours and minutes). The last column indicates the difference in the number of solved instances of all encodings w.r.t. 4OE (here all instances are counted, even the trivial ones). We can see, for example, that the computations finished about 7 hours sooner for 4OE than CS. This shows that using 4-column selection networks is more desirable than using 2-column selection/sorting networks for encoding cardinality constraints. Encodings CA and CS had the worst performance on PB15 suite. We can also see that even the state-of-the-art constraint solvers have larger running times and solved less instances on this set of benchmarks, as PBE and NPS finished computations more than about 3-4 hours later than 4OE. We count number of benchmarks for which 4OE showed speed-up or slow-down factor with respect to different encodings, the difference in total running time of each encoding w.r.t. 4OE and the difference in the number of solved instances of each encoding w.r.t. 4OE

Conclusions
In this paper we presented a multi-column selection network based on odd-even approach, that can be used to encode cardinality constraints. We showed that its CNF encoding is smaller than the 2-column 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 state-of-the-art 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 speed-up and overall runtime in the SAT-solver performance. On a final note, we would like to point out that the construction can be used to encode Pseudo-Boolean 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 Odd-Even or Pairwise Sorting Networks can lead to increase in the number of solved instances. In fact, we have developed a PBsolver which we call KP-MINISAT+ [14], which uses the algorithm presented in this paper. The code is available online 9 as well as results of the experimental evaluation. 10 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.