1 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 formulated 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 x1 + x2 + … + xnk, where x1, x2, … , xn 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 real-world problems including cumulative scheduling [20], timetabling [3] or formal hardware verification [8].

In a direct encoding of a cardinality constraint x1 + x2 + … + xn < k one can take all subsets of X = {x1, … , xn} 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 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 sub-problems 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 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 2m − 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 x1 + … + xnk 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.

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 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.

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 Xn. 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 xixi+ 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 ≤ abn, 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 ≤ in. 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 kn, if 〈x1, … , xk〉 is sorted and xkxi, 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 (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.

Example 1

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, y1 = max{x1, x2, x3, x4}.

Fig. 1
figure 1

Comparator network

Definition 2 (selection network, m-sorter)

A comparator network \({f^{n}_{k}}\) (where kn) is a k-selection network (or k-selector 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 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.

Definition 3 (m-merger)

A comparator network \({f^{s}_{k}}\) is an m-merger 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 x1 + … + xnk, where \(k \in \mathbb {N}\) and ∼ belongs to {<, ≤, =, ≥, >}. We will focus on cardinality constraints with less-than relation, i.e. x1 + … + xn < 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 yk of the network is forced to be 0 by adding ¬yk as a clause to the formula that encodes x1 + … + xn < 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 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 m-sorter is an m-selector of order m, therefore we need m auxiliary variables and 2m − 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 {z1, z2}, then the entire network can be encoded by encoding each 2-sorter separately. This produces the clause set {x1z1, x2z1, x1x2y2}∪{x3z2, x4z2, x3x4y4}∪{z1y1, z2y1, z1z2y3}. This approach uses 6 auxiliary variables (not counting xi’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 {x1y1, x2y1, x3y1, x4y1}, where we only need 1 additional variable and 4 clauses. Notice that to achieve y1 = max{x1, x2, x3, x4} we are only interested in the value of the top output variable, therefore we do not need to assert other output variables.

2.3 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 x1 + … + xn < k, as soon as k − 1 variables among the xi’s become true, unit propagation sets all other xi’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 ¬yk is added are said to be encoded in a standard way, and it is already known that such encodings are arc-consistent [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 non-trivial operation that we are allowed to use in generalized comparator networks is \({\textit {select}^{m}_{k}} : X^{m} \rightarrow X^{m}\), which is an k-selector of order m (for km ≤ 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 \(\overline {out}\) in which we store all throw-away variables in an arbitrary order. The sequence \(\overline {out}\) is then appended to the output of the algorithm.

3.1 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\_4merg{e^{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 \({\textit {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, n1 = 5, n2 = n3 = n4 = 2. First, the input is passed to the recursive calls, then the procedure \(oe\_4merg{e^{9}_{3}}\) is applied (Network 3).

Fig. 2
figure 2

An example of 4-Odd-Even Selection Network, with n = 11, k = 3, n1 = 5, n2 = n3 = n4 = 2

Theorem 1

Let\(n,k \in \mathbb {N}\), such thatkn. Then\(oe\_4se{l^{n}_{k}}\)isak-selection network.

Proof

Observe that \(\bar {y} = \bar {y}^{1} :: {\dots } :: \bar {y}^{4}\) is a permutation of the input sequence \(\bar {x}\). We prove by induction that for each \(n,k \in \mathbb {N}\) such that 1 ≤ kn and each \(\bar {x} \in \{0,1\}^{n}\): \(oe{\_sel^{n}_{k}}(\bar {x})\) is top k sorted. If 1 = kn then \(oe{\_sel^{n}_{k}} = max^{n}\), so the theorem is true. For the induction step assume that nk ≥ 2 and for each (n, k) ≺ (n, k) (in lexicographical order) the theorem holds. We have to prove that the sequence \(\bar {w} = \text {pref}(k_{1},\bar {y}^{1})::{\dots } ::\text {pref}(k_{4},\bar {y}^{4})\) contains k largest elements from \(\bar {x}\). If all 1’s from \(\bar {y}\) are in \(\bar {w}\), we are done. So assume that there exists \({y^{i}_{j}}= 1\) for some 1 ≤ i ≤ 4, ki < jni. We will show that \(|\bar {w}|_{1} \geq k\). Notice that ki = k, otherwise j > ki = ni – a contradiction. Since \(|\bar {y}^{i}| = n_{i} \leq n_{1} < n\), from the induction hypothesis we get that \(\bar {y}^{i}\) is top ki sorted. In consequence, each element of \(\text {pref}(k_{i},\bar {y}^{i})\) is greater or equal to \({y^{i}_{j}}\), which implies that |pref(ki, yi)|1 = ki = k. We conclude that \(|\bar {w}|_{1} \geq |\text {pref}(k_{i},y^{i})|_{1} = k\). Note also that in the case n = k we have all ki = min(ni, k) < k, so the case is correctly reduced.

figure a

Finally, using \(oe{\_merge^{s}_{k}}\) the algorithm returns k largest elements from \(\bar {x}\), which completes the proof. □

3.2 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.

figure b
figure c

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 4-way mergers and each merger consists of two sub-mergers 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 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 〈x1, y1, x2, y2, …〉 – this operation is also implicitly done in the standard 2-Odd-Even 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 compare-and-exchange yi with xi+ 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, 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 12m+ 101010, therefore we need just one comparator ym+ 1 : xm+ 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, a2i (a2i− 1), i = 1,… is defined by the equation in line 4 (line 5, respectively) of Network 2. They correspond to the two described-above 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) yia2i, (2) xi+ 2a2i, (3) yi− 1xi+ 1a2i, (4) yi− 1xia2i− 1 and (5) yi− 2xi+ 1a2i− 1 or, otherwise: (1) a2iyi− 1xi+ 2, (2) a2iyixi+ 1, (3) a2i− 1xi, (4) a2i− 1yi− 2 and (5) a2i− 1yi− 1xi+ 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.

Example 4

In Fig. 2, in dashed lines, a schema of 4-Odd-Even merger is presented with s = 9, k = 3, k1 = 3 and k2 = k3 = k4 = 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 haveajaj+ 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 a2i = 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))) = a2j+ 1. Consider now an odd j = 2i − 1 for which a2i− 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 a2j. Then the minimum of them will be also an upper bound on a2j.

We have the following inequalities as the consequence of the assumptions: x(l) ≥ y(l) ≥ x(l + 4) for any integer l. Using them and the monotonity of x(i), y(i) and the min/max functions, we have:

  1. (1)

    max(x(i+ 1), y(i− 1)) ≥ max(max(x(i+ 2), y(i)), min(x(i+ 1), y(i− 1))) = a2j,

  2. (2)

    x(i) ≥ max(max(x(i + 2), y(i)), min(x(i + 1), y(i − 1))) = a2j and

  3. (3)

    y(i − 2) ≥ max(max(x(i + 2), y(i)), min(x(i + 1), y(i − 1))) = a2j.

In (2) we use x(i) ≥ x(i + 1) ≥ min(x(i + 1), y(i − 1)). In (3) - the similar ones.

To prove the second part of the lemma let us introduce an intermediate sequence bj, 1 ≤ js + 1 such that b2i = max(x(i + 2), y(i)) and b2i− 1 = min(x(i), y(i − 2) and observe that it is a permutation of \(\bar {x} :: \bar {y} :: 0\), since a pair b2i and b2i+ 3 = min(x(i + 2), y(i)) is a permutation of the pair x(i + 2) and y(i). Now we can write a2i as max(b2j, b2j+ 1) and a2i+ 1 as min(b2j, b2j+ 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 sa, sb < s, since \(|\bar {w}| \geq 2\) and \(|\bar {x}| \geq 1\). By induction hypothesis, \(\bar {a}\) is top ka sorted and \(\bar {b}\) is top kb 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 ck will be 1, and the output will be top k sorted. Observe that ka + kbk, because sbsasb + 4 and sa + sb = sk so sa ≥ ⌈s/2⌉ and sb ≥ ⌈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 non-empty, so ka = ⌊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 ka ≥ ⌈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 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.

Table 1 Comparison of different encodings for cardinality constraints

To simplify the presentation we assume that kn/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 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. 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. 2.

    Recursively select top k sorted elements from \(\bar {x}^{1}\) and top k sorted elements from \(\bar {x}^{2}\).

  3. 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:

$$ |oe\_2se{l^{n}_{k}}| = \left\{ \begin{array}{l l} 2|oe\_2sel^{n/2}_{k}| + |oe\_2merge^{2k}_{k}| & \quad \text{if } k<n\\ |oe\_sort^{k}| & \quad \text{if }k=n \\ |max^{n}| & \quad \text{if }k = 1 \end{array} \right. $$
(1)

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.

Lemma 2

\(V(oe\_2merge^{2k}_{k}) = 2 k \log k + 2\), \(C(oe\_2merge^{2k}_{k}) = 3 k \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).

Fig. 3
figure 3

2-Odd-Even Selection Network

Now we do the counting for our 4-way 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 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 ai, where i > k, because such ai can be replaced by a zero in clauses containing it, and not for a1 = x1. Therefore, the total number of variables is bounded by solution to the following recurrence:

$$ B(s,k) = \left\{\begin{array}{ll} 0 & \quad \text{if } s \leq 4 \\ B(s_{a},k_{a})+B(s_{b},k_{b})+k-1 & \quad \text{otherwise} \end{array}\right. $$

where we have ks = sa + sb ≤ 4k, sbsasb + 4 and ka = min(sa, ⌊k/2⌋ + 2) and kb = min(sb, ⌊k/2⌋). Therefore s/2 ≤ sas/2 + 2, s/2 − 2 ≤ sbs/2, kak/2 + 2 and kbk/2. We claim that \(B(s,k) \leq (k-2)(\log s - 2) + \frac {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 + 3.5. Constant factor 3 is added because additional clauses can be added for values ak+ 1 and ak+ 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 2-column 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 \in \mathbb {N}\)such that 1 ≤ kn/4 andn andk are both powers of 4.Then:

$$ dsV_{k}(n) = V(oe\_2se{l^{n}_{k}}) - V(oe\_4se{l^{n}_{k}}) \geq \frac{(n-k)(5k + 2)}{3k}\log\left( \frac{k}{2}\right) + 3\left( \frac{n}{k}-1\right). $$

Proof

Let \(dV_{k} = 3V(oe\_2merge^{2k}_{k}) - V(oe\_4merge^{4k}_{k})\) (from Corollary 1 ), then:

$$ \begin{array}{@{}rcl@{}} dsV_{k}(n) &=& V(oe\_2se{l^{n}_{k}}) - V(oe\_4se{l^{n}_{k}}) \\ &=& 2V(oe\_2sel^{n/2}_{k}) + V(oe\_2merge^{2k}_{k}) - 4V(oe\_4sel^{n/4}_{k}) - V(oe\_4merge^{4k}_{k}) \\ &=& 4V(oe\_2sel^{n/4}_{k}) + 3V(oe\_2merge^{2k}_{k}) - 4V(oe\_4sel^{n/4}_{k}) - V(oe\_4merge^{4k}_{k}) \\ &=& 4dsV_{k}(n/4) + dV_{k} \end{array} $$

The solution to the above recurrence is \(dsV_{k}(n) \geq \frac {1}{3}(\frac {n}{k}-1)dV_{k}\). Therefore:

$$ \begin{array}{@{}rcl@{}} dsV_{k}(n) &\geq& \frac{1}{3}\left( \frac{n}{k}-1\right)\left( (5k + 2)\log\left( \frac{k}{2}\right) + 9\right) \\ &=&\frac{(n-k)(5k + 2)}{3k}\log\left( \frac{k}{2}\right) + 3\left( \frac{n}{k}-1\right). \end{array} $$

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 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.

5.1 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.Footnote 1MiniCard 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.Footnote 2

In our evaluation we use the state-of-the-art SAT-solver COMiniSatPS by Chanseok OhFootnote 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.Footnote 4 All experiments were carried out on the machines with Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz.

Detailed results are available online.Footnote 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.

5.2 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+Footnote 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:

  • 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 2-Odd-Even 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 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, at-most-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,Footnote 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.

5.3 Benchmarks

The set of benchmarks we used is PB15 suite, which is a set of instances from the Pseudo-Boolean Evaluation 2015.Footnote 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 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 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.

Fig. 4
figure 4

The number of solved instances of PB15 suite in given time

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.

Table 2 Comparison of encodings in terms of SAT-solver runtime on PB15 suite

6 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 PB-solver which we call KP-MiniSat+ [14], which uses the algorithm presented in this paper. The code is available onlineFootnote 9 as well as results of the experimental evaluation.Footnote 10