# Distance Constraint Satisfaction Problems

## Abstract

We study the complexity of constraint satisfaction problems for templates over the integers where the relations are first-order definable from the successor function. In the case that is locally finite (i.e., the Gaifman graph of has finite degree), we show that is homomorphically equivalent to a structure with one of two classes of polymorphisms (which we call modular max and modular min) and the CSP for can be solved in polynomial time, or is homomorphically equivalent to a finite transitive structure, or the CSP for is NP-complete. Assuming a widely believed conjecture from finite domain constraint satisfaction (we require the *tractability conjecture* by Bulatov, Jeavons and Krokhin in the special case of *transitive* finite templates), this proves that those CSPs have a complexity dichotomy, that is, are either in P or NP-complete.

[Theorem]Question [Theorem]Definition [Theorem]Notation [Theorem]Lemma [Theorem]Corollary [Theorem]Proposition [Theorem]Conjecture [Theorem]Example [Theorem]Claim

## 1Introduction

Constraint satisfaction problems appear naturally in many areas of theoretical computer science, for example in artificial intelligence, optimization, computer algebra, computational biology, computational linguistics, and type systems for programming languages. Such problems are typically NP-hard, but sometimes they are polynomial-time tractable. The question as to which CSPs are in P and which are hard has stimulated a lot of research in the past 15 years. For pointers to the literature, there is a collection of survey articles [15].

The *constraint satisfaction problem CSP* for a fixed (not necessarily finite) structure with a finite relational signature is the computational problem of deciding whether a given primitive positive sentence is true in . A formula is *primitive positive* if it is of the form where each is an *atomic formula* over , that is, a formula of the form or for a relation symbol of a relation from . The structure is also called the *template* of the CSP.

The class of problems that can be formulated as a CSP for a fixed structure is very large. It can be shown that for every computational problem there is a structure such that the CSP for is equivalent to this problem under polynomial-time Turing reductions [5]. This makes it very unlikely that we can give good descriptions of all those where the CSP for is in P. In contrast, the class of CSPs for a *finite* structure is quite restricted, and indeed it has been conjectured that the CSP for is either in P or NP-complete in this case [17]. So it appears to be natural to study the CSP for classes of infinite structures that share good properties with finite structures.

In graph theory and combinatorics, there are two major concepts of *finiteness* for infinite structures. The first is -categoricity: a countable structure is -categorical if and only if its automorphism group has for all only finitely many orbits in its natural action on -tuples [14]. This property has been exploited to transfer techniques that were known to analyze the computational complexity of CSPs with finite domains to infinite domains [10]; see also the introduction of [3].

The second concept of finiteness is the property of an infinite graph or structure to be *locally finite* (see Section 8 in [16]). A graph is called locally finite if every vertex is contained in a finite number of edges; a relational structure is called locally finite if its Gaifman graph (definition given in Section 2) is locally finite. Many conjectures that are open for general infinite graphs become true for locally finite graphs, and many results that are difficult become easy for locally finite graphs.

In this paper, we initiate the study of CSPs with locally finite templates by studying locally finite templates that have a *first-order definition* in , that is, has the domain and all relations of can be defined by a first-order formula over the successor relation on the integers, .

As an example, consider the directed graph with vertex set which has an edge between and if the difference, , between and is either or . This graph is the structure where , which has a first-order definition over since if and only if

Another example is the undirected graph with vertex set where two integers are linked in if the *distance*, , is one or two.

Structures with a first-order definition in are particularly well-behaved from a model-theoretic perspective: all of those structures are strongly minimal [23], and therefore uncountably categorical. Uncountable models of their first-order theory will be saturated; for implications of those properties for the study of the CSP, see [6]. In some sense, constitutes one of the simplest infinite structures that is not -categorical.

The corresponding class of CSPs contains many natural combinatorial problems. For instance, the CSP for the structure is the computational problem of labeling the vertices of a given finite directed graph such that if is an arc in , then the difference between the label for and the label for is one or three. It follows from our general results that this problem is in P. The CSP for the undirected graph is exactly the -coloring problem, and thus NP-complete. This is readily seen if one observes that any homomorphism of a graph into the template modulo gives rise to a -coloring of . In general, the problems that we study in this paper have the flavor of assignment problems where we have to assign integers to variables such that various given constraints on differences and distances (and Boolean combinations thereof) between variables are satisfied. We therefore call the class of CSPs whose template is locally finite and definable over *distance CSPs*. Our main result is the following classification result for distance CSPs.

If a locally finite structure with a first-order definition in has a finite core, then a widely accepted conjecture about finite domain CSPs implies that the CSP for is either NP-complete or in P. In fact, for this we only need the (open) special case of the conjecture of Feder and Vardi [17] that states that the CSP for finite templates with a transitive automorphism group is either in P or NP-complete (see Section 7 for details).

To show our theorem, we prove that if the first two items of the statement do not apply, then is homomorphically equivalent to a structure with a first-order definition in that has one of two specific classes of polymorphism which we call *modular max* and *modular min* (defined in Section 5). Using these polymorphisms, we further show that the CSP for , and hence also that for , can be solved in polynomial time by certain arc consistency techniques. Polynomial-time tractability results based on arc consistency were previously known for finite or -categorical templates; using the local finiteness assumption we manage to apply such techniques to templates which are not -categorical.

On the way to our classification result we derive several facts about structures definable in , and automorphisms and endomorphisms of these structures, which might be of independent interest in model theory, universal algebra, and combinatorics. For example, we show that every injective endomorphism of a connected locally finite structure with a first-order definition in is either of the form or of the form for some (see Theorem ?).

## 2Preliminaries

A *relational signature* is a set of relation symbols , each of which has an associated arity . A -structure consists of a set (the domain) together with a relation for each relation symbol from . We consider only finite signatures in this paper.

For , let be the distance between and , that is, . The relation is denoted by , and the relation is denoted by . It will be convenient to represent binary relations with a first-order definition in by sets of integers as follows.

A -ary relation is said to be *first-order (fo) definable* in a -structure if there is a first-order -formula such that . A structure is said to be fo-definable in if has the same domain as , and each of its relations is fo-definable in . For example, is fo-definable in (though the converse is false).

The structure induced by a subset of the domain of is denoted by . When and are two -structures with disjoint domains and , then the *disjoint union* of and is the structure with domain where for each . We say that a structure is *connected* if it cannot be written as the disjoint union of two non-empty structures. The *Gaifman graph* of a relational structure with domain is the following undirected reflexive graph: the vertex set is , and there is an edge between elements when or there is a tuple in one of the relations of that has both and as entries. A structure is readily seen to be connected if and only if its Gaifman graph is connected. The *degree* of a structure is defined to be the degree of the Gaifman graph of . The degree of a relation is defined to be the degree of the structure . The notation indicates the expansion of with the new relation .

A first-order formula is *primitive positive (pp)* if it is of the form

where is an atomic formula over , i.e., a formula of the form or of the form for a relation symbol of a relation from . A pp-sentence is a pp-formula with no free variables. For a structure with a finite relational signature, is the computational problem of deciding whether a given pp-sentence is true in . It is not hard to see that for any and with the same domain such that each of the relations of is pp-definable in (see [22]); here, indicates polynomial-time many-to-one reduction (though in fact, logspace reductions may be used).

Suppose is a finite structure with finite relational signature and domain . Let be the conjunction of the positive facts of , where the variables correspond to the elements . That is, appears as an atom in iff . Define the pp-sentence to be the *canonical query* of . Conversely, for a pp-sentence over the relational signature we define the *canonical database* as follows. Consider the undirected graph with vertices where two vertices are connected if contains the conjunct . The domain of the canonical database is the set of connected components of this graph, and for iff there are such that has a conjunct .

Let and be -structures. A *homomorphism* from to is a function from the domain of to the domain of such that, for each -ary relation symbol in and each -tuple from , if , then . In this case we say that the map *preserves* the relation . Injective homomorphisms that also preserve the complement of each relation are called *embeddings*. Surjective embeddings are called isomorphisms; homomorphisms and isomorphisms from to itself are called *endomorphisms* and *automorphisms*, respectively. The set of automorphisms of a structure forms a group under composition. A (-ary) *polymorphism* of a structure over domain is a function such that, for all -ary relations of , if , for all , then .

A unary function over domain is in the *local closure* of a set of unary functions over domain if for every finite there is a function such that and agree on all elements in . We say that *generates* if is in the local closure of the set of all functions that can be obtained from the members of by repeated applications of composition. It is well-known and easy to see that functions that are in the local closure of, or generated by, the endomorphisms of a structure are again endomorphisms of .

If there exist homomorphisms and then and are said to be *homomorphically equivalent*. It is a basic observation that CSP CSP if and are homomorphically equivalent. A structure is a *core* if all of its endomorphisms are embeddings [2] – a *core of a structure* is an induced substructure that is itself a core and is homomorphically equivalent to . It is well-known that if a structure has a finite core, then that core is unique up to isomorphism (the same is in general not true for infinite cores).

We could have equivalently defined the class of distance CSPs as the class of CSPs whose template is locally finite and first-order definable in , where is the unary successor *function*, since and fo-define the same structures. The structure admits *quantifier elimination*; that is, for every fo-formula there is a quantifier-free (qf) (possibly equal to *true* or *false*) such that ; this is easy to prove, and can be found explicitly in [18]. Thus we may have atomic formulas in of the form , where is the successor function composed on itself times. Let be a finite signature structure, fo-definable in , i.e., qf-definable in its functional variant . Let be the largest number such that appears as a term in the qf definition of a relation of . Consider now CSP, the problem of evaluating , where is a conjunction of atoms, on . Let . It is not hard to see that iff [S]. It follows that CSP will always be in NP.

**Convention.** From now on we assume that is a relational structure with domain which is first-order definable over and is locally finite.

## 3Endomorphisms

The main result of this section is the following theorem.

The proof of this theorem can be found at the end of this section, and makes use of a series of lemmata.

Before beginning the proof, we remark the following. If has a first-order definition in , then it is easy to see that the automorphisms of are also automorphisms of , and hence the two structures have the same automorphisms by Theorem ?. Now it is tempting to believe that also the converse holds, i.e., that if has the same automorphisms as , then is fo-definable in (this would be true for -categorical structures). However, this is not true: Let

and set . The function which sends every to is an automorphism of , so the automorphism group of equals that of , by Theorem ?. However, is not fo-definable in . To see this, suppose it were definable. Then is also definable in , and even with a quantifier-free formula since this structure has quantifier-elimination. Let be the maximal natural number such that occurs in . We claim that holds iff holds. To see this, we show that any atom of the formula , i.e., any occurrence of , where and , evaluates to *true* upon insertion of for the variables if and only if it evaluates to *true* upon insertion of for . This is obvious when since and have identical values for . If then the atom becomes *false* in both evaluations, so the only remaining case is where ; but then the atom becomes true in both evaluations if and only if and , so we are done. Now since holds iff holds, we have a contradiction since is an element of whereas is not.

Denote by the edge-relation of the Gaifman graph of . It is clear that every endomorphism of preserves . We claim that there are such that holds iff . To see this, observe that if and are so that , then also , because there is an automorphism of (and hence of ) which sends to and this automorphism also preserves . Hence, the relation is determined by distances. Moreover, there are only finitely many distances since is assumed to have finite degree.

The following basic claim characterizes when is connected in terms of the distance set.

Proof: If is the greatest common divisor of it is clear that all the nodes accessible from a node are of the form where . Conversely, every node of the form is accessible from because for some , by the extended Euclidean algorithm.

In order to lighten the notation we might use to denote , where is an endomorphism of and .

Proof: We first claim that for every , there exists a number such that for all endomorphisms of and all with . To see this, pick with and a path between and in the Gaifman graph of ; say this path has length . Then, since this path is mapped to a path under any endomorphism, we have for all endomorphisms . Since an isomorphic path exists for all with the same distance, our claim follows by setting . Set to be the maximum of the , and let an endomorphism and be given. Assume without loss of generality that . There exists and such that . Set , for all . Since and are adjacent in the Gaifman graph of for all , so are and , and hence . Therefore,

Observe that a constant not only exists, but can actually be calculated given the distances : by the proof of Lemma ?, it suffices to calculate a constant for all . To do this, one must find a path of length between two numbers with ; this again amounts to solving the equation (with variables ) over , which can be achieved by the extended Euclidean algorithm.

In the following, we will keep the symbol reserved for the minimal constant guaranteed by the preceding lemma.

Proof: Let be finite. We claim that generates a function which maps into a set of diameter at most . The lemma then follows by the following standard local closure argument: Let be the set of all those functions whose domain is a finite interval and whose range is contained in the interval , and which have the property that there exists a function generated by which agrees with on . By our claim, is infinite. For functions in , write iff is an extension of . Clearly, the set , equipped with this order, forms a finitely branching tree; since the tree is infinite, it has an infinite branch (this easily verified fact is called König’s lemma) . The branch defines a function from into the interval ; since generates functions which agree with on arbitrarily large intervals of the form , we have that is generated by , too. This completes the proof.

Enumerate the pairs with by . Now the hypothesis of the lemma implies that by successive applications of and shifts we can map to a pair of distance at most ; in other words, there exists generated by such that . Similarly, there exists generated by such that . Continuing like this we arrive at a function generated by such that . Now consider . Set and , for all ; so . Then, since by construction , we have that for all

and our claim follows.

Proof: Let be so that for all with . Let be arbitrary. Then, since , we have . We furthermore assume that ; the situation where can be treated symmetrically. We claim that for all . Suppose not, and say without loss of generality that there exists contradicting our claim. Then, since , we have . Take the minimal with satisfying this property. Then, by minimality, we have . Since by Lemma ? we have , we get that . On the other hand, . Inserting this into the previous inequality, we obtain , which yields . By our assumption on , we obtain , which yields , a contradiction.

Set . We next claim that for all . First observe that points at distance cannot be mapped by to points at larger distance since is by definition the largest distance in the Gaifman graph of . Since is a multiple of , we get that . On the other hand, since is also a multiple of and since for all , we obtain , proving the claim.

We now prove that for all . This is because

the latter inequality holding since is the maximal distance in the relation and cannot be increased. Subtracting on both sides, our claim follows.

Since points at distance cannot be mapped to points at larger distance under , we have for all , and we have proved the lemma.

The following lemma summarizes the preceding two lemmas.

Proof: Lemma ? shows that (i) implies (ii) and (iii). It follows from Lemma ? that (ii) implies (i). Finally, it is clear that (iii) implies (ii).

We know now that there are two types of endomorphisms of : Those which are periodic with period , and those which generate a finite range operation. We will next provide examples showing that both types really occur.

Observe that in the previous example, we checked that is of the non-finite-range type by virtue of the easily verifiable Item (iii) of Lemma ? and without calculating , which would be more complicated.

The proof of Lemma ? generalizes canonically to a more general situation.

Proof: This is the same argument as in the proof of Lemma ?, with replaced by .

Observe that if satisfies the various statements of Lemma ?, then is stable for . Note also that if are stable for , then they must have the same “direction”: We cannot have and for all .

Proof: Clearly, all multiples of are stable. Now for the other direction suppose that is stable but not divisible by . Write , where are positive numbers and . Since is not stable, composing and shifts we can build a function such that and . By the property of we should have or . But this is impossible since then , a contradiction.

Proof: Assume (otherwise can be chosen to be the identity and there is nothing to do). We claim that generates a function such that and . To see this, observe that since and since is the smallest positive number with the property that implies (Lemma ?), there exist with and . Write . If is not a multiple of , then there exist with and . Again, if is not a multiple of , then there exist with and . Consider the sequence of pairs of distance (setting ). By exchanging and if necessary, we may assume that iff , for all . There exist automorphisms of such that . Set . Then the endomorphism sends to , a pair of distance . Thus the sequence must end at some finite , by Lemma ?. By construction of the sequence, this happens only if is a multiple of . Therefore, . By applying shifts we may assume , , and . Set .

Now if , then consider the number . We claim that generates a function such that and is a multiple of . If already is a multiple of , then we can choose to be the identity. Otherwise, we can increase the distance of from successively by applying shifts and just as before, where we moved away from . After a finite number of steps, we arrive at a function such that is a multiple of . Applying a shift one more time, we may assume that , and so has the desired properties.

We continue inductively, constructing for every a function such that and is a multiple of for all . At the end, we set . Since satisfies either or , so does , as it is composed of and automorphisms of . It is also clear from the construction that holds. These two facts together imply that contains the set . For the other inclusion, let be arbitrary, and write , where and . Then or , which is a multiple of since is a multiple of by construction.

Observe that we did not need local closure in the preceding lemma.

Proof: If generates a finite range operation then the lemma follows immediately, so assume this is not the case. Then has a minimal stable number . Since is not an automorphism of , we have . But now the statement follows from the preceding lemma, since the function is not injective (e.g., maps surjectively to , so for some ).

Proof: This is a direct consequence of Lemma ?, since being surjective is preserved under composition (and we used just composition in Lemma ? and not local closure).

Define to be the substructure of induced by . Note that when is fo-definable in , then is isomorphic to a structure that is fo-definable in via the map which sends an element of to . From a defining quantifier-free formula for a relation of over , we obtain a definition for over as follows. For all not divisible by , replace every occurrence of by . For all other , replace every occurrence of by .

Proof: (of Theorem ?) We prove the first statement. It is a direct consequence of Lemma ? that the automorphism group of is contained in that of . Since is fo-definable in , its automorphism group contains that of . The statement now follows from the easily verifiable fact that there are no permutation groups properly between the automorphism groups of and .

For the second statement, suppose that has no finite range endomorphism. If all of its endomorphisms are automorphisms, then we are done. Otherwise, has an endomorphism as in Lemma ?, with . Let be a structure that is isomorphic to and first-order definable in . In , two points are adjacent iff ; moreover, is divisible by . Therefore, the remaining relevant distances are those divisible by . In other words, if are those distances from which are divisible by , then the Gaifman graph of is isomorphic to the graph on defined by the distances . Since before, from Lemma ?, the greatest common divisor of all possible distances was , we must have lost at least one distance, i.e., .

Observe that (and hence ) is connected as it is the image of an endomorphism of . Note moreover that (and hence cannot have a finite range endomorphism: If were such an endomorphism, then would be a finite range endomorphism for , contrary to our assumption. If all endomorphisms of are automorphisms, then we are done. Otherwise satisfies all assumptions that we had on , and we may repeat the argument. Since in every step we lose a distance for the Gaifman graph, this process must end, meaning that we arrive at a structure all of whose endomorphisms are automorphisms.

## 4Definability of Successor

In this section we show how to reduce the complexity classification for distance constraint satisfaction problems with template to the case where either has a finite core, or the relation is pp-definable in . We make essential use of the results of the previous section; but note that in this section we do *not* assume that is connected.

The following lemma demonstrates how the not necessarily connected case can be reduced to the connected case.

Proof: If all edges of the Gaifman graph of are self-loops, then the statement is clear. Otherwise, let be the greatest common divisor of (the *distances* in the Gaifman graph, see Section 3, Notation ?). If is connected, there is nothing to prove.

Otherwise, if is disconnected, by Lemma ?, we have . Then must be a disjoint union of copies of a connected structure (and these copies are isomorphic to each other by an isomorphism of the form , for appropriate constant ). In particular, is homomorphically equivalent to . Moreover, itself has a first-order definition in . The proof here is as in the proof of Theorem ?, with taking the role of .

The following is obvious.

Proof: Let be the smallest distance of the distances defining the Gaifman graph of (as in Section 3). By connectivity of , for each pair of elements from there is a path from to in . Fix such a path for each pair . Let be the smallest number such that all vertices on those paths are smaller than . We claim that is connected for all . To see that are connected, observe that both and are connected to vertices in (via a sequence of vertices at distance ). Since all vertices in are connected in by construction, we conclude that and are connected by a path in .

Proof: Let be the number from Lemma ?. Then for all , the structure is connected. Now, proceed as in Lemma ?.

Proof: Suppose that there are such that for all finite subsets of elements of that contain there is a homomorphism from to where . We have to show that has an endomorphism that is not an automorphism of . Let be a subset of that contains , and let be functions from . Then we define if there exists an automorphism of such that for all . Homomorphisms from to where will be called *good*. Observe that since all automorphisms of preserve distances, if one function in an equivalence class is good, then all other functions in the equivalence class are also good.

Let be the number from Lemma ?, and let be . Consider the following infinite forest : the vertices are the equivalence classes of good functions for , for all , and has an arc from one such equivalence class to another if there are , , such that is a restriction of , and is defined on , and is defined on , for some . Observe that

by our assumptions the forest is infinite;

by Lemma ?, for every there is a such that for all . Using Lemma ? it follows that is finitely branching;

the forest has only finitely many roots.

By König’s lemma, there is an infinite branch in . It is straightforward to use this infinite branch to construct an endomorphism of with . This endomorphism cannot be an automorphism of , which concludes the proof.

Proof: The proof is similar to the proof of Proposition ?.

Proof: First consider the case that is preserved by the unary operation , and let be arbitrary. Let be any two elements of at distance . Since all endomorphisms of are automorphisms of , they are automorphisms of by the first statement of Theorem ?. Hence we may apply Proposition ?, and there is a finite set such that every homomorphism from to satisfies . Let be the primitive positive formula obtained from the canonical query for by existentially quantifying all vertices except for and . We claim that is a pp-definition of .

The relation defined by contains the pair (since the identity mapping is a satisfying assignment for the canonical query ), and since is preserved by all automorphisms of it also contains all other pairs such that . Conversely, does not contain any pair with . Otherwise, there must be a assignment that satisfies the canonical query and maps to and to . This assignment is a homomorphism, and therefore contradicts the assumption that . This proves the claim.

Now consider the case that is *not* preserved by the unary operation . Again we use Theorem ? and this time Proposition ? to construct a primitive positive formula that defines the relation .

Proof: Observe that the primitive positive formula defines the relation . The structure decomposes into two copies of the structure . This structure has the endomorphism , and the image induced by this endomorphism is a cycle of length 5, which has a hard CSP (this is well-known; for a much stronger result on undirected graphs, see Hell and Nešetřil [19]).

Proof: (of Theorem ?) By Lemma ?, we can assume without loss of generality that is connected. If does not have a finite range endomorphism, then by Theorem ? there is an endomorphism of whose range induces in a substructure which is first-order definable in , and where all endomorphisms are automorphisms. Being the homomorphic image of the connected structure , must also be connected. We now apply Corollary ? to . If the relation is pp-definable in for every , then (which is equal to since and are homomorphically equivalent) is NP-hard by Proposition ?. Otherwise, by Corollary ?, the relation and in particular the relation is pp-definable in .

## 5Tractability of Modular Max

This section discusses the distance CSPs that can be solved in polynomial time.

The results of this section improve the algorithmic results that have been presented in the conference version of the present paper [4].

The proof of Theorem ? can be found at the end of the section. The first algorithm Solve-Semilattice we present in this section solves the CSP of structures that are closed under a *semilattice operation*. A semilattice operation is a binary function that is idempotent, commutative, and associative. Note that for , the -modular equals the maximum operation, which is a semilattice operation. The situation for the -modular is dual, and we therefore restrict our discussion in this section to the -modular in the following.

Also note that the -modular is not commutative when , i.e., is a semilattice operation only when . We first treat the special case , i.e., the case that is preserved by , using the technique of *sampling* presented in [8]. This case will then be used later to solve the general case.

Thus, in order to obtain the polynomial-time algorithm Solve-Semilattice for when is preserved by , it remains to prove that we can efficiently sample from . Note that is satisfiable in iff it is satisfiable in the substructure induced by on . The sampling algorithm for then simply returns this structure, and the running time is polynomial in .

We now present a more general algorithm that solves the CSP of any structure fo-definable in that is preserved by at least one of -modular max. We need the following concept, which is important also in Section 6.

A binary relation is called *trivial* if it is pp-definable in , and *non-trivial* otherwise. Note that is non-trivial if and only if . An arithmetic -progression is called *non-trivial* if .

Proof: Suppose for contradiction that is not a -progression. There are two cases.

contains and where . Let be the primitive positive definition of in . As is distinct from , in the canonical database of the vertices and must lie in the same connected component, and it follows that has finite degree. Therefore, and because is non-trivial, there exists a smallest so that . Choose some so that and , which exists by assumption. Applying to and we get and therefore , in contradiction to the choice of .

Suppose that case 1 does not apply. Then must contain some and but not for (with ). In this case, choose so that is minimal. Applying to and