First-order queries on classes of structures with bounded expansion

by   Wojtek Kazana, et al.

We consider the evaluation of first-order queries over classes of databases with bounded expansion. The notion of bounded expansion is fairly broad and generalizes bounded degree, bounded treewidth and exclusion of at least one minor. It was known that over a class of databases with bounded expansion, first-order sentences could be evaluated in time linear in the size of the database. We give a different proof of this result. Moreover, we show that answers to first-order queries can be enumerated with constant delay after a linear time preprocessing. We also show that counting the number of answers to a query can be done in time linear in the size of the database.



There are no comments yet.


page 1

page 2

page 3

page 4


Enumerating Answers to First-Order Queries over Databases of Low Degree

A class of relational databases has low degree if for all δ>0, all but f...

Towards Approximate Query Enumeration with Sublinear Preprocessing Time

This paper aims at providing extremely efficient algorithms for approxim...

Dynamic Query Evaluation Over Structures with Low Degree

We consider the evaluation of first-order queries over classes of databa...

Lacon- and Shrub-Decompositions: A New Characterization of First-Order Transductions of Bounded Expansion Classes

The concept of bounded expansion provides a robust way to capture sparse...

The Limits of Efficiency for Open- and Closed-World Query Evaluation Under Guarded TGDs

Ontology-mediated querying and querying in the presence of constraints a...

Evaluating Datalog via Tree Automata and Cycluits

We investigate parameterizations of both database instances and queries ...

Query Preserving Watermarking Schemes for Locally Treelike Databases

Watermarking is a way of embedding information in digital documents. Muc...
This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Preliminaries

In this paper a database is a finite relational structure. A relational signature is a tuple , each being a relation symbol of arity . A relational structure over is a tuple , where is the domain of D and is a subset of . We will often write instead of when D is clear from the context.

We use a standard notion of size. The size of , denoted is the number of tuples in multiplied by the arity . The size of the domain of D, denoted , is the number of elements in . Finally the size of D, denoted by , is

By query we mean a formula written in the first-order logic, FO, built from atomic formulas of the form or for some relation , and closed under the usual Boolean connectives () and existential and universal quantifications (). We write to denote a query whose free variables are , and the number of free variables is called the arity of the query. A sentence is a query of arity 0. We use the usual semantics, denoted , for first-order. Given a structure D and a query , an answer to in D is a tuple of elements of D such that . We write for the set of answers to in D, i.e. . As usual, denotes the size of .

Let be a class of structures. The model checking problem for FO over is the computational problem of given first-order sentence and a database to test whether or not.

We now introduce our running examples.

Example A- 1.

The first query has arity 2 and returns pairs of nodes at distance 2 in a graph. The query is of the form .

Testing the existence of a solution to this query can be easily done in time linear in the size of the database. For instance one can go trough all nodes of the database and check whether it has non-nill in-degree and out-degree. The degree of each node can be computed in linear time by going through all edges of the database and incrementing the counters associated to its endpoints.

Example B- 1.

The second query has arity 3 and returns triples such that is connected to and via an edge but is not connected to . The query is of the form .

It is not clear at all how to test the existence of a solution to this query in time linear in the size of the database. The problem is similar to the one of finding a triangle in a graph, for which the best know algorithm has complexity even slightly worse than matrix multiplication [1]. If the degree of the input structure is bounded by a constant , we can test the existence of a solution in linear time by the following algorithm. We first go through all edges of the database and add to a list associated to and to a list associated to . It remains now to go through all nodes of the database, consider all pairs of nodes in the associated list (the number of such pairs is bounded by ) and then test whether there is an edge between and (by testing whether is in the list associated to ).

We aim at generalizing this kind of reasoning to structures with bounded expansion.

Given a query , we care about “enumerating” efficiently. Let be a class of structures. For a query , the enumeration problem of over is, given a database , to output the elements of one by one with no repetition. The maximal time between any two consecutive outputs of elements of is called the delay. The definition below requires a constant time delay. We formalize these notions in the forthcoming section.

1.1 Model of computation and enumeration

We use Random Access Machines (RAM) with addition and uniform cost measure as a model of computation. For further details on this model and its use in logic see [8]. In the sequel we assume that the input relational structure comes with a linear order on the domain. If not, we use the one induced by the encoding of the database as a word. Whenever we iterate through all nodes of the domain, the iteration is with respect to the initial linear order.

We say that the enumeration problem of over a class of structures is in the class , or equivalently that we can enumerate over with constant delay, if it can be solved by a RAM algorithm which, on input , can be decomposed into two phases:

  • a precomputation phase that is performed in time ,

  • an enumeration phase that outputs with no repetition and a constant delay between two consecutive outputs. The enumeration phase has full access to the output of the precomputation phase but can use only a constant total amount of extra memory.

Notice that if we can enumerate with constant delay, then all answers can be output in time and the first output is computed in time linear in . In the particular case of boolean queries, the associated model checking problem must be solvable in time linear in . Notice also that the total amount of memory used after computing all answers is linear in , while a less restrictive definition requiring only a constant time delay between any two outputs may yield in a total amount of memory linear in .

Note that we measure the running time complexity as a function of . The multiplicative factor will depend on the class of database under consideration and, more importantly, on the query . In our case we will see that the multiplicative factor is non elementary in and that cannot be avoided, see the discussion in the conclusion section.

We may in addition require that the enumeration phase outputs the answers to using the lexicographical order. We then say that we can enumerate over with constant delay in lexicographical order.

Example A- 2.

Over the class of all graphs, we cannot enumerate pairs of nodes at distance 2 with constant delay unless the Boolean Matrix Multiplication problem can be solved in quadratic time [5]. However, over the class of graphs of degree , there is a simple constant delay enumeration algorithm. During the preprocessing phase, we associate to each node the list of all its neighbors at distance 2. This can be done in time linear in the size of the database as in Example B- 1. We then color in blue all nodes having a non empty list and make sure each blue node points to the next blue node (according to the linear order on the domain). This also can be done in time linear in the size of the database and concludes the preprocessing phase. The enumeration phase now goes through all blue nodes using the pointer structure and, for each of them, outputs all pairs where is in the list associated to .

Example B- 2.

Over the class of all graphs, the query of this example cannot be enumerated with constant delay because, as mentioned in Example B- 1, testing whether there is one solution is already non linear. Over the class of graphs of bounded degree, there is a simple constant delay enumeration algorithm, similar to the one from Example A- 2.

Note that in general constant delay enumeration algorithms are not closed under any boolean operations. For instance it is not because we can enumerate and with constant delay, that we can enumerate with constant delay as enumerating one query after the other would break the “no repetition” requirement. However, if we can enumerate with constant delay in the lexicographical order, then a simple argument that resembles the problem of merging two sorted lists shows closure under union:

Lemma 1.

If both queries and can be enumerated in lexicographical order with constant delay then the same is true for .


The preprocessing phase consists in the preprocessing phases of the enumeration algorithms for and .

The enumeration phase keeps two values, the smallest element from that was not yet output and similarly the smallest element from that was not yet output. It then outputs the smaller of the two values and replaces it in constant time with the next element from the appropriate set using the associated enumeration procedure. In case the elements are equal, the value is output once and both stored values are replaced with their appropriate successors. ∎

It will follow from our results that the enumeration problem of FO over the class of structures with “bounded expansion” is in . The notion of bounded expansion was defined in [17] for graphs and then it was generalized to structures via their Gaifman or Adjacency graphs. We start with defining it for graphs.

1.2 Graphs with bounded expansion and augmentation

By default a graph has no orientation on its edges and has colors on its vertices. In an oriented graph every edge is an arrow going from the source vertex to its target. We can view a (oriented or not) graph as a relational structure , where is the set of nodes, is the set of edges and, for each , is a predicate of arity . We omit the subscripts when G is clear from the context. In the nonoriented case, is symmetric and we denote by the edge between and . In the oriented case we denote by the edge from to . We will use the notation when the graph is oriented and G in the nonoriented case. An orientation of a graph G is any graph such that implies or . The in-degree of a node of is the number of nodes such that . We denote by the maximal in-degree of a node of . Among all orientations of a graph G, we choose the following one, which is computable in time linear in . We find the first node of minimal degree, orient its edges towards it and repeat this inductively in the induced subgraph obtained by removing this node. The resulting graph, denoted , has maximal in-degree which is at most twice the optimal value and that is enouh for our needs.

In [17] several equivalent definitions of bounded expansion were shown. We present here only the one we will use, exploiting the notion of “augmentations”.

Let be an oriented graph. A -transitive fraternal augmentation of is any graph with the same vertex set as and the same colors of vertices, including all edges of (with their orientation) and such that for any three vertices of we have the following:


if and are edges in , then is an edge in ,


if and are edges in , then at least one of the edges: , is in ,


moreover, if contains an edge that was not present in , then it must have been added by one of the previous two rules.

Note that the notion of -transitive fraternal augmentation is not a deterministic operation. Although transitivity induces precise edges, fraternity implies nondeterminism and thus there can possibly be many different -transitive fraternal augmentations. We care here about choosing the orientations of the edges resulting from the fraternity rule in order to minimize the maximal in-degree.

Following [18] we fix a deterministic algorithm computing a “good” choice of orientations of the edges induced by the fraternity property. The precise definition of the algorithm is not important for us, it only matters here that the algorithm runs in time linear in the size of the input graph (see Lemma 2 below). With this algorithm fixed, we can now speak of the -transitive fraternal augmentation of .

Let be an oriented graph. The transitive fraternal augmentation of is the sequence such that for each the graph is the -transitive fraternal augmentation of . We will say that is the -th augmentation of . Similarly we denote the transitive fraternal augmentation of a nonoriented graph G by considering its minimal orientation .

Definition 1.

[17] Let be a class of graphs. has bounded expansion if there exists a function such that for each graph its transitive fraternal augmentation of G is such that for each we have .

Consider for instance a graph of degree . Notice that the -transitive fraternal augmentation introduces an edge between nodes that were at distance at most 2 in the initial graph. Hence, when starting with a graph of degree , we end up with a graph of degree at most . This observation shows that the class of graphs of degree has bounded expansion as witnessed by the function . Exhibiting the function for the other examples of classes with bounded expansion mentioned in the introduction: bounded treewidth, planar graphs, graphs excluding at least one minor, requires more work [17].

The following lemma shows that within a class of bounded expansion the -th augmentation of can be computed in linear time, the linear factor depending on and on .

Lemma 2.

[18] Let be a class of bounded expansion. For each and each , is computable from G in time .

A transitive fraternal augmentation introduces new edges in the graphs in a controlled way. We will see that we can use these extra edges in order to eliminate quantifiers in a first-order formula. Lemma 2 shows that this quantifier elimination is harmless for enumeration as it can be done in time linear in the size of the database and can therefore be done during the preprocessing phase.

1.3 Graphs of bounded in-degree as functional structures

Given the definition of bounded expansion it is convenient to work with oriented graphs. These graphs will always be such that the maximal in-degree is bounded by some constant depending on the class of graphs under investigation. It is therefore convenient for us to represent our graphs as functional structures where the functions links the current node with its predecessors. This functional representation turns out to be also useful for eliminating some quantifiers.

A functional signature is a tuple , each being a functional symbol of arity and each being an unary predicate. A functional structure over is then defined as for relational structures. FO is defined as usual over the functional signature. In particular, it can use atoms of the form , which is crucial for the quantifier elimination step of Section 3 as the relational representation would require existential quantification for denoting the same element. A graph of in-degree and colored with colors can be represented as a functional structure , where the unary predicates encode the various colors and if is the element (according to some arbitrary order that will not be relevant in the sequel) such that is an edge of . We call such node the predecessor of (where “ predecessor” should really be viewed as an abbreviation for “the node such that ” and not as a reference to the chosen order). If we do not care about the and we only want to say that is the image of under some function, we call it a predecessor of . Given a nonoriented graph G we define to be the functional representation of as described above. Note that is computable in time linear in and that for each first order query , over the relational signature of graphs, one can easily compute a first order query , over the functional signature, such that .

Example A- 3.

Consider again the query computing nodes at distance 2 in a nonoriented graph. There are four possible ways to orient a path of length 2. With the functional point of view we further need to consider all possible predecessors. Altogether the distance 2 query is now equivalent to:

where the small disjuncts correspond to the four possible orientations and the big one to all possible predecessors, each of them corresponding to a function name, whose number depends on the function .

Example B- 3.

Similarly, the reader can verify that the query of Example B- 1 is equivalent to:

Augmentation for graphs as functional structures. The notion of -transitive fraternal augmentation can be adapted directly to the functional setting. However it will be useful for us to enhance it with extra information. In particular it will be useful to remember at which stage the extra edges are inserted. We do this as follows.

Given a graph , its -transitive fraternal augmentation is constructed as follows. The signature of expands the signature of with new function symbols for taking care of the new edges created during the expansion and is then an expansion of over this new signature.

For any pair of functions and in the signature of there is a new function in the signature of representing the transitive part of the augmentation. It is defined as the composition of and , i.e.

Similarly, for any pair of functions and in the signature of , and any node in the domain of both and there will be a function in the new signature representing the fraternity part of the augmentation. I.e is such that or .

Given a class of bounded expansion, the guarantees that the number of new function symbols needed for the -th augmentation is bounded by and does not depend on the graph. Hence a class of bounded expansion generates finite functional signatures such that for any graph and for all :

  1. is a functional structure over computable in linear time from G,

  2. is an expansion of ,

  3. for every FO query over and every we have that .

We denote by the number of function symbols of . Notice that we have .

We say that a functional signature is a recoloring of if it extends with some extra unary predicates, also denoted as colors, while the functional part remains intact. Similarly, a functional structure over is a recoloring of over if is a recoloring of and is a -expansion of . We write is over a recoloring of if is over and is a recoloring of . Notice that the definition of bounded expansion is not sensitive to the colors as it depend only on the binary predicates, hence adding arbitrarily many extra colors is harmless.

Given a class of graphs, for each , we define to be the class of all recolorings of for some . In other words is the class of functional representations of all recolorings of all -th augmentations of graphs from . Note that all graphs from are recolorings of a structure in , hence they use at most function symbols.

From now on we assume that all graphs from and all queries are in their functional representation. It follows from the discussion above that this is without loss of generality.

1.4 From structures to graphs

A class of structures is said to have bounded expansion if the set of adjacency graphs of the structures of the class has bounded expansion.

The adjacency graph of a relational structure D, denoted by , is a functional structure defined as follows. The set of vertices of is where is the domain of D and is the set of tuples occurring in some relation of D. For each relation in the schema of D, there is a unary symbol coloring the elements of belonging to . For each tuple such that for some relation of arity , we have an edge for all .

Observation 1.

It is immediate to see that for every relational structure D we can compute in time .

Let be a class of relational structures. We say that has bounded expansion if the class ’ of adjacency graphs (seen as graphs) of structures from has bounded expansion.

Remark 1.

In the literature, for instance [9, 11], a class of relational structures is said to have bounded expansion if the class of their Gaifman graphs has bounded expansion. It is easy to show that if the class of Gaifman graphs of structures from has bounded expansion then the class of adjacency graphs of structures from also has bounded expansion. The converse is not true in general. However the converse holds if the schema is fixed, i.e. is a class of structures all having the same schema. We refer to [13] for the simple proofs of these facts.

Let be the function given by Definition 1 for ’. The following lemma is immediate. For instance is rewritten as .

Lemma 3.

Let be a class of relational structures with bounded expansion and let ’ be the underlying class of adjacency graphs. Let . In time linear in the size of we can find a query over such that for all we have .

As a consequence of Lemma 3 it follows that model checking, enumeration and counting of first-order queries over relational structures reduce to the graph case. Therefore in the rest of the paper we will only concentrate on the graph case (viewed as a functional structure), but the reader should keep in mind that all the results stated over graphs extend to relational structures via this lemma.

2 Normal form for quantifier-free first-order queries

We prove in this section a normal form on quantifier-free first-order formulas. This normal form will be the ground for all our algorithms later on. It says that, modulo performing some extra augmentation steps, a quantifier-free formula has a very simple form.

Fix class of graphs with bounded expansion. Recall that we are now implicitly assuming that graphs are represented as functional structures.

A formula is simple if it does not contain atoms of the form , i.e. it does not contain any compositions of functions. We first observe that, modulo augmentations, any formula can be transformed into a simple one.

Lemma 4.

Let be a formula over a recoloring of . Then, for , there is a simple formula over a recoloring of such that:

for all graph there is a graph computable in time linear in such that .


This is a simple consequence of transitivity. Any composition of two functions in represents a transitive pair of edges and becomes an single edge in the -augmentation of . Then over is equivalent to over , where is one of the new function introduced by the augmentation and the newly introduced color holds for those nodes , for which the . As the nesting of compositions of functions is at most , the result follows. The linear time computability is immediate from Lemma 2. ∎

We make one more observation before proving the normal form:

Lemma 5.

Let . Let be a node of . Let be all the predecessors of in and set . Let be the -th augmentation of . There exists a linear order induced on by , such that for all , implies is an edge of for some function from .


This is because all nodes of are fraternal and the size of is at most . Hence, after one step of augmentation, all nodes of are pairwise connected and, after at most further augmentation steps, if there is a directed path from one node of to another node of , then there is also a directed edge from to . By induction on we show that there exists a node such that for all there is an edge from to . If there is nothing to prove. Otherwise fix and let . By induction we get a in satisfying the properties. If there is an edge from to , also works for and we are done. Otherwise there must be an edge from to . But then there is a path of length 2 from any node of to . By transitivity this means that there is an edge from any node of to and is the node we are looking for.

We then set as the minimal element of our order on and we repeat this argument with . ∎

Lemma 5 justifies the following definition. Let be a number and let . A -type is a quantifier-free formula over the signature with one free variable consisting in the conjunction of a maximal consistent set of clauses of the form or . Given a node of some graph of , its -type is the set of clauses satisfied by in the -th augmentation of . From Lemma 5 it follows that the -type of induces a linear order on its predecessors in . Indeed the predecessors of in can be deduced from the -type by looking at the clauses where is a function symbol from and the linear order can be deduced from the clauses . Lemma 5 guarantees that these latter clauses induce a linear order. In the sequel we denote this property as “the -type induces the order ” and for we refer to the linking to as .

Note that for a given there are only finitely many possible -types and that each of them can be specified with a conjunctive formula over .

We now state the normal form result.

Proposition 1.

Let be a simple quantifier-free query over a recoloring of . There exists that depends only on and and a quantifier-free query over a recoloring of that is a disjunction of formulas:


where contains a -type of ; is either empty or contains one clause of the form or one clause of the form for some suitable , and ; and contains arbitrarily many clauses of the form or . Moreover, is such that:

for all there is a computable in time linear in with .


Set as given by Lemma 5. We first put into a disjunctive normal form (DNF) and in front of each such disjunct we add a big disjunction over all possible -types of (recall that a type can be specified as a conjunctive formula). We deal with each disjunct separately.

Note that each disjunct is a query over of the form:

where all sub-formulas except for are as desired. Moreover, , and are in fact queries over . At this point contains arbitrarily many clauses of the form or . If it contains at least one clause of the form , we can replace each other occurrence of by and we are done.

Assume now that contains several conjuncts of the form . Assume wlog that is such that , where are all the predecessors of from . Let be the smallest index such that a clause of the form belongs to . We have in and recall that specifies for a function in such that . Then, as is of type , a clause of the form with is equivalent to . ∎

Example A- 4.

Let us see what Lemma 4 and the normalization algorithm do for and some of the disjuncts of the query of Example A- 3:

In the case of note that by transitivity, in the augmented graph, this clause is equivalent to one of the form (this case is handled by Lemma 4).

Consider now . It will be convenient to view this query when plays the role of in Proposition 1. Notice that in this case it is not in normal form as contains two elements. However, the two edges and are fraternal. Hence, after one augmentation step, a new edge is added between and and we either have or for some in the new signature.

Let be -type stating that and be -type stating that . It is now easy to see that the query is equivalent to

3 Model checking

In this section we show that the model checking problem of FO over a class of structures with bounded expansion can be done in time linear in the size of the structure. This gives a new proof of the result of [9]. Recall that by Lemma 3 it is enough to consider oriented graphs viewed as functional structures.

Theorem 1.

[9] Let be a class of graphs with bounded expansion and let be a sentence of FO. Then, for all , testing whether can be done in time .

The proof of Theorem 1 is done using a quantifier elimination procedure: given a query with at least one free variable we can compute a quantifier-free query that is “equivalent” to . Again, the equivalence should be understood modulo some augmentation steps for a number of augmentation steps depending only on and . When starting with a sentence we end-up with being a boolean combination of formulas with one variable. Those can be easily tested in linear time in the size of the augmented structure, which in turns can be computed in time linear from the initial structure by Lemma 2. The result follows. We now state precisely the quantifier elimination step:

Proposition 2.

Let be a class of graphs with bounded expansion witnessed by the function . Let be a quantifier-free formula over a recoloring of . Then one can compute a and a quantifier-free formula over a recoloring of such that:

for all there is a such that:

Moreover, is computable in time .


In view of Lemma 4 we can assume that is simple. We then apply Proposition 1 to and and obtain a and an equivalent formula in DNF, where each disjunct has the special form given by (1). As disjunction and existential quantification commute, it is enough to treat each part of the disjunction separately.

We thus assume that is a quantifier-free conjunctive formula over a recoloring of of the form (1):

Let’s assume that the -type satisfied by enforces , where are all the images of by a function from . Moreover, for each , contains an atom of the form for some function .

We do a case analysis depending on the value of .

If is for some function and some , then we replace with everywhere in resulting in a formula having obviously the desired properties.

Assume now that is of the form . Without loss of generality we can assume that is and . In other words is the constraint .

The general idea is to limit the quantification on to a finite set (whose size depends only on and ), depending only on . We then encode these set using suitable extra colors. To do this, for each node we first compute a set such that for each tuple we have iff . Moreover, for all , where is a number depending only on . We then encode these witness sets using suitable extra colors.

The intuition behind the Witness set is as follows. Assume first that is empty. Then we only need to test the existence of such that . To do so, we scan through all nodes , test if holds and if so we add to if this set is empty and do nothing otherwise. Clearly each witness set has size one and the desired properties are verified. It is then enough to color with a new color red all nodes having a non-empty witness set and is then equivalent to .

The situation is slightly more complicated if is not empty. Assume for instance that contains only constraints of the form . Then the previous procedure does not work because because may be such that it is equal to . However there are only nodes that we need to avoid, where depends only on the formula, hence if contains at least nodes we are sure that at least one of them will satisfy all the inequality constraints. We implement this by scanning through all nodes , test if holds and if so we add to if this set has a size smaller or equal to do nothing otherwise. The difficulty is to encode this set into the formula. If the witness set is of size one of its element must make all inequalities true hence a new color as before does the job. When the set has a smaller size we need to test each of its elements against the inequalities. For this we introduce a predicates , and add a node to if has been added as the element in . Once this is done we can check that the witness of verify by testing that is not the witness of , i.e. : .

The general case, when contains also clauses of the form is more complex and require an even bigger witness set but this is essentially what we do.

Computation of the Witness function

We start by initializing for all .

We then successively investigate all nodes of and do the following. If then we move on to the next . If then let be the current value of - if is empty then we add to this set and move on to the next node of .

Let be .

Let be minimal such that there exists with (notice that ). Note that because for all verifying and all , this implies that and agree on each with and disagree on each with .

Let , where is in the case where . If then we add to .

Analysis of the Witness function

Clearly the algorithm computing the witness function runs in linear time.

Moreover, for each node , can be represented as the leaves of a tree of depth at most and of width . To see this, notice that all nodes of are such that . Note also that if two nodes and satisfying share a predecessor, , then for all , and agree as for all nodes satisfying . The depth of the least common ancestor of two nodes and of is defined as the least such that and agree on . One can then verify that by construction of the tree has the claimed sizes. Hence the size of is bounded by .

We now show that for each tuple and each node such that there is a node in such that .

To see this assume . If we are done. Otherwise note that and that . Let and be as described in the algorithm when investigating . As was not added to , we must have . Let be the elements of providing pairwise different values for . Among these, at most of them may be of the form for some and as each has at most predecessors. Notice that for all and all , agree with on and therefore they also agree on for as for all nodes satisfying . When the values of and must be different if as otherwise and would also agree on as for all nodes satisfying . Therefore, for each and each there are at most of the such that is a predecessor of .

Altogether most nodes may falsify an inequality constraint. As is strictly bigger than that, one of the is the desired witness.

Recoloring of

Based on Witness we recolor as follows. Let