44
The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV Simon Fraser University The Counting Constraint Satisfaction Problem (#CSP(H)) over a finite relational structure H can be expressed as follows: given a relational structure G over the same vocabulary, determine the number of homomorphisms from G to H. In this paper we characterize relational structures H for which #CSP(H) can be solved in polynomial time and prove that for all other structures the problem is #P-complete. Categories and Subject Descriptors: F.4.1 [Mathematical Logic and Formal Languages]: Mathematical Logic— Logic and constraint programming; G.2.1 [Discrete Mathematics]: Combinatorics—Combinatorial algorithms; F.1.3 [Computation by Abstract Devices]: Complexity Measures and Classes—Reducibility and completeness General Terms: Algorithms, Theory Additional Key Words and Phrases: Constraint Satisfaction problem, counting problems, com- plexity, dichotomy theorem, homomorphism problem 1. INTRODUCTION In the Counting Constraint Satisfaction Problem, #CSP(H), over a finite relational struc- ture H the objective is, given a finite relational structure G, to compute the number of homomorphisms from G to H. Various particular cases of the #CSP arise and have been extensively studied in a wide range of areas from logic and graph theory [Bubley et al. 1999; Creignou and Hermann 1996; Dyer and Greenhill 2000; Greenhill 2000; Hunt III et al. 1998; Linial 1986; Provan and Ball 1983; Valiant 1979a; 1979b], to artificial intel- ligence [Orponen 1990; Roth 1996], to statistical physics [Brightwell and Winkler 1999; Burton and Steif 1994; Lebowitz and Gallavotti 1971]. In different areas this problem often appears in different equivalent forms: (1) the problem of finding the number of mod- els of a conjunctive formula, (2) the problem of computing the size (number of tuples) of the evaluation Q(D) of a conjunctive query (without projection) Q on a database D and also (3) the problem of counting the number of assignments to a set of variables subject to specified constraints. Since the seminal papers [Schaefer 1978; Feder and Vardi 1998], the complexity of the decision counterpart of #CSP, the Constraint Satisfaction Problem or CSP for short, has been an object of intensive study. The ultimate goal of that research direction is to classify finite relational structures with respect to the complexity of the corresponding CSP. We shall refer to this research problem as the classification problem. A number of significant results have been obtained, see e.g. [Schaefer 1978; Feder and Vardi 1998; Bulatov 2006b; Permission to make digital/hard copy of all or part of this material without fee for personal or classroom use provided that the copies are not made or distributed for profit or commercial advantage, the ACM copyright/server notice, the title of the publication, and its date appear, and notice is given that copying is by permission of the ACM, Inc. To copy otherwise, to republish, to post on servers, or to redistribute to lists requires prior specific permission and/or a fee. c 20YY ACM 0004-5411/YY/00-0001 $5.00 Journal of the ACM, Vol. V, No. N, 20YY, Pages 1–0??.

The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting ConstraintSatisfaction Problem

ANDREI A. BULATOVSimon Fraser University

The Counting Constraint Satisfaction Problem (#CSP(H)) over a finite relational structure Hcan be expressed as follows: given a relational structure G over the same vocabulary, determinethe number of homomorphisms from G to H. In this paper we characterize relational structuresH for which #CSP(H) can be solved in polynomial time and prove that for all other structures

the problem is #P-complete.

Categories and Subject Descriptors: F.4.1 [Mathematical Logic and Formal Languages]: Mathematical Logic—Logic and constraint programming; G.2.1 [Discrete Mathematics]: Combinatorics—Combinatorial algorithms;F.1.3 [Computation by Abstract Devices]: Complexity Measures and Classes—Reducibility and completeness

General Terms: Algorithms, Theory

Additional Key Words and Phrases: Constraint Satisfaction problem, counting problems, com-

plexity, dichotomy theorem, homomorphism problem

1. INTRODUCTION

In the Counting Constraint Satisfaction Problem, #CSP(H), over a finite relational struc-ture H the objective is, given a finite relational structure G, to compute the number ofhomomorphisms from G to H. Various particular cases of the #CSP arise and have beenextensively studied in a wide range of areas from logic and graph theory [Bubley et al.1999; Creignou and Hermann 1996; Dyer and Greenhill 2000; Greenhill 2000; Hunt IIIet al. 1998; Linial 1986; Provan and Ball 1983; Valiant 1979a; 1979b], to artificial intel-ligence [Orponen 1990; Roth 1996], to statistical physics [Brightwell and Winkler 1999;Burton and Steif 1994; Lebowitz and Gallavotti 1971]. In different areas this problemoften appears in different equivalent forms: (1) the problem of finding the number of mod-els of a conjunctive formula, (2) the problem of computing the size (number of tuples) ofthe evaluation Q(D) of a conjunctive query (without projection) Q on a database D andalso (3) the problem of counting the number of assignments to a set of variables subject tospecified constraints.

Since the seminal papers [Schaefer 1978; Feder and Vardi 1998], the complexity of thedecision counterpart of #CSP, the Constraint Satisfaction Problem or CSP for short, hasbeen an object of intensive study. The ultimate goal of that research direction is to classifyfinite relational structures with respect to the complexity of the corresponding CSP. Weshall refer to this research problem as the classification problem. A number of significantresults have been obtained, see e.g. [Schaefer 1978; Feder and Vardi 1998; Bulatov 2006b;

Permission to make digital/hard copy of all or part of this material without fee for personal or classroom useprovided that the copies are not made or distributed for profit or commercial advantage, the ACM copyright/servernotice, the title of the publication, and its date appear, and notice is given that copying is by permission of theACM, Inc. To copy otherwise, to republish, to post on servers, or to redistribute to lists requires prior specificpermission and/or a fee.c⃝ 20YY ACM 0004-5411/YY/00-0001 $5.00

Journal of the ACM, Vol. V, No. N, 20YY, Pages 1–0??.

Page 2: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

2 · Andrei A. Bulatov

2003; Barto et al. 2008; Barto 2011], but a full classification is far from being completed.Although the classification problem for the general #CSP has been tackled for the first

time very recently, a massive amount of work has been done in the study of the com-plexity of various particular counting CSPs. These particular problems include classicalcombinatorial problems such as #CLIQUE, GRAPH RELIABILITY, ANTICHAIN, PERMA-NENT etc. [Linial 1986; Provan and Ball 1983; Valiant 1979a; 1979b] expressible in theform of #CSP; the counting SATISFIABILITY and GENERALIZED SATISFIABILITY prob-lems (in these problems the objective is to find the number of satisfying assignments toa propositional formula) [Creignou and Hermann 1996; Roth 1996] which correspond to#CSP(H) for 2-element structures H, counting the number of solution of equations overfinite semigroups [Nordh and Jonsson 2004; Klıma et al. 2006] and many others.

However, the main focus of research in this area has been the #H -COLORING prob-lem and its variants. In the #H -COLORING problem the aim is to find the number ofhomomorphisms from a given graph G to the fixed graph H . Thus, it is equivalent to#CSP(H) where H is a graph. Dyer and Greenhill [Dyer and Greenhill 2000] provedthat, for every undirected graph H , its associated #H -COLORING problem is either in FP(we shall call such problems tractable) or is #P-complete. They also provided a completecharacterization of the tractable problems. This result has been extended to the countingLIST #H -COLORING problem [Donner 1992; Diaz et al. 2001], which allows additionalrestrictions on possible images of a node. Recently, Dyer, Goldberg, and Paterson [Dyeret al. 2007] obtained a similar classification for directed acyclic graphs. Furthermore, someother variants of the #H -COLORING problem for undirected graphs have been intensivelystudied during the last few years [Diaz et al. 2004; 2005]. Another direction in this areais the study of problems with restricted input, that is subproblems of the #H -COLORINGproblem in which the input graph G must be planar [Hunt III et al. 1998; Vadhan 2001], apartial k-tree [Diaz et al. 2002], sparse or of low degree [Greenhill 2000; Hell and Nesetril2004], etc. Finally, we should mention the approach to counting problems using approxi-mation and randomized algorithms, see e.g. [Jerrum and Sinclair 1996; Dyer et al. 2003;Dyer et al. 2002; Dyer et al. 2010].

The counting CSP admits various generalizations. In one of them, Weighted #CSP everytuple from relations is assigned a weight that is used to compute weights of mappings fromone relational structure to another, and the problem is to find the sum of the weights ofall mappings [Dyer et al. 2009]. A particular case of the Weighted #CSP, in which onlyone binary relation is allowed, is often referred to as partition functions [Lovasz 2006;Freedman et al. 2007]. Partition functons are widely used in statistical phisics [Brightwelland Winkler 1999; Burton and Steif 1994; Lebowitz and Gallavotti 1971]. Recently, furthergeneralizations of the counting CSPs attracted considerable attention in connection withthe study of holographic reductions, see e.g. [Cai et al. 2008; Cai and Lu 2011].

In [Bulatov and Dalmau 2007] we started a systematic study of the classification prob-lem for the general #CSP. The main approach chosen was the algebraic approach whichhas proved to be quite useful in the study of the decision CSP [Jeavons 1998; Jeavons et al.1998; Bulatov 2003; 2006b; 2011; Barto et al. 2008; Barto 2011]. This approach usesinvariance properties of predicates definable in relational structures. Invariance propertiesare usually expressed as polymorphisms of the predicates, that is (multi-ary) operations onthe universe of the relational structure compatible with the predicates.

In [Bulatov and Dalmau 2007], we proved that if #CSP(H) is tractable, then H hasJournal of the ACM, Vol. V, No. N, 20YY.

Page 3: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 3

a Mal’tsev polymorphism, that is a ternary operation m(x, y, z) satisfying the identitiesm(x, y, y) = m(y, y, x) = x. Another observation was that the congruences, i.e. thedefinable equivalence relations, of H play a very important role. In particular, these resultsallowed us to come up with a simple proof of the result of [Dyer and Greenhill 2000]1. In[Bulatov and Grohe 2005], another necessary condition for the tractability of #CSP(H)was identified. It imposes certain restrictions onto possible congruences of H, in terms ofcardinalities of their equivalence classes.

In this paper, after giving general definitions (Sections 2.1 and 2.2) and introducing thebasics of the algebraic approach (Sections 2.3, 2.4 and 2.5), we go deeper into the structureof congruence lattices of relational structures with a Mal’tsev polymorphism (Sections 3.1and 3.3), its connections with types of prime quotients (Section 3.2), and the structure ofrelations with a Mal’tsev polymorphism (Section 3.4). In Section 4 we identify two moreconditions, again expressed in terms of properties of congruences, that are satisfied by anystructure that gives rise to a tractable problem. Then, in Section 5, several observationsare made in preparation to introducing an algorithm solving the problem #CSP(H) forevery relational structure H satisfying all the conditions obtained. The algorithm is thendescribed in details in Section 6. Thus, we completely solve the classification problemfor the general counting CSP. Finally, in Section 7 we compare our result with a recentresult of [Dyer et al. 2007] classifying the complexity of the #H -COLORING problem fordirected acyclic graphs.

We intensively use methods and results from a number of areas of algebra: lattice theory,tame congruence theory, commutator theory and ring theory. To make the paper availablefor a wider audience we avoid excessive use of algebraic terminology. In spite of that,some parts of the paper, Section 4 and especially proofs, may require from the reader somefamiliarity with basic algebraic objects and ideas. The keen reader is referred to textbooks[Burris and Sankappanavar 1981; Freese and McKenzie 1987; Gratzer 2003; Hobby andMcKenzie 1988]. The reader should be aware that to avoid yet another layer of objectswe use algebraic terminology for relational structures, while in the algebraic literature thesame concepts are used for “dual” objects, universal algebras.

2. PRELIMINARIES

2.1 Relational structures and homomorphisms

Our notation concerning relations and relational structures is fairly standard. Let [n] de-note the set {1, . . . , n}. The set of all n-tuples of elements from a set H is denoted byHn. We denote tuples of elements in boldface, for instance, a, and their components bya[1],a[2], . . .. For a subset I = {i1, . . . , ik} ⊆ [n] and an n-tuple a, by prIa we denotethe projection of a onto I , the k-tuple (a[i1], . . . ,a[ik]). For an n-ary relation R ⊆ Hn,its projection onto I is defined to be prIR = {prIa | a ∈ R}. If Di = priR fori ∈ [n] we say that R is a subdirect product of D1, . . . , Dn. If D1 = . . . = Dn = Hthen R is said to be an n-th (or n-ary) subdirect power of H . For a = (a[1], . . . ,a[n])and b = (b[1], . . . ,b[m]), (a,b) denotes the tuple (a[1], . . . ,a[n],b[1], . . . ,b[m]), while⟨a,b⟩ denotes the pair of tuples. Sometimes we need more complicated indexing. LetI, J ⊆ [n] be disjoint, I = {i1, . . . , ik}, J = {j1, . . . , jℓ}, and assume that i1 < . . . < ik

1Note that the hardness results [Dyer and Greenhill 2000] remain true even for graphs of degree at most 3, andso are stronger than those in [Bulatov and Dalmau 2007].

Journal of the ACM, Vol. V, No. N, 20YY.

Page 4: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

4 · Andrei A. Bulatov

and j1 < . . . < jℓ. Let also a = (a[i1], . . . ,a[ik]) and b = (b[j1], . . . ,b[jℓ]). Then(a,b) denotes the tuple c whose entries are indexed by elements of the set I ∪ J such thatc[i] = a[it] if i = it ∈ I and c[i] = b[jt] if i = jt ∈ J .

A vocabulary is a finite set of relational symbols R1, . . . , Rn each of which has a fixedarity. A relational structure over vocabularyR1, . . . , Rn is a tuple H = (H;RH

1 , . . . , RHn )

such that A is a non-empty set, called the universe of H, and each RHi is a relation on

H having the same arity as the symbol Ri. Let G,H be relational structures over thesame vocabulary R1, . . . , Rn. A homomorphism from G to H is a mapping φ : G → Hfrom the universe G of G (the instance) to the universe H of H (the template) such that,for every relation RG (say, m-ary) of G and every tuple (a1, . . . , am) ∈ RG , we have(φ(a1), . . . , φ(am)) ∈ RH.

A relation R is said to be primitive positive definable (pp-) in H, if it can be expressedusing the predicates RH

i of H together with the binary equality predicate on H (denoted∆H ), conjunction, and existential quantification. We use def(H) to denote the set of allpp-definable relations.

Example 2.1. Let H be a 3-element structure with the universe {a, b, c} and one binarydisequality relationR. Structure H can be thought of as a 3-element complete graph. Thenthe pp-formula

Q(x, y, z) = ∃t, u, v, w(R(t, x) ∧R(t, y) ∧R(t, z) ∧R(u, v) ∧R(v, w)∧R(w, u) ∧R(u, x) ∧R(v, y) ∧R(w, z))

defines the relation

Q =

a a b a b b a a c a c c b b c b c ca b a b a b a c a c a c b c b c b cb a a b b a c a a c c a c b b c c b

,

consisting of all triples containing exactly 2 different elements from {a, b, c} (triples arewritten vertically). Fig. 1(a) shows the graph built from the pp-formula; the vertices of thegraph represent variables and edges show on which variables the relation R is applied.

Another useful way to represent relation Q is to view it as the set of restriction of homo-morphisms from the graph shown in Fig. 1(b) to H, where homomorphisms are restrictedto {x, y, z}. Observe that this connection between pp-definitions and restrictions of homo-morphisms is rather general.

Q

QQQ

Q

Q

Q Q

Q

(a)

a

b c

(b)

x y zx y z

u

t

w

v

t

v

u w

Fig. 1.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 5: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 5

2.2 Constraint Satisfaction Problem

The counting constraint satisfaction problem can be formulated in several ways (see Sec-tion 1). We use the model theoretic form of this problem.

Definition 2.2. Let H be a class of relational structures. In the counting constraintsatisfaction problem associated with H (#CSP(H)), the objective is, given a structureH ∈ H and a structure G, to compute the number of homomorphisms from G to H. Wewill refer to this problem as to a uniform #CSP.

If H consists of a single structure H, then we write #CSP(H) instead of CSP({H})and refer to such problem as a non-uniform homomorphism problem, because the inputsare just source structures.

Example 2.3 (#H -COLORING, [Dyer and Greenhill 2000; Hell and Nesetril 1990; Levin 1973]).A graph H is a structure with a vocabulary consisting of one binary symbol R. Then#CSP(H) is widely known as the #H -COLORING Problem, in which the objective is tocompute the number of homomorphisms from a given graph into H.

Example 2.4 (#3-SAT, [Creignou and Hermann 1996; Creignou et al. 2001; Valiant 1979a; 1979b]).An instance of the #3-SAT problem is specified by giving a propositional logic formula inCNF each clause of which contains 3 literals, and asking how many assignments satisfy it.Therefore, #3-SAT is equivalent to #CSP(S3), where S3 is the 2-element relational struc-ture with the universe {0, 1} and the vocabulary R1, . . . , R8. Predicates RS3

1 , . . . , RS38 are

the 8 predicates expressible by 3-clauses.

Example 2.5 (Systems of linear equations). Let F be a finite field and let #LINEAREQUATIONS(F ) be the problem of finding the number of solutions to a system of lin-ear equations over F . It is not hard to see that #LINEAR EQUATIONS(F ) is equivalentto #CSP(L), where L is the class of relational structures with the universe F and therelations corresponding to hyperplanes of finite-dimensional vector spaces over F .

In fact, #LINEAR EQUATIONS(F ) cannot be straightforwardly reduced to #CSP(L) inpolynomial time. The reason is that the representation of relations by linear equations ismuch more concise than that by a list of tuples, see discussion after Example 2.6. However,in this case a reduction to a problem in #CSP exists. It is carried out by first reducinga system of linear equations to a system of equations each of which contains at most 3variables; clearly, some new variables must be introduced at this step. Then such a systemis straightforwardly reduced to #CSP(L3), where L3 is the the relational structure fromL containing all ternary relations expressible by linear equations.

Example 2.6 (Equations over semigroups, [Nordh and Jonsson 2004; Klıma et al. 2006]).Let S be a finite semigroup, that is, a set with a binary associative operation. An equationover S is an expression of the form x1 ·x2 · . . . ·xm = y1 · y2 · . . . · ym where · is the semi-group operation, and xi, yj are either indeterminates or constants. Then #EQN∗

S standsfor the problem of counting the number of solutions to a system of semigroup equations.

The problem #EQN∗S is equivalent to the problem #CSP(S) where S is the class of

structures with universe S and relations expressible as the set of solutions of a semigroupequation.

In the last two examples, as well as for many other uniform problems, there is a minorambiguity concerning a representation of the input. We always assume that in uniformproblems the relations of the template are represented explicitly, by a list of tuples of the

Journal of the ACM, Vol. V, No. N, 20YY.

Page 6: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

6 · Andrei A. Bulatov

relation. In Examples 2.5, 2.6 such a representation is not the most natural one. However,the class of relations admitting a succinct representation is rather limited (see, e.g. [Idziaket al. 2007]), and thus such representations are unsuitable for the study of the generalproblem.

Every counting CSP belongs to the class #P. However, the exact complexity of #CSP(H)strongly depends on the structure H. We say that a relational structure H is #-tractable if#CSP(H) is solvable in polynomial time; H is #P-complete if #CSP(H) is #P-complete.Note that all reductions used in this paper are Turing reductions. The research problem wedeal with in this paper is the following one.

PROBLEM 1 (CLASSIFICATION PROBLEM). Characterize #-tractable and #P-completerelational structures.

Example 2.7. (1) Dyer and Greenhill [2000] proved that if H is an undirected graphthen #H -COLORING can be solved in polynomial time if and only if every connectedcomponent of H is either a complete bipartite graph, or a complete graph with all loopspresent, or a single vertex. Otherwise the problem is #P-complete.(2) A 2-element relational structure H is #-tractable if and only if every predicate of Hcan be represented by a system of linear equations over the 2-element field [Creignou andHermann 1996; Creignou et al. 2001]. Otherwise, H is #P-complete.(3) #CSP(L3) is solvable in polynomial time.(4) The problem #EQN∗

S is solvable in polynomial time if and only if S is a direct productof a uniformly inflated Abelian group, inflated left-zero semigroup, and an inflated right-zero semigroup. Otherwise #EQN∗

S is #P-complete. For details see [Klıma et al. 2006].

2.3 Polymorphisms, Algebras and Complexity

Any operation on a set H can be extended in a standard way to an operation on tuples overH , as follows. For any (m-ary) operation f , and any collection of tuples a1, . . . ,am ∈ Hn,define f(a1, . . . ,am) to be (f(a1[1], . . . ,am[1]), . . . , f(a1[n], . . . ,am[n])), that is, f actson Hn component-wise. Then f preserves an n-ary relation R (or R is invariant under f ,or f is a polymorphism ofR) if for any a1, . . . ,am ∈ R the tuple f(a1, . . . ,am) belongs toR. For a given set of operations, C, the set of all relations invariant under every operationfrom C is denoted by Inv(C). For a relational structure H we use Pol(H) to denote the setof all operations preserving every relation of H.

Example 2.8. LetR be the solution space of a system of linear equations over a field F .Then the operation m(x, y, z) = x− y+ z is a polymorphism of R. Indeed, let A · x = bbe the system defining R, and x,y, z ∈ R. Then

A ·m(x,y, z) = A · (x− y + z) = A · x−A · y +A · z = b.

In fact, the converse can also be shown: if R is invariant under m, where m is defined ina certain finite field F then it is the solution space of some system of linear equations overF .

The following proposition links together polymorphisms and pp-definability of relations.

PROPOSITION 2.9 [GEIGER 1968; BODNARCHUK ET AL. 1969]. Let H be a finitestructure, and let R ⊆ Hn be a non-empty relation. Then R is preserved by all poly-morphisms of H if and only if R is pp-definable in H.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 7: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 7

The connection between polymorphisms and the complexity of counting CSPs is pro-vided by the following result.

PROPOSITION 2.10 [BULATOV AND DALMAU 2007]. Let H1 and H2 be relationalstructures with the same universe. If Pol(H1) ⊆ Pol(H2) then #CSP(H2) is polynomialtime reducible to #CSP(H1).

Proposition 2.10 amounts to saying that all the information about the complexity of #CSP(H)can be extracted from the family of polymorphisms of H. Sets of polymorphisms oftenprovide a more convenient and concise way of describing a class of constraint satisfac-tion problems. For example, in [Bulatov and Dalmau 2007], we used polymorphisms toidentify some conditions necessary for the #-tractability of a relational structure. A ternaryoperation m(x, y, z) on a set H is said to be Mal’tsev if m(x, y, y) = m(y, y, x) = x forall x, y ∈ H .

PROPOSITION 2.11 [BULATOV AND DALMAU 2007]. If H is a relational structurewhich is invariant under no Mal’tsev operation then H is #P-complete.

Notice that if H has a Mal’tsev polymorphism then the decision CSP corresponding to Hcan be solved in polynomial time [Bulatov 2002b; Bulatov and Dalmau 2006].

Example 2.12. Mal’tsev operation m(x, y, z) is a polymorphism of graph H1 shownin Fig. 2, where m is given by

m(i1j1, i2j2, i3j3) = ij,

i = i1 [j = j1] unless i1 = i2 [j1 = j2], in this case i = i3 [j = j3].Graph H2 has no Mal’tsev polymorphisms. Indeed, if some f(x, y, z) is a Mal’tsev

operation, then

f

((ac

),

(ad

),

(bd

))=

(bc

)∈ E(H2).

H2

b

dc

a

11

01

10

00

H1

Fig. 2.

In our algebraic definitions we follow [Burris and Sankappanavar 1981; McKenzie et al.1987]. For algebraic notions and results concerning the decision CSP the reader is referredto [Bulatov and Jeavons 2001; Bulatov et al. 2005].

A (universal) algebra is an ordered pair A = (A,F ) where A is a non-empty set and Fis a family of finitary operations on A. The set A is called the universe of A, operationsfrom F are called basic. An algebra with a finite universe is referred to as a finite algebra,while the set of basic operations needs not to be finite.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 8: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

8 · Andrei A. Bulatov

Any relational structure H with universe H can be converted into an algebra Alg(H) =(H;Pol(H)). Conversely, every algebra A = (A;F ) corresponds to a class of structuresStr(A) with universe A and relations from Inv(F ). Using this correspondence we candefine #-tractable algebras. An algebra A is said to be #-tractable if every structure H ∈Str(A) is #-tractable; it is said to be #P-complete if some H ∈ Str(A) is #P-complete.

We shall express the complexity of #CSP(H) in terms of Alg(H). For example, if analgebra has a Mal’tsev operation, it is called a Mal’tsev algebra. Proposition 2.11 impliesthat if #CSP(H) is tractable then Alg(H) is Mal’tsev.

2.4 Subalgebras and congruences

We shall use various constructions on algebras, but two of these constructions, subalgebrasand congruences, can be defined for relational structures, and are very useful and illustra-tive in this context.

A subalgebra of a structure H = (H;RH1 , . . . , R

Hk ) is a unary relation pp-definable

in H, and a congruence of H is an equivalence relation pp-definable in H. For a subsetB ⊆ H , the substructure of H induced by B is defined to be H

B= (B;RH

1B, . . . , RH

k B),

where RHi B

= RHi ∩ Bmi , Ri is mi-ary. For an equivalence relation α and a ∈ H ,

the class of α containing a is denoted by aα and the set of all classes of α by H/α.The quotient structure H/α is defined to be H/α = (H/α;R

H1 /α, . . . , R

Hk /α), where

RHi /α = {(aα1 , . . . , aαmi

) | (a1, . . . , ami) ∈ RHi }.

Example 2.13. Let H = (V,E) be a digraph without sources and sinks, i.e. the in-degree and out-degree of each vertex is non-zero. We define two binary relations, ξHand ζH, on the vertex set H of H: ⟨a, b⟩ ∈ ξH if and only if a, b have a common out-neighbour and ⟨a, b⟩ ∈ ζH if and only if a, b have a common in-neighbour; in other words,ξH = {⟨a, b⟩ | (a, c), (b, c) ∈ E for a certain c ∈ H}, ζH = {⟨a, b⟩ | (c, a), (c, b) ∈ E fora certain c ∈ H}. Relations ξH and ζH are pp-definable in H, as the following pp-formulasshow

ξH(x, y) = ∃z(E(x, z) ∧ E(y, z)), ζH(x, y) = ∃z(E(z, x) ∧ E(z, y)).

In general, ξH, ζH are reflexive and symmetric relations. However, if H has a Mal’tsevpolymorphism m, they are also transitive. Indeed, suppose that ⟨a, b⟩ ∈ ξH, d ∈ H is theircommon out-neighbour, and c is an out-neighbour of a. If c is not an out-neighbour of b,then H contains H2 (see Fig. 2) as a subgraph and (b, c) is not an edge, which contradictsthe assumption that H has a Mal’tsev polymorphism. Therefore, the out-neighbourhoodsof a, b are equal whenever ⟨a, b⟩ ∈ ξH, which implies transitivity. Thus, ξH, ζH are con-gruences of H.

For the graph H3 shown in Fig. 3, the ξH3-classes are {a, b, c}, {d, e}, {f, g}, {h}, {i},and the ζH3-classes are {a, b, d}, {c, e}, {f}, {g}, {h, i}.

PROPOSITION 2.14 [BULATOV AND DALMAU 2007]. Let H be a relational structure,B and let α ibe a subalgebra and a congruence respectively.(1) If H is #-tractable then so are H

Band H/α.

(2) If HB

or H/α is #P-complete then H is #P-complete.

In a similar way we define congruences of relations. Let R ∈ def(H) be an n-aryJournal of the ACM, Vol. V, No. N, 20YY.

Page 9: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 9

f g

edcba

h iH3

Fig. 3.

relation. It can be viewed as a subalgebra of an nth direct power of H. A congruence on Ris a 2n-ary relationQ ∈ def(H) such that pr{1,...,n}Q = pr{n+1,...,2n}Q = R, and, ifQ istreated as a binary relation on R, it is an equivalence relation. The set of all congruence ofrelational structure H will be denoted by Con(H). An important example of a congruenceon R is the following. Let α be a congruence of H and denote by αn the relation on Rgiven by ⟨a,b⟩ ∈ αn if and only if ⟨a[i],b[i]⟩ ∈ α for all i ∈ [n]. As the followingpp-definition shows, αn is a congruence of R

αn(x1, . . . , xn; y1, . . . , yn) = R(x1, . . . , xn) ∧R(y1, . . . , yn) ∧n∧

i=1

α(xi, yi).

Example 2.15. We give an example of a nontrivial congruence of a ternary relation.Let us reconsider relation Q on the 3-element set {a, b, c}, whose pp-definition is given inExample 2.1. We show that the binary relation T on Q that relates triples with the sameset of entries is a congruence of Q. This can be done in two ways: we may verify that thefollowing pp-formula defines exactly that (6-ary on {a, b, c}) relation

T (x, y, z, x′, y′, z′) = ∃t, u, v, w, u′, v′, w′(R(t, x) ∧R(t, y) ∧R(t, z) ∧R(u, v)∧R(v, w) ∧R(w, u) ∧R(u, x) ∧R(v, y) ∧R(w, z) ∧R(t, x′) ∧R(t, y′) ∧R(t, z′)∧R(u′, v′) ∧R(v′, w′) ∧R(w′, u′) ∧R(u′, x′) ∧R(v′, y′) ∧R(w′, z′),

or we may observe that the T is formed by restrictions of homomorphisms from the graphshown in Fig. 4 to H onto {x, y, z, x′, y′, z′}.

a

b c

z’

y’

z

y

x

x’

H

Fig. 4.

The existence of a Mal’tsev polymorphism provides a necessary condition for the #-tractability of a relational structure. However, it is not a sufficient condition, as Exam-ple 2.17 below shows. In Section 4 we prove two other conditions that are also necessary.A particular case of one of them is that proved in [Bulatov and Grohe 2005].

Let α, β be congruences of H, and letJournal of the ACM, Vol. V, No. N, 20YY.

Page 10: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

10 · Andrei A. Bulatov

A1, . . . , Ak andB1, . . . , Bℓ be the α- and β-classes respectively. ThenM(α, β) denotesthe k × ℓ-matrix (mij), where mij = |Ai ∩Bj |.

PROPOSITION 2.16 [BULATOV AND GROHE 2004]. Let H be a relational structure,and let α, β be congruences of H. If rank(M(α, β)) > k, where k is the number of classesof the smallest congruence containing both α and β, then #CSP(H) is #P-complete.

Classes of the smallest congruence γ containing both α and β can be easily representedin terms of matrixM(α, β): This matrix (as well as any other matrix) after suitable permu-tations of rows and columns can be partitioned into a collection of rectangular cells sittingon the diagonal, so that all entries outside the cells equal zero. The finest partition of thiskind gives the classes of γ.

Example 2.17. Let H be the graph H3 shown in Fig. 3, α = ξH3 and β = ζH3 (seeExample 2.13). We have A1 = {a, b, c}, A2 = {d, e}, A3 = {f, g}, A4 = {h}, A5 = {i},and B1 = {a, b, d}, B2 = {c, e}, B3 = {f}, B4 = {g}, B5 = {h, i}. Thus

M(α, β) =

2 1 0 0 01 1 0 0 00 0 1 1 00 0 0 0 10 0 0 0 1

.

Graph H3 is similar to the graph considered in [Bulatov and Dalmau 2007, Example 5]. Inparticular it can be straightforwardly verified that it has the same Mal’tsev polymorphism.However, by Proposition 2.16, the problem #CSP(H3) is #P-complete.

2.5 Varieties and Complexity

It will be convenient for us to jump back and forth between model-theoretic and algebraicviews to the CSP. The language of relational structures is more convenient when describingalgorithms. On the other hand, standard algebraic constructions allow us to strengthen nec-essary conditions for #-tractability, and eventually formulate a criterion for #-tractability.

Definition 2.18. (1) Let A = (A;F ) be an algebra. The k-th direct power of A is thealgebra Ak = (Ak;F ) where we treat each (say, n-ary) operation f ∈ F as acting on Ak

component-wise.

(2) Let A = (A;F ) be an algebra, and let B be a subset of A such that, for any (say,n-ary) f ∈ F , and for any b1, . . . , bn ∈ B, we have f(b1, . . . , bn) ∈ B. Then the algebraB = (B;F

B), where F

Bconsists of restrictions of operations f ∈ F onto B, is called a

subalgebra of A.(3) Let A1 = (A1;F1) and A2 = (A2;F2) such that F1 = {f1i | i ∈ I}, F2 = {f2i |

i ∈ I}, and f1i , f2i are of the same arity ni, i ∈ I . A mapping φ : A1 → A2 is called

a homomorphism from A1 to A2 if φf1i (a1, . . . , ani) = f2i (φ(a1), . . . , φ(ani)) holds forall i ∈ I and all a1, . . . , ani ∈ A1. If the mapping φ is onto then A2 is said to be ahomomorphic image of A1.

A common way of constructing homomorphic images is through congruences and quo-tient algebras. A congruence of an algebra A = (A;F ) is an equivalence relation onA invariant under all operations from F . Let θ be a congruence of A. The algebraJournal of the ACM, Vol. V, No. N, 20YY.

Page 11: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 11

A/θ = (A/θ;F/θ), where F/θ = {f/θ | f ∈ F} and f/θ is given by f/θ(aθ1, . . . , a

θn) =

(f(a1, . . . , an))θ is called a quotient algebra. Observe that an equivalence relation is a

congruence of a structure H if and only if it is a congruence of Alg(H).

THEOREM 2.19 [BULATOV AND DALMAU 2007]. Let A = (A;F ) be a finite alge-bra. Then

(1) if A is #-tractable then so is every subalgebra, homomorphic image, and direct powerof A.

(2) if A has a #P-complete subalgebra, homomorphic image, or direct power, then A is#P-complete.

For an algebra A the class of algebras that are homomorphic images of subalgebras ofdirect powers of A is called the variety generated by A, and is denoted by var(A). Anoperation f on the universe of an algebra A = (A;F ) that preserves all relations invariantunder F is called a term operation of A. Every term operation of A can be obtained fromoperations of F by means of superposition.

An operation f on a set A is said to be idempotent if the equality f(x, . . . , x) = xholds for all x ∈ A. An algebra all of whose term operations are idempotent is said tobe idempotent. Let Kh denote the constant relation {(h)}, containing only one tuple,namely (h), and let Hid denote the expansion of a relational structure H by unary relationsKh, h ∈ H. It can be easily seen that all polymorphisms of Hid are idempotent. Wewill need the following simple observation about relational structures with idempotentpolymorphisms.

LEMMA 2.20. Let H be a relational structure whose polymorphisms are idempotent,R ∈ def(H) an n-ary relation, α a congruence of R, and B an α-class. Then B is arelation pp-definable in H.

PROOF. Let a ∈ B. Since every polymorphism of H is idempotent, the constant rela-tions Ka[i], i ∈ [n], are pp-definable in H. Then

B(x1, . . . , xn) = ∃y1, . . . , yn(R(x1, . . . , xn) ∧ α(x1, . . . , xn; y1, . . . , yn)∧Ka[1](y1) ∧ . . . ∧Ka[n](yn)).

The following theorem shows the connection between complexity and full idempotentreducts.

THEOREM 2.21 [BULATOV AND DALMAU 2007]. A relational structure H is #-trac-table [#P-complete] if and only if so is Hid.

If A is an idempotent algebra and the condition of Proposition 2.16 is true for every pairof congruences of A then A is said to be congruence singular. If every finite algebra ina variety is congruence singular then the variety is called congruence singular. We calla relational structure H congruence singular if Alg(H) generates a congruence singularvariety. By Proposition 2.16 and Theorems 2.19, 2.21, every structure H that is not #P-complete is congruence singular. The main result of the paper is that this condition issufficient for #-tractability.

THEOREM 2.22. A relational structure H [an algebra A], is #-tractable if and only ifHid is congruence singular [Id(A) generates a congruence singular variety].

Journal of the ACM, Vol. V, No. N, 20YY.

Page 12: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

12 · Andrei A. Bulatov

Observe that the condition of having a Mal’tsev polymorphism (term operation) is notincluded into the criterion. As we shall see later (Lemma 3.2) every congruence singularstructure has a Mal’tsev polymorphism.

We complete this section with a more combinatorial characterization of congruence sin-gular relational structures. Let H be a relational structure, R a relation pp-definable in H,and α, β, δ congruences of R such that δ ⊆ α, β. By M(R;α, β; δ) we denote the matrixM(α, β) computed for R/δ. More precisely, let A1, . . . , Ak and B1, . . . , Bℓ be the α- andβ-classes respectively. ThenM(R;α, β; δ) is the k× ℓ-matrix (mij) wheremij equals thenumber of δ-classes in Ai ∩Bj .

LEMMA 2.23. A relational structure H is congruence singular if and only if for anyrelation R pp-definable in H and any congruences δ, α, β of R such that δ ≤ α, β, therank of the matrix M(R;α, β; δ) equals the number of classes in the smallest congruencecontaining both α and β.

PROOF. Let A = Alg(H). We show that for any finite algebra B from the varietygenerated by A and congruences α, β of B there is a relation R pp-definable in H andcongruences δ, α′, β′ of R with δ ⊆ α, β such that M(α, β) = M(R;α′, β′; δ); and,conversely, for any R, δ, α′, β′, there are B and α, β satisfying the above equality.

Take B, α, and β. By the HSP-Theorem (see, e.g., [Burris and Sankappanavar 1981])B is a homomorphic image of a subalgebra of (say, k-th) direct power of A. Let C denotethe subalgebra of the direct power, and let B be a homomorphic image of C, let φ be thehomomorphism, and let γ be the corresponding congruence of C, that is ⟨a, b⟩ ∈ γ if andonly if φ(a) = φ(b). The universe C of C can be viewed as a subset of Hk — recallthat H is the universe of A — invariant under all polymorphisms of H. Thus C is a k-ary relation pp-definable in H. We choose R = C. Then the term operations of C arethe polymorphisms of H acting on R component-wise. Furthermore, γ is an equivalencerelation on C invariant under all operations of C, and therefore under all polymorphismsof H. Hence γ is a congruence of R, and we set δ = γ. Finally, define α′, β′ as follows:α′ = {⟨a,b⟩ ∈ R2 | ⟨φ(a), φ(b)⟩ ∈ α}, and β′ = {⟨a,b⟩ ∈ R2 | ⟨φ(a), φ(b)⟩ ∈ β}.Every α′- or β′-class D corresponds to the α-, respectively, β-class φ(D) = {φ(a) | a ∈D}, and this correspondence is one-to-one. The δ-classes insideD are also in a one-to-onecorrespondence with the elements of φ(D). This implies the equality of the matrices.

Now take a k-ary relation R pp-definable in H and congruences δ, α′, β′ of R. Firstwe set C = (R; {fC | f ∈ Pol(H)}), where fC acts on k-tuples from R component-wise. SinceR is invariant under all polymorphisms of H these operations are well-defined.Algebra B can be defined as the quotient algebra C/δ, and congruences α, β as follows:α = {⟨aδ,bδ⟩ | ⟨a,b⟩ ∈ α′} and β = {⟨aδ,bδ⟩ | ⟨a,b⟩ ∈ β′}. As before, we have one-to one correspondences between α-, β- and α′-, β′- classes, as well as, between δ-classesand elements of B, which implies the result.

2.6 Outline of the proof

Since the hardness part of Theorem 2.22 follows from the previous result, we only need todesign an algorithm solving #CSP(H) whenever Hid is congruence singular.

The set of all congruences of structure H ordered by inclusion is denoted by Con(H),and is called the congruence lattice of H (for more details see the next section). The leastelement of Con(H) is the equality relation, denoted by ∆, and the greatest element is thetotal relation, denoted by ▽. We start with choosing a chain ∆ = θ0 ≤ θ1 ≤ . . . ≤ θk = ▽Journal of the ACM, Vol. V, No. N, 20YY.

Page 13: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 13

in Con(H). For an instance G of #CSP(H) and s ∈ {0, . . . , k}, let τ be a mapping fromG to H/θs. By Φ(G,H; τ) we denote the set of homomorphisms φ from G to H (that is,elements of Φ(G,H)) such that φ(g)θs = τ(g) (we say that φ agrees with τ ).

Obviously, if s = k then there is only one mapping τ : G → H/θs, as H/θs is 1-elementstructure. So in this case Φ(G,H; τ) = Φ(G,H). On the other hand, if s = 0 then anyhomomorphism τ : G → H/θs is a homomorphism of G to H and thus |Φ(G,H; τ)| =1. The algorithm reduces finding the number |Φ(G,H; τ)| for τ : G → H/θs to finding|Φ(G,H; τi)| for τi : G → H/θs−1

, i ∈ [r], for a small number r. As we shall see, r isbounded from above by a linear polynomial in |G|. Since the depth of recursion dependsonly on H, and therefore is constant, this gives a polynomial time algorithm for finding|Φ(G,H)|.

In the next section we show that the chain ∆ = θ0 ≤ θ1 ≤ . . . ≤ θk = ▽ can bechosen such that every interval θs−1 ≤ θs has one of the two types: Boolean or affine. Thereduction to lower level mappings mentioned above is carried out differently depending onthe type of the interval θs−1 ≤ θs.

If the type of θs−1 ≤ θs is Boolean then for any homomorphism τ : G → H/θi thenumbers |Φ(G,H;φ)| for homomorphisms φ : G → H/θs−1

that agree with τ can bearranged into a multidimensional array of rank 1. The number |Φ(G,H; τ)| is then the sumof all entries in this array, and can be found using linear dependencies between rows of thisarray without finding all its entries.

If the type of θs−1 ≤ θs is affine then, for any homomorphism φ : G → H/θs−1that

agrees with τ , the number |Φ(G,H;φ)| is the same. Therefore we only need to find thenumber of homomorphisms φ : G → H/θs−1

agreeing with τ . This is done by a modifi-cation of the algorithm for the decision CSP from [Bulatov and Dalmau 2006].

3. CONGRUENCE LATTICES AND THE STRUCTURE OF RELATIONS

3.1 Lattices and congruence lattices

In this section we look closer at the family of congruences of a relational structure H.All definitions and results given here were originally introduced for algebras. As our al-gorithms are described in terms of relational structures, we reformulate them in terms ofstructures, replacing congruences of algebras with congruences of structures, and term op-erations of an algebra with polymorphisms of a structure. However, the notions we arriveto for a structure H are exactly the same as those defined for the algebra Alg(H).

The set of all congruences of structure H is denoted by Con(H). Let α, β ∈ Con(H).The intersection of α and β is again a congruence of H and is denoted α ∧ β. As is wellknown, the smallest equivalence relation containing both α and β is the transitive closureof α ∪ β. It can be shown that this equivalence relation is a congruence of H, denotedby α ∨ β. The set Con(H) together with the operations ∧ (meet) and ∨ (join) is calledthe congruence lattice of H. The set Con(H) is naturally ordered by inclusion. The leastelement of Con(H) is the equality relation, denoted by ∆, and the greatest element is thetotal relation, denoted by ▽.

If R is a relation pp-definable in H, then Con(R) denotes the set of all congruences ofR. This set depends on H as well as on R, but usually H is clear from the context. The setCon(R) is also a lattice.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 14: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

14 · Andrei A. Bulatov

Lattices can as well be introduced in an abstract way, as a set along with operations ∧and ∨ satisfying certain conditions, see [Gratzer 2003]. The structure of a lattice allowsone to define a partial order ≤ on L: a ≤ b if and only if a∧ b = a, or, equivalently, a ≤ bif and only if a∨ b = b. Note that a∧ b and a∨ b are the greatest lower and the least upperbound of a, b, respectively, in terms of this order.

We will deal with lattices of several particular types. A lattice L is said to be (a) modularif, for any a, b, c ∈ L such that b ≤ a, the equality a∧ (b∨ c) = b∨ (a∧ c) holds; (b) meetsemi-distributive if, for any a, b, c ∈ L such that a∧b = a∧c, the equality a∧b = a∧(b∨c)holds; (c) distributive if for any a, b, c ∈ L, the equality a∧(b∨c) = (a∧b)∨(a∧c) holds.Modular and distributive lattices are very well studied, see, e.g., [Gratzer 2003, Ch. II,IV]. We will use the following folklore observation: Every modular meet semidistributivelattice is distributive (it is mentioned, for example, in [Jonsson and Rival 1971]2).

One particularly useful property of modular lattices is the following. A pair a, b ofelements from a lattice L is called a prime quotient, denoted a ≺ b, if a ≤ b and there isno c ∈ L such that a ≤ c ≤ b and c = a, b. Suppose a ≤ b. A sequence a = c0 ≺ c1 ≺. . . ≺ ck = b is called a maximal chain from a to b. Observe that such a chain is maximalin the sense that there are no other elements between the ci. Number k is called the lengthof the chain.

PROPOSITION 3.1 (THE JORDAN-HOLDER CHAIN CONDITION, [GRATZER 2003], TH. 1, CH. II.2).For any two elements a ≤ b of a finite modular lattice, all maximal chains from a to b havethe same length.

For elements a, b of a lattice L such that a ≤ b, the interval [a, b] is the set of all c witha ≤ c ≤ b. Intervals [a, b] and [c, d] are said to be perspective if b ∨ c = d, b ∧ c = a ora ∨ d = b, a ∧ d = c (see Fig. 5(a)). Thus perspectivity is a binary relation on the set ofintervals of L. Two intervals that belong to the transitive closure of this relation are said tobe projective to each other.

01

01

0 01 1 0 01 1 0 01 1

0 01 1

01 010 0

0 0

1 1

1 1

0 01 1

0 0

0 0

1 1

1 1

0 01 1

join−irreducible

(b) (c)

meet−irreducible

(a)

a

b

d

c

Fig. 5. Perspective intervals [a, b] and [c, d] (a), join-irreducible (b), and meet-irreducible elements (c)

3.2 Congruence lattices and types of prime quotients

As usual, by ◦ we denote the product of binary relations: (a, b) ∈ R ◦Q iff there is c suchthat (a, c) ∈ R and (c, b) ∈ Q. If H has a Mal’tsev polymorphism, the set Con(H) cannotbe just an arbitrary collection of equivalence relations. In particular, any two membersα, β of Con(H) must be permutable, that is α ◦ β = β ◦ α. This means that, for any

2In that paper the observation stated in a different but equivalent way: The intersection of the varieties of modularand meet semidistributive lattices is the variety of distributive lattices.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 15: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 15

α-class A and any β-class B belonging the same α ∨ β-class, A ∩ B is non-empty Sinceα ∨ β = α ◦ β ◦ α ◦ . . . (sufficiently many times), congruences α, β are permutable if andonly if α ◦ β = β ◦ α = α ∨ β.

LEMMA 3.2. If a relational structure H is congruence singular [an algebra A gener-ates a congruence singular variety], then it has a Mal’tsev polymorphism [a Mal’tsev termoperation].

Therefore for any relationR pp-definable in H its congruence lattice Con(R) is modular.

PROOF. By the well known result of Mal’tsev [Burris and Sankappanavar 1981, Theo-rem 12.1] an algebra A has a Mal’tsev term operation if and only if any two congruencesof any algebra in the variety generated by A are permutable. Therefore it suffices to provethat if the variety generated by Alg(H) for a structure H is congruence singular then it iscongruence permutable.

Suppose H is congruence singular, B ∈ var(Alg(H)), and α, β ∈ Con(B). If α ⊆ βor β ⊆ α then they are obviously permutable. If the congruences are incomparable thenrank(M(α, β)) = k where k is the number of α ∨ β-classes. If we group the rows andcolumns of matrix M(α, β) according to the α ∨ β-classes, all the non-zero entries ofM(α, β) concentrate in rectangular blocks, the cells of the matrix. Although in generalcells can contain zero entries, the equality rank(M(α, β)) = k implies, in particular, thatall entries in a cell are non-zero. Therefore, for any a, b from the same α ∨ β-class, say,a belongs to α-class A1 and β-class B1, and b belongs to α-class A2 and β-class B2, wehave A1 ∩ B2 = ∅ and A2 ∩ B1 = ∅, as the corresponding entries of M(α, β) must benon-zero. Then ⟨a, b⟩ ∈ α ◦ β, as any c ∈ A1 ∩ B2 witnesses, and ⟨a, b⟩ ∈ β ◦ α, as anyd ∈ A2 ∩B1 witnesses. Thus α ◦ β = β ◦ α = α ∨ β.

The second part of the lemma follows from the observation that Con(R) is the con-gruence lattice of certain algebra in the variety generated by Alg(H) and the fact that thecongruence lattice of a congruence permutable algebra is modular.

A pair of congruences ⟨α, β⟩ is said to be a prime quotient if they form a prime quotientin the congruence lattice.

We shall use some notions and results of tame congruence theory [Hobby and McKenzie1988]. Tame congruence theory is a tool to study a local structure of universal algebrasthrough certain properties of prime quotients of the congruence lattice. We apply thistheory to relational structures that give rise from algebras. In general, this theory identifiesfive possible types of such quotients defined in a fairly sophisticated way. Fortunately, inour case of relational structures with a Mal’tsev polymorphism, only two of those typescan occur, and the definition of these possible types can be significantly simplified.

A prime quotient α ≺ β is said to be of the affine type, if, for any β-class B, thereis a module MB with the base set B/α over a ring RB such that for any f(x1, . . . , xn,y1, . . . , ym) ∈ Pol(H) and a1, . . . , am ∈ H , if the operation g(x1, . . . , xn) = f(x1, . . . , xn,a1, . . . , am) preserves B, then it can be represented as an operation of the module MB :

(gB(x1, . . . , xn))/α = c1x1 + . . .+ cnxn + a.

In all other cases, α ≺ β has the Boolean type. While this definition does not match theone in tame congruence theory, [Gumm 1979] implies that they are equivalent.

Example 3.3. Let L2 be a 2-element relational structure whose relational symbols areinterpreted as solution spaces of systems of linear equations. Then L2 has only two con-

Journal of the ACM, Vol. V, No. N, 20YY.

Page 16: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

16 · Andrei A. Bulatov

κ 3

λ 3��������

����

����

��������

����

����

����

��������

��������

����

����

��

��

��(b)

1

2

3

4

κ 2

λ 2{1,2,4}

{1,2}

{1}

{1,3}

{1,2,3}

{1,2,3,4}

(a)

Fig. 6.

gruences: ∆2, the equality relation, and ∇2, the total binary relation. If L2 has at leastone nontrivial relation (which is not a direct product of unary relations, equalities, anddisequalities), the prime quotient ∆2 ≺ ∇2 is of the affine type, see, e.g., [Szendrei 1986,Chapter 2]. Thus, the affine type corresponds to some kind of “linearity” in a broad sense.

Prime quotients α1 ≺ β1 and α2 ≺ β2 are said to be perspective [projective] if the intervals[α1, β1] and [α2, β2] are perspective [projective] in Con(H).

LEMMA 3.4 ([HOBBY AND MCKENZIE 1988], LEMMA 6.2). If α1 ≺ β1 and α2 ≺β2 are projective quotients in Con(H), then they have the same type.

3.3 Congruence lattices of relational structures with a Mal’tsev polymorphism

We will often distinguish two cases: when the congruence lattice of our relational structureomits the affine type, and when the affine type occurs in this lattice. Note that, sinceby Lemma 3.2 we need to consider only structures with a Mal’tsev polymorphism, allcongruence lattices we consider are modular

3.3.1 Distributive lattices and structures omitting the affine type. If H omits the affinetype then, by Theorem 9.15(4) of [Hobby and McKenzie 1988], Con(H) is meet semi-distributive, and therefore it is distributive. We will need several properties of distributivelattices. An element a of a lattice L is said to be join-irreducible if it is not the least elementof the lattice, and for any b, c ∈ L such that a = b∨ c either b = a or c = a (see Fig, 5(b)).Element a is said to be meet-irreducible if it is not the greatest element of the lattice, andfor any b, c ∈ L such that a = b ∧ c either b = a or c = a (see Fig, 5(c))

PROPOSITION 3.5 ([GRATZER 2003], THEOREM 9, COROLLARY 11, COROLLARY 14, CH. II.1).(1) For any finite distributive lattice L there is a finite set, Z, and a injective mappingJ : L → 2Z (the set of all subsets) such that J (a ∨ b) = J (a) ∪ J (b) and J (a ∧ b) =J (a) ∩ J (b).

(2) Set Z can be chosen to be J(L), the set of all join irreducible elements of L, andJ (a) to be the set of all b ∈ J(L) with b ≤ a.

(3) If Z is a smallest set representing L, then every maximal chain of L has length |Z|.Example 3.6. The lattice shown in Fig. 6(a) is distributive. Its representation as a lattice

of subsets is shown.

It will be convenient for us to use a particular set representation of elements of Con(H)when it is distributive. For a relational structure H and its congruence lattice Con(H) weJournal of the ACM, Vol. V, No. N, 20YY.

Page 17: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 17

use the following notation. Let C be a maximal chain ∆H = θ0 ≺ θ1 ≺ . . . ≺ θℓ = ▽H .The set Z is defined to be the set of the prime quotients of this chain. The bottom end of aprime quotient ω ∈ Z will be denoted by ω−, and the top one by ω+.

For a congruence θ ∈ Con(H) let Z(θ) denote the set of quotients from Z that areprojective to quotients of the form γ ≺ β ≤ θ. The following proposition comprisesproperties of Con(H) that follow easily from the representation of this lattice as a latticeof subsets.

PROPOSITION 3.7. (1) Every prime quotient α ≺ β in Con(H) is projective to oneand only one interval ω of C, and this interval satisfies the condition J (ω+)− J (ω−) =J (β)− J (α).(2) Mapping Z is a representation of Con(H) by subsets of Z.(3) For any ω ∈ Z, that is, any prime quotient in C, there is a greatest prime quotientκω ≺ λω projective to ω; that is, for any α ≺ β projective to ω we have α ≤ κω andβ ≤ λω.(4) For any ω ∈ Z, the congruence κω is meet-irreducible.

PROOF. Let J denote the set of join-irreducible elements of Con(H), and let mappingJ : Con(H) → 2J be the set representation of Con(H) by J .

(1) Observe first that any prime quotient α ≺ β in Con(H) can be extended to a maximalchain. Since all elements of this chain are different by Proposition 3.5(2), |J (β)−J (α)| =1. Let J (β)−J (α) = {a} ⊆ J . Then there is ω ∈ [k] such that J (ω+)−J (ω−) = {a}.Set η = α ∨ ω− and θ = β ∨ ω−. Clearly, J (θ) = J (η) ∪ {a}, so β ∧ η = α, β ∨ η = θand η ∧ ω+ = ω−, η ∨ ω+ = θ. Intervals [α, β] and [ω−, ω+] are projective.

On the other hand, if two intervals [α, β] and [γ, δ] are perspective, that is, α = β ∧ γ,δ = β∨γ, then J (δ) = J (γ)∪J (β) = J (α)∪(J (β)−J (α))∪J (γ) = J (γ)∪(J (β)−J (α)), and J (β)−J (α) and J (γ) are disjoint. Therefore J (β)−J (α) = J (δ)−J (γ).Since for all ω ∈ Z the sets J (ω+)−J (ω−) are different, this implies that for any primequotient α ≺ β in Con(H) there is at most one ω ∈ Z projective to α ≺ β.

(2) From the proof of part (1) it follows that there is a bijection φ : Z → J such thatJ (ω+)− J (ω−) = {φ(ω)}. It suffices to show that J (α) = {φ(ω) | ω ∈ Z(α)}.

Let β ∈ J (α), that is, β is a join-irreducible element and β ≤ α. Let also β′ be theonly element with β′ ≺ β. Then J (β) − J (β′) = {β}. By part (1) β′ ≺ β is projectiveto ω− ≺ ω+ with β = φ(ω), and so ω ∈ Z(α).

Conversely, if ω ∈ Z(α) then there is a prime quotient β′ ≺ β ≤ α such that J (β) −J (β′) = {φ(ω)}. Hence φ(ω) ∈ J (α).

(3) Let κω be the join of all α ∈ Con(H) such that ω ∈ Z(α). By parts (2) and (3) of theproposition ω ∈ Z(κω). Then set λω = κω ∨ ω+. Since ω ∈ Z(ω−), we have ω− ≤ κωand Z(λω) = Z(κω) ∪ Z(ω−) ∪ {ω} = Z(κω) ∪ {ω}.

Let α ≺ β be a prime quotient projective to ω, that is Z(β) − Z(α) = {ω}. Thenω ∈ Z(α), so α ≤ κω . As Z(β) − Z(α) = Z(λω) − Z(κω), we have β ≤ λω , and bypart (1) α ≺ β and κω ≺ λω are projective.

(4) Suppose κω = α ∧ β. Then ω ∈ Z(α) or ω ∈ Z(β). By the choice of κω , eitherα ≤ κω or β ≤ κω.

3.3.2 Relational structures admitting the affine type. Let us again consider the con-gruence lattice Con(H). A congruence β is said to be solvable over α if there are α =

Journal of the ACM, Vol. V, No. N, 20YY.

Page 18: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

18 · Andrei A. Bulatov

��������

��������

��������

��������

��������

��������

��������

��������

��������

Con( )H

��������

��������

��������

��������

Con( )/H ~s

Fig. 7. Congruence lattice and its quotient lattice modulo s∼. Prime quotients of the affine type are shown bythick lines; the greatest elements in the classes of s∼ are encircled

θ1 ≺ . . . ≺ θk = β such that all the prime quotients θi ≺ θi+1 have the affine type. Thens∼ denotes the binary relation on Con(H) defined as follows: α s∼ β if and only if α ∨ β issolvable over α ∧ β.

PROPOSITION 3.8. (1) s∼ is an equivalence relation and, moreover, a congruence ofCon(H); that is, for any α1, α2, β1, β2 ∈ Con(H) such that α1

s∼ α2, β1s∼ β2, we have

(α1 ∨ β1)s∼ (α2 ∨ β2), (α1 ∧ β1)

s∼ (α2 ∧ β2).(2) Every class S of s∼ has a greatest αS and a least βS elements (with respect to ≤), andequals the interval [βS , αS ]. Every prime quotient inside S has the affine type.(3) The quotient lattice LH = Con(H)/ s∼ is distributive (see Fig. 7).

PROOF. (1) is Lemma 7.4 of [Hobby and McKenzie 1988].(2) The first part follows from the well known fact (see [Gratzer 2003, Chapter 3]) that

every class of any congruence of a finite lattice is an interval, and therefore every class hasa least and a greatest elements. Let α ≺ β be a prime quotient in S. We have α s∼ β, thatis α = α ∧ β and α ∨ β = β are connected with a chain of prime quotients of the affinetype. However, Con(H) is modular, hence α ≺ β is the only such chain.

(3) Theorem 7.7(2) from [Hobby and McKenzie 1988] shows that LH is meet semi-distributive. Since Con(H) is modular, so is any its quotient lattice such as LH (see[Gratzer 2003, Chapter 4]), and therefore LH is distributive.

The s∼-class containing congruence α will be denoted by α∼.Proposition 3.8(2) implies that LH can be represented as a lattice of subsets of a finite

set Z. Similar to Subsection 3.3.1, Z can be chosen to be the set of prime quotients of amaximal chain C in LH. Note that the endpoints of ω ∈ Z are sets S1, S2 of congruencesfrom Con(H) (S1 corresponds to the bottom end of ω). By ω− we denote the greatestelement of S1, and by ω+ the least element of S2 such that ω− ≤ ω+. Let β ≺ γ be thegreatest quotient in LH projective to ω. Again, elements β and γ of LH represent setsT1, T2 of congruences from Con(H) (T1 corresponds to β). By κω we denote the greatestelement of T1, and λω the least element in T2 such that κω ≤ λω (see Fig. 8).

PROPOSITION 3.9. (1) Intervals [ω−, ω+] and [κω, λω] are prime quotients.(2) Prime quotient ω− ≺ ω+ is projective to κω ≺ λω .(3) Prime quotients ω− ≺ ω+ and κω ≺ λω have the Boolean type.(4) Congruence κω is meet-irreducible.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 19: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 19

κ

λ

ω

ω

ω

ω

ω

S

ST

T

1

22

1

+

Fig. 8. Congruence lattice and congruences κω , λω . Solid lines represent prime intervals of the Boolean type,ovals represent s∼-classes

PROOF. (1) Let ω− ≤ α ≤ ω+. Since (ω−)∼ ≺ (ω+)

∼, congruence α belongs to oneof the two s∼-classes. It cannot be the case that α ∈ (ω−)

∼ and α = ω−, because ω− isthe greatest element in (ω−)

∼. If α ∈ (ω+)∼ then α = ω+, as ω− ≤ α and ω+ is the least

element in (ω+)∼ with this property.

For κω and λω the argument is the same.(2) Since (ω−)

∼ ≤ (κω)∼ and κω is the greatest element in (κω)

∼, it follows thatω− ≤ κω . Then (κω ∧ ω+)

∼ = (κω)∼ ∧ (ω+)

∼ = (ω−)∼, hence, as ω− is the greatest

element in (ω−)∼, we obtain ω− ≤ κω ∧ ω+ ≤ ω−, that is, κω ∧ ω+ = ω−. Next, (κω ∨

ω+)∼ = (κω)

∼ ∨ (ω+)∼ = (λω)

∼. Since κω ≤ κω ∨ ω+, it follows that κω ∨ ω+ ≥ λω.Thus intervals [ω−, ω+] and [κω, κω ∨ ω+] are projective. By the Isomorphism Theoremfor modular lattices, see Theorem 2, Chapter IV of [Gratzer 2003], they are isomorphic.Hence, as ω− ≺ ω+ is a prime quotient, [κω, κω ∨ ω+] is also a prime quotient, whichimplies κω ∨ ω+ = λω .

(3) If ω− ≺ ω+ or κω ≺ λω had the affine type, the s∼-classes (ω−)∼ and (ω+)

∼,or (κω)∼ and (λω)

∼, respectively, would be equal. A contradiction with the assumptionsmade.

(4) Suppose κω = α ∧ β, then α∼ ∧ β∼ = (κω)∼. By Proposition 3.7 (κω)

∼ is meet-irreducible, therefore α∼ = κ∼ω or β∼ = κ∼ω . If, say, α∼ = (κω)

∼ then α = κω .

3.4 Structure of relations invariant under a Mal’tsev operation

3.4.1 Basic properties. The following proposition contains some basic properties ofrelations invariant under a Mal’tsev operation, which will be constantly used.

PROPOSITION 3.10. Let H be a structure with a Mal’tsev polymorphism m and let Rbe an n-ary relation pp-definable in H. Then for any I ⊆ [n] the following properties hold.

(1) R is rectangular, that is if a,b ∈ prIR, c,d ∈ pr[n]−IR and (a, c), (a,d), (b, c) ∈R, then (b,d) ∈ R.

(2) The relation θI = {⟨a,b⟩ ∈ (prIR)2 | there is d ∈ pr[n]−IR such that (a,d), (b,d) ∈ R}

is a congruence of prIR.(3) There is a one-to-one correspondence π between θI - and θ[n]−I -classes such that R

is a disjoint union of sets of the form B × C, where B and C is a θI - and θ[n]−I -class,respectively, related by π.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 20: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

20 · Andrei A. Bulatov

PROOF. (1) It suffices to observe that

m

((ad

),

(ac

),

(bc

))=

(bd

).

(2) It is straightforward that θI is reflexive and symmetric. If ⟨a,b⟩, ⟨b, c⟩ ∈ θI , say,(a,d), (b,d) ∈ R and (b, e), (c, e) ∈ R, then by rectangularity (a, e) ∈ R implying⟨a, c⟩ ∈ θI . Finally, if, say, I = {1, . . . , k} and [n] − I = {k + 1, . . . , n} then θI isdefined by the pp-formula

θI(x1, . . . , xk, y1, . . . , yk) = ∃xk+1, . . . , xn(R(x1, . . . , xk, xk+1, . . . , xn)

∧R(y1, . . . , yk, xk+1, . . . , xn)).

(3) Let B be a θI -class and C a θ[n]−I -class such that (a,b) ∈ R for some a ∈ B,b ∈ C. Then for any c ∈ C there is d ∈ prIR with (d,b), (d, c) ∈ R. By rectangularitywe get (a, c) ∈ R. Repeating the same argument for tuples from B we conclude B ×C ⊆ R. Finally, if for some a ∈ B there is b ∈ pr[n]−IR − C with (a,b) ∈ R then,as (a, c) ∈ R for any c ∈ C, we have ⟨b, c⟩ ∈ θ[n]−I , contradicting the assumptionb ∈ pr[n]−IR− C.

Binary relations invariant with respect to a Mal’tsev operation have particularly simpleform. Let B1, B2 be subalgebras of H and let α1 ∈ Con(B1), α2 ∈ Con(B2) be such that|B1/α1

| = |B2/α2|. Let also φ be a one-to-one mapping fromB1/α1

toB2/α2. The thick

mapping corresponding to φ is the binary relation R = {(a, b) ∈ B1 × B2 | φ(aα1) =bα2}. Any congruence α is the thick mapping corresponding to the identity mapping onH/α. Proposition 3.10(3) implies the following

COROLLARY 3.11. Let H be a relational structure with a Mal’tsev polymorphism.Then every binary relation R pp-definable in H is a thick mapping.

Indeed, let R be a subdirect product of B1 and B2, and let α1 = θ{1}, α2 = θ{2}. Thenby Proposition 3.10(3) there is a one-to-one correspondence φ between α1- and α2-classessuch that R is a disjoint union of sets of the form B × φ(B), B is an α1-class. Thus R isthe thick mapping corresponding to φ.

We shall use thick mappings throughout the paper. Somewhat related to thick mappingsis the following relation on the set of coordinate positions of a relation. Let R be a k-arysubdirect power of H, and α an equivalence relation on H . By α∗ we denote a relation onthe set [k] defined as follows: i, j are not in α∗ if there are a,b ∈ R such that ⟨a[i],b[i]⟩ ∈α, but ⟨a[j],b[j]⟩ ∈ α, or ⟨a[j],b[j]⟩ ∈ α, but ⟨a[i],b[i]⟩ ∈ α.

3.4.2 The Boolean type and rectangularity properties. Let A be a finite algebra. Alge-bra A is called subdirectly irreducible if there is a congruence µ, the monolith of A, suchthat ∆A ≺ µ and for any congruence γ = ∆A we have µ ≤ γ. Similarly, we call a rela-tional structure H subdirectly irreducible if Con(H) has a monolith, that is a congruenceµ satisfying the conditions above. Observe that Con(H) has a monolith if and only if theleast element of this lattice is meet-irreducible.

Let R ∈ def(H), where H is a subdirectly irreducible structure with a Mal’tsev poly-morphism, be a k-ary subdirect power of H. The equivalence relation µ∗ is defined in thesame way as before. If the prime quotient ∆H ≺ µ has the Boolean type, Lemma 2.7 from[Bulatov 2006a] characterizes µ∗-classes in terms of so-called coherent sets. It shows thatJournal of the ACM, Vol. V, No. N, 20YY.

Page 21: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 21

in this case µ∗-classes are the coherent sets. Then Lemma 2.6 of [Bulatov 2006a] can berestated as follows.

LEMMA 3.12 [BULATOV 2006A, LEMMA 2.6]. LetR be an n-ary subdirect power ofH and the structure H is subdirectly irreducible. Let also µ be its monolith, let prime quo-tient ∆H ≺ µ have the Boolean type, and let I1, . . . , Iℓ be the µ∗-classes (or, equivalently,the coherent sets). Let also B1, . . . , Bn be µ-classes such that R∩ (B1 × . . .×Bn) = ∅,and

RIj = prIjR ∩∏i∈Ij

Bi.

Then R ∩ (B1 × . . .×Bn) = RI1 × . . .×RIℓ .

Recall that for a congruence α ∈ Con(H), we denote by αn the congruence of R con-sisting of pairs ⟨a,b⟩ of tuples such that ⟨a[i],b[i]⟩ ∈ α for all i ∈ [n].

COROLLARY 3.13. Let H be a structure with a Mal’tsev polymorphism, let M be amaximal chain in Con(H), let R be an n-ary subdirect power of H and ω ∈ M . Let alsoB1, . . . , Bn be some classes of λω and I1, . . . , Iℓ the classes of λ∗ω . Let also R′ = R/κnω

,

where R/κnω= {((a[1])κω , . . . , (a[n])κω ) | a ∈ R}, and B′

i = Bi/κωfor i ∈ [n]. Then

either R ∩ (B1 × . . .×Bn) = ∅, or

R′ ∩ (B′1 × . . .×B′

n) = R′I1 × . . .×R′

Iℓ,

where R′Ij

= prIjR′ ∩

∏i∈Ij

B′i.

PROOF. Relation R′ can be treated as a subdirect power of H/κω . Since κω is meet-irreducible by Proposition 3.9(4), the congruence lattice of structure H/κω has a monolith,λω , and therefore is subdirectly irreducible. Now the result follows straightforwardly fromProposition 3.9(3) and Lemma 3.12.

REMARK 3.14. Another way to state Corollary 3.13 is the following. Let i1, . . . , iℓ berepresentatives of the λ∗ω-classes. Then for any choice of κω-classes a′im ∈ B′

im, m ∈ [ℓ],

there is a ∈ R such that a[im] ∈ a′im for all m ∈ [ℓ].

4. CONSEQUENCES OF CONGRUENCE SINGULARITY

In this section we prove two further conditions for #-tractability that follow from congru-ence singularity, Proposition 2.16. They help us to design an algorithm for #CSP.

If the algebra corresponding to a structure H does not omit the affine type, then we havea stronger necessary condition for the tractability of #CSP(H).

PROPOSITION 4.1. If H is congruence singular then for any congruences δ ≤ α <β ∈ Con(H) such that α s∼ β, any n-ary relation R ∈ def(H), and any sequencesA1, . . . , An and B1, . . . , Bn of α-classes such that Ai, Bi belong to the same β-class foreach i ∈ [n], if R1 = R ∩ (A1 × . . .× An) = ∅ and R2 = R ∩ (B1 × . . .× Bn) = ∅,then |R1/δn| = |R2/δn|.

Suppose that Proposition 4.1 is proved in the case α ≺ β, that is, the following lemmais true (we prove it later).

Journal of the ACM, Vol. V, No. N, 20YY.

Page 22: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

22 · Andrei A. Bulatov

LEMMA 4.2. If H is congruence singular then for any congruences δ ≤ α ≺ β ∈Con(H) such that α ≺ β has the affine type, any n-ary relation R ∈ def(H), and anysequencesA1, . . . , An andB1, . . . , Bn of α-classes such thatAi, Bi belong to the same β-class for all i ∈ [n], ifR1 = R∩(A1×. . .×An) = ∅ andR2 = R∩(B1×. . .×Bn) = ∅,then |R1/δn| = |R2/δn|.

Then the general case follows.

PROOF OF PROPOSITION 4.1. We proceed by induction on the length of a maximalchain α = α1 ≺ . . . ≺ αk = β. Lemma 4.2 provides the base case of induction. Supposethat the proposition is proved for δ ≤ α < γ where γ ≺ β. That is for any sequencesA′

1, . . . , A′n and B′

1, . . . , B′n of α-classes such that Ai, Bi belong to the same γ-class for

each i ∈ [n], if R′1 = R ∩ (A′

1 × . . . × A′n) = ∅ and R′

2 = R ∩ (B′1 × . . .× B′

n) = ∅,then |R′

1/δn| = |R′2/δn|.

Let A′′i , B

′′i be the γ-classes containing Ai, Bi, respectively, and R′′

1 = R ∩ (A′′1 ×

. . . × A′′n), R

′′2 = R ∩ (B′′

1 × . . . × B′′n). Since γ ≺ β and this prime quotient has

the affine type, we can apply Lemma 4.2 to the triple of congruences δ ≤ γ ≺ β toobtain |R′′

1/δn| = |R′′2/δn|. Then we apply Lemma 4.2 to the triple of congruences α ≤

γ ≺ β, and obtain the equality |R′′1/αn| = |R′′

2/αn|; denote this number by N . By theinduction hypothesis, every αn-class inside R′′

1 (and inside R′′2 ) contains the same number

of δn-classes. Therefore |R′′1/δn| = N · |R1/δn| and |R′′

2/δn| = N · |R2/δn|. Equality|R1/δn| = |R2/δn| follows.

To prove Lemma 4.2 we make use of some basics of commutator theory in congruencemodular varieties (see [Freese and McKenzie 1987]). As usual we introduce all requirednotions for relational structures rather than for algebras. Let H be a relational structurewith a Mal’tsev polymorphism m, R ∈ def(H) a k-ary relation, and α, β, γ congruencesof R. Congruence α centralizes β modulo γ, denoted C(α, β; γ), if, for any (n-ary) poly-morphism f of H, any ⟨u,v⟩ ∈ α and any ⟨a1,b1⟩, . . . , ⟨an−1,bn−1⟩ ∈ β,

⟨f(u,a1, . . . ,an−1), f(u,b1, . . . ,bn−1)⟩ ∈ γ

⇐⇒ ⟨f(v,a1, . . . ,an−1), f(v,b1, . . . ,bn−1)⟩ ∈ γ.

The smallest congruence γ such that C(α, β; γ) is called the commutator of α, β, denoted[α, β].

Example 4.3. We illustrate the definition above with the following example. Let Hbe a 3-element structure with the universe H = {0, 1, 2} and 4-ary relation R defined asfollows. Let A0 = {0} and A1 = {1, 2}. Then

R =∪

a+b≡c+d (mod 2)

Aa ×Ab ×Ac ×Ad.

Consider unary relation H . Set β = ▽H , and set α to be the congruence with classesA0, A1. Observe that α is a congruence, since it is given by the following pp-formula

α(x, y) = ∃zR(x, y, z, z).

It is not hard to show that the polymorphisms of H are the operations f(x1, . . . , xn)satisfying the following condition: there is an operation g(y1, . . . , yn) on {0, 1} such that(a) g(y1, . . . , yn) = e1y1 + . . .+ enyn + e (mod 2), and (b) if xi ∈ Ayi for i ∈ [n] thenf(x1, . . . , xn) ∈ Ag(y1,...,yn).Journal of the ACM, Vol. V, No. N, 20YY.

Page 23: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 23

We show that [β, β] ≤ α. Let f(x1, . . . , xn) be a polymorphism of H and g(y1, . . . , yn) =e1y1+. . .+enyn+e the corresponding linear operation on {0, 1}. Let also u, v, a1, . . . , an−1,b1, . . . , bn−1 ∈ H be such that ⟨u, v⟩ ∈ β and ⟨ai, bi⟩ ∈ β (as β is the total relation,these are just any elements of H). Let u ∈ Au′

, v ∈ Av′and ai ∈ Aa′

i , bi ∈ Ab′i fori ∈ [n − 1]. If ⟨f(u, a1, . . . , an−1), f(u, b1, . . . , bn−1)⟩ ∈ α then g(u′, a′1, . . . , a

′n−1) =

g(u′, b′1, . . . , b′n−1). Using the linearity of g we have e2a′1 + . . . + ena

′n−1 + e = e2b

′1 +

. . . + enb′n−1 + e (mod 2). Therefore g(v′, a′1, . . . , a

′n−1) = g(v′, b′1, . . . , b

′n−1), and

so ⟨f(v, a1, . . . , an−1), f(v, b1, . . . , bn−1)⟩ ∈ α. The converse implication is similar.

The next proposition follows from Proposition 4.3 and Theorem 4.9 of [Freese andMcKenzie 1987], Theorem 7.2 of [Hobby and McKenzie 1988]

PROPOSITION 4.4. Let H be a relational structure with a Mal’tsev polymorphism,R ∈def(H) a (k-ary) relation, and α, β congruences of R. Then(1) [α, β] = [β, α] (follows from [Freese and McKenzie 1987, Proposition 4.3]);(2) if α ≺ β, then this prime quotient has the affine type if and only if [β, β] ≤ α (followsfrom [Hobby and McKenzie 1988, Theorem 7.2]);(3) if α ≤ β and [β, β] ≤ α, there is a congruence θ of β (where β is considered as a2k-ary relation from def(H)) such that the set {(a,b) | ⟨a,b⟩ ∈ α} is a class of θ. (Usingthe notation from [Freese and McKenzie 1987, Theorem 4.9], θ = ∆θ,θ ∨ α2.)

Now we are in a position to prove Lemma 4.2.

PROOF OF LEMMA 4.2. By switching to the quotient structure H/δ we may assumethat δ is the equality relation. To prove Lemma 4.2 we consider several congruences ofR, including αn and βn. As we are concerned about α-classes within some β-classes,we can restrict R to a single βn-class. By Lemma 2.20 every βn class of R is a relationpp-definable in H, so let R′ be an arbitrary such class.

CLAIM 1. [βn, βn] ≤ αn.Let f be a (k-ary) polymorphism of H, and let ⟨u,v⟩ ∈ βn and ⟨a1,b1⟩, . . . , ⟨ak−1,bk−1⟩ ∈

βn where u,v,ai,bi ∈ R′ for i ∈ [k− 1]. If ⟨f(u,a1, . . . ,ak−1), f(u,b1, . . . ,bk−1)⟩ ∈αn then ⟨f(u[i],a1[i], . . . ,ak−1[i]), f(u[i],b1[i], . . . ,bk−1[i])⟩ ∈ α for each i ∈ [n].Since C(β, β;α), this implies ⟨f(v[i],a1[i], . . . ,ak−1[i]), f(v[i],b1[i], . . . ,bk−1[i])⟩ ∈α for each index i ∈ [n]. Thus ⟨f(v,a1, . . . ,ak−1), f(v,b1, . . . ,bk−1)⟩ ∈ αn.

Every αn-class ofR′ has the formR′∩(A1×. . .×An) for certain α-classesA1, . . . , An.Let C1, . . . , Ck be the αn-classes of R′, and |Ci| = ℓi. We have to prove that ℓi = ℓj forany i, j ∈ [k].

We treat the congruence βn restricted onto R′ as a 2n-ary relation pp-definable in H; letus denote it by Q. By the choice of R′ we have Q = R′2. Proposition 4.4(3) implies thatthere is a congruence γ of Q such that the set D of pairs of the form (a,b), a,b ∈ R′ and⟨a,b⟩ ∈ αn, is a γ-class. Let γ′ = γ ∨ α2n.

CLAIM 2. (1) Every class E of γ′ is the union (C1 × CφE(1)) ∪ . . . ∪ (Ck × CφE(k)) fora certain bijective mapping φE : [k] → [k].(2) The set D is a class of γ′; and for this class φD is the identity mapping.

Note that for any tuples a,b, c,d ∈ R such that a, c ∈ Ci and b,d ∈ Cj we have⟨(a,b), (c,d)⟩ ∈ α2n.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 24: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

24 · Andrei A. Bulatov

We start with (2). Clearly, D has the required form of a union for the identity mappingφD. Since D is a class of γ and a union of α2n-classes, it is a class of γ ∨ α2n = γ′.

(1) It suffices to prove three claims: (a) for any Ci, Cj , if (Ci × Cj) ∩ E = ∅ thenCi ×Cj ⊆ E; (b) if (a,b), (c,d) ∈ E and ⟨a, c⟩ ∈ αn, then ⟨b,d⟩ ∈ αn; and (c) for anyCi there is Cj such that (Ci × Cj) ∩ E = ∅.

Property (a) follows from the inclusion α2n ≤ γ′.To prove (b) suppose that there are (a,b), (c,d) ∈ E such that ⟨a, c⟩ ∈ αn, but ⟨b,d⟩ ∈

αn. As α2n ≤ γ′, we may assume a = c. Since γ′ is a congruence on Q, and thereforeis reflexive, (a,a,a,a), (a,b,a,b) ∈ γ′, considering γ′ as a 4-ary relation on R′. As(b,b), (d,d) ∈ D and γ ≤ γ′, the tuple (b,b,d,d) is also from γ′. Finally, by (a)(a,b,a,d) ∈ γ′. Then we have

m

aaaa

abad

bbdd

=

bada

∈ γ′ and m

abab

aaaa

bada

=

bbdb

∈ γ′,

which implies that (b,d) ∈ D, and therefore ⟨b,d⟩ ∈ αn, a contradiction.To prove (c) suppose that, for someCi and for anyCj , (Ci×Cj)∩E = ∅. Take a ∈ Ci

and (b, c) ∈ E. Then (b, c,b, c), (b,b,b,b), (a,a,b,b) ∈ γ′ (the last tuple belongs toγ′ because (a,a), (b,b) ∈ D). We have

m

bcbc

bbbb

aabb

=

adbc

∈ γ,

where d = m(c,b,a). Thus (a,d) ∈ E, a contradiction

Suppose that ℓi = ℓj for some i, j ∈ [k]; clearly if such i, j exist we can choose i = 1.Without loss of generality we also assume ℓ1 < ℓj . We present a pair of congruences ofQ that violate the condition of Proposition 2.16. One of them is γ′ the other one is β′

defined to be the congruence αn × βn. In other words, ⟨(a,b), (c,d)⟩ ∈ β′ if and only if⟨a, c⟩ ∈ αn. It is not hard to see that γ′ ∨ β′ = βn × βn and γ′ ∧ β′ = αn × αn. Indeed,α2n ≤ γ′ ∧ β′. If ⟨(a,b), (c,d)⟩ ∈ γ′ ∧ β′ then ⟨a, c⟩ ∈ αn, since ⟨(a,b), (c,d)⟩ ∈ β′,and by Claim 2 this implies ⟨b,d⟩ ∈ αn. Thus γ′ ∧ β′ ≤ αn × αn. Let (a,b), (c,d) ∈Q. As β2n is the total binary relation on Q these pairs are in the same β2n-class. ByClaim 2 there is e ∈ R′ such that ⟨(a,b), (c, e)⟩ ∈ γ′. Since ⟨(c, e), (c,d)⟩ ∈ β′ we have⟨(a,b), (c,d)⟩ ∈ γ′ ◦ β′ ⊆ γ′ ∨ β′.

Every class of αn ×αn is the Cartesian product of two classes Ci, Cj of αn. Therefore,its cardinality equals ℓiℓj . Thus, the row of the matrix M(γ′, β′) corresponding to a γ′-class E looks as follows (observe that since M(γ′, β′) is the matrix of a single congruenceclass, all its entries are positive)(

ℓ1ℓφE(1) ℓ2ℓφE(2) · · · ℓkℓφE(k)

).

The row corresponding to the class D is(ℓ21 ℓ22 · · · ℓ2k

).

As Q = R′2, there is a γ′-class E such that C1 × Cj ⊆ E (recall that ℓ1 < ℓj). SinceH is congruence singular, the rows of M(γ′, β′) corresponding to classes D and E areJournal of the ACM, Vol. V, No. N, 20YY.

Page 25: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 25

proportional, that is

ℓ1ℓφE(1)

=ℓ2

ℓφE(2)= . . . =

ℓkℓφE(k)

.

Let j1 = 1, j2 = φE(1) = j, and jt = φE(jt−1) for t > 2. Let alsom > 1 be the minimalnumber such that jm = 1. We prove ℓjt > ℓjt−1 that leads to a contradiction, as it wouldimply that ℓ1 < ℓjm = ℓ1. By the assumption made ℓj1 = ℓ1 < ℓj = ℓj2 , which gives usthe base case. From the equalities above we have ℓ2jt = ℓjt−1ℓjt+1 . Therefore if ℓjt−1 < ℓtthen ℓt < ℓjt+1 , which proves the induction step.

Example 4.3 (continued). Reconsider the relational structure H from Example 4.3. ByProposition 4.1 the problem #CSP(H) is #P-complete. Indeed, consider congruences αand β = ▽H of H . We showed that [β, β] ≤ α, therefore by Proposition 4.4, primequotient α ≺ β has the affine type. Setting δ = ∆H we see that α-classes A0 and A1

contain different number of elements.The construction used in the proof of Proposition 4.1 in this case looks as follows. Con-

gruence β is the binary relation H2. Congruence γ′ of β such that D = {(0, 0), (1, 1),(1, 2), (2, 1), (2, 2)} is its class can be chosen to be the congruence with classes D andE = {(0, 1), (0, 2), (1, 0), (2, 0)}; and it is easy to see that we can use R defined inExample 4.3 for that. Finally, the classes of β′ = α × β are {(0, 0), (0, 1), (0, 2)} and{(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)}. Therefore

M(R; γ′, β′;∆H) =

(1 42 2

),

and its rank equals 2.

We will also need another corollary from Proposition 2.16. Let T be a k-dimensionalarray, that is a collection of numbers T [i1, . . . , ik] indexed by tuples (i1, . . . , ik), where1 ≤ ik ≤ mk. Array T has rank 1, denoted rank(T ) = 1, if for each ℓ ∈ [k] there arenumbers tℓ1, . . . , t

ℓmk

such that

T [i1, . . . , ik] = t1i1 · . . . · tkik.

Observe that if k = 2, and thus T is a matrix, T has rank 1 in the sense introduced aboveif and only if T has the row- (column-) rank 1.

LEMMA 4.5. Array T has rank 1 if and only if for each ℓ ∈ [k], and any i1, . . . , iℓ−1,iℓ+1, . . . , ik, j1, . . . , jℓ−1, jℓ+1, . . . , jk with iu, ju ∈ [mu], we have

T [i1, . . . , iℓ−1, 1, iℓ+1, . . . , ik]

T [j1, . . . , jℓ−1, 1, jℓ+1, . . . , jk]= . . . =

T [i1, . . . , iℓ−1,mℓ, iℓ+1, . . . , ik]

T [j1, . . . , jℓ−1,mℓ, jℓ+1, . . . , jk]. (1)

PROOF. If rank(T ) = 1 then equations (1) are trivially true. To prove the converse weobserve that equations (1) implies that for any i1, . . . , ik and ℓ ∈ [k]

T [i1, . . . , ik] = T [i1, . . . , iℓ−1, 1, iℓ+1, . . . , ik] ·T [1, . . . , 1, iℓ, 1, . . . , 1]

T [1, . . . , 1].

Therefore

T [i1, . . . , ik] = T [i1, 1, . . . , 1] ·k∏

ℓ=2

T [1, . . . , 1, iℓ, 1, . . . , 1]

T [1, . . . , 1].

Journal of the ACM, Vol. V, No. N, 20YY.

Page 26: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

26 · Andrei A. Bulatov

γ γ γ

γ γ γ γ γ γ

1 2 3

1 2 2 331

γ γ1 2

γ3

γ2 3γγ

1

Fig. 9. Congruences satisfying condition (2)

Choosing t1i = T [i, 1, . . . , 1] for i ∈ [mi] and tji = T [1,...,1,i,1,...,1]T [1,...,1] for 2 ≤ j ≤ k and

i ∈ [mj ] we obtain the result.

Now let R be a relation pp-definable in a structure H with a Mal’tsev polymorphism,and let γ1, . . . , γk be congruences on R such that for each i ∈ [k]

γi ∨ (γ1 ∧ . . . ∧ γi−1 ∧ γi+1 ∧ . . . ∧ γk) = γ1 ∨ . . . ∨ γk (2)

Condition (2) means that the sublattice of Con(H) generated by γ1, . . . , γk is close tothe lattice of subsets of a k-element set (Fig. 9), and that each (γ1 ∨ . . . ∨ γk)-class ofAlg(H)/γ1 ∧ . . . ∧ γk is a direct product of Alg(H)/γi

, i ∈ [k], see [Burris and Sankap-

panavar 1981]. Let also C be a class of γ = γ1 ∨ . . . ∨ γk, and let Ai1, . . . , A

imi

be theclasses of γi from C. Condition (2) means that for any j1, . . . , jk the set A1

j1∩ . . . ∩ Ak

jkis a nonempty class of β = γ1 ∧ . . . ∧ γk. Indeed, let ℓ be the smallest number such thatfor certain j1, . . . , jℓ the set A1

j1∩ . . . ∩Aℓ

jℓ= ∅. Then for any a,b ∈ C we have

⟨a,b⟩ ∈ γℓ ∨ (γ1 ∧ . . . ∧ γℓ−1 ∧ γℓ+1 ∧ . . . ∧ γk) ≤ γℓ ∨ (γ1 ∧ . . . ∧ γℓ−1).

Moreover, as congruences of R are permutable, ⟨a,b⟩ ∈ γℓ ◦ (γ1 ∧ . . . ∧ γℓ−1). Takea ∈ Aℓ

j and b ∈ A1j1∩ . . .∩Aℓ−1

jℓ−1, a class of γ1 ∧ . . .∧γℓ−1. Then there exists c such that

c ∈ Aℓj and c ∈ A1

j1∩ . . . ∩ Aℓ−1

jℓ−1, a contradiction. It is also clear that any two classes of

this form are different. We consider a k-dimensional array M(C; γ1, . . . , γk), where

M(C; γ1, . . . , γk)[i1, . . . , ik] = |A1i1 ∩ . . . ∩A

kik|.

PROPOSITION 4.6. Let γ1, . . . , γk be congruences of a structure H that has a Mal’tsevpolymorphism, let them satisfy condition (2), and let C be a class of γ1 ∨ . . . ∨ γk. Then,if H is congruence singular then rank(M(C; γ1, . . . , γk)) = 1.

PROOF. We consider congruences γi and βi = γ1∧ . . .∧γi−1∧γi+1∧ . . .∧γk. To sim-plify the notation we assume i = k. If H is congruence singular, then rank(M(C; γk, βk; ∆H)) =1. Let Aj

1, . . . , Ajmj

be the classes of γj from C. The classes of βk have the formA1

i1∩ . . . ∩ Ak−1

ik−1, the classes of γk ∧ βk are the classes of γ1 ∧ . . . ∧ γk. Therefore

every row of M(C;βk, γk;∆H) is equal to

(M(C; γ1, . . . , γk)[i1, . . . , ik−1, 1], . . . ,M(C; γ1, . . . , γk)[i1, . . . , ik−1,mk])

Journal of the ACM, Vol. V, No. N, 20YY.

Page 27: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 27

for some i1, . . . , ik−1. Since rank(M(C; γk, βk)) = 1, we get

M(C; γ1, . . . , γk)[i1, . . . , ik−1, 1]

M(C; γ1, . . . , γk)[j1, . . . , jk−1, 1]= . . . =

M(C; γ1, . . . , γk)[i1, . . . , ik−1,mk]

M(C; γ1, . . . , γk)[j1, . . . , jk−1,mk],

for any j1, . . . , jk−1, js ∈ [ms]. The proposition is proved.

An important example of a collection of congruences satisfying condition (2) is thefollowing (we prove it in Section 5.3). Let ω ∈ M , and let I1, . . . , Ik be the classes ofκ∗ω . Congruence γj is given by: ⟨a,b⟩ ∈ γj if and only if ⟨a[i],b[i]⟩ ∈ ω− for i ∈ Ij and⟨a[i],b[i]⟩ ∈ ω+ otherwise.

5. ALGORITHMS: PREREQUISITES

5.1 Decision CSPs over structures with a Mal’tsev polymorphism.

If a relational structure H has a Mal’tsev polymorphism, then the decision CSP with thetemplate H can be solved in polynomial time [Bulatov 2002b; Bulatov and Dalmau 2006].Here we shall use the algorithm presented in [Bulatov and Dalmau 2006], and we call itMAL’TSEV. This algorithm builds a sort of a succinct (polynomial size) representation forthe set of all solutions.

Let n be a positive integer, let H be a finite set, let a, b be n-tuples of elements of H ,and let (i, a, b) be any element in [n] × H2. We say that pair ⟨a,b⟩ witnesses (i, a, b) ifpr[i−1]a = pr[i−1]b, a[i] = a, and b[i] = b. We also say that a and b witness (i, a, b)meaning that ⟨a,b⟩ witnesses (i, a, b).

Let R be any n-ary relation on H . The signature of R, SigR ⊆ [n] ×H2, is defined tobe the set containing all triples (i, a, b) ∈ [n]×H2 witnessed by tuples in R, that is

SigR = {(i, a, b) ∈ [n]×H2 | there are a,b ∈ R such that ⟨a,b⟩ witnesses (i, a, b)}.

Note that in our notation (i, a, b) ∈ SigR if and only if ⟨a, b⟩ belongs to the relation θicomputed for the relation pr[i]R (see Section 3.4.1). In particular, as H has a Mal’tsevpolymorphism, relation pr[i]R is rectangular, and hence for any (i, a, b) ∈ SigR and anya ∈ pr[i]R with a[i] = a, the tuple b such that pr[i−1]b = pr[i−1]a and b[i] = b alsobelongs to pr[i]R.

A subset R′ of R is called a representation of R if SigR′ = SigR. If furthermore,|R′| ≤ 2|SigR| then R is called a compact representation of R. Observe that every relationR has a compact representation.

Let H be a relational structure and R′ ⊆ Hn for some n. By ⟨R′⟩H we denote therelation generated by R′, that is, the smallest relation R pp-definable in H and suchthat R′ ⊆ R. Alternatively, ⟨R′⟩H can be constructed from R′ by adding every tuplea that can be obtained as f(a1, . . . ,an) where f is an (n-ary) polymorphism of H anda1, . . . ,an ∈ R′. Since H is usually clear from the context we shall omit this subscript.The key lemma proved in [Bulatov and Dalmau 2006] states that if R is a relation pp-definable in a relational structure with a Mal’tsev polymorphism, and R′ is a representa-tion of R, then ⟨R′⟩ = R. Note that every member of ⟨R′⟩ in this case can be obtainedin a certain regular way by repeated applications of the Mal’tsev polymorphism. Given aninstance G of the constraint satisfaction problem CSP(H), m = |G|, the set of all solutionsΦ(G,H) to this problem can be thought of as an m-ary relation pp-definable in H. Thealgorithm presented in [Bulatov and Dalmau 2006] finds a compact representation of thisset.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 28: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

28 · Andrei A. Bulatov

We will need to know the unary and binary projections of the relation Φ(G,H), that is,sets of the form Φg = {φ(g) | φ ∈ Φ(G,H)} for g ∈ G, and Φg,h = {(φ(g), φ(h)) | φ ∈Φ(G,H)} for g, h ∈ G. Let R′ be a compact representation of Φ(G,H). If a ∈ Φg then(g, a, a) ∈ SigΦ(G,H), so Φg = pr{g}R

′. It is also not hard to see (see also [Bulatov andDalmau 2006]) that Φg,h is equal to ⟨prg,hR′⟩. Since the structure H is fixed, we assumethat we have all necessary information about H. This includes the relation ⟨R⟩ generatedby any set R ⊆ H2. Therefore, we assume the relation Φg,h can be found using a compactrepresentation R′ in linear time.

5.2 Reduction to subdirect powers.

In general, for an instance G of #CSP(H) the sets Φg , g ∈ G, are subalgebras of H thatare not necessarily equal to H. For us, however, it is much more convenient to deal withthe case when Φ(G,H) is a subdirect power of H, that is Φg = H for all g ∈ G. We showhow to transform the problem so that Φg is H for all g ∈ G. To do this we borrow somemethods from the multi-sorted CSP, see, e.g. [Bulatov and Jeavons 2003].

Let D1, . . . , Dℓ be the nonempty subalgebras of H (including H itself). We define arelational structure χ(H) as follows. The universe of χ(H) is D = D1 × . . . × Dℓ; theith component of an element a ∈ D is denoted by a[i]. For any (n-ary) relation R pp-definable in H and such that prjR = Dij , we set (a1, . . . , an) ∈ χ(R) if and only if(a1[i1], . . . , an[in]) ∈ R. In particular, each unary relation of χ(H) corresponding to arelation of H contains all elements of D and, therefore, are redundant in the new structure(however, they are still needed for reductions between #CSP(H) and #CSP(χ(H))) . Forany coordinate position i of any non-unary relation R, the set priχ(R) equals D. Finally,to define χ(H) formally, for each relational symbol R, we interpret it as Rχ(H) = χ(R).

It is sometimes useful to replace a relational structure H with its expansion. Let H bea relational structure with vocabulary τ and universe H . Structure H′ is said to be anexpansion of H if it has the same universe H , and vocabulary τ ′ ⊇ τ , where every symbolfrom τ is interpreted in H′ in the same way as in H. An expansion of a structure can bethought of as throwing in some extra relations. If all the added relations are pp-definablein H then #CSP(H′) is polynomial time reducible to #CSP(H). Therefore expanding astructure by adding pp-definable relations does not change the complexity of the problem[Bulatov and Dalmau 2007]. By taking an expansion of H if necessary, we shall assumethat along with every (n-ary) relational symbol R and any subalgebras Di1 , . . . , Din thevocabulary of H contains a symbol R′ such that R′H = R ∩ (Di1 × . . .×Din).

LEMMA 5.1. A relational structure H is congruence singular if and only if χ(H) iscongruence singular.

PROOF. Suppose first that H is congruence singular. Let R be an n-ary relation over D.It naturally defines an ℓn-ary relation fla(R) over H that we call flattening of R:

fla(R) = {fla(a) ∈ Hℓn | there is a ∈ R such that(fla(a)[ℓ(j − 1) + 1], . . . , fla(a)[ℓj]) = a[j] for each j ∈ [n]}.

As is easily seen, fla is a one-to-one mapping between the set of n-tuples and the set ofℓn-tuples, and also between n-ary and ℓn-ary relations.

CLAIM 1. |fla(R)| = |R|.CLAIM 2. If R is pp-definable in χ(H) then fla(R) is pp-definable in H.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 29: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 29

The following convention for indexing variables of predicates will be helpful. If R isn-ary and R(x1, . . . , xn) is the corresponding predicate, we use fla(R)(x11, . . . , x

ℓ1, . . . ,

x1n, . . . , xℓn) for the predicate corresponding to fla(R).

First, we prove the claim for a relation R = χ(R′) where R′ is a relation from H.Suppose that R′ is n-ary and prjR

′ = Dij for j ∈ [n]. It is not hard to see that

fla(R)(x11, . . . , xℓ1, . . . , x

1n, . . . , x

ℓn) = R′(xi11 , . . . , x

inn ) ∧

n∧j=1

ℓ∧i=1

Di(xij).

Now we proceed by induction on the structure of a pp-definition of R. If R = R1 ∧ R2

then fla(R) = fla(R1) ∧ fla(R2). If R(x1, . . . , xn) = ∃yR′(x1, . . . , xn, y) then

fla(R)(x11, . . . , xℓ1, . . . , x

1n, . . . , x

ℓn) = ∃y1, . . . , yℓ

fla(R′)(x11, . . . , xℓ1, . . . , x

1n, . . . , x

ℓn, y

1, . . . , yℓ) ∧ℓ∧

i=1

Di(yi).

CLAIM 3. Let R ∈ def(χ(H)) be an n-ary relation, α, β its congruences. Then (a) forany binary relation θ on R and any a,b ∈ R, ⟨a,b⟩ ∈ θ if and only if ⟨fla(a), fla(b)⟩ ∈fla(θ); (b) relations fla(α), fla(β) are congruences of fla(R), (c) equalities fla(α ∧ β) =fla(α) ∧ fla(β), fla(α ∨ β) = fla(α) ∨ fla(β) hold, and (d) the number of α- [β-] classesequals that of fla(α)- [respectively, fla(β)-] classes, and |B| = |fla(B)| for each α- [β-]class B.

(a) follows from the observation that fla(a,b) = (fla(a), fla(b)) for any a,b ∈ R.(b) To prove it use part (a) along with Claim 2.(c) Note that if fla(a) = fla(b) then a = b. Hence, fla(α ∧ β) = fla(α ∩ β) =

fla(α) ∩ fla(β) = fla(α) ∧ fla(β). To prove fla(α ∨ β) = fla(α) ∨ fla(β) we can use (a) toshow that transitive closure is preserved by fla, that implies the result.

(d) For any α-class B by Claim 1 we have |B| = |fla(B)|. Using (a) we can also finda one-to-one correspondence between α- and fla(α)-classes [respectively, β- and fla(β)-classes].

Finally, let α, β, and δ with δ ≤ α, β be congruences of R, and let A1, . . . , Am andB1, . . . , Bk be the α- and β-classes respectively. Then fla(A1), . . . , fla(Am) andfla(B1), . . . , fla(Bℓ) are the fla(α)- and fla(β)-classes, respectively. Moreover, the num-ber of δ-classes in each α ∧ β-class B is equal to that of fla(δ)-classes in fla(B), andthe number of α ∨ β-classes is equal to the number of fla(α) ∨ fla(β)-classes. ThereforeM(R;α, β; δ) =M(fla(R); fla(α), fla(β); fla(δ)).

Now we prove that if H is not congruence singular then so is χ(H). First observethat if R is a relation and α its congruence, then, for any ⟨b, c⟩ ∈ α and any b′ ∈χ(b), c′ ∈ χ(c) (here χ(b) is viewed as if b is a singleton relation), ⟨b′, c′⟩ ∈ χ(α).Therefore for any R and congruences α, β, γ of R, γ ≤ α, β, we have M(R;α, β; γ) =M(χ(R);χ(α), χ(β);χ(γ)). If we prove that χ(R) is pp-definable in χ(H) and χ(α),χ(β), and χ(γ) are congruences of χ(R), then χ(R), χ(α), χ(β), χ(γ) witness that χ(H)is not congruence singular. Thus the following Claim completes the proof.

CLAIM 4. If R is pp-definable in H then χ(R) is pp-definable in χ(H).

We assume that along with any (n-ary) relation R and any subalgebras D1, . . . , Dn

structure H also contains the relation R ∩ (D1 × . . . × Dn). If R is a relation from H,Journal of the ACM, Vol. V, No. N, 20YY.

Page 30: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

30 · Andrei A. Bulatov

Algorithm SubdirectINPUT: an instance G of #CSP(H)OUTPUT: an instance G′ of #CSP(χ(H)) with the same universe as G

Step 1 find a compact representation of Φ(G,H) using MAL’TSEV

Step 2 for each g ∈ G find Φg

Step 3 for each (n-ary) relational symbol R doStep 3.1 for each tuple (g1, . . . , gn) ∈ RG doStep 3.1.1 let R′ be the relational symbol such that R′H = RH ∩ (Φg1 × . . .× Φgn )

Step 3.1.2 include (g1, . . . , gn) into R′G′

endforendfor

Step 4 output G′

Fig. 10.

then χ(R) is a relation of χ(H) by definition. We proceed by induction on the length ofpp-definitions. Suppose that the claim is true for all relations pp-definable in H whosepp-definition is shorter than that of R. If R(x1, . . . , xn) = ∃y R′(x1, . . . , xn, y) then, asis easily seen χ(R)(x1, . . . , xn) = ∃y χ(R′)(x1, . . . , xn, y).

So, let R = R1 ∧ R2. Observe, first, that if for any i ∈ [n], priR = priR1 = priR2

then χ(R) = χ(R1)∧χ(R2). Therefore, it suffices to consider the case when R2 is unary.Indeed, for R′

j(x1, . . . , xn) = Rj(x1, . . . , xn) ∧ pr1R(x1) ∧ . . . ∧ prnR(xn), j = 1, 2,we have R = R′

1 ∧ R′2 and priR = priR1 = priR2 for i ∈ [n], so if χ(R′

1), χ(R′2) are

pp-definable in χ(H) then so is χ(R).LetR(x1, . . . , xn) = R1(x1, . . . , xn)∧R2(xi), and letR1(x1, . . . , xn) = Ψ(x1, . . . , xn)

be a pp-definition of R1. Let Q(y1, . . . , yk) be an occurrence of a relation Q from H informula Ψ such that xi ∈ {y1, . . . , yk}. Let alsoQ′ be the relationQ(y1, . . . , yk)∧R2(xi),which is also a relation from H. Replace in Ψ this occurrence ofQwithQ′(y1, . . . , yk). Asis easily seen the pp-definition Ψ′(x1, . . . , xn) obtained by replacing all such occurrencesdefines R(x1, . . . , xn), and has the same length as Ψ. Therefore χ(R) is pp-definable inχ(H) by the induction hypothesis.

For an instance G of #CSP(H), the algorithm in Fig. 10 constructs an instance G′ of#CSP(χ(H)). The following lemma completes the reduction.

LEMMA 5.2. Let G be an instance of #CSP(H) and G′ the instance of #CSP(χ(H))constructed by algorithm Subdirect. Let also Φg = prgΦ(G,H) for g ∈ G. ThenΦ(G′, χ(H)) is a subdirect power of χ(H) and

|Φ(G′, χ(H))| = |Φ(G,H)| ·∏g∈G

|D||Φg|

.

Moreover, Subdirect is polynomial time.

PROOF. Let φ ∈ Φ(G,H) be a homomorphism from G to H. Let a set of mappingsχ(φ) from G′ to χ(H) be given by

χ(φ) = {ψ : G′ → χ(H) | for any g ∈ G′ if Φg = Di and ψ(g) = a then a[i] = φ(g)}.

(Note that G and G′ have a common universe.) We show that every ψ ∈ χ(φ) is a homo-morphism from G′ to χ(H). Let R′ be a relational symbol and (g1, . . . , gn) ∈ R′G′

. TupleJournal of the ACM, Vol. V, No. N, 20YY.

Page 31: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 31

(g1, . . . , gn) comes to R′G′on Step 3.1.2 from some RG such that R′H = RH ∩ (Φg1 ×

. . . × Φgn). Therefore (φ(g1), . . . , φ(gn)) ∈ R′H. Since priR′H = Φgi for i ∈ [n], we

also have (ψ(g1), . . . , ψ(gn)) ∈ χ(R′H). Thus ψ is a homomorphism.For any g ∈ G′ and any a ∈ Φg there is φ ∈ Φ(G,H) such that φ(g) = a, hence, for

any ψ ∈ χ(φ) we have ψ(g)[i] = a. Since for any aj ∈ Dj , j ∈ [ℓ] − {i}, there existsψ ∈ χ(φ) with ψ(g)[j] = aj , this implies that Φ(G′, χ(H)) is a subdirect power of χ(H).

Let φ ∈ Φ(G′, χ(H)) be a homomorphism from G′ to χ(H). Let us define a mappingχ−1(φ) from G to H as follows. For g ∈ G ifφ(g) = a and Φg = Di then set χ−1(φ)(g) =a[i]. By the construction of χ(H) and G′, if we change the value a = φ(g) for some g ∈ Gwith Φg = Di to any b such that b[i] = a[i], then the resulting mapping φ′ is still ahomomorphism from G′ to χ(H) and χ−1(φ′) = χ−1(φ). For a fixed g this can be donein |D|

|Φg| ways. Conversely, for any homomorphism ψ ∈ Φ(G,H), any mapping φ : G′ →χ(H) such that χ−1(φ) = ψ is a homomorphism of G′ to χ(H). Therefore for eachhomomorphism ψ ∈ Φ(G,H) there are

∏g∈G

|D||Φg| homomorphisms φ ∈ Φ(G′, χ(H))

such that χ−1 = ψ. The result follows.Finally, since Step 3 makes only one pass over every tuple of relations in G, this step can

be done in linear time. Thus the time complexity of the algorithm is dominated by Step 1,which is polynomial time, as so is algorithm MAL’TSEV.

5.3 Structure of Mal’tsev instances

Let G be a #CSP(H) instance and |G| = m. In this section we study certain structuralproperties of the set of homomorphisms Φ(G,H) from G to H. It will be convenient toassume that the universe G of G equals [m]. Set Φ(G,H) can be thought of as an m-aryrelation pp-definable in H. By the results of the previous subsection we may assume thatR = Φ(G,H) is a subdirect power of H. Recall that for a congruence θ ∈ Con(H) by θm

we denote the congruence of R such that ⟨a,b⟩ ∈ θm if and only if ⟨a[g],b[g]⟩ ∈ θ for allg ∈ G. For congruences β ≤ γ ∈ Con(H) and a mapping τ : G → H/β, by τγ we denotea mapping from G to H/γ given by τγ(g) = τ(g)γ .

Let Z be the set of prime quotients of a maximal chain in L = Con(H)/ s∼. As beforewe assume Z = {1, . . . , ℓ}. Let also ω ∈ Z. We use notation ω+, ω− introduced inSection 3.3. Take τ , an element of R/ωm

+. It can be thought of as a mapping from G

to H/ω+. This mapping is always a homomorphism from G to H/ω+

, but not everysuch homomorphism belongs to R/ωm

+. Indeed, if ω = ℓ and ω+ is the total relation, a

homomorphism from any G to H/ω+, a 1-element structure, always exists, however,R can

be empty. By Φ(G,H; τ) we denote the set of elements ϱ from R, that is, homomorphismsfrom G to H, such that ϱω+ = τ .

We study the structure of Φ(G,H; τ) up to ω−. More precisely, let E1, . . . , Er be theω∗−-classes and h1, . . . , hr representatives of these classes. For any homomorphism ϱ ∈

Φ(G,H; τ) and any h ∈ Ei, the value ϱ(h)ω− is completely determined by the valueϱ(hi), so we may focus on possible values of such homomorphisms on h1, . . . , hr. Ourgoal is to show that these values are in some sense independent, meaning that for anycollection a1 ∈ τ(h1)/ω−

, . . . , ar ∈ τ(hr)/ω−(recall that τ(hi) is a ω+-class) there is

ϱ ∈ Φ(G,H; τ) such that ϱ(hi)ω− = ai. Unfortunately, this statement is false in general,however, in the end of this section we prove a result sufficiently close to this one. Note also

Journal of the ACM, Vol. V, No. N, 20YY.

Page 32: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

32 · Andrei A. Bulatov

that Φ(G,H; τ) is considered as a part of Φ(G,H). Although, it is possible to restrict theoriginal instance so that its solutions are only members of Φ(G,H; τ), it leads to severalcomplications. The most important of them is that under solutions from Φ(G,H; τ) thepossible images of elements of G are restricted to a proper subset of H that destroys thesubdirect powers condition we worked so hard to achieve.

First we consider a similar problem for another prime quotient, κω ≺ λω . This willhelp us because, since κω ∧ ω+ = ω−, values ϱ(hi)κω and τ(hi) determine ϱ(hi)ω− . Weprove that the required property is true in this case. Let A1, . . . , Ak be the κ∗ω-classes andg1, . . . , gk representatives of these classes. ByCu

1 , . . . , Cusu we denote the κω-classes from

τ(gu)λω , u ∈ [k].

LEMMA 5.3. For any choice of iu ∈ [su], u ∈ [k], there is a homomorphism ϱ ∈ Rsuch that for each u ∈ [k]

ϱ(gu)κω = Cu

iu .

PROOF. If we setBg to be the λω-class containing τ(g) then τ witnesses thatR∩(B1×. . .× Bm) = ∅. Let R′ = R/κmω

and B′g = Bg/κω

for g ∈ G. Then, by Corollary 3.13,we have

R′ ∩ (B′1 × . . .×B′

m) = R′A1

× . . .×R′Ak,

where R′Au

= prAuR′ ∩

∏g∈Au

B′g The result follows.

If κmω ∨ (ω+)m were equal to λmω this would mean that Φ(G,H; τ) intersects with every

κmω -class, and since κω ∧ ω+ = ω−, this non-empty intersection would provide a homo-morphism with prescribed values modulo ω−. However in general κmω ∨ (ω+)

m = λmω ,so it is important to find κmω ∨ (ω+)

m. To do that we describe the interval [κmω , λmω ] in the

congruence lattice Con(R). It will be more convenient to think of elements of R as tuplesrather than mappings.

LEMMA 5.4. Every prime quotient in the interval [κmω , λmω ] of the congruence lattice

Con(R) has the Boolean type, the interval [κmω , λmω ] is a distributive lattice isomorphic to

the lattice 2[k] of subsets of a k-element set, where k is the number of κ∗ω-classes, and everycongruence in this interval can be represented as ηJ , J ⊆ [k], given by: ⟨a,b⟩ ∈ ηJ if andonly if ⟨a[gu],b[gu]⟩ ∈ κω whenever u ∈ [k]− J and ⟨a[gu],b[gu]⟩ ∈ λω when u ∈ J .

PROOF. Replacing R with R/κmωwe may assume that κω = ∆H . Thus if tuples a,b ∈

R are such that a[gu] = b[gu] for all u ∈ [k], then a = b. Therefore, it suffices to considerrelation R′ = pr{g1,...,gk}R. We study intervals of the form [ηJ , ηJ∪{v}] for J ⊆ [k] andv ∈ [k]−J . Any such interval is non-trivial, meaning ηJ < ηJ∪{v}. Indeed, by Lemma 5.3,for any J ⊆ [k] and v ∈ [k] − J there are tuples a,b ∈ R such that a[gv] = b[gv], buta[gu] = b[gu] for all u ∈ [k]− {v}. By the same reason ∆k

H < η{v} for any v ∈ [k].First, we show that every such interval is a prime quotient. Note that interval [ηJ , ηJ∪{v}]

is perspective to [∆kH , η{v}]. Indeed, if ⟨a,b⟩ ∈ ηJ then a[gu] = b[gu] for u ∈ [k] − J ,

and if ⟨a,b⟩ ∈ η{v} then a[gu] = b[gu] for all u = v, implying ηJ ∧ η{v} = ∆kH . If

⟨a,b⟩ ∈ ηJ∪{v}, then by Lemma 5.3 there is a tuple c such that a[gu] = c[gu] for allu = v and c[gu] = b[gu] for all u ∈ [k] − J . Hence ηJ ∨ η{v} = ηJ∪{v}. It sufficesto show that the intervals of the form ∆k

H < η{v} are prime quotients. To simplify thenotation we assume v = 1.Journal of the ACM, Vol. V, No. N, 20YY.

Page 33: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 33

Let ∆kH < α ≤ η{1}. For any ⟨a,b⟩ ∈ α and any u = 1, a[gu] = b[gu]. This means

that α is determined by the relation

β = {⟨a, b⟩ ∈ H2 | there are a,b ∈ R′ such that ⟨a,b⟩ ∈ α, a[g1] = a, b[g1] = b,and a[gu] = b[gu] for all u = 1}.

By the rectangularity of R′ relation β is a congruence of H and ∆H < β ≤ λω . As∆H ≺ λω , we get β = λω , and the rectangularity of R implies α = η{1}.

Let us now check that quotient ∆kH ≺ η{1} has the Boolean type. By Proposition 3.9(3)

∆H ≺ λω has the Boolean type, which means that there is a polymorphism f(x1, . . . , xn)of H and elements c, d, a1, . . . , an−1, b1, . . . , bn−1 such that ⟨c, d⟩ ∈ λω , ⟨ai, bi⟩ ∈ λωfor i ∈ [n − 1], and f(c, a1, . . . , an−1) = f(c, b1, . . . , bn−1) but f(d, a1, . . . , an−1) =f(d, b1, . . . , bn−1). By Lemma 5.3 there are c,d and ai,bi, i ∈ [n − 1], from R′ suchthat c[g1] = c, d[g1] = d, ai[g1] = ai, bi[g1] = bi, and c[gu] = d[gu], ai[gu] = bi[gu]for i ∈ [n − 1] and u ∈ [k] − {1}. Observe that ⟨c,d⟩, ⟨a1,b1⟩, . . . , ⟨an−1,bn−1⟩ ∈η{1}. Then we have f(c,a1, . . . ,an−1) = f(c,b1, . . . ,bn−1) but f(d,a1, . . . ,an−1) =f(d,b1, . . . ,bn−1), that implies that η{1} does not centralize itself modulo ∆k

H , and soby Proposition 4.4(2) ∆H ≺ η{1} has the Boolean type.

We have proved that any interval of the form [ηJ , ηJ∪{v}] is a prime quotient, and, byLemma 3.4, it has the Boolean type. Next we show that every prime quotient α ≺ β with∆k

H ≤ α ≺ β ≤ λkω is projective to one of such intervals, and therefore has the Booleantype. Suppose the contrary, and let β ≤ λkω be a maximal congruence such that, for someα ≺ β, [α, β] is projective to [ηJ , ηJ∪{v}] for no J ⊆ [k], and v ∈ [k] − J . Let J bea maximal set such that ηJ ≤ α, and v any member of [k] − J . Then ηJ ≺ ηJ∪{v}.Since α ∧ ηJ∪{v} = ηJ , if ηJ∪{v} ≤ β the interval [α, β] is perspective to [ηJ , ηJ∪{v}], acontradiction with the assumption made. Otherwise, by the modularity of Con(R), [α, β]is perspective to [α∨ ηJ∪{v}, β ∨ ηJ∪{v}], a contradiction with the maximality of β. Thus,every prime quotient from interval [∆k

H , λkω] has the Boolean type.

Finally, by Lemma 6.6 of [Hobby and McKenzie 1988], this implies that this intervaldoes not contain a diamond, and, as Con(R) is modular, [∆k

H , λkω] is distributive. Since the

congruences η{1}, . . . , η{ℓ} are join-irreducible elements of this lattice, and η1∨ . . .∨ηℓ =λmω , every element θ of this interval can be represented in the form

θ =∨u∈J

ηu = ηJ

for some J ⊆ [k].

Now we obtain a result similar to Lemma 5.3 for homomorphisms modulo ω−. Notethat ω∗

−-classes cannot be used, because, in general, they have nothing in common withκ∗ω-classes. Indeed, a pair ⟨g, g′⟩ belongs to α∗ for some congruence α if, for any mappingsϱ1, ϱ2, ⟨ϱ1(g), ϱ2(g)⟩ ∈ α if and only if ⟨ϱ1(g′), ϱ2(g′)⟩ ∈ α. For different congruencesα such conditions are incomparable. However, if some homomorphism τ ∈ R/ωm

+is

fixed, inside τ classes of ω− and of κ∗ω are much stronger related to each other. Sinceκω ∧ω+ = ω−, homomorphism τ and a choice of values for g1, . . . , gk (provided they aretaken from τ(gu)

λω ) determine a mapping ϱ : G → H/ω−. For any g ∈ G, the values

of g1, . . . , gk determine the κω-class ϱ(g) belongs to, and τ(g) determines the ω+-classof ϱ(g). Therefore every homomorphism from Φ(G,H; τ) up to ω− can be defined by a

Journal of the ACM, Vol. V, No. N, 20YY.

Page 34: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

34 · Andrei A. Bulatov

certain choice of values for g1, . . . , gk. The difficulty is that some choices do not defineany homomorphism. The next lemma shows which combinations of values for g1, . . . , gkcorrespond to elements of Φ(G,H; τ).

LEMMA 5.5. There is Jω ⊆ [k] such that for any τ ∈ R/ωm+

(we use notation for

κω-classes introduced before Lemma 5.3), there are iu with iu ∈ [su], u ∈ [k] − Jω,satisfying the following conditions. For any homomorphism ϱ ∈ R/ωm

−with ϱω+ = τ the

collection of iu, u ∈ [k]− Jω , can be completed by iu with iu ∈ [su] for u ∈ Jω such thatϱ(gu) ∈ τ(gu)∩Cu

iufor u ∈ [k]; and, for any g ∈ Au, u ∈ [k], we have ϱ(g) = τ(g)∩C,

where C is the κω-class corresponding to the choice of Cuiu

for gu.Conversely, for any choice of Cu

iu, u ∈ Jω , the mapping ϱ defined in this way is an

element of R/ωm−

, and ϱω+ = τ .

PROOF. Observe that in the congruence lattice Con(R) we have κmω ∧ ωm+ = ωm

− andκmω ≤ κmω ∨ ωm

+ ≤ λmω . By Lemma 5.4, κmω ∨ ωm+ = ηJω for some Jω ⊆ [k]. This means

that there are fixed iu, u ∈ [k]− Jω , with iu ∈ [su], such that for any ϱ ∈ R/(ω−)m, with

ϱω+ = τ , we have ϱ(gu) ∈ Cuiu

for u ∈ [k]− Jω .Take ϱ ∈ R/ωm

−with ϱω+ = τ . Clearly, ϱκω belongs to τλω/κmω

, and by what we

showed above ϱ(gu) ∈ Cuiu

for u ∈ [k]− Jω . The first part of the lemma follows.To prove the converse statement, let us denote the ηJω -class containing τ by D. Since

κmω and ωm+ permute, for any κmω -classC ⊆ D and any ωm

+ -classC ′, the intersectionC∩C ′

is nonempty. Therefore, for any φ ∈ R/κmωsuch that φ(gu) = Cu

iufor u ∈ [k]− Jω , there

is ϱ ∈ R/ωm−

such that ϱκω = φ and ϱω+ = τ ; that is ϱ(g) = φ(g) ∩ τ(g). The lemma is

proved.

We complete this section by presenting a collection of congruences related to ω−, ω+

and satisfying condition (2), see p.26. Let A1, . . . , Ak be the κ∗ω-classes, and let Jω ⊆ [k]be the set defined in Lemma 5.5 for ω ∈ Z. Congruences γu, u ∈ Jω , are defined asfollows: ⟨a,b⟩ ∈ γu if and only if ⟨a[i],b[i]⟩ ∈ ω− for i ∈ Au ∪

∪v∈[k]−Jω

Av, and⟨a[i],b[i]⟩ ∈ ω+ otherwise. (Here again we treat elements of R as tuples.) Condition (2)for the congruences γu means that every class of R/ωm

+can be thought of as a direct

product of γu-classes, u ∈ Jω . Therefore the number of elements in it can be representedusing a |Jω|-dimensional array, and can be evaluated (modulo ω−) using Proposition 4.6.

LEMMA 5.6. Congruences γu, u ∈ Jω , satisfy condition (2).

PROOF. Again we use notation introduced before Lemma 5.3. Without loss of general-ity we assume Jω = {1, . . . , q}. First, observe that γ1∧ . . .∧γq = ωm

− . Let βu = γu∨κmω ,that is, ⟨a,b⟩ ∈ βu if and only if ⟨a[i],b[i]⟩ ∈ κω for i ∈ Au ∪

∪v∈[k]−J Av and

⟨a[i],b[i]⟩ ∈ λω otherwise. Let also θ = γ1 ∨ . . . ∨ γq . It is not hard to see thatβ1 ∧ . . . ∧ βq = κmω and βi ∨ βj = ηJω for any i, j ∈ [q]. Since lattice Con(R) ismodular, intervals [ωm

− , θ] and [κmω , ηJ ] are isomorphic, where an isomorphism can be de-fined by φ(x) = x ∨ κmω , see [Gratzer 2003] Cha. IV, Theorem 2. Again by modularityequalities θ ∨ κmω = ωm

+ ∨ κmω = ηJω and θ ∧ κmω = ωm+ ∧ κmω = ωm

− imply θ = ωm+ .

Therefore we may consider β1, . . . , βq instead of γ1, . . . , γq , where we also may assumethat κω = ∆H . To simplify the notation we prove condition (2) for i = 1.Journal of the ACM, Vol. V, No. N, 20YY.

Page 35: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 35

By Lemma 5.5, ⟨a,b⟩ ∈ β1 if and only if prA1∪Aq+1∪...∪Aka = prA1∪Aq+1∪...∪Ak

b,tuples prA2∪...∪Aq

a, prA2∪...∪Aqb belong to prA2∪...∪Aq

R, and ⟨a[g],b[g]⟩ ∈ λω forg ∈ A2∪ . . .∪Aq . Similarly, ⟨a,b⟩ ∈ β2∧ . . .∧βq if and only if prA1

a, prA1b ∈ prA1

R,⟨a[g],b[g]⟩ ∈ λω for g ∈ A1, and prA2∪...∪Ak

a = prA2∪...Akb ∈ prA2∪...∪Ak

R. Takea,b ∈ R such that ⟨a,b⟩ ∈ λmω and a[g] = b[g] for g ∈ Aq+1∪. . .∪Ak, and define c to bethe tuple with c[g] = a[g] for g ∈ A1 and c[g] = b[g] for g ∈ A2∪. . .∪Ak. By Lemma 5.5,c ∈ R and ⟨a, c⟩ ∈ β1, ⟨c,b⟩ ∈ β2 ∧ . . . ∧ βq . Thus ⟨a,b⟩ ∈ β1 ∨ (β2 ∧ . . . ∧ βq).

6. ALGORITHM: COMPUTING THE NUMBER OF SOLUTIONS

In this section we use the results proved in the previous sections to design an algorithmsolving counting CSPs for congruence singular structures.

Suppose that H is congruence singular. Let G be an instance of #CSP(H); assume thatthe universe G of G is [m]. As before Z = {1, . . . , ℓ} is the set of prime quotients of amaximal chain in the lattice Con(H)/ s∼. If ℓ+ = ▽H or 1− = ∆H then we add extraelements (ℓ+ 1)− or 0+ to the set of congruences ω−, ω+, ω ∈ Z, see Fig 11. Otherwisewe assume (ℓ + 1)− = ℓ+ and 0+ = 1−, respectively. In Con(H) the chain correspondsto a number of prime quotients of the form ω− ≺ ω+ that have the Boolean type, andintervals [ω+, (ω + 1)−] such that every prime quotient from this interval has the affinetype, see Fig. 11. A mapping τ : G → H/θ for θ ∈ Con(H) will be called a mappingof level θ. Recall that for a mapping τ of level θ, by Φ(G,H; τ) we denote the set of allhomomorphisms ϱ ∈ Φ(G,H) with ϱθ = τ . For ω ∈ Z and a mapping τ from G to H/ω+

or to H/ω−, we show how to reduce computing the number |Φ(G,H, τ)| to computing

numbers |Φ(G,H, ϱ)| for certain ϱ, mappings from G to H/ω−or to H/(ω − 1)+

, respec-tively. The two cases, τ : G → H/ω+

and τ : G → H/ω−will be considered in the next

two subsections.

( +1)l −

l+

l −

( −1)l+2

1+

1−

0+

Fig. 11. The congruence lattice of H, a maximal chain in Con(H)/ s∼, the corresponding prime quotients, ands∼-classes. Prime quotients ω− ≺ ω+ are shown by solid lines, s∼-classes by ovals, dashed lines represent chainsof the affine type (not to be confused with the dotted line).

Journal of the ACM, Vol. V, No. N, 20YY.

Page 36: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

36 · Andrei A. Bulatov

6.1 Prime quotients of the Boolean type

Let A1, . . . , Ak be the κ∗ω-classes and g1, . . . , gk their representatives. Let τ be a mappingfrom Φ(G,H)/ωm

+, that is, τ(g) is a ω+-class for g ∈ G. By Jω we denote the subset of [k]

identified in Lemma 5.5. Without loss of generality we assume Jω = [q]. Let Cu1 , . . . , C

usu

be the κω-classes from τ(gu)λω , the λω-class containing elements from τ(gu), for u ∈ [k].

Recall that by definition g ∈ Au if and only if for any ϱ, ϱ′ ∈ Φ(G,H) if ⟨ϱ(gu), ϱ′(gu)⟩ ∈κω then ⟨ϱ(g), ϱ′(g)⟩ ∈ κω and vice versa. Therefore, for any g ∈ Au, u ∈ [k], and for anyϱ ∈ Φ(G,H; τ) the value ϱ(g)ω− is determined by ϱ(gu)ω− , and that ϱ(g)ω− = ϱ(g)κω ∩τ(g). In other words, there is a one-to-one mapping φg from the set {Cu

1 , . . . , Cusu} to the

set of κω-classes of τ(g)λω such that ϱ(g)ω− = φg(ϱ(gu)κω )∩ τ(g). Let iu, u ∈ [k]− Jω

and iu ∈ [su], be the κω-classes corresponding to τ as in Lemma 5.5.

PROPOSITION 6.1. (1) For any q-tuple r such that r[u] ∈ [su], the mapping ϱr : G →H/ω−

, where for each u ∈ [k]

ϱr(gu) =

{Cu

r[u] ∩ τ(gu), if u ∈ JωCu

iu∩ τ(gu), otherwise,

and for each g ∈ Au, u ∈ [q], ϱr(g)ω− = φg(ϱr(gu)κω ) ∩ τ(g), belongs to R/(ω−)

m.

(2) |Φ(G,H, τ)| =∑r

|Φ(G,H, ϱr)|.

(3) Sets Φ(G,H, ϱr) are the classes of congruence (ω−)m of the relation Φ(G,H, τ).

PROOF. (1) follows straightforwardly from Lemma 5.5.(2) Every homomorphism ϱ from Φ(G,H; τ) belongs to a certain set Φ(G,H, ϱr), namely,

the one with r[u] = ju where ϱ(gu) ∈ Cuju

for u ∈ [q]. On the other hand all sets of thisform are disjoint.

(3) Since κω ∧ ω+ = ω−, all elements from Φ(G,H, ϱr) are (ω−)m-related. If ϱ ∈

Φ(G,H, ϱr) and ϱ′ ∈ Φ(G,H, ϱr′) where r[u] = r′[u] then ⟨ϱ(gu), ϱ′(gu)⟩ ∈ ω−, andtherefore ⟨ϱ, ϱ′⟩ ∈ (ω−)

m.

We use the congruences γ1, . . . , γq introduced in Section 5.3: ⟨ϱ, ϱ′⟩ ∈ γu if and only if⟨ϱ(g), ϱ′(g)⟩ ∈ ω− if g ∈ Au or g ∈ Aq+1 ∪ . . . ∪ Ak, and ⟨ϱ(g), ϱ′(g)⟩ ∈ ω+ otherwise.By Lemma 5.6 congruences γ1, . . . , γq satisfy condition (2), and (ω−)

m = γ1 ∧ . . . ∧ γq.Recall that τ can be treated as a ωm

+ -class and that M(τ, γ1, . . . , γq) denotes the q-dimensional s1 × . . .× sq-array such that its entry indexed by r is equal to |Φ(G,H, ϱr)|.By Proposition 4.6, M(τ, γ1, . . . , γq) has rank 1, that is, there are numbers tu1 , . . . , t

usu , for

u ∈ [q], such that

|Φ(G,H, ϱr)| = t1r[1] · . . . · tqr[q].

Journal of the ACM, Vol. V, No. N, 20YY.

Page 37: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 37

If numbers tij are known, we have

Φ(G,H, τ) =∑r

Φ(G,H, ϱr) =∑r

t1r[1] · . . . · tqr[q]

= t11

∑r[2],...,r[q]

t2r[2] · . . . · tqr[q]

+ . . .+ t1s1

∑r[2],...,r[q]

t2r[2] · . . . · tqr[q]

= . . . =

q∏i=1

sj∑j=1

tij ,

that can be computed easily.To find the numbers tij we use the approach from the proof of Lemma 4.5. Fix a tuple

r, say, r = (1, . . . , 1). By rij we denote the tuple, all entries of which are equal to thecorresponding entries of r, except for the i-th entry that is equal to j. Then set

t1j = |Φ(G,H, ϱr1j )| and tij =|Φ(G,H, ϱrij )||Φ(G,H, ϱr)|

for i ∈ {2, . . . , q}.

Thus, we have reduced computing the number |Φ(G,H, τ)|, where mapping τ is of levelω+, to computing numbers of the form |Φ(G,H, ϱrij )|, where ϱrij is of level ω−.

6.2 Quotients of the affine type

Let τ ∈ Φ(G,H)/(ω + 1)m−for some ω ∈ Z−{ℓ}. Congruence (ω+1)− is solvable over

ω+, and we make use of the following implication of Proposition 4.1.

COROLLARY 6.2. (1) Let ϱ1, ϱ2 ∈ Φ(G,H, τ)/ωm+

. Then |Φ(G,H, ϱ1)| = |Φ(G,H, ϱ2)|.(2) For any ϱ ∈ Φ(G,H, τ)/ωm

+,

|Φ(G,H, τ)| = |Φ(G,H, ϱ)| · |Φ(G,H, τ)/ωm+|.

Thus, to reduce computing |Φ(G,H, τ)|, where τ is of level (ω + 1)−, to computing|Φ(G,H, ϱ)|, where ϱ is of level ω+, it suffices to find the number |Φ(G,H, τ)/ωm

+|.

We consider first the case when ω+ is the equality relation, that is, ω = 0. In this casethe required number can be found using the signature SigR of the relation R = Φ(G,H, τ)in a very simple way through the following lemma. Observe that it does not apply to thecase when 1− = ∆H .

LEMMA 6.3. Let SigR be the signature of Φ(G,H, τ), and αg be the relation {⟨a, b⟩ |(g, a, b) ∈ SigR}. Then

(1) αg is a congruence of τ(g);

(2) all αg classes have the same cardinality, denoted by vg;

(3) |Φ(G,H, τ)| = v1 · . . . · vm.

PROOF. (1) Relation αg is pp-definable in H as the following formula shows

αg(x, y) = ∃z1, . . . , zg−1, zg+1, . . . , zm, ug+1, . . . , um

(R(z1, . . . , zg−1, x, zg+1, . . . , zm) ∧R(z1, . . . , zg−1, y, ug+1, . . . , um)).

Journal of the ACM, Vol. V, No. N, 20YY.

Page 38: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

38 · Andrei A. Bulatov

Due to rectangularity of R this relation is an equivalence relation.(2) follows straightforwardly from Proposition 4.1, as αg ≤ (ω+1)−, and so (ω+1)−

s∼αg .

(3) As every element of pr[m−1]Φ(G,H, τ) can be extended to an element of Φ(G,H, τ)by any member of a certain αm-class, the number of such extensions equals vm, and wehave |Φ(G,H, τ)| = |pr[m−1]Φ(G,H, τ)| ·vm. Continuing this way we get |Φ(G,H, τ)| =v1 · . . . · vm.

To find the signature of Φ(G,H, τ) we can use algorithm MAL’TSEV applied to theinstance modified in the following way. We shall assume that for each subalgebra B ofH the vocabulary of H contains a unary relational symbol RB such that RH

B = B. Letg1, . . . , gk ∈ G, and let B1, . . . , Bk be subalgebras of H. By G ∪{⟨g1, B1⟩, . . . , ⟨gk, Bk⟩}we denote the relational structure with the same universe as G, and such that the interpre-tation of every relational symbol R ∈ {RB1 , . . . , RBk

} equals RG while the interpretationof RB equals RG

B ∪{gi | Bi = B}. Thus, the elements g1, . . . , gk are forced to be mappedto B1, . . . , Bk respectively. It is not hard to check that Φ(G,H, τ) is the set of solutionsfor the instance G ∪ {⟨g, τ(g)⟩ | g ∈ [m]}.

Observe that if we know the signature of relation Φ(G,H, τ)/ωm+

, which is a relation

over H/ω+, we still can use Lemma 6.3 to find the cardinality of |Φ(G,H, τ)/ωm

+|. In

order to do that we just have to replace H with H/ω+. Therefore the problem we are

facing now is how to find the signature of this relation. Unfortunately, it is not clear at allhow to obtain this signature using the signature or a compact representation of Φ(G,H, τ),nor can we use algorithm MAL’TSEV to compute the signature of Φ(G,H/ω+

, τ), sincein general Φ(G,H/ω+

, τ) = Φ(G,H, τ)/ωm+

. Instead, to compute each member of the

required signature we find a compact representation of a certain modified problem usingalgorithm MAL’TSEV.

More specifically, we first find the ω+-signature of the relation Φ(G,H, τ). Let n be apositive integer, let H be a finite set, let θ be an equivalence relation on H , let a, b be n-tuples, and let (i, a, b) be any element in [n]×H2. We say that ⟨a,b⟩ θ-witnesses (i, a, b)if (a[j],b[j]) ∈ θ for each j < i, a[i] = a, and b[i] = b. Let R be an n-ary relation onH . The θ-signature of R, θSigR ⊆ [n] ×H2, is defined to be the set containing all those(i, a, b) ∈ [n]×H2 θ-witnessed by tuples in R, that is

θSigR = {(i, a, b) ∈ [n]×H2 | there are a,b ∈ R such that ⟨a,b⟩ θ-witnesses (i, a, b)}.

LEMMA 6.4. Let τ ∈ Φ(G,H)/(ω + 1)m−.

(1) Algorithm ω-SIGNATURE (see Fig. 12) finds the ω+-signature ω+SigR ofR = Φ(G,H, τ).(2) The signature of Φ(G,H, τ)/ωm

+can then be found by replacing each (g, a, b) ∈

ω+SigR by (g, aω+ , bω+)

PROOF. (1) For any g ∈ [m], a triple (g, a, b) is added to S only if there are ϱ, ϱ′ ∈Φ(G,H, τ) such that ϱ(g) = a, ϱ′(g) = b, and ⟨ϱ(h), ϱ′(h)⟩ ∈ ω+ for every h < g.Therefore, S ⊆ ω+SigR. If (g, a, b) ∈ ω+SigR then a ∈ prgR. Hence there is ϱ ∈ R′

such that ϱ(g) = a. Suppose that ⟨ϱ′, ϱ′′⟩ ω+-witnesses the triple (g, a, b). We have toshow that there is ϱ′′′ such that the pair ⟨ϱ, ϱ′′′⟩ ω+-witnesses (g, a, b). It is straightforwardthat ϱ′′′ can be chosen to be m(ϱ, ϱ′, ϱ′′), where m is a Mal’tsev polymorphism of H.Journal of the ACM, Vol. V, No. N, 20YY.

Page 39: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 39

Algorithm ω-SignatureINPUT: an instance G of #CSP(H), ω ∈ M , and τ ∈ Φ(G,H)/

(ω + 1)m−OUTPUT: a ω+-signature of Φ(G,H, τ)Step 1 find a compact representation R′ of Φ(G,H, τ) using MAL’TSEV

Step 2 set S :=∅ (the ω+-signature of Φ(G,H, τ))Step 3 for each (g, a, b) ∈ [m]×H2 doStep 3.1 if there is ϱ ∈ R′ such that ϱ(g) = a then doStep 3.1.1 find a compact representation R′′ of Φ(G′,H, τ) where

G′ = G ∪ {⟨1, ϱ(1)ω+ ⟩, . . . , ⟨g − 1, ϱ(g − 1)ω+ ⟩}Step 3.1.2 if b ∈ prgR

′′ then S := S ∪ {(g, a, b)}endif

endforStep 5 return S

Fig. 12.

Algorithm CountingINPUT: an instance G of #CSP(H) such that Φ(G,H) is a subdirect power of HOUTPUT: the number of homomorphisms from G to H, i.e. |Φ(G,H)|Step 1 let τ be a (unique) mapping from G to H/▽H

;

return Counting-mapping(G, (ℓ+ 1)−, τ)

Fig. 13.

(2) By the definition, (g, a′, b′) belongs to the signature of Φ(G,H, τ)/ωm+

if and only if

there are ϱ, ϱ′ ∈ Φ(G,H, τ) such that ϱ(g)ω+ = a′, ϱ′(g)ω+ = b′, and ⟨ϱ(h), ϱ′(h)⟩ ∈ ω+

for all h < g. These conditions mean that the pair ⟨ϱ, ϱ′⟩ ω+-witnesses that (g, ϱ(g), ϱ′(g)) ∈ω+SigR.

6.3 The algorithm

We summarize results of the previous two subsections and present an algorithm solving#CSP(H) for a congruence singular structure H, see Fig. 13, 14. The first of the pre-sented algorithms just initiates a recursive process, while the second one implements themethod discussed in the two previous subsections. We assume that all information aboutH required for the algorithm is known. This includes, for instance, congruences, types ofprime quotients, subalgebras generated by certain sets, etc. As usual, Z denotes the set ofprime quotients of a maximal chain in Con(H)/ s∼.

Comments on the algorithm. Classes of κ∗ω can be computed on Step 2.1 by exploringa compact representation Q of Φ(G,H); such representation can be found by means ofthe algorithm MAL’TSEV. Equivalence relation κ∗ω is defined by binary projections ofΦ(G,H), that are relations generated by prg,hQ for g, h ∈ [m]. Set Jω contains thoseκ∗ω-classes Au, for which projection prguΦ(G,H, τ) equals τ(gu). Again, one can find acompact representation of Φ(G,H, τ) by applying algorithm MAL’TSEV to the problemG ∪ {⟨g, τ(g)⟩ | g ∈ [m]}. Finally, to find a solution ϱ ∈ Φ(G,H, τ)/ω∗

+on Step 3.1 it

suffices to compute a compact representation of Φ(G,H, τ) in the same way as before, andthen for any member of the representation find the corresponding quotient mapping.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 40: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

40 · Andrei A. Bulatov

Algorithm Counting-mappingINPUT: an instance G of #CSP(H) such that Φ(G,H) is a subdirect power of H,

a congruence θ ∈ {0+, 1−, 1+, . . . , ℓ+, (ℓ+ 1)−}, and a mapping τ of level θOUTPUT: the number |Φ(G,H, τ)|Step 1 if θ = 0+ then return 1Step 2 if θ = ω+ for some ω ∈ Z the doStep 2.1 find the κ∗

ω-classes A1, . . . , Ak and choose their representatives g1, . . . , gk ,set Jω = {u1, . . . , uq} ⊆ [k] as in Proposition 6.1, let Cu

1 , . . . , Cusu

be κω-classes(we assume Jω = [q] and rename variables otherwise) belonging to τ(gu)λω for u ∈ J

Step 2.2 set r0[1] := 1, . . . , r0[q] := 1

Step 2.3 set t :=Counting-mapping(G, ω−, ϱr0 )Step 2.4 for v = 1 to su1 doStep 2.4.1 set r[1] := v and r[2] := 1, . . . , r[q] := 1Step 2.4.2 t1v :=Counting-mapping(G, ω−, ϱr)

endforStep 2.5 for u = 2 to q doStep 2.5.1 for v = 1 to su doStep 2.5.1.1 set r[1] := 1, . . . , r[u− 1] := 1, r[u] := v, and r[u+ 1] := 1, . . . , r[q] := 1

Step 2.5.1.2 set tuv :=Counting-mapping(G, ω−, ϱr)

Step 2.5.1.3 set tuv :=tuvt

endforendfor

Step 2.6 return

(q∏

u=1

su∑v=1

tuv

)endif

Step 3 else if θ = (ω + 1)− doStep 3.1 find ϱ ∈ Φ(G,H, τ)/

ωm+

Step 3.2 set t0 :=Counting-mapping(G, ω+, ϱ)

Step 3.3 set S :=θ-Signature(G, τ, ω+)Step 3.4 set vg to be the size of ηg-classes, ηg = {⟨aω+ , bω+ ⟩ | (g, a, b) ∈ S}

Step 3.5 return

t0 ·m∏

g=1

vg

endif

Fig. 14.

Complexity. Observe that the depth of recursion of the algorithm is at most 2ℓ and doesnot depend on the input. On each step considering a prime quotient of the Boolean casethe problem of finding the number |Φ(G,H, τ)| reduces to finding s1 + . . .+ sk numbersof the form |Φ(G,H, ϱ)|, where ϱ : G → H/ω−

. Since k ≤ m and each su does notexceed |H|, every step of this kind requires solving at most |H|m smaller problems. Oneach step considering an interval of the affine type computing |Φ(G,H, τ)| reduces tosolving a problem of the form |Φ(G,H, ϱ)|, ϱ : G → H/ω+

and finding the ω+-signature

of Φ(G,H, τ). To find the ω+-signature the algorithm runs MAL’TSEV at most m · |H|2times. If the time complexity of algorithm MAL’TSEV is bounded by a polynomial p(m)(such a polynomial exists by [Bulatov and Dalmau 2006]), then the overall time complexityof our algorithm is (|H|3m2 · p(m))ℓ.Journal of the ACM, Vol. V, No. N, 20YY.

Page 41: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 41

7. #H -COLORING

Theorem 2.22 provides a complete classification of #P-complete and polynomial time solv-able #H -COLORING problems. However, it is difficult to express the criterion stated in thetheorem in terms of (di)graphs. By [Dyer and Greenhill 2000], an (undirected) graph Hgives rise to a polynomial time solvable #H -COLORING problem if and only if every con-nected component of H is either trivial, or a complete bipartite graph, or a complete graphwith loops at all vertices. In [Bulatov and Dalmau 2007], we observed that an undirectedgraph satisfies this condition if and only if it is invariant under a Mal’tsev operation.

In this section we compare the classification result from [Dyer et al. 2007] for directedacyclic graphs (DAGs for short) with Theorem 2.22. We show that every congruencesingular DAG satisfies the Lovasz-goodness condition introduced in [Dyer et al. 2007].The two conditions must be equivalent, however, the converse implication probably usessome nontrivial properties of pp-definitions in DAGs and remains an open problem. Notethat similar difficulties arise when we try to translate other general results on constraintsatisfaction problems for (di)graphs.

A DAG H = (V,E) is called layered if V can be partitioned into subsets V1, . . . , Vℓsuch that for any (v, w) ∈ E we have v ∈ Vi, w ∈ Vi+1 for a certain i < ℓ. Let v ∈ Vi,w ∈ Vj , i < j. Then Hv∗ denotes the subgraph of H induced by the vertices u such thatthere is a directed path from v to u; similarly, H∗w denotes the subgraph of H induced bythe vertices u such that there is a directed path from u to w; and Hvw = Hv∗ ∩H∗w. Thevertex set of the graphHxyHx′y′ , whereHxy = (V ′, E′) andHx′y′ = (V ′′, E′′), is the set((V ′∩Vi)×(V ′′∩Vi))∪ . . .∪((V ′∩Vj)×(V ′′∩Vj)), a pair ((v, v′), (w,w′)) is an edge ifand only if (v, w) ∈ E′ and (v′, w′) ∈ E′′. It is proved in [Dyer et al. 2007] thatHxyHx′y′

for x, x′ ∈ Vi and y, y′ ∈ Vj has only one connected component that spans all layers fromi to j. If such main connected components of graphs HxyHx′y′ and HztHz′t′ , z, z′ ∈ Vi,t, t′ ∈ Vj , are isomorphic then we write HxyHx′y′ ≡ HztHz′t′ . Finally a layered graph issaid to be Lovasz-good if for any i, j, 1 ≤ i < j ≤ ℓ, and any x, x′ ∈ Vi, y, y′ ∈ Vj wehave HxyHx′y′ ≡ Hxy′Hx′y .

The key lemma for this result is a special case of the result of [Lovasz 1967] that westate in our notation.

LEMMA 7.1. If |Φ(G,H1)| = |Φ(G,H2)| for all graphs G then graphs H1,H2 areisomorphic.

We show that if H is congruence singular then |Φ(G,HxyHx′y′)| = |Φ(G,Hxy′Hx′y)|for any x, x′ ∈ Vi, y, y′ ∈ Vj , where 1 ≤ i < j ≤ ℓ, and any graph G. This impliesthat HxyHx′y′ and Hxy′Hx′y are isomorphic, and so HxyHx′y′ ≡ Hxy′Hx′y . We use anobservation made in [Dyer et al. 2007] that |Φ(G,H1H2)| = |Φ(G,H1)| · |Φ(G,H2)|. IfG = (W,F ) is not layered then |Φ(G,HxyHx′y′)| = |Φ(G,Hxy′Hx′y)| = 0. Let W1,W2

denote the set of vertices on the highest and on the lowest layers of G, respectively. As weknow, Φ(G,H) is a relation pp-definable inH . Now, let η1, η2 be congruences of Φ(G,H)such that ⟨φ,φ′⟩ ∈ ηi, i = 1, 2, iff φ(v) = φ′(v) for all v ∈ Wi. It is not hard to see thatsets of the form Hu∗ are classes of η1, sets of the form H∗w are classes of η2, and sets ofthe form Huw are classes of η1 ∧ η2 (although there are classes of those congruences notrepresentable in the form Hu∗, H∗w, or Huw). Since H is congruence singular, we have

Journal of the ACM, Vol. V, No. N, 20YY.

Page 42: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

42 · Andrei A. Bulatov

rank(M(η1, η2)) = k where k is the number of classes in η1 ∨ η2. Hence∣∣∣∣ |Φ(G,Hxy)| |Φ(G,Hxy′)||Φ(G,Hx′y)| |Φ(G,Hx′y′)|

∣∣∣∣ = 0,

or Φ(G,Hxy),Φ(G,Hx′y′) or Φ(G,Hxy′),Φ(G,Hx′y) are in different classes of η1 ∨ η2.In the latter case either |Φ(G,Hx′y)| = |Φ(G,Hxy′)| = 0 or |Φ(G,Hxy)| = |Φ(G,Hx′y′)| =0. The result follows.

Observe that in this argument congruence singularity is used in a very restricted way:only projection congruences of somewhat restricted type are used.

8. CONCLUDING REMARKS AND OPEN PROBLEMS

The result obtained in the paper is rather general. It includes as particular case the resultsof [Creignou and Hermann 1996; Dyer and Greenhill 2000; Diaz et al. 2001; Dyer et al.2007; Klıma et al. 2006]. However, those results are stated in terms of particular problems,and deriving them from Theorem 2.22 requires extra research.

PROBLEM 2. Characterize congruence singular digraphs.

We also should note that in some cases, e.g., [Dyer and Greenhill 2000], the #P-comp-leteness results obtained for particular problems are stronger than those which follow fromour result. For instance, #P-complete #H -COLORING problems in the case of undirectedgraphs remain #P-complete even when restricted to inputs of bounded degree.

PROBLEM 3. Let H be a relational structure that is not congruence singular. Doesthe problem #CSP(H) remains #P-complete when restricted to the class of structures ofbounded degree? a class of structures with other natural restrictions?

A major question left unanswered in this paper is how to check if a given relational struc-ture is congruence singular. A solution to this problem was found in [Dyer and Richerby2011] along with an alternative characterization of polynomial time solvable #CSPs: Theproblem of deciding, given a relational structure H whether #CSP(H) is polynomial time,belongs to NP.

REFERENCES

BARTO, L., KOZIK, M., AND NIVEN, T. 2008. Graphs, polymorphisms and the complexity of homomorphismproblems. In STOC. 789–796.

BARTO, L. 2008. The dichotomy for conservative constraint satisfaction problems revisited. In LICS. 301–310.BODNARCHUK, V., KALUZHNIN, L., KOTOV, V., AND ROMOV, B. 1969. Galois theory for Post algebras. I.

Kibernetika 3, 1–10.BRIGHTWELL, G. AND WINKLER, P. 1999. Graph homomorphisms and phase transitions. Journal of Combi-

natorial Theory, Ser. B 77, 221–262.BUBLEY, R., DYER, M., GREENHILL, C., AND JERRUM, M. 1999. On approximately counting colourings of

small degree graphs. SIAM Journal of Computing 29, 387–400.BULATOV, A. A. 2002b. Mal’tsev constraints are tractable. Tech. Rep. PRG-RR-02-05, Computing Laboratory,

University of Oxford, Oxford, UK.BULATOV, A. A. 2003. Tractable conservative constraint satisfaction problems. In LICS. 321–330.BULATOV, A. A. AND GROHE, M. 2004. The complexity of partition functions. In ICALP. 294–306.BULATOV, A. A. AND JEAVONS, P. 2001. Algebraic approach to multi-sorted constraints. Tech. Rep. PRG-RR-

01-18, Computing Laboratory, University of Oxford, Oxford, UK.BULATOV, A. A. AND JEAVONS, P. 2003. An algebraic approach to multi-sorted constraits. In CP. 197–202.BULATOV, A. A. 2006. Three-element Mal’tsev algebras. Acta Sci. Math. (Szeged) 72, 519–550.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 43: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

The Complexity of the Counting CSP · 43

BULATOV, A. A. 2006. A dichotomy theorem for constraint satisfaction problems on a 3-element set. J.ACM 53 1, 66–120.

BULATOV, A. A. AND DALMAU, V. 2006. A simple algorithm for Mal’tsev constraints. SIAM J. Comput. 36, 1,16–27.

BULATOV, A. A. AND DALMAU, V. 2007. Towards a dichotomy theorem for the counting constraint satisfactionproblem. Information and Computation 205, 5, 651–678.

BULATOV, A. A. AND GROHE, M. 2005. The complexity of partition functions. Theoretical Computer Sci-ence 348, 2-3, 148–186.

BULATOV, A. A., JEAVONS, P., AND KROKHIN, A. A. 2005. Classifying the complexity of constraints usingfinite algebras. SIAM Journal on Computing 34, 3, 720–742.

BULATOV, A. A. 2011. Complexity of conservative constraint satisfaction problems. ACM Trans. Comput.Log. 12 4, 43–109.

BURRIS, S. AND SANKAPPANAVAR, H. 1981. A course in universal algebra. Graduate Texts in Mathematics,vol. 78. Springer-Verlag, New York-Berlin.

BURTON, R. AND STEIF, J. 1994. Nonuniqueness of measures of maximal entropy for subshifts of finite type.Ergodic Theory and Dynamical Systems 14, 213–236.

CAI, J., LU, P., AND XIA, M. 2008. Holographic algorithms by Fibonacci gates and holographic reductions forhardness. In FOCS. 644–653.

CAI, J. AND LU, P. 2011. Holographic algorithms: From art to science. J. Comput. Syst. Sci. 77 1, 41–61.CREIGNOU, N. AND HERMANN, M. 1996. Complexity of generalized satisfiability counting problems. Infor-

mation and Computation 125, 1, 1–12.CREIGNOU, N., KHANNA, S., AND SUDAN, M. 2001. Complexity Classifications of Boolean Constraint Satis-

faction Problems. SIAM Monographs on Discrete Mathematics and Applications, vol. 7.DIAZ, J., SERNA, M., AND THILIKOS, D. 2001. Counting list H-colorings and variants. Tech. Rep. LSI-01-

27-R, Departament LSI, Universitat Politecnica de Catalunya.DIAZ, J., SERNA, M., AND THILIKOS, D. 2002. Counting H-colorings of partial k-trees. Theoretical Computer

Science 281, 291–309.DIAZ, J., SERNA, M., AND THILIKOS, D. 2004. DIMACS/DIMATIA Workshop on Graphs, Morphism and

Statistical Physics. DIMACS Series in Discrete Mathematics and Theoretical Computer Science Chapter.Recent results on parameterized H-coloring. American Mathematical Society.

2005. DIAZ, J., SERNA, M., AND THILIKOS, D. The restrictive H-coloring. Discrete Applied Mathemat-ics 145, 2, 297–305.

DONNER, Q. 1992. On the number of list H-colorings. J. Graph Theory 16, 3, 239–245.DYER, M., FRIEZE, A., AND JERRUM, M. 2002. On counting independent sets in sparse graphs. SIAM Journal

on Computing 31, 1527–1541.DYER, M., GOLDBERG, L. A., GREENHILL, C., AND JERRUM, M. 2003. On the relative complexity of ap-

proximate counting problems. Algorithmica 38, 3, 471–500.DYER, M., GOLDBERG, L. A., AND PATERSON, M. 2007. On counting homomorphisms to directed acyclic

graphs. J. ACM 54, 6.DYER, M. AND GREENHILL, C. 2000. The complexity of counting graph homomorphisms. Random Structures

and Algorithms 17, 260–289.DYER, M. E., GOLDBERG, L. A., AND JERRUM, M. 20010. An approximation trichotomy for Boolean #CSP.

J. Comput. Syst. Sci. (JCSS) 76, 3-4, 267–277.DYER, M. E., GOLDBERG, L. A., AND JERRUM, M. 2009. The complexity of weighted Boolean #CSP. SIAM

J. Comput. 38, 5, 1970–1986.DYER, M.,AND RICHERBY, D. 2011. The #CSP dichotomy is decidable. In STACS. 261–272.FEDER, T. AND VARDI, M. 1998. The computational structure of monotone monadic SNP and constraint

satisfaction: A study through datalog and group theory. SIAM Journal of Computing 28, 57–104.FREEDMAN, M., LOVASZ, L., AND SCHRIJVER, A. 2007. Reflection positivity, rank connectivity, and homo-

morphism of graphs. J. Amer. Math. Soc. 20, 1, 37–51.FREESE, R. AND MCKENZIE, R. 1987. Commutator theory for congruence modular varieties. London Math.

Soc. Lecture Notes, vol. 125. London.GEIGER, D. 1968. Closed systems of function and predicates. Pacific Journal of Mathematics, 95–100.

Journal of the ACM, Vol. V, No. N, 20YY.

Page 44: The Complexity of the Counting Constraint Satisfaction Problemabulatov/papers/counting-acm.pdf · The Complexity of the Counting Constraint Satisfaction Problem ANDREI A. BULATOV

44 · Andrei A. Bulatov

GRATZER, G. 2003. General Lattice Theory. Birkhauser Verlag, Basel.GREENHILL, C. 2000. The complexity of counting colourings and independent sets in sparse graphs and hyper-

graphs. Computational Complexity 9, 52–73.GUMM, H.-P. 1979. Algebras in permutable varieties: geometrical properties of affine algebras. Algebra Uni-

versalis 9 1, 8–34.HELL, P. AND NESETRIL, J. 2004. Counting list homomorphisms for graphs with bounded degrees. In Graphs,

Morphisms and Statistical Physics. DIMACS Series in Discrete Mathematics and Theoretical Computer Sci-ence 63, 105–112.

HELL, P. AND NESETRIL, J. 1990. On the complexity of H-Coloring. Journal of Combinatorial Theory,Ser.B 48, 92–110.

HOBBY, D. AND MCKENZIE, R. 1988. The Structure of Finite Algebras. Contemporary Mathematics, vol. 76.American Mathematical Society, Providence, R.I.

HUNT III, H., MARATHE, M., RADHAKRISHNAN, V., AND STEARNS, R. 1998. The complexity of planarcounting problems. SIAM Journal on Computing 27, 1142–1167.

IDZIAK, P., MARKOVIC, P., MCKENZIE, R., VALERIOTE, M., AND WILLARD, R. 2007. Tractability andlearnability arising from algebras with few subpowers. SIAM J. Comput. 39, 7, 3023–3037.

JEAVONS, P. 1998. On the algebraic structure of combinatorial problems. Theoretical Computer Science 200,185–204.

JEAVONS, P., COHEN, D., AND COOPER, M. 1998. Constraints, consistency and closure. Artificial Intelli-gence 101, 1-2, 251–265.

JERRUM, M. AND SINCLAIR, A. 1996. The Markov chain Monte Carlo method: an approach to approximatecounting and integration. In Approximation Algorithms for NP-hard Problems. PSW, 482–520.

JONSSON, B. AND RIVAL,I. 1979. Lattice varieties covering the smallest non-modular variety. Pacific J. ofMath. 82, 2, 1129–1133.

KLIMA, O., LAROSE, B., AND TESSON, P. 2006. Systems of equations over finite semigroups and the #CSPdichotomy conjecture. In MFCS. 584–595.

LEBOWITZ, J. AND GALLAVOTTI, G. 1971. Phase transitions in binary lattice gases. Journal of Math.Physics 12, 1129–1133.

LEVIN, L. 1973. Universal enumeration problems. Problems on Information Transmission 9, 265–266.LINIAL, N. 1986. Hard enumeration problems in geometry and combinatorics. SIAM Journal on Algebraic and

Discrete Methods 7, 2, 331–335.LOVASZ, L. 1967. Operations with structures. Acta. Math. Acad. Sci. Hung. 18, 321–328.LOVASZ, L. 2006. The rank of connection matrices and the dimension of graph algebras. European Journal of

Combinatorics 27, 6, 962–970.MCKENZIE, R., MCNULTY, G., AND TAYLOR, W. 1987. Algebras, Lattices and Varieties. Vol. I. Wadsworth

and Brooks, California.NORDH, G. AND JONSSON, P. 2004. The complexity of counting solutions to systems of equations over finite

semigroups. In COCOON. 370–379.ORPONEN, P. 1990. Dempster’s rule of combination is #P-complete. Artificial Intelligence 44, 245–253.PROVAN, J. AND BALL, M. 1983. The complexity of counting cuts and of computing the probability that a

graph is connected. SIAM Journal on Computing 12, 4, 777–788.ROTH, D. 1996. On the hardness of approximate reasonning. Artificial Intelligence 82, 273–302.SCHAEFER, T. 1978. The complexity of satisfiability problems. In STOC. 216–226.SZENDREI, A. 1986. Clones in universal algebra. Seminaire de Mathematiques Superieures, 99. Presses de

l’Universite de Montreal.VADHAN, S. 2001. The complexity of counting in sparse, regular and planar graphs. SIAM Journal on Comput-

ing 31, 2, 398–427.VALIANT, L. 1979a. The complexity of computing the permanent. Theoretical Computing Science 8, 189–201.VALIANT, L. 1979b. The complexity of enumeration and reliability problems. SIAM Journal on Computing 8, 3,

410–421.

Journal of the ACM, Vol. V, No. N, 20YY.