the complexity of global cardinality constraints

26
THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS ANDREI A. BULATOV AND D ´ ANIEL MARX School of Computing Science, Simon Fraser University, Burnaby, Canada e-mail address: [email protected] Tel Aviv University, Tel Aviv, Israel e-mail address: [email protected] ABSTRACT. In a constraint satisfaction problem (CSP) the goal is to find an assignment of a given set of variables subject to specified constraints. A global cardinality constraint is an additional require- ment that prescribes how many variables must be assigned a certain value. We study the complexity of the problem CCSP(Γ), the constraint satisfaction problem with global cardinality constraints that allows only relations from the set Γ. The main result of this paper characterizes sets Γ that give rise to problems solvable in polynomial time, and states that the remaining such problems are NP-complete. 1. I NTRODUCTION In a constraint satisfaction problem (CSP) we are given a set of variables, and the goal is to find an assignment of the variables subject to specified constraints, and a constraint is usually expressed as a requirement that combinations of values of a certain (usually small) set of variables belong to a certain relation. CSPs have been intensively studied in both theoretical and practical perspectives. On the theoretical side the key research direction has been the complexity of the CSP when either the interaction of sets constraints are imposed on, that is, the hypergraph formed by these sets, is restricted [17, 18, 19], or restrictions are on the type of allowed relations [22, 10, 6, 7, 2]. In the latter direction the main focus has been on the so called Dichotomy conjecture [15] suggesting that every CSP restricted in this way is either solvable in polynomial time or is NP-complete. This ‘pure’ constraint satisfaction problem is sometimes not enough to model practical prob- lems, as some constraint that have to be satisfied are not ‘local’ in the sense that they cannot be viewed as applied to only a limited number of variables. Constraints of this type are called global. Global constraints are very diverse, the current Global Constraint Catalog (see http://www.emn.fr/x-info/sdemasse/gccat/) lists 313 types of such constraints. In this paper we focus on global cardinality constraints [3, 5, 25]. A global cardinality constraint π is specified for a set of values D and a set of variables V , and is given by a mapping π : D N that assigns a natural number to each element of D such that aD π(a)= |V |. An assignment of variables V satisfies π if for each a D the number of variables that take value a equals π(a). In a CSP with global cardinality constraints, given a CSP instance and a global cardinality constraint π, the goal is to decide if there is a solution of the CSP instance satisfying π. The restricted class 1998 ACM Subject Classification: 68Q25, 68W40. Key words and phrases: constraint satisfaction problem, cardinality constraints, complexity. LOGICAL METHODS IN COMPUTER SCIENCE DOI:10.2168/LMCS-??? c A.Bulatov and D.Marx Creative Commons 1

Upload: others

Post on 03-Apr-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS

ANDREI A. BULATOV AND D ANIEL MARX

School of Computing Science, Simon Fraser University, Burnaby, Canadae-mail address: [email protected]

Tel Aviv University, Tel Aviv, Israele-mail address: [email protected]

ABSTRACT. In a constraint satisfaction problem (CSP) the goal is to find an assignment of a given setof variables subject to specified constraints. A global cardinality constraint is an additional require-ment that prescribes how many variables must be assigned a certain value. We study the complexityof the problemCCSP(Γ), the constraint satisfaction problem with global cardinality constraints thatallows only relations from the setΓ. The main result of this paper characterizes setsΓ that give rise toproblems solvable in polynomial time, and states that the remaining such problems are NP-complete.

1. INTRODUCTION

In a constraint satisfaction problem (CSP) we are given a setof variables, and the goal is to findan assignment of the variables subject to specified constraints, and a constraint is usually expressedas a requirement that combinations of values of a certain (usually small) set of variables belong toa certain relation. CSPs have been intensively studied in both theoretical and practical perspectives.On the theoretical side the key research direction has been the complexity of the CSP when eitherthe interaction of sets constraints are imposed on, that is,the hypergraph formed by these sets, isrestricted [17, 18, 19], or restrictions are on the type of allowed relations [22, 10, 6, 7, 2]. In thelatter direction the main focus has been on the so calledDichotomy conjecture[15] suggesting thatevery CSP restricted in this way is either solvable in polynomial time or is NP-complete.

This ‘pure’ constraint satisfaction problem is sometimes not enough to model practical prob-lems, as some constraint that have to be satisfied are not ‘local’ in the sense that they cannot beviewed as applied to only a limited number of variables. Constraints of this type are calledglobal.Global constraints are very diverse, the current Global Constraint Catalog (seehttp://www.emn.fr/x-info/sdemasse/gccat/) lists 313 types of such constraints. In thispaper we focus onglobal cardinality constraints[3, 5, 25]. A global cardinality constraintπ isspecified for a set of valuesD and a set of variablesV , and is given by a mappingπ : D → N

that assigns a natural number to each element ofD such that∑

a∈D π(a) = |V |. An assignment ofvariablesV satisfiesπ if for eacha ∈ D the number of variables that take valuea equalsπ(a). Ina CSP with global cardinality constraints, given a CSP instance and a global cardinality constraintπ, the goal is to decide if there is a solution of the CSP instance satisfyingπ. The restricted class

1998 ACM Subject Classification:68Q25, 68W40.Key words and phrases:constraint satisfaction problem, cardinality constraints, complexity.

LOGICAL METHODSIN COMPUTER SCIENCE DOI:10.2168/LMCS-???

c© A.Bulatov and D.MarxCreative Commons

1

2 A.BULATOV AND D.MARX

of CSPs with global cardinality constraints such that everyinstance from this class uses only re-lations from a fixed setΓ of relations (such a set is often called aconstraint language) is denotedby CCSP(Γ). We consider the following problem: Characterize constraint languagesΓ such thatCCSP(Γ) is solvable in polynomial time.

The complexity ofCCSP(Γ) has been studied in [13] for constraint languagesΓ on a 2-elementset. It was shown thatCCSP(Γ) is solvable in polynomial time if every relation inΓ is width-2-affine, i.e. it can be expressed as the set of solutions of a system oflinear equations over a 2-elementfield containing at most 2 variables, or, equivalently, using the equality and disequality clauses;otherwise it is NP-complete (we assume P6=NP). In the 2-element caseCCSP(Γ) is also knownas thek-ONES(Γ) problem, since a global cardinality constraint can be expressed by specifyinghow many ones (the set of values is thought to be{0, 1}) one wants to have among the values ofvariables. The parameterized complexity ofk-ONES(Γ) has also been studied [24], wherek is usedas a parameter.

In the case of a 2-element domain, the polynomial classes canbe handled by a standard ap-plication of dynamic programming. Suppose that the instance is given by a set of unary clausesand binary equality/disequality clauses. Consider the graph formed by the binary clauses. Thereare at most two possible assignments for each connected component of the graph: setting the valueof a variable uniquely determines the values of all the othervariables in the component. Thus theproblem is to select one of the two assignments for each component. Trying all possibilities wouldbe exponential in the number of components. Instead, fori = 1, 2, . . . , we compute the setSi of allpossible pairs(x, y) such that there is a partial solution on the firsti components containing exactlyx zeroes and exactlyy ones. It is not difficult to see thatSi+1 can be computed ifSi is alreadyknown.

We generalize the results of [13] for arbitrary finite sets and arbitrary constraint languages. Asusual, the characterization for arbitrary finite sets is significantly more complex and technical thanfor the 2-element set. As a straightforward generalizationof the 2-element case, we can observe thatthe problem is polynomial-time solvable if every relation can be expressed by binary mappings. Inthis case, setting a single value in a component uniquely determines all the values in the component.Therefore, if the domain isD, then there are at most|D| possible assignments in each component,and the same dynamic programming technique can be applied (but this time the setSi contains|D|-tuples instead of pairs).

One might be tempted to guess that the class described in the previous paragraph is the onlyclass whereCCSP is polynomial-time solvable. However, it turns out that there are more generaltractable classes. First, suppose that the domain is partitioned into equivalence classes, and the bi-nary constraints are mappings between the equivalence classes. This means that the values in thesame equivalence class are completely interchangeable. Thus it is sufficient to keep one representa-tive from each class, and then the problem can be solved by thealgorithm sketched in the previousparagraph. Again, one might believe that this constructiongives all the tractable classes, but thefollowing example shows that it does not cover all the tractable cases.

Example 1.1. LetR = {(1, 2, 3), (1, 4, 5), (a, b, c), (d, e, c)}. We claim thatCCSP({R}) is poly-nomial-time solvable. Consider the graph on the variables where two variables are connected ifand only if they appear together in a constraint. As before, for each component, we compute a setcontaining all possible cardinality vectors, and then use dynamic programming. In each component,we have to consider only two cases: either every variable is in {1, 2, 3, 4, 5} or every variable is in{a, b, c, d, e}. If every variable of componentK is in {1, 2, 3, 4, 5}, thenR can be expressed bythe unary constant relation{1}, and the binary relationR′ = {(2, 3), (4, 5)}. The binary relationspartition componentK into sub-componentsK1, . . . ,Kt. SinceR′ is a mapping, there are at most 2

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 3

possible assignments for each sub-component. Thus we can use dynamic programming to computethe set of all possible cardinality vectors onK that use only the values in{1, 2, 3, 4, 5}. If everyvariable ofK is in {a, b, c, d, e}, thenR can be expressed as the unary constant relation{c} and thebinary relationR′′ = {(a, b), (d, e)}. Again, binary relationR′′ partitionsK into sub-components,and we can use dynamic programming on them. Observe that the sub-components formed byR′

and the sub-components formed byR′′ can be different: in the first case,u andv are adjacent ifthey appear in the second and third coordinates of a constraint, while in the second case,u andvare adjacent if they appear in the first and second coordinates of a constraint.

It is not difficult make Example 1.1 more complicated in such away that we have to look atsub-subcomponents and perform multiple levels of dynamic programming. This suggests that itwould be difficult to characterize the tractable relations in a simple combinatorial way.

We give two characterizations of finiteCCSP, one more along the line of the usual approach tothe CSP, using polymorphisms, and another more combinatorial one. The latter is more technical,but it is much more suitable for algorithms.

A polymorphism of a constraint language is an operation thatpreserves every relation from thelanguage. The types of polymorphisms we need here are quite common and have appeared in theliterature many times. A ternary operationm satisfying the equationsm(x, x, y) = m(x, y, x) =m(y, x, x) = x is said to bemajority, and a ternary operationh satisfyingh(x, y, y) = h(y, y, x) =x is said to beMal’tsev. An operation isconservativeif it always takes a value equal to one (notnecessarily the same one) of its arguments.

Theorem 1.2. For a constraint languageΓ, the problemCCSP(Γ) is polynomial time solvable ifand only ifΓ has a majority polymorphism and a conservative Mal’tsev polymorphism. Otherwiseit is NP-complete.

Observe that for constraint languages over a 2-element domain, Theorem 1.2 implies the char-acterization of Creignou et al. [13]. Width-2 affine is equivalent to affine and bijunctive (definablein 2SAT), and over a 2-element domain, affine is equivalent tohaving a conservative Mal’tsev poly-morphism and bijunctive is equivalent to having a majority polymorphism.

The second characterization uses logical definability. Theright generalization of mappings isgiven by the notion of rectangularity. A binary relationR is calledrectangularif (a, c), (a, d), (b, d) ∈R implies (b, c) ∈ R. We say that a pair of equivalence relationsα andβ over the same domaincross, if there is anα-classC and aβ-classD such that neither ofC \D,C∩D, andD\C is empty.A relation is2-decomposableif it can be expressed as a conjunction of binary relations. We denoteby 〈〈Γ〉〉 the set of all relations that are primitive positive definable in Γ. A constraint languageis said to benon-crossing decomposableif every relation from〈〈Γ〉〉 is 2-decomposable, every bi-nary relation from〈〈Γ〉〉 is rectangular, and no pair of equivalence relations from〈〈Γ〉〉 cross. Fordetailed definitions and discussion see Section 2.

Theorem 1.3. For a constraint languageΓ, the problemCCSP(Γ) is polynomial time solvable ifand only ifΓ is non-crossing decomposable. Otherwise it is NP-complete.

The equivalence of the two characterizations will be provedin Section 3.Following [13], we also study the counting problem#CCSP(Γ) corresponding toCCSP(Γ),

in which the objective is to find the number of solutions of a CSP instance that satisfy a globalcardinality constraint specified. Creignou et al. [13] proved that ifΓ is a constraint language on a 2-element set, say,{0, 1}, then#CCSP(Γ) are solvable in polynomial time exactly whenCCSP(Γ)is, that is, if every relation fromΓ is width-2-affine. Otherwise it is P#P-complete.

4 A.BULATOV AND D.MARX

We prove that in the general case as well,#CCSP(Γ) is polynomial time solvable if and onlyif CCSP(Γ) is. However, in this paper we do not prove a complexity dichotomy, as we do notdetermine the exact complexity of the hard counting problems. All such problems are NP-hard asTheorems 1.2 and 1.3 show; and we do not claim that the NP-hardcases are actually P#P-hard.

Theorem 1.4. For a constraint languageΓ, the problem#CCSP(Γ) is polynomial time solvable ifand only ifΓ has a majority polymorphism and a conservative Mal’tsev polymorphism; or, equiva-lently, if and only ifΓ is non-crossing decomposable. Otherwise it is NP-hard.

We also consider the so calledmeta-problemfor CCSP(Γ) and#CCSP(Γ): Suppose setD isfixed. Given a finite constraint languageΓ onD, decide whether or notCCSP(Γ) (and#CCSP(Γ))is solvable in polynomial time. By Theorems 1.2 and 1.4 it suffices to check ifΓ has a majority anda conservative Mal’tsev polymorphism. Since the setD is fixed, this can be done by checking, foreach possible ternary function with the required properties, whether or not it is a polymorphism ofΓ. To check if a ternary operationf is a polymorphism ofΓ one just needs for each relationR ∈ Γto applyf to every triple of tuples inR. This can be done in a time cubic in the total size of relationsin Γ.

Theorem 1.5.LetD be a finite set. The meta-problem forCCSP(Γ) and#CCSP(Γ) is polynomialtime solvable.

Note that Theorem 1.5 does not answer if the meta problem is polynomial time solvable if thesetD is not fixed, and is a part of the input. The algorithm sketchedabove is then super-exponential.

2. PRELIMINARIES

Relations and constraint languages.Our notation concerning tuples and relations is fairly stan-dard. The set of all tuples of elements from a setD is denoted byDn. We denote tuples in boldface,e.g.,a, and their components bya[1],a[2], . . .. An n-ary relation on setD is any subset ofDn.Sometimes we use instead of relationR the corresponding predicateR(x1, . . . , xn). A set of rela-tions onD is called aconstraint languageoverD.

For a subsetI = {i1, . . . , ik} ⊆ {1, . . . , n} with i1 < . . . < ik and ann-tuplea, by prIa wedenote theprojection of a onto I, thek-tuple (a[i1], . . . ,a[ik]). TheprojectionprIR of R is thek-ary relation{prIa | a ∈ R}.

Pairs from equivalence relations play a special role, so such pairs will be denoted by, e.g.,〈a, b〉.If α is an equivalence relation on a setD thenD/α denotes the set ofα-classes, andaα for a ∈ Ddenotes theα-class containinga. We say that the equivalence relationα on a setD is trivial if Dis the onlyα class. Sometimes we need to emphasize that the unary projections pr1R, pr2R of abinary relationR are setsA andB. We denote this byR ⊆ A×B.

Constraint Satisfaction Problem with cardinality constraints. Let D be a finite set andΓ aconstraint language overD. An instance of theConstraint Satisfaction Problem(CSP for short)CSP(Γ) is a pairP = (V, C), whereV is a finite set ofvariablesandC is a set ofconstraints.Every constraint is a pairC = 〈s, R〉 consisting of annC-tuples of variables, called theconstraintscopeand annC-ary relationR ∈ Γ, called theconstraint relation. A solution ofP is a mappingϕ : V → D such that for every constraintC = 〈s, R〉 the tupleϕ(s) belongs toR.

A global cardinality constraintfor a CSP instanceP is a mappingπ : D → N with∑

a∈D π(a) =|V |. A solutionϕ of P satisfies the cardinality constraintπ if the number of variables mapped to

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 5

eacha ∈ D equalsπ(a). The variant ofCSP(Γ) allowing global cardinality constraints will bedenoted byCCSP(Γ); the question is, given an instanceP and a cardinality constraintπ, whetherthere is a solution ofP satisfyingπ.

Example 2.1. If Γ is a constraint language on the 2-element set{0, 1} then to specify a globalcardinality constraint it suffices to specify the number of ones we want to have in a solution. Thisproblem is also known as thek-ONES(Γ) problem [13].

Example 2.2. Let Γ3-COL be the constraint language onD = {0, 1, 2} containing only the bi-nary disequality relation6=. It is straightforward thatCSP(Γ3-COL) is equivalent to the GRAPH 3-COLORABILITY problem. ThereforeCCSP(Γ3-COL) is equivalent to the GRAPH 3-COLORABILITY

problem in which the question is whether there is a coloring with a prescribed number of verticescolored each color.

Sometimes it is convenient to use arithmetic operations on cardinality constraints. Letπ, π′ :D → N be cardinality constraints on a setD, andc ∈ N. Thenπ + π′ andcπ denote cardinalityconstraints given by(π + π′)(a) = π(a) + π′(a) and (cπ)(a) = c · π(a), respectively, for anya ∈ D. Furthermore, we extend addition to setsΠ, Π′ of cardinality vectors in a convolution sense:Π + Π′ is defined as{π + π′ | π ∈ Π, π′ ∈ Π′}.

It is possible to consider an even more general CSP with global cardinality constraints, inwhich every instance ofCSP(Γ) is accompanied with a set of global cardinality constraints, andthe question is whether or not there exists a solution of the CSP instance that satisfies one of thecardinality constraints. Sometimes such a set of cardinality constraints can be represented concisely,for example, all constraintsπ with π(a) = k. We denote suchextended CSP with global cardinalityconstraintscorresponding to a constraint languageΓ by ECCSP(Γ).

Example 2.3. The problemECCSP(Γ3-COL) admits a wide variety of questions, e.g. whether agiven graph admits a 3-coloring with 25 vertices colored 0, and odd number of vertices colored 1.

However, in our setting (as|D| is a fixed constant and we are investigating polynomial-timesolvability) the extended problems are not very interesting from the complexity point of view.

Lemma 2.4. For any constraint languageΓ the problemECCSP(Γ) is Turing reducible toCCSP(Γ).

Proof. SinceD is fixed, for any instanceP of CSP(Γ) there are only polynomially many globalcardinality constraints. Thus we can try each of the cardinality constraints given in an instance ofECCSP(Γ) in turn.

Note that the algorithm in Section 4 forCCSP actually finds the set of all feasible cardinalityconstraints. ThusECCSP can be solved in a more direct way than the reduction in Lemma 2.4.

Primitive positive definitions and polymorphisms. Let Γ be a constraint language on a setD. ArelationR is primitive positive (pp-) definablein Γ if it can be expressed using (a) relations fromΓ,(b) conjunction, (c) existential quantifiers, and (d) the binary equality relations (see, e.g. [14]). Theset of all relations pp-definable inΓ will be denoted by〈〈Γ〉〉.

Example 2.5. An important example of pp-definitions that will be used throughout the paper is theproductof binary relations. LetR,Q be binary relations. ThenR ◦Q is the binary relation given by

(R ◦Q)(x, y) = ∃zR(x, z) ∧Q(z, y).

6 A.BULATOV AND D.MARX

In this paper we will need a slightly weaker notion of definability. We say thatR is pp-definablein Γ without equalitiesif it can be expressed using only items (a)–(c) from above. The set of allrelations pp-definable inΓ without equalities will be denoted by〈〈Γ〉〉′. Clearly, 〈〈Γ〉〉′ ⊆ 〈〈Γ〉〉.The two sets are different only on relations with redundancies. LetR be a (say,n-ary) relation. Aredundancyof R is a pairi, j of its coordinate positions such that, for anya ∈ R, a[i] = a[j].

Example 2.6. In some cases if a relationR has redundancies, the equality relation is pp-definablein {R} without equalities. LetR be a ternary relation onD = {0, 1, 2} given by (triples, membersof the relation, are written vertically)

R =

0 0 1 1 2 20 0 1 1 2 21 2 0 2 0 1

.

Then the equality relation is expressed by∃zR(x, y, z).In other cases the equality relation cannot be expressed that easily, but its restriction onto a

subset ofD can. LetQ be a 4-ary onD = {0, 1, 2} given by

R =

0 0 0 2 2 20 0 0 2 2 21 2 0 2 0 10 0 1 1 2 2

.

Then the formula∃z, tR(x, y, z, t) defines the equality relation on{0, 2}.

Lemma 2.7. For every constraint languageΓ, everyR ∈ 〈〈Γ〉〉 without redundancies belongs to〈〈Γ〉〉′.

Proof. Consider a pp-definition ofR in Γ. Suppose that the definition contains an equality relationon the variablesx andy. If none ofx andy is bound by an existential quantifier, then the relationR has two coordinates that are always equal, i.e.,R is redundant. Thus one of the variables, sayx,is bound by an existential quantifier. In this case, replacing x with y everywhere in the definitiondefines the same relationR and decreases the number of equalities used. Repeating thisstep, wecan arrive to an equality-free definition ofR.

A polymorphismof a (say,n-ary) relationR onD is a mappingf : Dk → D for somek suchthat for any tuplesa1, . . . ,ak ∈ R the tuple

f(a1, . . . ,ak) = (f(a1[1], . . . ,ak[1]), . . . , f(a1[n], . . . ,ak[n]))

belongs toR. Operationf is a polymorphism of a constraint languageΓ if it is a polymorphismof every relation fromΓ. There is a tight connection, aGalois correspondence, between poly-morphisms of a constraint language and relations pp-definable in the language, see [16, 4]. Thisconnection has been extensively exploited to study the ordinary constraint satisfaction problems[22, 10]. Here we do not need the full power of this Galois correspondence, we only need thefollowing folklore result:

Lemma 2.8. If operationf is a polymorphism of a constraint languageΓ, then it is also a polymor-phism of any relation from〈〈Γ〉〉, and therefore of any relation from〈〈Γ〉〉′.

For a (say,n-ary) relationR over a setD and a subsetD′ ⊆ D, byR|D′ we denote the relation{(a1, . . . , an) | (a1, . . . , an) ∈ R anda1, . . . , an ∈ D′}. For a constraint languageΓ overD weuseΓ|D′ to denote the constraint language{R|D′ | R ∈ Γ}.

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 7

If f is a polymorphism of a constraint languageΓ overD andD′ ⊂ D, thenf is not necessarilya polymorphism ofΓ|D′ . However, it remains a polymorphism in the following special case. Ak-ary polymorphismf is conservative,if f(a1, . . . , ak) ∈ {a1, . . . , ak} for everya1, . . . , ak ∈ D. Itis easy to see that iff is a conservative polymorphism ofΓ, thenf is a (conservative) polymorphismof everyΓ|D′ for everyD′ ⊆ D.

Polymorphisms help to express many useful properties of relations. A (say,n-ary) relationRis said to be2-decomposableif a ∈ R if and only if, for anyi, j ∈ {1, . . . , n}, pri,ja ∈ pri,jR,see [1, 21]. Decomposability sometimes is a consequence of the existence of certain polymor-phisms. A ternary operationm on a setD is said to be amajority operationif it satisfies equationsm(x, x, y) = m(x, y, x) = m(y, x, x) = x for all x, y ∈ D. By [1] if a majority operationm is a polymorphism of a constraint languageΓ thenΓ is 2-decomposable. The converse is nottrue: there are 2-decomposable relations not having a majority polymorphism. Furthermore, 2-decomposability is not preserved by pp-definitions, thus wecannot expect to characterize it bypolymorphisms.

Example 2.9. Consider the disequality relation6= over the setD = {1, 2, 3}. Relation6= is trivially2-decomposable, since it is binary. LetR(x, y, z) = ∃q((x 6= q) ∧ (y 6= q) ∧ (z 6= q)). The binaryprojections ofR areD×D, butR is notD×D×D: it does not allow thatx, y, z are all different.

A binary relationR is said to berectangularif for any (a, c), (a, d), (b, d) ∈ R, the pair(b, c)also belongs toR. Rectangular relations and their generalizations play a very important role in thestudy of CSP [8, 9, 20]. Binary rectangular relations can also be represented asthick mappings. Abinary relationR ⊆ A × B is called a thick mapping if there are equivalence relationsα andβ onA andB, respectively, and a one-to-one mappingϕ : A/α → B/β (thus, in particular,|A/α| =

|B/β|) such that(a, b) ∈ R if and only if bβ = ϕ(aα). In this case we shall also say thatR is athick mapping with respect toα, β, andϕ. Given a thick mappingR the corresponding equivalencerelations will be denoted byα1

R andα2R. Observe thatα1

R = R ◦ R−1 andα2R = R−1 ◦ R;

thereforeα1R, α

2R ∈ 〈〈{R}〉〉. Thick mappingR is said to betrivial if both α1

R andα2R are the total

equivalence relations(pr1R)2 and(pr2R)2. In a graph-theoretical point of view, a thick mappingdefines a bipartite graph where every connected component isa complete bipartite graph.

As with decomposability, rectangularity follows from the existence of a certain polymorphism.A ternary operationh is said to beMal’tsev if h(x, x, y) = h(y, x, x) = y for all x, y ∈ D. Thefirst part of following lemma is straightforward, while the second part is folklore

Lemma 2.10. (1) Binary relationR is a thick mapping if and only if it is rectangular.

(2) If a binary relationR has a Mal’tsev polymorphism then it is rectangular.

A ternary operationh satisfying equationsh(x, x, y) = h(x, y, x) = h(y, x, x) = y for allx, y ∈ D is said to be aminority operation. Observe that every Mal’tsev operation is minority, butnot the other way round.

Consistency. Let us fix a constraint languageΓ on a setD and letP = (V, C) be an instance ofCSP(Γ). A partial solutionof P on a set of variablesW ⊆ V is a mappingψ : W → D thatsatisfies every constraint〈W ∩ s,prW∩s

R〉 ∈ C. HereW ∩ s denotes the subtuple ofs consistingof those entries ofs that belong toW , and we consider the coordinate positions ofR indexed byvariables froms. InstanceP is said to bek-consistentif for any k-element setW ⊆ V and anyv ∈ V \W any partial solution onW can be extended to a partial solution onW ∪ {v}, see [21].As we only needk = 2, all further definitions are given under this assumption.

8 A.BULATOV AND D.MARX

Any instanceP = (V, C) can be transformed to a 2-consistent instance by means of thestandard2-CONSISTENCY algorithm. This algorithm works as follows. First, for eachpair v,w ∈ V itcreates a constraint〈(v,w), Rv,w〉whereRv,w is the binary relation consisting of all partial solutionsψ on {v,w}, i.e.Rv,w includes pairsψ(v), ψ(w)). These new constraints are added toC, let theresulting instance be denoted byP ′ = (V, C′). Second, for each pairv,w ∈ V , every partial solutionψ ∈ Rv,w, and everyu ∈ V \{v,w}, the algorithm checks ifψ can be extended to a partial solutionof P ′ on {v,w, u}. If not, it updatesP ′ by removingψ fromRv,w. The algorithm repeats this stepuntil no more changes happen.

Lemma 2.11. LetP = (V, C) be an instance ofCSP(Γ).(a) The problem obtained fromP by applying 2-CONSISTENCY is 2-consistent;(b) On every step of 2-CONSISTENCY for any pairv,w ∈ V the relationRv,w belongs to〈〈Γ〉〉′.

Proof. (a) follows from [12].(b) Since after the first phase of the algorithm every relationRv,w is an intersection of unary and

binary projections of relations fromΓ, it belongs to〈〈Γ〉〉′. Then when considering a pairv,w ∈ Vandu ∈ V \ {v,w}, the relationRv,w is replaced withRv,w ∩ prv,wQ, whereQ is the set of allsolution of the current instance on{v,w, u}. As every relation of the current instance belongs to〈〈Γ〉〉′, the relationQ is pp-definable inΓ without equalities. Thus the updated relationRv,w alsobelongs to〈〈Γ〉〉′.

Note that Theorem 2.11(b) implies that any polymorphism ofΓ is also a polymorphism of everyRv,w.

If Γ has a majority polymorphism, by Theorem 3.5 of [21], every 2-consistent problem instanceis globally consistent, that is every partial solution can be extended to a global solution of theproblem. In particular, ifP is 2-consistent, then for anyv,w ∈ V , any pair(a, b) ∈ Rv,w can beextended to a solution ofP. The same is true for anya ∈ pr1Rv,w and anyb ∈ pr2Rv,w.

3. EQUIVALENCE OF THE CHARACTERIZATIONS

In this section, we prove that the two characterizations in Theorems 1.2 and 1.3 are equivalent.Recall that two equivalence relationsα andβ over the same domaincross, if there is anα-classAand aβ-classB such that neither ofA \B,A ∩B, andB \A is empty. A constraint languageΓ issaid to benon-crossing decomposableif every relation from〈〈Γ〉〉 is 2-decomposable, every binaryrelation from〈〈Γ〉〉 is a thick mapping, and no pair of equivalence relations from〈〈Γ〉〉 cross.

One of the directions is easy to see.

Lemma 3.1. If Γ has a majority polymorphismm and a conservative Mal’tsev polymorphismh,thenΓ is non-crossing decomposable.

Proof. As m is a polymorphism of every relation in〈〈Γ〉〉, by [1] every relation in〈〈Γ〉〉 is 2-decomposable. Similarly, every binary relation in〈〈Γ〉〉 is invariant under the Mal’tsev polymor-phism, which, by Lemma 2.10, implies that the binary relation is a thick mapping.

Finally, suppose that there are two equivalence relationsα, β ∈ 〈〈Γ〉〉 over the same domainD′

that cross. This means that for somea, b, c ∈ D′ we have that〈a, b〉 ∈ α, 〈b, c〉 ∈ β, but 〈a, b〉 6∈ β,〈b, c〉 6∈ α. Let h be a Mal’tsev polymorphism ofΓ, and considerd = h(a, b, c). First of all, ash isconservative,d ∈ {a, b, c}. Then, sinceh preservesα andβ,

h

(

a b ca a c

)

=

(

dc

)

∈ α, h

(

a b ca c c

)

=

(

da

)

∈ β,

which is impossible.

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 9

To prove the other direction of the equivalence, we need to construct the two polymorphisms.The following definition will be useful for this purpose.

Definition 3.2. Given a constraint languageΓ, we say that(a|bc) is true if 〈〈Γ〉〉 contains an equiv-alence relationα with

〈a, a〉, 〈b, b〉〈c, c〉, 〈b, c〉, 〈c, b〉 ∈ α and〈a, b〉, 〈a, c〉, 〈b, a〉, 〈c, a〉 6∈ α.

In other words, the domain of the equivalence relation contains all three elements,b andc arein the same class, buta is in a different class.

Lemma 3.3. Let Γ be a non-crossing decomposable constraint language overD. For everya, b, c ∈D, at most one of(a|bc), (b|ac), (c|ab) is true.

Proof. Suppose that, say, both(a|bc) and(b|ac) are true; letα, β be the corresponding equivalencerelations from〈〈Γ〉〉. LetDα,Dβ be the domains ofα andβ, respectively. We can considerDα andDβ as unary relations, and they are in〈〈Γ〉〉. Therefore,D′ = Dα ∩Dβ andα′ = α ∩ (D′ ×D′)andβ′ = β ∩ (D′ ×D′) are also in〈〈Γ〉〉. As is easily seen,α′ andβ′ are over the same domainD′

and they cross, a contradiction.

Lemma 3.4. Let Γ be a non-crossing decomposable constraint language overD. Let R ∈ 〈〈Γ〉〉be a binary relation such that(a, a′), (b, b′), (c, c′) ∈ R, but (p, q) 6∈ R for somep ∈ {a, b, c} andq ∈ {a′, b′, c′}. Then

(a|bc) ⇐⇒ (a′|b′c′)

(b|ac) ⇐⇒ (b′|a′c′)

(c|ac) ⇐⇒ (c′|a′b′).

Proof. If, say, a andb are in the sameα1R class, thenc has to be in a different class. SinceR is

a thick mapping, this means thata′ andb′ are in the sameα2R class andc′ is in a different class.

Therefore, both(c|ab) and(c′|a′b′) are true, and by Lemma 3.3, none of the other statements canbe true. Therefore, we can assume thata, b, c are in differentα1

R classes, and hencea′, b′, c′ are indifferentα2

R classes.Suppose that(a|bc) is true; letα ∈ 〈〈Γ〉〉 be the corresponding equivalence relation. Con-

sider the relationR′ = α ◦ R, that is,R′(x, y) = ∃z(α(x, z) ∧ R(z, y)), which has to be athick mapping. Letβ1

R′ , β2R′ be the equivalence classes ofR′. We claim that〈b′, c′〉 ∈ β2

R′ and〈a′, b′〉 6∈ β2

R′ , showing that(a′|b′c′) is true. It is clear that〈b′, c′〉 ∈ β2R′ : as(b, b), (b, c) ∈ α, we

have(b, b′), (b, c′) ∈ R′. Since(a, a′), (b, b′) ∈ R′, if 〈a′, b′〉 ∈ β2R′ , then(a, b′), (b, a′) ∈ R′ has

to hold as well (sinceR′ is a thick mapping). That is, there arex, y such that〈a, x〉, 〈b, y〉 ∈ αand(x, b′), (y, a′) ∈ R. Now equivalence relationα shows that(y|ax) is true (since(a|bc) is trueand〈a, x〉, 〈b, y〉 ∈ α and〈b, a〉 6∈ α) and equivalence relationα1

R shows that(x|ay) is true (since(a, a′), (y, a′) ∈ R shows〈a, y〉 ∈ α1

R, (b, b′), (x, b′) ∈ R shows〈b, x〉 ∈ α1R, and we know that

〈a, b〉 6∈ α1R). By Lemma 3.3,(y|ax) and(x|ay) cannot be both true, a contradiction.

Let Γ be a non-crossing decomposable language overD. Let minor(a, b, c) be a if a, b, care all different or all the same, otherwise let it be the value that appears only once amonga, b, c.Similarly, let major(a, b, c) bea if a, b, c are all different or all the same, otherwise let it be the valuethat appears more than once amonga, b, c. Because of Lemma 3.3, the following two functions arewell defined:

10 A.BULATOV AND D.MARX

m(a, b, c) =

b if (a|bc) is true,

a if (b|ac) is true,

b if (c|ab) is true,

major(a, b, c) if none of(a|bc), (b|ac), (c|ab) is true,

h(a, b, c) =

a if (a|bc) is true,

b if (b|ac) is true,

c if (c|ab) is true,

minor(a, b, c) if none of(a|bc), (b|ac), (c|ab) is true,

Lemma 3.5. Operationsm andh are conservative majority and minority operations, respectively,and〈〈Γ〉〉 is invariant underm andh.

Proof. It is clear thatm and h are conservative. To show thatm is a majority operation, it issufficient to consider the case when one of(a|bc), (b|ac), (c|ab) is true. If b = c (resp.,a = c,a = b), then only(a|bc) (resp.,(b|ac), (c|ab)) can be true, which means thatm(a, b, c) = b (resp.,a, b), as required. A similar argument shows thath is a minority function.

Since every relation in〈〈Γ〉〉 is 2-decomposable, it is sufficient to show that every binaryrelation in 〈〈Γ〉〉 is invariant underm and h. We show invariance underh, the proof is simi-lar for h. Let R ∈ 〈〈Γ〉〉 be a binary relation, which is a thick mapping by assumption.Take(a, a′), (b, b′), (c, c′) ∈ R. If a, b, c are in the sameα1

R-class thena′, b′, c′ are in the sameα2R-class.

Sinceh(a, b, c) ∈ {a, b, c} andh(a′, b′, c′) ∈ {a′, b′, c′}, it follows that (h(a, b, c), h(a′ , b′, c′)) ∈R. If a, b, c are not all in the sameα1

R-class, then one of(a|bc), (b|ac), (c|ab) is true, and byLemma 3.4, the corresponding statement from(a′|b′c′), (b′|a′c′), (c′|a′b′) is also true. Now the pair(h(a, b, c), h(a′ , b′, c′)) has to be one of(a, a′), (b, b′), (c, c′), hence it is inR. For the operationmthe argument is similar.

Remark 3.6. Interestingly, Lemma 3.5 gives more than just the existenceof a majority polymor-phism, and a conservative Mal’tsev polymorphism. The operationsm andh are both conservative,andh is a minority operation, not just a Mal’tsev one. Therefore,we have that a constraint lan-guage has a majority and conservative Mal’tsev polymorphisms if and only if it has a majority andminority polymorphisms, both conservative.

The following two consequences of having a conservative Mal’tsev polymorphism will be usedin the algorithm.

Lemma 3.7. Let Γ be a finite constraint language having a conservative Mal’tsev polymorphism.LetR,R′ ∈ 〈〈Γ〉〉 be two nontrivial thick mappings such thatpr2R = pr1R

′. ThenR ◦ R′ is alsonon-trivial.

Proof. SinceR, R′ are nontrivial, there are valuesa, a′, b, b′ such that〈a, a′〉 6∈ α1R, 〈b, b′〉 6∈ α2

R′ .If R ◦ R′ is trivial, then(a, b′), (a, b), (a′, b) ∈ R ◦ R′, which means that there are (not necessarilydistinct) valuesx, y, z such that

(a, x) ∈ R, (x, b′) ∈ R′,

(a, y) ∈ R, (y, b) ∈ R′,

(a′, z) ∈ R, (z, b) ∈ R′.

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 11

Letm be a conservative Mal’tsev polymorphism. Letq = m(x, y, z) ∈ {x, y, z}. Applyingm onthe pairs above, we get that(a′, q) ∈ R and(q, b′) ∈ R′. It is not possible thatq ∈ {x, y}, sincethis would mean that〈a, a′〉 ∈ α1

R. It is not possible thatq = z either, since that would imply〈b, b′〉 ∈ α2

R′ , a contradiction.

Recall that ifα andβ are equivalence relations on the same setS, thenα ∨ β denotes thesmallest (in terms of the number of pairs it contains) equivalence relation onS containing bothαandβ. It is well known that ifα andβ have a Mal’tsev polymorphism then theypermute, that is,α ◦ β = β ◦ α = α ∨ β.

Lemma 3.8. Let Γ be a finite constraint language having a conservative Mal’tsev polymorphism.Let α, β ∈ 〈〈Γ〉〉 be two nontrivial equivalence relations on the same setS. Thenα ∨ β is alsonon-trivial.

Proof. Sinceα ∨ β = α ◦ β, by Lemma 3.7, it is non-trivial.

4. ALGORITHM

In this section we fix a constraint languageΓ that has conservative majority and minority poly-morphisms. We present a polynomial-time algorithm for solving CCSP(Γ) and#CCSP(Γ) in thiscase.

4.1. Prerequisites. In this subsection we prove several properties of instancesof CCSP(Γ) and#CCSP(Γ) that will be very instrumental for our algorithms. First of all, we show that everysuch instance can be supposed binary, that is, that every itsconstraint is imposed only on twovariables. Then we introduce a graph corresponding to such an instance, and show that if thisgraph is disconnected then a solution to the whole problem can be obtained by combining arbitrarilysolutions for the connected components. Finally, if the graph is connected, the set of possible valuesfor each variable can be subdivided into several subsets, sothat if the variable takes a value fromone of the subsets, then each of the remaining variables is forced to take values from a particularsubset of the corresponding partition.

Observe that if a constraint languageΓ satisfy the conditions of Theorem 1.2 then by Re-mark 3.6 the constraint languageΓ′ obtained fromΓ by adding all unary relations also satisfies theconditions of Theorem 1.2. Indeed,Γ has conservative majority and minority polymorphisms thatare also polymorphisms ofΓ′. Therefore we will assume thatΓ contains all unary relations. It willalso be convenient to assume thatΓ contains all the binary relations from〈〈Γ〉〉.

Let Γ be a constraint language and letP = (V, C) be a 2-consistent instance ofCSP(Γ).By bin(P) we denote the instance(V, C′) such thatC′ is the set of all constraints of the form〈(v,w), Rv,w〉 wherev,w ∈ V andRv,w is the set of all partial solutions on{v,w}.

Lemma 4.1. Let Γ be a constraint language with a majority polymorphism. Thenif P is a 2-consistent instance ofCSP(Γ) thenbin(P) has the same solutions asP.

Proof. Let us denote byR,R′ the |V |-ary relation consisting of all solutions ofP and bin(P),respectively. RelationsR,R′ are pp-definable inΓ without equalities, andR ⊆ R′. To show thatR = R′ we use the result from [21] stating that, sinceΓ has a majority polymorphism for anyv,w ∈ V and any(a, b) ∈ Rv,w we have(a, b) ∈ prv,wR, i.e. prv,wR = prv,wR

′. SinceR is2-decomposable, ifa ∈ R′, that is,prv,wa ∈ Rv,w = prv,wR for all v,w ∈ V , thena ∈ R.

12 A.BULATOV AND D.MARX

Let P = (V, C) be an instance ofCSP(Γ). Applying algorithm 2-CONSISTENCY we mayassume thatP is 2-consistent. By the assumption aboutΓ, all constraint relations fromP are 2-decomposable, andbin(P) has the same solutions asP itself. Therefore, replacingP with bin(P),if necessary, every constraint ofP can be assumed to be binary.

Let constraints ofP be 〈(v,w), Rvw〉 for each pair of differentv,w ∈ V . Let Sv, v ∈ V ,denote the set ofa ∈ D such that there is a solutionϕ of P such thatϕ(v) = a. By [21], P isglobally consistent, therefore,Sv = pr1Rvw for anyw ∈ V , w 6= v.

ConstraintCvw = 〈(v,w), Rvw〉 is said to betrivial if Rvw = Sv × Sw, otherwise it is saidto benon-trivial. Thegraph of P, denotedG(P), is a graph with vertex setV and edge setE ={vw | v,w ∈ V andCvw is non-trivial}.

The 2-consistency ofP implies, in particular, the following simple property.

Lemma 4.2. By the 2-consistency ofP, for anyu, v,w ∈ V , Ruv ⊆ Ruw ◦Rwv.

Therefore, by Lemma 3.7, the graphG(P) is transitive, i.e., every connected component is aclique.

If G(P) is not connected, every combination of solutions for its connected components giverise to a solution of the entire problem. More precisely, letV1, . . . , Vk be the connected componentsofG(P), and letP|Vi

denote the instance(Vi, Ci) whereCi includes all the constraints〈(v,w), Rvw〉for whichv,w ∈ Vi.

Lemma 4.3. Let ϕ1, . . . , ϕk be solutions ofP|V1, . . . ,P|Vk

. Then the mappingϕ : V → D suchthatϕ(v) = ϕi(v) wheneverv ∈ Vi is a solution ofP.

Proof. We need to check that all constraints ofP are satisfied. ConsiderCvw = 〈(v,w), Rvw〉. Ifv,w ∈ Vi for a certaini, then(ϕ(v), ϕ(w)) = (ϕi(v), ϕi(w)) ∈ Rvw sinceϕi is a solution toPi. Ifv,w belong to different connected components, thenRvw is trivial, and soCvw is satisfied.

Suppose thatG(P) is connected and fixv ∈ V . In this case, the graph is a clique, and thereforefor anyw ∈ V the constraintCvw is non-trivial. Note that due to 2-consistency, every relation α1

Rvw

for w ∈ V \ {v} is over the same setSv. Setηv =∨

w∈V \{v} α1Rvw

; as everyαRvw is non-trivial,Lemma 3.8 implies thatηv is non-trivial.

Lemma 4.4. SupposeG(P) is connected. Equivalence relationsηv andα1Rvw

(for anyw ∈ V \{v})are non-trivial.

Lemma 4.5. SupposeG(P) is connected.

(1) For anyv,w ∈ V there is a one-to-one correspondenceψvw betweenSv/ηvandSw/ηw

such

that for any solutionϕ of P if ϕ(v) ∈ A ∈ Sv/ηv, thenϕ(w) ∈ ψvw(A) ∈ Sw/ηw

.

(2) The mappingsψvw are consistent, i.e. for anyu, v,w ∈ V we haveψuw(x) = ψvw(ψuv(x)) foreveryx.

Proof. (1) LetRvw be a thick mapping with respect to a mapping%, andα = α1R, α′ = α2

R. Recallthat% is a one-to-one mapping fromSv/α to Sw/α′. Suppose that% does not induce a one-to-onemapping betweenSv/ηv

andSw/ηw. Then without loss of generality there area, b ∈ Sv such that

〈a, b〉 ∈ ηv, but for certaina′, b′ ∈ Sw we have(a, a′), (b, b′) ∈ R and〈a′, b′〉 6∈ ηw. Sinceα′ ⊆ ηw,〈a′, b′〉 6∈ α′, hence〈a, b〉 6∈ α. There isu ∈ V such thatRvu is a thick mapping with respect toβ, β′

and〈a, b〉 ∈ β. Therefore for somec ∈ Su we have(a, c), (b, c) ∈ Rvu. SinceRvu ⊆ Rvw ◦ Rwu,there existd1, d2 ∈ Sw satisfying the conditions(a, d1), (b, d2) ∈ Rvw and(d1, c), (d2, c) ∈ Rwu.The first pair of inclusions imply that〈a′, d1〉, 〈b

′, d2〉 ∈ α′, while the second one implies that〈d1, d2〉 ∈ ηw. Sinceα′ ⊆ ηw, we obtain〈a′, b′〉 ∈ ηw, a contradiction.

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 13

(2) If for someu, v,w ∈ V there is a classA ∈ Su/ηusuch thatψvw(ψuv(A)) 6= ψuw(A) then

Ruw 6⊆ Ruv ◦Rvw, a contradiction.

Fix a variablev0 of P and take aηv0-classA. LetPA = (V, CA) denote the problem instance

over the same variables, where for everyv,w ∈ V the setCA includes the constraint〈(v,w), RAvw〉

RAvw = Rvw ∩ (ψv0v(A) × ψv0w(A)).

Lemma 4.6. ProblemPA belongs toCCSP(Γ).

Proof. It suffices to show thatRAvw ∈ Γ for anyv,w ∈ V . By Lemma 2.11Rvw ∈ 〈〈Γ〉〉, and by

the assumption madeRvw ∈ Γ. Since unary relationsψv0v(A), ψv0w(A) belong toΓ, relationRAvw

is pp-definable inΓ, and, as a binary relation, belongs to it.

4.2. Algorithm: The decision problem. We split the algorithm into two parts. Algorithm CAR-DINALITY (Figure 1) just ensures 2-consistency and initializes a recursive process. The main partof the work is done by EXT-CARDINALITY (Figure 2).

Algorithm EXT-CARDINALITY solves the more general problem of computing the set of allcardinality constraintsπ that can be satisfied by a solution ofP. Thus it can be used to solvedirectly CSP with extended global cardinality constraints, see Preliminaries.

The algorithm considers three cases. Step 2 handles the trivial case when the instance consistsof a single variable and there is only one possible value it can be assigned. Otherwise, we decomposethe instance either by partitioning the variables or by partitioning the domain of the variables. IfG(P) is not connected, then the satisfying assignments ofP can be obtained from the satisfyingassignments of the connected components. Thus a cardinality constraintπ can be satisfied if itarises as the sumπ1 + · · ·+πk of cardinality constraints such that thei-th component has a solutionsatisfyingπi. Instead of considering all such sums (which would not be possible in polynomialtime), we follow the standard dynamic programming approachof going through the componentsone by one, and determining all possible cardinality constraints that can be satisfied by a solutionfor the firsti components (Step 3).

If the graphG(P) is connected, then we fix a variablev0 and go through each classA of thepartitionηv0

(Step 4). Ifv0 is restricted toA, then this implies a restriction for every other variablew. We recursively solve the problem for the restricted instance PA arising for each classA; ifconstraintπ can be satisfied, then it can be satisfied for one of the restricted instances.

The correctness of the algorithm follows from the discussion above. The only point that hasto be verified is that the instance remains 2-consistent after the recursion. This is obvious if werecurse on the connected components (Step 3). In Step 4, 2-consistency follows from the fact that if(a, b) ∈ Rvw can be extended byc ∈ Su, then in every subproblem either these three values satisfythe instance restricted to{v,w, u} or a, b, c do not appear in the domain ofv, w, u, respectively.

To show that the algorithm runs in polynomial time, observe first that every step of the algorithm(except the recursive calls) can be done in polynomial time.Here we use thatD is fixed, hence thesize of the setΠ is polynomially bounded. Thus we only need to bound the size of the recursiontree. If we recurse in Step 3, then we produce instances whosegraphs are connected, thus it cannotbe followed by recursing again in Step 3. In Step 4, the domainof every variable is decreased: byLemma 4.4,ηw is nontrivial for any variablew. Thus in any branch of the recursion tree, recursionin Step 4 can occur at most|D| times, hence the depth of the recursion tree isO(|D|). As thenumber of branches is polynomial in each step, the size of therecursion tree is polynomial.

14 A.BULATOV AND D.MARX

INPUT: An instanceP = (V, C) of CCSP(Γ) with a cardinality constraintπOUTPUT: YES ifP has a solution satisfyingπ, NO otherwise

Step 1. apply 2-CONSISTENCY toPStep 2. setΠ :=EXT-CARDINALITY (P)Step 3. if π ∈ Π output YES

else outputNO

Figure 1: Algorithm CARDINALITY .

INPUT: A 2-consistent instanceP = (V, C) of #CCSP(Γ)OUTPUT: The set of cardinality constraintsπ such thatP has a solution that satisfiesπ

Step 1. construct the graphG(P) = (V,E)Step 2. if |V | = 1 and the domain of this variable is a singleton{a} then doStep 2.1 setΠ := {π} whereπ(x) = 0 except forπ(a) = 1Step 3. else ifG(P) is disconnected andG1 = (V1, E1), . . . , Gk = (Vk, Ek) are

its connected componentsthen doStep 3.1 setΠ := ∅Step 3.2 for i = 1 to k doStep 3.2.1 setΠ := Π + EXT-CARDINALITY (P|Vi

)endfor

endifStep 4. else doStep 4.1 for eachv ∈ V find ηv

Step 4.2 fix v0 ∈ V and setΠ := ∅Step 4.3 for eachηv0

-classA doStep 4.3.1 setPA := (V, CA) where for everyv,w ∈ V the setCA includes

the constraint〈(v,w), Rvw ∩ (ψv0v(A) × ψv0w(A))〉Step 4.3.2 setΠ := Π ∪ EXT-CARDINALITY (PA)

endforenddo

Step 4. output Π

Figure 2: Algorithm EXT-CARDINALITY .

4.3. Solving the counting problem. In this section we observe that algorithm CARDINALITY canbe modified so that it also solves counting CSPs with global cardinality constraints, providedΓsatisfies the conditions of Theorem 1.2.

The counting algorithm works very similar to algorithm CARDINALITY , except that instead ofdetermining the set of satisfiable cardinality constraints, it keeps track of the number of solutionsthat satisfy every cardinality constraint possible. It considers the same 3 cases. In the trivial caseof a problem with one variable and one possible value for thisvariable, the algorithm assigns 1 tothe cardinality constraint satisfied by the only solution ofthe problem and 0 to all other cardinalityconstraints. In the case of disconnected graphG(P) if a cardinality constraint can be representedin the formπ = π1 + . . . + πk, then solutions on the connected components ofG(P) satisfyingπ1, . . . , πk, respectively, contribute the product of their numbers into the number of solutions satis-fied byπ. We again use the dynamic programming approach, and, for each i compute the number

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 15

of solutions onV1 ∪ . . .∪Vi satisfying every possible cardinality constraint. Observe, that the set ofcardinality constraints considered is also changed dynamically, as the number of variables grows.Finally, if G(P) is connected, then the different restrictions have disjoint sets of solutions, hencethe numbers of solutions are computed independently.

INPUT: An instanceP = (V, C) of #CCSP(Γ) with a cardinality constraintπOUTPUT: The number of solutions ofP that satisfyπ

Step 1. apply 2-CONSISTENCY toPStep 2. set% :=#EXT-CARDINALITY (P)

% %(π′) is the number of solutions ofP satisfying cardinality constraintπ′

Step 3. output %(π)

Figure 3: Algorithm #CARDINALITY .

INPUT: A 2-consistent instanceP = (V, C) of CCSP(Γ)OUTPUT: Function% that assigns to every cardinality constraintπ with

a∈D π(a) = |V |,the number%(π) of solutions ofP that satisfyπ

Step 1. construct the graphG(P) = (V,E)Step 2. if |V | = 1 and the domain of this variable is a singleton{a} then doStep 2.1 set%(π) := 1 whereπ(x) = 0 except forπ(a) = 1, and%(π′) := 0 for all π′ 6= π

with∑

x∈D π′(x) = 1

Step 3. else ifG(P) is disconnected andG1 = (V1, E1), . . . , Gk = (Vk, Ek) are itsconnected componentsthen do

Step 3.1 setΠ := {π} whereπ : D → N is given byπ(a) = 0 for a ∈ D, %(π) := 1 for π ∈ ΠStep 3.2 for i = 1 to k doStep 3.2.1 setΠ′ := {π : D → N |

a∈D π(a) = |Vi|} and%′ :=#EXT-CARDINALITY (P|Vi)

Step 3.2.2 setΠ′′ := {π : D → N |∑

a∈D π(a) = |V1| + . . .+ |Vi|}, %′′(π) := 0 for π ∈ Π′′

Step 3.2.3 for eachπ ∈ Π and π′ ∈ Π′ set%′′(π + π′) := %′′(π + π′) + %(π) · %(π′)Step 3.2.4 setΠ := Π′′, % := %′′

endforendif

Step 4. else doStep 4.1 for eachv ∈ V find ηv

Step 4.2 fix v0 ∈ V and set%(π) := 0 for π with∑

a∈D π(a) = |V |Step 4.3 for eachηv0

-classA doStep 4.3.1 setPA := (V, CA) where for everyv,w ∈ V the setCA includes the constraint

〈(v,w), Rvw ∩ (ψv0v(A) × ψv0w(A))〉Step 4.3.2 set%′ :=#EXT-CARDINALITY (PA)Step 4.3.3 set%(π) := %(π) + %′(π)

endforenddo

Step 4. output %

Figure 4: Algorithm #EXT-CARDINALITY .

16 A.BULATOV AND D.MARX

5. DEFINABLE RELATIONS, CONSTANT RELATIONS, AND THE COMPLEXITY OF CCSP

We present two reductions that will be crucial for the proofsin Section 6. In Section 5.1, weshow that adding relations that are pp-definable (without equalities) does not make the problemharder, while in Section 5.2, we show the same for unary constant relations.

5.1. Definable relations and the complexity of cardinality constraints.

Theorem 5.1. Let Γ be a constraint language andR a relation pp-definable inΓ without equalities.ThenCCSP(Γ ∪ {R}) is polynomial time reducible toCCSP(Γ).

Proof. We proceed by induction on the structure of pp-formulas. Thebase case of induction is givenbyR ∈ Γ. We need to consider two cases.

CASE 1. R(x1, . . . , xn) = R1(x1, . . . , xn) ∧R2(x1, . . . , xn).

Observe that by introducing ‘fictitious’ variables for predicatesR1, R2 we may assume thatboth relations involved have the same arity. A reduction from CCSP(Γ ∪ {R}) to CCSP(Γ) istrivial: in a given instance of the first problem replace eachconstraint of the form〈(v1, . . . , vn), R〉with two constraints〈(v1, . . . , vn), R1〉 and〈(v1, . . . , vn), R2〉.

CASE 2. R(x1, . . . , xn) = ∃xR′(x1, . . . , xn, x).

Let P = (V, C) be aCCSP(Γ ∪ {R}) instance. Without loss of generality letC1, . . . , Cq bethe constraints that involveR. InstanceP ′ of CCSP(Γ) is constructed as follows.

• Variables: Replace every variablev from V with a setWv of variables of sizeq|D| andintroduce a set of|D| variables for each constraint involvingR. More formally,

W =⋃

v∈V

Wv ∪ {w1, . . . , wq} ∪

q⋃

i=1

{w1i , . . . , w

|D|−1i }.

• Non-R constraints: For everyCi = 〈(v1, . . . , v`), Q〉 with i > q, introduce all possibleconstraints of the form〈(u1, . . . , u`), Q〉, whereuj ∈Wvj

for j ∈ {1, . . . , `}.• R constraints: For everyCi = 〈(v1, . . . , v`), R〉, i ≤ q, introduce all possible constraints of

the form〈(u1, . . . , u`, wi), R′〉, whereuj ∈Wvj

, j ∈ {1, . . . , `}.

CLAIM 1. If P has a solution satisfying cardinality constraintπ thenP ′ has a solution satisfyingthe cardinality constraintπ′ = q|D| · π + q.

Letϕ be a solution ofP satisfyingπ. It is straightforward to verify that the following mappingψ is a solution ofP ′ and satisfiesπ′:

• for eachv ∈ V and eachu ∈Wv setψ(u) = ϕ(v);• for each wi, where Ci = 〈(v1, . . . , vn), R〉, set ψ(wi) to be a value such that

(ϕ(v1), . . . , ϕ(vn), ψ(wi)) ∈ R′.

• for eachi ≤ q andj ≤ |D|−1 setψ(wji ) to be such that{ψ(wi), ψ(w1

i ), . . . , ψ(w|D|−1|i )} =

D.

CLAIM 2. If P ′ has a solutionψ satisfying the cardinality constraintπ′ = q|D| · π+ q, thenPhas a solution satisfying constraintπ.

Let a ∈ D andUa(ψ) = ψ−1(a) = {u ∈ W | ψ(u) = a}. Observe first that ifϕ : V → Dis a mapping such thatUϕ(v)(ψ) ∩Wv 6= ∅ for everyv ∈ V (i.e.,ψ(v′) = ϕ(v) for at least onevariablev′ ∈Wv), thenϕ satisfies all the constraints ofP. Indeed, consider a constraintC = 〈s, Q〉of P whereQ 6= R. Let s = (v1, . . . , v`). For everyvi, there is av′i ∈ Wvi

such thatϕ(vi) =ψ(v′i). By the wayP ′ is defined, it contains a constraintC ′ = 〈s′, Q〉 wheres

′ = (v′1, . . . , v′`).

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 17

Now the fact thatψ satisfiesC ′ immediately implies thatϕ satisfiesC: (ϕ(v1), . . . , ϕ(v`)) =(ψ(v′1), . . . , ψ(v′`)) ∈ Q . The argument is similar ifQ = R.

We show that it is possible to construct such aϕ that also satisfies the cardinality constraintπ.Since|Wv| = q|D|, for anya ∈ D with π(a) 6= 0, even if setUa(ψ) contains allq|D| variables ofthe formwi andwj

i , it has to intersect at leastπ(a) setsWv (as(π(a) − 1)q|D| + q|D| < π′(a) =π(a) · q|D| + q). Consider the bipartite graphG = (T1 ∪ T2, E), whereT1, T2 is a bipartition and

• T1 is the set of variablesV ;• T2 is constructed from the setD of values by takingπ(a) copies of each valuea ∈ D;• edge(v, a′), wherea′ is a copy ofa from T1, belongs toE if and only ifWv ∩ Ua(ψ) 6= ∅.

Note that|T1| = |T2| and a perfect matchingE′ ⊆ E corresponds to a required mappingϕ:ϕ(v) = a if (v, a′) ∈ E′ for some copya′ or a.

Take any subsetS ⊆ T2, let S contains some copies ofa1, . . . , as. Then by the observationabove,S has at leastπ(a1) + . . . + π(as) neighbours inT1. SinceS contains at mostπ(ai) copiesof ai,

π(a1) + . . . + π(as) ≥ |S|.

By Hall’s Theorem on perfect matchings in bipartite graphs,G has a perfect matching, concludingthe proof that the requiredϕ exists.

5.2. Constant relations and the complexity of cardinality constraints. Let D be a set, and leta ∈ D. Theconstant relationCa is the unary relation that contains only one tuple,(a). If a con-straint languageΓ overD contains all the constant relations, then they can be used inthe correspond-ing constraint satisfaction problem to force certain variables to take some fixed values. The goal ofthis section is to show that for any constraint languageΓ the problemCCSP(Γ ∪ {Ca | a ∈ D})is polynomial time reducible toCCSP(Γ). For the ordinary decision CSP such a reduction existswhenΓ does not have unary polymorphisms that are not permutations, see [10].

LetR be a (say,n-ary) relation on a setD, and letf be a mapping fromD to 2D, the powersetof D. Mappingf is said to be amulti-valued morphismof R if for any tuple(a1, . . . , an) ∈ R thesetf(a1) × . . . × f(an) is a subset ofR. Mappingf is a multi-valued morphism of a constraintlanguageΓ if it is a multi-valued morphism of every relation inΓ. For a multi-valued morphismfand setA ⊆ D, we definef(A) :=

a∈A f(a). The product of two multi-valued morphismsf1

andf2 is defined by(f1 ◦ f2)(a) := f1(f2(a)) for everya ∈ D. We denote byf i thei-th power off , with the convention thatf0 mapsa to {a} for everya ∈ A.

Theorem 5.2. Let Γ be a finite constraint language over a setD. ThenCCSP(Γ ∪ {Ca | a ∈ D})is polynomial time reducible toCCSP(Γ).

Proof. Let D = {d1, . . . , dk} anda = d1. We show thatCCSP(Γ ∪ {Ca}) is polynomial timereducible toCCSP(Γ). This clearly implies the result. We make use of the following multi-valuedmorphism gadgetMVM(Γ, n) (i.e. a CSP instance). Observe that it is somewhat similar totheindicator problem[23].

• The set of variables isV (n) =

k⋃

i=1

Vdi, whereVdi

containsn|D|+1−i elements. All setsVdi

are assumed to be disjoint.• The set of constraints is constructed as follows: For every (say,r-ary) R ∈ Γ and every

(a1, . . . , ar) ∈ R we include all possible constraints of the form〈(v1, . . . , vr), R〉 wherevi ∈ Vdi

for i ∈ {1, . . . , k}.

18 A.BULATOV AND D.MARX

Now, given an instanceP = (V, C) of CCSP(Γ∪{Ca}), we construct an instanceP ′ = (V ′, C′)of CCSP(Γ).

• Let W ⊆ V be the set of variablesv, on which the constant relationCa is imposed, thatis, C contains the constraint〈(v), Ca〉. Setn = |V |. The setV ′ of variables ofP ′ is thedisjoint union of the setV (n) of variables ofMVM(Γ, n) andV \W .

• The setC′ of constraints ofP ′ consists of three parts:(a) C′

1, the constraints ofMVM(Γ, n);(b) C′

2, the constraints ofP that do not include variables fromW ;(c) C′

3, for any constraint〈(v1, . . . , vn), R〉 ∈ C whose scope contains variables con-strained byCa (without loss of generality letv1, . . . , v` be such variables),C′

3 containsall constraints of the form〈(w1, . . . , wk, v`+1, . . . , vn), R〉, wherew1, . . . , w` ∈ Va.

We show thatP has a solution satisfying a cardinality constraintπ if and only if P ′ has asolution satisfying cardinality constraintπ′ given by

π′(di) =

{

π(a) + (|Va| − |W |), if i = 1,π(di) + |Vdi

|, otherwise.

Suppose thatP has a right solutionϕ. Then a required solution forP ′ is given by

ψ(v) =

{

ϕ(v), if v ∈ V \W,di, if v ∈ Vdi

.

It is straightforward thatψ is a solution toP ′ and that it satisfiesπ′.Suppose thatP ′ has a solutionψ that satisfiesπ′. Sinceπ′(a) = π(a)+n|D|−|W | ≥ n|D|−n >

|V ′ \ Va|, there isv ∈ Va such thatψ(v) = a. Thus the assignment

ϕ(v) =

{

ψ(v), if v ∈ V \W,a if v ∈W

is a satisfying assignmentP, but it might not satisfyπ. Our goal is to show thatP ′ has a solutionψ, whereϕ obtained this way satisfiesπ. Observe that what we need is that inψ valuedi appearson exactlyπ′(di) − |Vdi

| variables ofV \W .

CLAIM 1. Mappingf taking everydi ∈ D to the set{ψ(v) | v ∈ Vdi} is a multi-valued

morphism ofΓ.

Indeed, let(a1, . . . , an) ∈ R, R is an (n-ary) relation fromΓ. Then by the construction ofMVM(Γ, n) the instance contains all the constraints of the form〈(v1, . . . , vn), R〉 with vi ∈ Vai

,i ∈ {1, . . . , n}. Therefore,

{ψ(v1) | v1 ∈ Va1} × . . . × {ψ(vn) | vn ∈ Van}

= f(a1) × . . . × f(an) ⊆ R.

CLAIM 2. Letf be the mapping defined in Claim 1. Thenf∗ defined byf∗(b) := f(b) ∪ {b}for everyb ∈ D is also a multi-valued morphism ofΓ.

We show that for everydi ∈ D, there is anni ≥ 1 such thatdi ∈ f j(di) for everyj ≥ ni.Taking the maximumn = max1≤i≤k ni of all these integers, we get thatdi ∈ fn+1(di) andf(di) ⊆fn+1(di) (sincedi ∈ fn(di)) for everyi, proving the claim.

The proof is by induction oni. If di ∈ f(di), then we are done as we can setni = 1 (note thatthis is always the case fori = 1, since we observed above that valued1 has to appear on a variableof Vd1

). So let us suppose thatdi 6∈ f(di). LetDi = {d1, . . . , di} and letgi : Di → 2Di definedby gi(dj) := f(dj) ∩Di. Observe thatgi(dj) is well-defined, i.e.,gi(dj) 6= ∅: the setVdj

contains

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 19

n|D|+1−j ≥ n|D|+1−i variables, while the number of variables where values not fromDi appear is∑k

`=i+1 π′(d`) ≤ n+

∑k`=i+1 n

|D|+1−` < n|D|+1−i.Let T :=

`≥1 g`i (di). We claim thatdi ∈ T . Suppose thatdi 6∈ T . By the definition ofT and

the assumptiondi 6∈ f(di), for everyb ∈ T ∪ {di}, the variables inVb can have values only fromTand fromD \Di. The total number of variables inVb, b ∈ T ∪ {di} is

b∈T∪{di}n|D|+1−b, while

the total cardinality constraint of the values fromT ∪ (D \Di) is

b∈T∪(D\Di)

π′(b) = n+∑

b∈T

n|D|+1−b +k

`=i+1

n|D|+1−`

<∑

b∈T

n|D|+1−b + n|D|+1−i =∑

b∈T∪{di}

n|D|+1−b,

a contradiction. Thusdi ∈ T , that is, there is a valuej < i such thatdj ∈ f(di) anddi ∈ f s(dj)for somes ≥ 1. By the induction hypothesis,dj ∈ fn(dj) for everyn ≥ nj, thus we have thatdi ∈ fn(di) if n is at leastni := 1 + nj + s. This concludes the proof of Claim 2.

LetD+ (resp.,D−) be the set of those valuesdi ∈ D that appear on more than (resp., less than)π′(i) − |V (di)| variables ofV \W . It is clear that if|D+| = |D−| = 0, thenϕ obtained fromψsatisfiesπ. Furthermore, if|D+| = 0, then|D−| = 0 as well. Thus suppose thatD+ 6= ∅ and letS :=

b∈D+,s≥1 fs(b).

CLAIM 3. S ∩D− 6= ∅.

SupposeS ∩D− = ∅. Everyb ∈ S ⊆ D \D− appears on at leastπ(b) − |V (b)| variables inV \W , implying that every suchb appears on at most|V (b)| variables in the gadgetMVM(Γ, n).Thus the total number of variables in the gadget having valuefrom S is at most

b∈S |V (b)|; infact, it is strictly less than that sinceD+ is not empty. By the definition ofS, only values fromS canappear inVb for everyb ∈ S. However, the total number of these variables is exactly

b∈S |V (b)|,a contradiction.

By Claim 3, there is a valued− ∈ S ∩ D−, which means that there is ad+ ∈ D+ and asequence of distinct valuesb0 = d+, b1, . . . , b` = d− such thatbi+1 ∈ f(bi) for every0 ≤ i < `.Let v ∈ V \W be an arbitrary variable having valued+. We modifyψ the following way:

(1) The value ofv is changed fromd+ to d−.(2) For every0 ≤ i < `, one variable inVbi

with valuebi+1 is changed tobi.

Note that these changes do not modify the cardinalities of the values: the second step increases onlythe cardinality ofb0 = d+ (by one) and decreases only the cardinality ofb` = d− (by one). We haveto argue that the transformed assignment still satisfies theconstraints ofP ′. Sinced− ∈ f `(d+),the multi-valued morphismf∗ of Claim 2 implies that changingd+ to d− on a single variable andnot changing anything else also gives a satisfying assignment. To see that the second step does notviolate the constraints, observe first that constraints of type (b) are not affected and constraints oftype (c) cannot be violated (since variables inVd1

are changed only tod1, and there is already atleast one variable with valued1 in Vd1

). To show that constraints of type (a) are not affected, itis sufficient to show that the mappingf ′ described by the gadget after the transformation is still amulti-valued morphism. This can be easily seen asf ′(b) ⊆ f(b) ∪ {bi} = f∗(b), wheref∗ is themulti-valued morphism of Claim 2.

Thus the modified assignment is still a solution ofP ′ satisfyingπ′. It is not difficult to showthat repeating this operation, in a finite number of steps we reach a solution whereD+ = D− = ∅,

20 A.BULATOV AND D.MARX

i.e., every valueb ∈ D(b) appears exactlyπ′(b) − |V (b)| times on the variables ofV \ W . Asobserved above, this implies thatP has a solution satisfyingπ.

6. HARDNESS

Now we prove that if constraint languageΓ does not satisfy the conditions of Theorem 1.2 thenCCSP(Γ) is NP-complete. First, we can easily simulate the restriction to a subset of the domain bysetting to 0 the cardinality constraint on the unwanted values:

Lemma 6.1. For any constraint languageΓ over a setD and anyD′ ⊆ D, the problemCCSP(Γ|D′)is polynomial time reducible toCCSP(Γ).

Proof. For an instanceP ′ = (V, C′) of CCSP(Γ|D′) with a global cardinality constraintπ′ : D′ →N we construct an instanceP = (V, C) of CCSP(Γ) such that for each〈s, R|D′〉 ∈ C′ we include〈s, R〉 into C. The cardinality constraintπ′ is replaced withπ : D → N such thatπ(a) = π′(a) fora ∈ D′, andπ(a) = 0 for a ∈ D \D′. It is straightforward thatP has a solution satisfyingπ if andonly if P ′ has a solution satisfyingπ′.

Suppose now that a constraint languageΓ does not satisfy the conditions of Theorem 1.2.Then one of the following cases takes place: (a)〈〈Γ〉〉 contains a binary relation that is not a thickmapping; or (b)〈〈Γ〉〉 contains a crossing pair of equivalence relations; or (c)Γ contains a relationwhich is not 2-decomposable. Since relations occurring in cases (a), (b) are not redundant, andrelations that may occur in case (c) can be assumed to be not redundant, by Lemma 2.7〈〈Γ〉〉 canbe replaced with〈〈Γ〉〉′. Furthermore, by Theorem 5.2 all the constant relations canbe assumed tobelong toΓ. We consider these three cases in turn. Furthermore, by a minimality argument, we canassume that ifΓ is overD, then for everyS ⊂ D, constraint languageΓ|S satisfies the requirementsof Theorem 1.2.

One of the NP-complete problems we will reduce toCCSP(R) is the BIPARTITE INDEPEN-DENT SET problem (or BIS for short). In this problem given a connectedbipartite graphG withbipartition V1, V2 and numbersk1, k2, the goal is to verify if there exists an independent setS ofG such that|S ∩ V1| = k1 and |S ∩ V2| = k2. The NP-completeness of the problem follows forexample from [11], which shows the NP-completeness of the complement problem under the nameCONSTRAINED M INIMUM VERTEX COVER. It is easy to see that the problem is hard even forgraphs containing no isolated vertices. By representing the edges of a bipartite graph with the rela-tionR = {(a, c), (a, d), (b, d)}, we can express the problem of finding a bipartite independent set.Valueb (resp.,a) represents selected (resp., unselected) vertices inV1, while valuec (resp.,d) repre-sents selected (resp., unselected) vertices inV2. With this interpretation, the only combination thatrelationR excludes is that two selected vertices are adjacent. By Lemma 2.10, if a binary relationis not a thick mapping, then it contains something very similar toR. However, some of the valuesa, b, c, andd might coincide and the relation might contain further tuples such as(c, d). Thus weneed a delicate case analysis to show that the problem is NP-hard for binary relations that are notthick mappings.

Lemma 6.2. Let R be a binary relation which is not a thick mapping. ThenCCSP({R}) is NP-complete.

Proof. SinceR is not a thick mapping, there are(a, c), (a, d), (b, d) ∈ R such that(b, c) 6∈ R.By Lemma 6.1 the problemCCSP(R′), whereR′ = R|{a,b,c,d}, is polynomial time reducibleto CCSP(R). ReplacingR with R′ if necessary we can assume thatR is a relation overD =

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 21

{a, b, c, d} (note that some of those elements can be equal). We suppose thatR is a ‘smallest’ rela-tion that is not a thick mapping, that is, for anyR′ definable in{R} with R′ ⊂ R, the relationR′ isa thick mapping, and for any subsetD′ of D the restriction ofR ontoD′ is a thick mapping.

LetB = {x | (a, x) ∈ R}. SinceB(x) = ∃yR(y, x)∧Ca(y), the unary relationB is definablein R. If B 6= pr2R, by settingR′(x, y) = R(x, y) ∧ B(y) we get a binary relationR′ that is not athick mapping. Thus by the minimality ofR, we may assume that(a, x) ∈ R for anyx ∈ pr2R,and symmetrically,(y, d) ∈ R for anyy ∈ pr1R.

CASE 1. |{a, b, c, d}| = 4.

We claim that|pr1R| = |pr2R| = 2. Suppose, without loss of generality thatx ∈ {a, b}appears inpr2R. If (b, x) ∈ R, then the restrictionR|{a,b,c} is not a thick mapping, contradictingthe minimality ofR (here we use that(a, x) ∈ R). Similarly, if (b, x) 6∈ R, thenR|{a,b,d} is not athick mapping. Thus we havepr1R = {a, b} andpr2R = {c, d}.

Let G = (V,E), V1, V2, k1, k2 be an instance of BIS. Construct an instanceP = (V, C) byincluding intoC, for every edge(v,w) of G, the constraint〈(v,w), R〉, and defining a cardinalityconstraint asπ(a) = |V1| − k1, π(b) = k1, π(c) = k2, π(d) = |V2| − k2. It is straightforward thatfor any solutionϕ of P the setSϕ = {v ∈ V | ϕ(v) ∈ {b, c}} is an independent set,Sϕ ∩ V1 ={v | ϕ(v) = b}, Sϕ ∩ V2 = {v | ϕ(v) = c}. SetSϕ satisfies the required conditions if and only ifϕsatisfiesπ. Conversely, for any independent setS ⊆ V mappingϕ given by

ϕS(v) =

a, if v ∈ V1 \ S,b, if v ∈ V1 ∩ S,c, if v ∈ V2 ∩ S,d, if v ∈ V2 \ S,

is a solution ofP that satisfiesπ if and only if |S ∩ V1| = k1 and|S ∩ V2| = k2.

CASE 2. |{a, b, c, d}| = 2.

ThenR is a binary relation with 3 tuples in it over a 2-element set. By [13] CCSP(R) isNP-complete.

Thus in the remaining cases, we can assume that|{a, b, c, d}| = 3.

CLAIM 1. One of the projectionspr1R or pr2R contains only 2 elements.

Let us assume the converse. Letpr2R = {c, d, x}, x ∈ {a, b} (asR is over a 3-element set).We consider two cases. Suppose firstc 6∈ {a, b} (implying d ∈ {a, b}). If (b, x) 6∈ R, then therestriction ofR onto{a, b} contains(a, d), (b, d), (a, x), but does not contain(b, x). Thus it is nota thick mapping, a contradiction. If(b, x) ∈ R then the setB = {a, b} = {x | (b, x) ∈ R} isdefinable inR. Observe thatR′(x, y) = R(x, y) ∧B(x) is not a thick mapping and definable inR.A contradiction with the choice ofR.

Now suppose thatd 6∈ {a, b} (implying c ∈ {a, b}). If (b, x) ∈ R, then the restrictionR|{a,b}

is not a thick mapping, as(a, c), (a, x), (b, x) ∈ R and(b, c) 6∈ R. Otherwise let(b, x) 6∈ R. Bythe assumption made|pr1R| = 3, that is,d ∈ pr1R. We consider 4 cases depending on whether(d, c) and(d, x) are contained inR. If (d, c), (d, x) 6∈ R, then, asa ∈ {x, c}, the relationR|{a,d}

is not a thick mapping (recall that(d, d) ∈ R). If (d, c), (d, x) ∈ R, then we can restrictR on{d, b} (note thatb ∈ {c, x}). Finally, if (d, c) ∈ R, (d, x) 6∈ R [or (d, x) ∈ R, (d, c) 6∈ R], thenthe relationB = {d, c} [respectively,B = {d, x}] is definable inR. It remains to observe thatR′(x, y) = R(x, y) ∧B(x) is not a thick mapping. This concludes the proof of the claim.

Thus we can assume that one of the projectionspr1R or pr2R contains only 2 elements. With-out loss of generality, letpr1R = {a, b}. In the remaining cases, we assumepr2R = {c, d, x},wherex ∈ {a, b} andx may not be present.

22 A.BULATOV AND D.MARX

CASE 3. {a, b} ∩ {c, d} 6= ∅ and either

• c 6∈ {a, b} (SUBCASE 3A), or• d 6∈ {a, b} and(b, x) 6∈ R (SUBCASE 3B).

In this case, given an instanceG = (V,E), V1, V2, k1, k2 of BIS, we construct an instanceP = (V ′, C) of CCSP(R) as follows.

• V ′ = V2 ∪⋃

w∈V1

V w, where all the setsV2 andV w, w ∈ V1 are disjoint, and|V w| = 2|V |.

• For any(u,w) ∈ E the setC contains all constraints of the form〈(v,w), R〉 wherev ∈ V u.• The cardinality constraintπ is given by the following rules:

– Subcase 3a:π(c) = k2, π(a) = (|V1| − k1) · 2|V |, π(b) = k1 · 2|V | + (|V2| − k2) if d = b, andπ(c) = k2, π(a) = (|V1| − k1) · 2|V | + (|V2| − k2), π(b) = k1 · 2|V | if d = a.

– Subcase 3b:π(d) = |V2| − k2, π(a) = (|V1| − k1) · 2|V |, π(b) = k1 · 2|V | + k2 if c = b, andπ(d) = |V2| − k2, π(a) = (|V1| − k1) · 2|V | + k2, π(b) = k1 · 2|V | if c = a.

If G has a required independent setS, then consider a mappingϕ : V ′ → D given by

ϕ(v) =

a, if v ∈ V w andw ∈ V1 \ S,b, if v ∈ V w andw ∈ V1 ∩ S,c, if v ∈ V2 ∩ S,d, if v ∈ V2 \ S,

For any〈(u, v), R〉 ∈ C, u ∈ V w, eitherw 6∈ S or v 6∈ S. In the first caseϕ(u) = a and so(ϕ(u), ϕ(v)) ∈ R. In the second caseϕ(u) = b andϕ(v) = d. Again,(ϕ(u), ϕ(v)) ∈ R. Finally itis straightforward thatϕ satisfies the cardinality constraintπ.

Suppose thatP has a solutionϕ that satisfiesπ. Sincepr1R = {a, b} and we can assume thatG has no isolated vertices, for anyu ∈ V w, w ∈ V1, we haveϕ(u) ∈ {a, b}. Also if for someu ∈ V w it holds thatϕ(u) = b andϕ(v) = c for v ∈ V2 then(w, v) 6∈ E. We include intoS ⊆ Vall verticesw ∈ V1 such that there isu ∈ V w with ϕ(u) = b. By the choice of the cardinality ofV w

andπ(b) there are at leastk1 such vertices. In Subcase 3a, we include inS all verticesv ∈ V2 withϕ(v) = c. There are exactlyk2 vertices like this, and by the observation aboveS is an independentset. In Subcase 3b, we include inS all verticesv ∈ V2 with ϕ(v) ∈ {a, b}. By the choice ofπ(d),there are at leastk2 such vertices. To verify thatS is an independent set it suffices to recall that inthis case(b, x) 6∈ R, and so(b, a), (b, b) 6∈ R.

CASE 4. d 6∈ {a, b} and(b, x) ∈ R.

In this case{c, x} = {a, b} and(a, c), (a, x), (b, x) ∈ R while (b, c) 6∈ R. ThereforeR|{a,b} isnot a thick mapping. A contradiction with the choice ofR.

Next we show hardness in the case when there is a crossing pairof equivalence relations. Witha simple observation, we can obtain a binary relation that isnot a thick mapping and then applyLemma 6.2.

Lemma 6.3. Let α, β be a crossing pair of equivalence relations. ThenCCSP({α, β}) is NP-complete.

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 23

Proof. Let α, β be equivalence relations on the same domainD. This means that there area, b, c ∈D such that〈a, c〉 ∈ α \ β and〈c, b〉 ∈ β \ α. Letα′ = α|{a,b,c} andβ′ = β|{a,b,c}. Clearly,

α′ = {(a, a), (b, b), (c, c), (a, c), (c, a)},

β′ = {(a, a), (b, b), (c, c), (b, c), (c, b)}.

By Lemma 6.1,CCSP({α′, β′}) is polynomial-time reducible toCCSP({α, β}). Consider therelationR = α′ ◦β′, that is,R(x, y) = ∃z(α′(x, z)∧β′(z, y)). We have thatCCSP(R) is reducibleto CCSP({α′, β′}) and

R = {(a, a), (b, b), (c, c), (a, c), (c, a), (b, c), (c, b), (a, b)}.

Observe thatR is not a thick mapping and by Lemma 6.2,CCSP(R) is NP-complete.

Finally, we prove hardness in the case when there is a relation that is not 2-decomposable. Anexample of such a relation is a ternary Boolean affine relation x + y + z = c for c = 0 or c = 1.The CSP with global cardinality constraints for this relation is NP-complete by [13]. Our strategyis to obtain such a relation from a relation that is not 2-decomposable. However, as in Lemma 6.2,we have to consider several cases.

We start with the following simple lemma:

Lemma 6.4. Letα be an equivalence relation on a setD anda ∈ D. Thenaα ∈ 〈〈α,Ca〉〉′.

Proof. The unary relationR(x) = ∃y((〈x, y〉 ∈ α) ∧ Ca(y)) is equal toaα and is clearly in〈〈α,Ca〉〉

′.

Lemma 6.5. LetR be a relation that is not 2-decomposable. ThenCCSP({R}) is NP-complete.

Proof. We can assume that every binary relation in〈〈{R} ∪ {Ca | a ∈ D}〉〉′ is a thick mapping,otherwise the problem is NP-complete by Theorems 5.1, 5.2, and Lemma 6.2. Furthermore, wecan chooseR to be the ‘smallest’ non-2-decomposable relation in the sense that every relationobtained fromR by restricting on a proper subset ofD is 2-decomposable, and every relationR′ ∈ 〈〈{R} ∪ {Ca | a ∈ D}〉〉′ that either have smaller arity, orR′ ⊂ R, is also 2-decomposable.

We claim that relationR is ternary. Indeed, it cannot be binary by assumptions made about it.Suppose thata 6∈ R is a tuple such thatprija ∈ prijR for anyi, j. Let

R′(x, y, z) = ∃x4, . . . , xn(R(x, y, z, x4, . . . , xn) ∧

Ca[4](x4) ∧ . . . ∧ Ca[n](xn)).

It is straightforward that(a[1],a[2],a[3]) 6∈ R′, while, since any proper projection ofR is 2-decomposable,pr{2,...,n}a ∈ pr{2,...,n}R, pr{1,3,...,n}a ∈ pr{1,3,...,n}R, pr{1,2,4,...,n}a ∈ pr{1,2,4,...,n}R,implying (a[2],a[3]) ∈ pr23R

′, (a[1],a[3]) ∈ pr13R′, (a[1],a[2]) ∈ pr12R

′, respectively. ThusR′

is not 2-decomposable, a contradiction with assumptions made.Let (a, b, c) 6∈ R and(a, b, d), (a, e, c), (f, b, c) ∈ R, and letB = {a, b, c, d, e, f}. AsR|B is

not 2-decomposable, we should haveR = R|B.If R12 = pr12R is a thick mapping with respect toη12, η21, R13 = pr13R is a thick mapping

with respect toη13, η31, andR23 = pr23R is a thick mapping with respect toη23, η32, then〈a, f〉 ∈η12 ∩ η13, 〈b, e〉 ∈ η21 ∩ η23, and〈c, d〉 ∈ η31 ∩ η32. Let the corresponding classes ofη12 ∩ η13,η21∩η23, andη31∩η32 beB1, B2, andB3, respectively. ThenB1 = pr1R,B2 = pr2R,B3 = pr3R.Indeed, if one of these equalities is not true, since by Lemma6.4 the relationsB1, B2, B3 are pp-definable inR without equalities, the relationR′(x, y, z) = R(x, y, z) ∧B1(x)∧B2(y) ∧B3(z) ispp-definable inR and the constant relations, is smaller thanR, and is not 2-decomposable.

24 A.BULATOV AND D.MARX

Next we show that(a, g) ∈ pr12R for all g ∈ pr2R. If there isg with (a, g) 6∈ pr12R thensettingC(y) = ∃z(pr12R(z, y) ∧ Ca(z)) we haveb, e ∈ C andC 6= pr2R. ThusR′(x, y, z) =R(x, y, z) ∧ C(y) is smaller thanR and is not 2-decomposable. The same is true fora andpr3R,and forb andpr3R. Since every binary projection ofR is a thick mapping this implies thatpr12R =pr1R× pr2R, pr23R = pr2R× pr3R, andpr13R = pr1R× pr3R.

For eachi ∈ {1, 2, 3} and everyp ∈ priR, the relationRpi (xj , xk) = ∃xi(R(x1, x2, x3) ∧

Cp(xi)), where{j, k} = {1, 2, 3} \ {i}, is definable inR and therefore is a thick mapping withrespect to, say,ηp

ij, ηpik. Our next step is to show thatR can be chosen such thatηp

ij does not dependon the choice ofp andi.

If one of these relations, say,Rp1, equalspr2R × pr3R, while another one, sayRq

1 does not,then considerRc

3. We have{p} × pr2R ⊆ Rc3. Moreover, since by the choice ofR relationRq

1 isa non-trivial thick mapping there isr ∈ pr2R such that(r, c) 6∈ Rq

1, hence(q, r) 6∈ Rc3. Therefore

Rc3 is not a thick mapping, a contradiction. SinceRa

1 does not equalpr2R × pr3R, everyηpij is

non-trivial. Observe that due to the equalitiesprijR = priR × prjR, we also have that the unaryprojections ofRp

i are equal to those ofR for anyp; and therefore all the equivalence relationsηpji,

for a fixedj, are on the same domain,prjR. Let

ηi =∨

j∈{1,2,3}\{i}p∈prjR

ηpji =

j∈{1,2,3}\{i}p∈prjR

ηpji.

Since for any non-crossingα, β we haveα ∨ β = α ◦ β, the relationηi is pp-definable inR andconstant relations without equalities. Since all theηp

ji are non-trivial,ηi is also non-trivial. We set

R′(x, y, z) = ∃x′, y′, z′(R(x′, y′, z′) ∧ η1(x, x′) ∧

η2(y, y′) ∧ η3(z, z

′)).

LetQpi be defined forR′ in the same way asRp

i for R. Observe that since(p, q, r) ∈ R′ if and onlyif there is(a′, b′, c′) ∈ R such that〈a, a′〉 ∈ η1, 〈b, b′〉 ∈ η2, 〈c, c′〉 ∈ η3, the relationsQp

1, Qq2, Qr

3

for p ∈ pr1R′, q ∈ pr2R

′, r ∈ pr3R′ are thick mappings with respect to the equivalence relations

η1, η2, relationsη2, η3, and relationsη1, η3, respectively. Since all the binary projections ofR′ equalto the direct product of the corresponding unary projections andη1, η2, η3 are non-trivial,R′ is not2-decomposable, and we can replaceR with R′. Thus we have achieved thatηp

ij does not dependon the choice ofp andi.

Next we show thatR can be chosen such thatpr1R = pr2R = pr3R, η1 = η2 = η3, and foreachi ∈ {1, 2, 3} there isr ∈ priR such thatRr

i is a reflexive relation. If, say,pr1R 6= pr2R, orη1 6= η2, orRr

3 is not reflexive for anyr ∈ pr3R, consider the following relation

R′(x, y, z) = ∃y′, z′(R(x, y′, z) ∧R(y, y′, z′) ∧Cd(z′)).

First, observe thatprijR′ = priR

′×prjR′ for anyi, j ∈ {1, 2, 3}. Then, for any fixedr ∈ pr3R

′ =

pr3R the relationQr3 = {(p, q) | (p, q, r) ∈ R′} is the productRr

3 ◦ (Rd3)

−1, that is, a non-trivialthick mapping. ThusR′ is not 2-decomposable. Furthermore,pr1R

′ = pr2R′ = pr1R, for any

r ∈ pr3R′ the relationQr

3 is a thick mapping with respect toη1, η1, andQd3 is reflexive. Repeating

this procedure for the first and third coordinate positions,we obtain a relationR′′ with the requiredproperties. ReplacingR with R′′ if necessary, we may assume thatR also has all these properties.

SetB = pr1R = pr2R = pr3R andη = η1 = η2 = η3. Letp ∈ B be such thatRp1 is reflexive.

Let alsoq ∈ B be such that〈p, q〉 6∈ η. Then(p, p, p), (p, q, q) ∈ R while (p, p, q) 6∈ R. Choosersuch that(r, p, q) ∈ R. Then the restriction ofR onto 3-element set{p, q, r} is not 2-decomposable.ThusR can be assumed to be a relation on a 3-element set.

THE COMPLEXITY OF GLOBAL CARDINALITY CONSTRAINTS 25

If η is not the equality relation, say,〈p, r〉 ∈ η, then as the restriction ofR onto{p, q} is still anot 2-decomposable relation,R itself is a relation on the set{p, q}. It is not hard to see that it is theaffine relationx+ y+ z = 0 on{p, q}. The CSP with global cardinality constraints for this relationis NP-complete by [13].

Suppose thatη is the equality relation. Since each ofRp1, R

q1, R

r1 is a mapping andRp

1 ∪ Rq1 ∪

Rr1 = B2, it follows that the three relations are disjoint. AsRp

1 is the identity mapping,Rq1 andRr

1

are two cyclic permutations of (the 3-element set)B. Hence either(p, q) or (q, p) belongs toRq1.

Let it be (p, q). RestrictingR onto{p, q} we obtain a relationR′ whose projectionpr23R′ equals

{(p, p), (q, q), (p, q)}, which is not a thick mapping. A contradiction with the choice ofR.

REFERENCES

[1] K.A. Baker and A.F. Pixley. Polynomial interpolation and the chinese remainder theorem.MathematischeZeitschrift, 143:165–174, 1975.

[2] Libor Barto, Marcin Kozik, and Todd Niven. Graphs, polymorphisms and the complexity of homomorphism prob-lems. InSTOC, pages 789–796, 2008.

[3] Christian Bessiere, Emmanuel Hebrard, Brahim Hnich, and Toby Walsh. The complexity of global constraints. InAAAI, pages 112–117, 2004.

[4] V.G. Bodnarchuk, L.A. Kaluzhnin, V.N. Kotov, and B.A. Romov. Galois theory for post algebras. i.Kibernetika,3:1–10, 1969.

[5] Stephane Bourdais, Philippe Galinier, and Gilles Pesant. Hibiscus: A constraint programming application to staffscheduling in health care. InCP, pages 153–167, 2003.

[6] A.A. Bulatov. Tractable conservative constraint satisfaction problems. InLICS, pages 321–330, 2003.[7] Andrei A. Bulatov. A dichotomy theorem for constraint satisfaction problems on a 3-element set.J. ACM, 53(1):66–

120, 2006.[8] Andrei A. Bulatov and Vıctor Dalmau. A simple algorithmfor mal’tsev constraints.SIAM J. Comput., 36(1):16–27,

2006.[9] Andrei A. Bulatov and Vıctor Dalmau. Towards a dichotomy theorem for the counting constraint satisfaction prob-

lem. Information and Computation, 205(5):651–678, 2007.[10] Andrei A. Bulatov, Peter Jeavons, and Andrei A. Krokhin. Classifying the complexity of constraints using finite

algebras.SIAM J. Comput., 34(3):720–742, 2005.[11] Jianer Chen and Iyad A. Kanj. Constrained minimum vertex cover in bipartite graphs: complexity and parameterized

algorithms.J. Comput. Syst. Sci., 67(4):833–847, 2003.[12] M.C. Cooper. An optimal k-consistency algorithm.Artificial Intelligence, 41:89–95, 1989.[13] Nadia Creignou, Henning Schnoor, and Ilka Schnoor. Non-uniform boolean constraint satisfaction problems with

cardinality constraint. InCSL, pages 109–123, 2008.[14] K. Denecke and S.L. Wismath.Universal algebra and applications in Theoretical Computer Science. Chapman and

Hall/CRC Press, 2002.[15] T. Feder and M.Y. Vardi. The computational structure ofmonotone monadic SNP and constraint satisfaction: A

study through datalog and group theory.SIAM J. Computing, 28:57–104, 1998.[16] D. Geiger. Closed systems of function and predicates.Pacific Journal of Mathematics, pages 95–100, 1968.[17] G. Gottlob, L. Leone, and F. Scarcello. Hypertree decompositions: A survey. InMFCS, volume 2136 ofLNCS,

pages 37–57. Springer-Verlag, 2001.[18] Martin Grohe. The complexity of homomorphism and constraint satisfaction problems seen from the other side.J.

ACM, 54(1), 2007.[19] Martin Grohe and Daniel Marx. Constraint solving via fractional edge covers. InSODA, pages 289–298, 2006.[20] P. Idziak, P. Markovic, R. McKenzie, M. Valeriote, and R. Willard. Tractability and learnability arising from algebras

with few subpowers. InProceedings of the 22th Annual IEEE Simposium on Logic in Computer Science. IEEEComputer Society, 2007.

[21] P.G. Jeavons, D.A. Cohen, and M.C. Cooper. Constraints, consistency and closure.Artificial Intelligence, 101(1-2):251–265, 1998.

[22] P.G. Jeavons, D.A. Cohen, and M. Gyssens. Closure properties of constraints.J. ACM, 44:527–548, 1997.

26 A.BULATOV AND D.MARX

[23] P.G. Jeavons, D.A. Cohen, and M. Gyssens. How to determine the expressive power of constraints.Constraints,4:113–131, 1999.

[24] Daniel Marx. Parameterized complexity of constraintsatisfaction problems.Computational Complexity, 14(2):153–183, 2005. Special issue “Conference on Computational Complexity (CCC) 2004.”.

[25] Jean-Charles Regin and Carla P. Gomes. The cardinality matrix constraint. InCP, pages 572–587, 2004.