Output-Sensitive Evaluation of Acyclic Conjunctive Regular Path Queries
Abstract
Conjunctive Regular Path Queries, or CRPQs for short, are an essential construct in graph query languages. In this paper, we propose the first output-sensitive algorithm for evaluating acyclic CRPQs. It is output-sensitive in the sense that its complexity is a function of the sizes of the input graph and of the query output and not of the output sizes of the regular expressions that appear in the query, as these latter sizes can be larger than the query output size.
Our algorithm proceeds in two stages. In the first stage, it contracts the given query into a free-connex acyclic one such that the output of the original query can be obtained from the output of the contracted one. This contraction removes bound variables by composing regular expressions or by promoting bound variables to free ones. The minimum necessary number of promoted bound variables gives the contraction width, which is a novel parameter specific to CRPQs. In the second stage, our algorithm evaluates the free-connex acyclic CRPQ and projects away the columns of the promoted bound variables. It ensures output-sensitivity by computing the calibrated outputs of the regular expressions appearing in the free-connex acyclic CRPQ in time proportional to their sizes.
Our algorithm has lower complexity than the state-of-the-art approaches for problem instances where the query output is asymptotically smaller than the output sizes of the regular expressions that appear in the query.
Keywords and phrases:
graph databases, regular path queries, output-sensitive algorithmsCopyright and License:
Zilu Tian; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Information systems Graph-based database models ; Theory of computation Database query processing and optimization (theory) ; Theory of computation Graph algorithms analysisFunding:
This work was partially supported by NSF-BSF 2109922, NSF-IIS 2314527, NSF-SHF 2312195, and SNSF 200021-231956.Editors:
Balder ten Cate and Maurice FunkSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
The evaluation of conjunctive regular path queries is a central problem in graph databases. Such queries are an essential construct in graph query languages in both academia and industry, e.g., [17, 16, 27, 26, 12, 15]. Regular path queries have been investigated extensively, e.g., [23, 9, 29, 5, 4], as they represent the key differentiator of graph query languages from the purely relational query languages.
Let be a directed graph, where is the set of vertices and is the set of edges labeled with symbols from an alphabet . A regular path query, or RPQ for short, is a regular expression over . Its semantics is given by the set of vertex pairs such that can be reached in from via a path labeled with a string from the language defined by . A conjunctive regular path query, or CRPQ for short, is a conjunction of binary atoms, with each atom defined by a regular path query: , where each is an RPQ, each is a pair of variables, and is the set of free variables.
Our main result is stated by the following theorem:
Theorem 1.
Any acyclic CRPQ over an edge-labeled graph can be evaluated in data complexity, where OUT is the size of the output of and is the contraction width of .
The complexity stated in Theorem 1 is achieved by an algorithm that is output-sensitive. Its data complexity is a function of the sizes of the input graph and of the query output and not of the sizes of the RPQs in the query. This algorithm falls in the class of output-sensitive algorithms for acyclic conjunctive queries [30, 19] and RPQs [20]. It is different from algorithms that rely on computing a compact representation of the query output followed by constant-delay enumeration of the output tuples and whose data complexity is a sum of a function in the input size and of the output size OUT, e.g., factorized query processing [25], FAQ [21], or PANDA [3]. It is also different from algorithms that first compute the RPQs in the query, followed by the computation of the resulting conjunctive query, e.g., [11]. In this sense, ours is the first output-sensitive algorithm for evaluating acyclic111The notion of acyclicity for CRPQs is more restrictive than -acyclicity for conjunctive queries, in the sense that it allows neither atoms in which the same variable appears twice nor two distinct atoms defined over the same pair of variables (see Definition 6 and Remark 7). CRPQs.
In a nutshell, our algorithm proceeds in two stages. In the first stage, it contracts the given CRPQ into a free-connex CRPQ such that the output of can be obtained from the output of . This contraction repeatedly uses two mechanisms to rewrite the query until they cannot be applied anymore. The first mechanism removes a bound variable that appears in at most two atoms of . This corresponds to concatenating the RPQs in the two atoms into a single RPQ. It exploits the closure of RPQs under composition and does not increase the data complexity of RPQ evaluation. The second mechanism promotes those bound variables that violate the free-connex property to free variables so that the query becomes free-connex. This can increase the query output size by a multiplicative factor of at most for each promoted bound variable. The minimum necessary number of promoted bound variables gives the contraction width of , which is a novel parameter specific to acyclic CRPQs. The size of the output of can thus be at most , where OUT is the size of the output of .
In the second stage, our algorithm evaluates the free-connex acyclic CRPQ . For this, it first computes the calibrated outputs of the RPQs in in time proportional to their sizes. For an RPQ atom in , where is the set of free variables of that occur in this atom, its calibrated output is the projection of the output of onto . This calibration ensures that the time to compute the individual RPQs does not exceed the size of the output of . It is reminiscent of the classical Yannakakis algorithm [30]: It is done in one bottom-up pass followed by one top-down pass over the join tree of . However, whereas the Yannakakis algorithm works on input relations that are materialized and have sizes linear in the input database size, our algorithm works with non-materialized RPQs whose output sizes can be larger than the input graph size and even than the query output size. In our setting, calibration is thus essential for output sensitivity. By joining the calibrated RPQ outputs, the algorithm then computes the output of . The output of is then computed by projecting away the columns of the promoted bound variables from the output of .
Although the contraction width measures the complexity of transforming acyclic CRPQs into free-connex acyclic CRPQs, it differs from the free-connex fractional hypertree width for acyclic conjunctive queries [25, 19].
Proposition 2.
There is an infinite class of CRPQs such that the contraction width of the queries in the class is bounded but their free-connex fractional hypertree width is unbounded.
For instance, the free-connex acyclic CRPQs have free-connex fractional hypertree width and contraction width . The -path CRPQs, where the join variables are bound and the other variables are free, have and . The star CRPQs, where the join variable is bound and all other variables are free, have unbounded free-connex fractional hypertree width and .
Of particular interest are CRPQs with contraction width 0, since our algorithm evaluates them with the lowest complexity among all CRPQs: . This matches the best known output-sensitive data complexity for evaluating RPQs [20]. Examples of CRPQs with contraction width 0 include: (i) free-connex acyclic CRPQs, but also (ii) non-free-connex acyclic CRPQs that are obtained from free-connex acyclic CRPQs by replacing any RPQ atom, whose variables are free, by an arbitrarily long path CRPQ whose free variables are the same as of the replaced atom. For instance, the -path CRPQs with and an arbitrary set of free variables have contraction width 0.
For free-connex acyclic CRPQs, we give a more refined complexity of our algorithm that uses the maximum size of the calibrated output of any RPQ in the query. Note that by construction, . Also, since all RPQ atoms are binary, .
Proposition 3.
-
For any free-connex acyclic CRPQ , it holds .
-
Any free-connex acyclic CRPQ over an edge-labeled graph can be evaluated in data complexity.
Comparison with Related Work
None of the existing evaluation algorithms for CRPQs is output-sensitive: Their complexity is not only a function of the input data graph and query output sizes but also of the output sizes of the RPQs in the query, yet the RPQ outputs can be asymptotically larger than the query output. In the following, we overview the state-of-the-art algorithms for CRPQ evaluation and show that, for problem instances where the query output is asymptotically smaller than the largest output size of any of the RPQs in the query, our algorithm can outperform them.
The classical evaluation algorithm for RPQs, called PG, computes the product graph of the data graph and the non-deterministic finite automaton defined by the given RPQ and runs in data complexity [5, 22, 23]. This cannot be improved by combinatorial algorithms: There is no combinatorial222This term is not defined precisely. A combinatorial algorithm does not rely on Strassen-like fast matrix multiplication. Using fast matrix multiplication, the data complexity of PG can be improved to , where is the matrix multiplication exponent [10] and currently . algorithm to compute RPQs with data complexity333The lower bound in [10] has instead of , but the proof also holds for the formulation that just uses . for any , unless the combinatorial Boolean Matrix Multiplication conjecture fails [10].444The combinatorial Boolean Matrix Multiplication conjecture is as follows: Given two Boolean matrices , there is no combinatorial algorithm that multiplies and in for any . When taking the query output into consideration, however, PG’s complexity can be improved. The output-sensitive variant of PG, called OSPG, evaluates any RPQ with data complexity, where OUT is the output size of the RPQ [20].555The data complexity of OSPG is [20]. The user-defined threshold was set to in [20], yielding . For , we obtain instead , where the additive term is necessary to scan the input data even when for . The complexity of OSPG is at most the complexity of PG, since .
The state-of-the-art evaluation algorithm for CRPQs proceeds in two stages [11]. Using PG, it first materializes the binary relations representing the output of the RPQs in the given CRPQ . This stage turns the CRPQ into an equivalent conjunctive query over materialized binary relations. The algorithm then evaluates using a worst-case optimal join algorithm [28, 24]. For acyclic CRPQs with arbitrary free variables, we can immediately improve this state-of-the-art. First, we use OSPG instead of PG to materialize the RPQs with data complexity, where is the maximum size of the non-calibrated output of the RPQs.666 is not to be confused with , which is the maximum size of the calibrated output of any RPQ in . It holds that and there can be instances for which yet . Then, can be evaluated using the classical Yannakakis algorithm, called here Yan, [30] with data complexity, or using its output-sensitive refinement [19], called here OSYan, in data complexity, where is the free-connex fractional hypertree width of and OUT is the output of (and also of ). Overall, the best complexity for evaluating acyclic CRPQs using prior work can therefore be achieved by combining OSPG and OSYan: . Yet this approach, as well as all previous approaches, are not output-sensitive: Indeed, one can easily construct data graphs for which the output of some RPQs in consists of all quadratically many pairs of vertices in the data graph, i.e., , while the query output is empty, i.e., . In that case, our algorithm takes time , whereas OSPG+OSYan takes .
For free-connex acyclic CRPQs, which have free-connex fractional hypertree width 1, OSPG+OSYan needs , which simplifies to , since the time to compute the RPQs cannot be smaller than the maximum output size of the RPQs. In contrast, our algorithm needs (Proposition 16). Since , this implies that for free-connex acyclic queries, our algorithm performs at least as good as OSPG+OSYan and outperforms OSPG+OSYan in case is larger than . The next examples compare our algorithm with OSPG+OSYan on CRPQs that are not free-connex acyclic. Further examples are given in Appendix A.
Example 4.
Consider the -path CRPQ , where are RPQs. The contraction width of is , since we can compose the RPQs into a single RPQ and rewrite into the free-connex query . The free-connex fractional hypertree width of is . Thus, can be computed in time using our algorithm (Theorem 1) and in time using OSPG+OSYan. Depending on the scale of , one algorithm is asymptotically faster than the other. Our algorithm outperforms OSPG+OSYan when or , i.e., when the RPQ outputs are asymptotically larger than or the query output size OUT.
Example 5.
Consider the -star CRPQ , where are RPQs for . The contraction width of is and the free-connex fractional hypertree width is . Thus, can be computed in time using our algorithm (Theorem 1) and in time using OSPG+OSYan. A fine-grained comparison of the two approaches is rather daunting. Yet it can be observed that depending on the scale of and OUT, one algorithm incurs a lower runtime than the other. One regime where our algorithm is faster is for large RPQ outputs but small overall query output: for instance, for and , our algorithm is faster by an factor. Our algorithm achieves the stated complexity as follows. It first promotes the bound variable to free, so that the obtained query becomes free-connex. Then it evaluates with the same complexity as computing RPQs using OSPG, i.e., in time . The output of can be a multiplicative factor larger than the output of . The complexity thus has in place of OUT.
The rest of the paper is organized as follows. Section 2 introduces basic concepts. Section 3 defines and illustrates the contraction width for CRPQs. Sections 4 and 5 detail our algorithms for free-connex and general acyclic CRPQs, respectively. We conclude in Section 6. Appendix A compares our algorithm with prior approaches via illustrating examples. Appendix B gives the proof of one of our main statements. Further details are deferred to the extended technical report [2].
2 Preliminaries
For a natural number , we define . In case , we have .
Regular Languages
An alphabet is a finite set of symbols. The set of all (finite) strings over an alphabet is denoted as . A language over is a subset of . We use the standard definition of regular expressions composed of symbols from an alphabet , the empty string symbol , and the concatenation, union, and Kleene star operators. We denote by the language defined by a regular expression . A language is regular if it can be defined by a regular expression.
The reverse of a string is defined recursively as follows: and if for some and . The reverse of a language is . For every regular expression , we can construct in time a regular expression that defines the reverse of [18].
Edge-Labeled Graphs
An edge-labeled graph is a directed graph , where is a set of vertices, is an alphabet, and is a set of labeled edges. A triple denotes an edge from vertex to vertex labeled by 777Without loss of generality, we consider edge-labeled graphs where every vertex has at least one incident edge. A single pass over the edge relation suffices to extract these vertices.. A path in from vertex to vertex consists of a sequence of vertices from and a sequence of symbols from for some such that for every , there is an edge . We say that the string is the label of the path .
The symmetric closure of contains for each edge in labeled by some symbol , an additional edge in opposite direction labeled by a fresh symbol . That is, , where and .
Conjunctive Regular Path Queries
A regular path query (RPQ) over an alphabet is a regular expression using . The output of an RPQ evaluated over an edge-labeled graph is the set of all pairs of vertices such that contains a path from to labeled by a string from .
A conjunctive regular path query (CRPQ), or query for short, is of the form
| (1) |
where: each is an RPQ; each is an atom; is the set of variables; is the set of free variables; is the set of bound variables; is the set of atoms; and the conjunction of the atoms is the body of the query. The query is called full if all its variables are free.
The query graph of is an undirected multigraph , where is the variable set of and is a multiset that contains an undirected edge for each atom , i.e., . Hence, an atom with is represented by the singleton set in the query graph. The leftmost graph in Figure 1 visualizes the query graph (free variables are underlined) of the CRPQ in Example 11. A cycle in a query graph is a sequence of vertices and edges for such that (1) for and , (2) for and , and (3) .
The notion of acyclicity for CRPQs is slightly more restrictive than -acyclicity for conjunctive queries [8], in the sense that an acyclic CRPQ contains neither atoms in which the same variable appears twice (i.e., atoms of the form ) nor two distinct atoms that involve the same pair of variables (e.g., two atoms of the form and ):
Definition 6 (Acyclic Query).
A CRPQ is acyclic if its query graph is cycle-free.
It follows from the above definition that the graph of an acyclic CRPQ contains neither self-loops (i.e., edges of the form ) nor multiple edges between the same vertex pair. This means that the query graph is a forest.
Remark 7.
The restriction imposed by Definition 6 to acyclic CRPQs is justified by the observation that a CRPQ containing an atom of the form is already as hard to evaluate as the cyclic conjunctive query , for any , where the materialized relation consists of all vertex pairs in the input graph that are connected by an edge labeled by the symbol . Indeed, we can choose to be the regular expression , which consists of the concatenation of occurrences of . A similar observation holds for CRPQs containing a subquery of the form or .
For any acyclic query, we can construct a join tree, which is a tree such that: (1) the nodes of the tree are the atoms of the query, and (2) for each variable, the following holds: if the variable appears in two atoms, then it appears in all atoms on the unique path between these two atoms in the tree [8].
Since all atoms in a CRPQ are binary, the standard definition of free-connex acyclic queries [7] simplifies as follows:
Definition 8 (Free-Connex Acyclic Query).
An acyclic CRPQ is free-connex if each tree of the query graph contains a connected subtree whose variables are exactly ’s free variables.
Any free-connex acyclic query has a free-top join tree, which is a join tree where all atoms containing a free variable form a connected subtree including the root.
We define the semantics of the CRPQ from Eq. (1) over an edge-labeled graph using homomorphisms, which are mappings from the query variables to the vertices of [4]. A mapping is in the output of , if it can be extended to a mapping such that has a path from to labeled by a string from , for every . For convenience, we represent a mapping by the tuple , assuming a fixed order on the free variables.
The calibrated output of an RPQ in consists of all tuples in the projection of the output of onto the variables and . Hence, the calibrated output of RPQs whose variables are both bound is empty.
Our evaluation algorithms use at intermediate steps conjunctive queries that have atoms of the form , where references a -ary materialized relation for some . They also use queries of the form where is an RPQ and each is a unary or nullary materialized relation over the vertex set of the input graph. The semantics of such queries is straightforward (more details on conjunctive and this kind of queries are given in the extended technical report [2]).
The CRPQ Evaluation Problem and Complexity Measures
The evaluation problem for a CRPQ has the following input and output:
| Input | An edge-labeled graph |
| Output | The set of all tuples in the output of over |
We use data complexity, i.e., we consider the query fixed and of constant size and measure the time complexity of solving the evaluation problem for CRPQs in terms of the number of vertices and the number of edges of the input graph, the size OUT of the output of the query, and the maximum size of the calibrated output of any RPQ in the query.
We consider the RAM model of computation and assume that each materialized relation over the vertex set of the input graph is implemented by a data structure such that: for every and vertex tuple over , the tuples in can be enumerated with constant delay.
3 A Width Measure for Acyclic CRPQs
In this section, we introduce the contraction width, a new measure to express the complexity of evaluating acyclic CRPQs. We do so in two stages. First, we introduce an elimination procedure for query graphs that allows to remove or contract edges through the elimination of bound variables (Section 3.1). To obtain the contraction width of a CRPQ , we take a free-connex tree decomposition of and apply the elimination procedure to each query induced by a bag of the decomposition (Section 3.2). The contraction width of the decomposition is the maximal number of remaining bound variables in any induced query. The contraction width of is the minimal contraction width of any free-connex tree decomposition of .
3.1 Elimination Procedure for CRPQ Graphs
Consider a cycle-free undirected graph . The degree of a vertex in is the number of its neighbors, i.e., the number of vertices with and . We define two contraction operations on that eliminate vertices of degree 1 or 2:
-
Elimination of a degree-2 vertex: A graph is the result of the elimination of a degree-2 vertex in if for distinct and . That is, is obtained from by removing and the two edges to its only neighbors and and adding a new edge connecting and .
-
Elimination of a degree-1 vertex: A graph is the result of the elimination of a degree-1 vertex in if for some and , such that: (1) and; (2) if there is no with , or otherwise. In other words, results from by removing the vertex , the edge to its only neighbor , and also if has no neighbor other than .
We refer to the graph obtained from after eliminating a vertex by .
Definition 9 (Elimination Order).
Consider an acyclic CRPQ and its query graph . An elimination order for is a sequence of bound variables such that there is a sequence of undirected graphs with , for , and all bound variables in have degree at least 3. We define and call a contracted query graph of .
Any elimination order leads to the same contracted query graph:
Proposition 10.
Every acyclic CRPQ has a unique contracted query graph.
We denote the contracted query graph of a CRPQ by .
Example 11.
Consider the following CRPQ whose query graph is depicted on the left in Figure 1 with free variables being underlined: . The degree of the bound variable is 2, so we eliminate it and obtain the second graph in Figure 1. Next, we eliminate the bound variable with degree 1, which results in the third graph in Figure 1. Finally, we eliminate the bound variable with degree 1, resulting in the fourth graph in Figure 1. This graph is the contracted query graph of , since the only remaining bound variables and have both degree 3.
3.2 Contraction Width
Definition 12 (Tree Decomposition).
A tree decomposition of a CRPQ is a pair , where is a tree with vertices and maps each vertex of to a subset of variables of such that the following properties hold:
-
for every , it holds for some , and
-
for every , the set is a non-empty connected subtree of .
The sets are called the bags of the tree decomposition. We denote the set of bags of by .
The CRPQ induced by a bag has the same body as and has the free variables .888Our definition of induced query is tailored to CRPQs and differs from the standard notion of induced conjunctive queries. Given a tree decomposition for a conjunctive query , the query induced by a bag is defined as . Applying this definition to CRPQs can lead to induced queries that are not CRPQs, since their atoms can have less than two variables. A tree decomposition is free-connex if contains a connected subtree such that the union of the bags in this subtree is the set of free variables. We use to denote the set of all free-connex tree decompositions of the query .
The contraction width of a tree decomposition is the maximal number of bound variables in the contracted query graph of any induced query. The contraction width of a CRPQ is the minimal contraction width of any of its free-connex tree decompositions:
Definition 13 (Contraction Width).
The contraction width of any acyclic CRPQ and the contraction width of any tree decomposition of are:
where is the set of bound variables in the contracted query graph of the query induced by the bag .
Note that two induced queries with different sets of free variables can impact differently the contraction width of the decomposition, even though they have the same body. This is because fewer free variables can enable the elimination of a larger number of bound variables.
The next example shows that a decomposition with several bags can lead to a lower contraction width compared to a trivial decomposition consisting of a single bag.
Example 14.
Consider again the CRPQ from Example 11 and the trivial tree decomposition for with a single bag. The query induced by the single bag is the query itself. Hence, the contraction width of this decomposition is 2, as shown in Example 11.
Now, consider the free-connex tree decomposition for from Figure 2 (left). The figure next shows the query graph of the induced query for the bag . The next graph is the query graph obtained after eliminating . The second graph from the right is obtained after eliminating the variables , , , and one after the other. We obtain the rightmost graph after eliminating . This graph is the contracted query graph of , since its only bound variable is of degree 3. Hence, . Analogously, we can show that the number of bound variables in the contracted graphs of each of the other induced queries is 1. Hence, . It can be shown that .
The next example illustrates that the contraction width of free-connex acyclic queries is 0, as claimed in Proposition 3:
Example 15.
Consider the free-connex acyclic CRPQ and its free-connex tree decomposition consisting of the two bags and . The induced query is the same as . Figure 3 depicts on the left the query graph of and right to it the graphs obtained after the elimination of the bound variables , , and . The final contracted query graph has no bound variables. Similarly, we can show that contracted query graph of has no bound variables. This implies .
Another well-known width measure based on tree decompositions is the fractional hypertree width (). Its definition is given in the extended technical report [2].
4 Evaluation of Free-Connex Acyclic CRPQs
In this section, we present our evaluation strategy for free-connex acyclic CRPQs and show that it runs within the time bound given in Proposition 3.
4.1 Evaluation Strategy
Given a free-connex acyclic CRPQ, we construct a free-top join tree for the query and perform, analogous to the Yannakakis algorithm [30], a bottom-up and a top-down pass over the tree to compute the calibrated outputs of the RPQs occurring in the query. We then join these output relations to obtain the final output. The key difference from the Yannakakis algorithm is that the outputs of the RPQs are not materialized prior to calibration. In what follows, we first present our evaluation strategy and subsequently illustrate it in Example 17.
| EvalFreeConnex (free-connex acyclic CRPQ , edge-labeled graph ) |
| 1 let |
| 2 let be a free-top join tree for ; let = root of ; let |
| 3 BottomUp; TopDownG; |
| 4 assume is |
| 5 return the output of |
| BottomUp(atom , variable set , |
| tree , relation set , edge-labeled graph ) |
| 1 foreach |
| 2 BottomUp |
| 3 if has no parent |
| 4 let |
| 5 update to |
| 6 else |
| 7 let |
| 8 let |
| TopDown(atom , variable set , |
| tree , relation set , edge-labeled graph ) |
| 1 if |
| 2 return |
| 3 if has parent |
| 4 let |
| 5 let |
| 6 let |
| 7 update to |
| 8 foreach |
| 9 TopDown() |
The procedure EvalFreeConnex in Figure 4 describes our evaluation strategy for a free-connex acyclic CRPQ and an edge-labeled graph . It starts with constructing a free-top join tree for , which means that the atoms containing a free variable form a connected subtree in including the root. The bottom-up and top-down passes are described by the subprocedures BottomUp and TopDown, respectively. During these passes, the procedure computes, among other relations, a set of relations whose join is the output of . This set consists of the calibrated output of the root atom and of all atoms that have a free variable. Next, we describe the procedures BottomUp and TopDown in more detail.
The main purpose of BottomUp is to materialize the calibrated output of the root atom in the join tree. It computes at each non-root atom a relation that joins with the relations computed at the children of and projects the result onto the common variables of and its parent atom (Lines 7-8 of BottomUp). If is the root, the procedure computes the relation , defined analogously to except that its set of variables is the intersection of with the free variables of (Lines 3–4 of BottomUp). The relation is the calibrated output of .
The main purpose of the subprocedure TopDown is to materialize at each atom that is not the root of the join tree and contains at least one free variable of , the calibrated output of the RPQ . For this, the procedure computes at each such atom with parent , a relation that is the projection of the relation , computed at the parent atom, onto the common variables of and . Then, it materializes the relation by computing the projection of the join of and onto the free variables of (Lines 4–6 of TopDown).
After the execution of BottomUp and TopDown, the set consists of the calibrated outputs of the root atom of the join tree and all other atoms that have at least one free variable. The procedure returns the output of the conjunctive query , which is the output of (Line 5 of EvalFreeConnex). We can show:
Proposition 16.
Given a free-connex acyclic CRPQ and an edge-labeled graph , the procedure EvalFreeConnex evaluates in data complexity, where is the maximum size of the calibrated output of any RPQ in .
Example 17.
Consider the CRPQ . Figure 5 depicts a free-top join tree for the query, along with the materialized relations computed by our evaluation strategy. The relations , , , and are computed during the bottom-up pass over the join tree, while the relations , , , and are materialized during the top-down pass. The relation serves as a filter to discard tuples from the output of that do not join with any tuple from the output of . Similarly, and serve as filters to discard tuples from the output of that do not appear in the final output.
The relations and filter out tuples from the outputs of and , respectively, that do not join with any tuple from the output of . The relation is the calibrated output of , i.e., it is the projection of the output of onto the variables and . The relations and are the calibrated outputs of and , respectively. We obtain the final output of by joining , , and .
4.2 Time Analysis
We sketch the proof of the time complexity in Proposition 16. The full proof is given in the extended technical report [2]. Since the query size is considered fixed, a free-top join tree for can be constructed in constant time. The relations occurring in the query in Line 4 of the procedure EvalFreeConnex are the calibrated outputs of RPQs that appear in a connected subtree of the join tree for . This means that the query forms a full acyclic conjunctive query. Hence, its output can be computed in time using the Yannakakis algorithm [30]. Since the relations are calibrated, the time complexity simplifies to . It remains to show that all relations constructed by the subprocedures BottomUp and TopDown can be computed in time. To show this, we use the following lemma:
Lemma 18.
Consider an edge-labeled graph and the query for some , where is an RPQ and each , , and is a materialized relation. The query can be evaluated in data complexity if , and in data complexity if , where OUT is the output size of the query.
Since the query is acyclic, it cannot contain two atoms with the same set of variables. Hence, the definition of each relation computed in the procedure BottomUp is of the form for some relations , , and and , where . By Lemma 18, the relation can be computed in time .
The definition of the relations computed in the procedures BottomUp and TopDown is of the same form, except that the set of variables is not necessarily a strict subset of . By Lemma 18, each relation can be computed in time. Since is the calibrated output of , we can write the complexity as .
Each relation computed in TopDown is a projection of the relation and can thus be computed by a single pass over . Since is computed in time, its size is upper-bounded by the same asymptotic complexity. Hence, each relation can be computed in time.
5 Evaluation of General Acyclic CRPQs
We present our evaluation strategy for general acyclic CRPQs, which follows free-connex tree decompositions. Given an acyclic CRPQ and a free-connex tree decomposition for , we first create CRPQs induced by the bags of the decomposition. Then, we evaluate each induced query as follows. We begin by eliminating bound variables following an elimination order for (see Section 3.1) while preserving the query output. Then, we promote the remaining bound variables of to free variables, which turns into a free-connex acyclic query . Afterwards, we evaluate using our strategy for free-connex acyclic queries (see Section 4) and project the output onto the free variables of . Finally, we obtain the output of by joining the outputs of all induced queries.
In Section 5.1, we explain how to remove bound variables in a CRPQ and adapt the input graph without changing the output of the query. In Section 5.2, we detail our overall evaluation strategy for acyclic CRPQs.
5.1 Elimination of Bound Variables in CRPQs
Given a CRPQ and an input graph, we explain how to eliminate bound variables following an elimination order and adapt the input graph in linear time while preserving the output of the query. We illustrate our strategy by an example.
Example 19.
Consider again the CRPQ from Example 11. The query graph of and the graphs obtained after the elimination of each of the variables in the elimination order are depicted in Figure 1. Let be the input graph. We start with constructing the symmetric closure of . The graph allows us to evaluate RPQs while traversing the edges of the original input graph in opposite direction. Next, we explain how at each elimination step, the query and the graph are adjusted.
At the elimination step for the variable , we replace the atoms and by the atom ; here, is obtained from by replacing every symbol with and is the regular expression that defines the reverse language of . Observe that the input graph contains three vertices such that there is a path labeled by from to and a path labeled by from to if and only if has two vertices and connected by a path labeled by .
At the elimination step for the variable , we remove the atom from the query. We also compute a filter relation defined by that consists of all vertices in the input graph from which there is a path labeled by a string from .
At the elimination step for the variable , we remove the atom from the query. Then, we compute a filter relation defined by and add to each vertex in the input graph, a self-loop edge using a fresh symbol . Finally, we add to the query the atom for some fresh variable .
Let the query and input graph obtained after the above transformations be denoted as and , respectively. The query is of the form . The contracted query graph of (rightmost graph in Figure 1) is the query graph of after skipping the atom . The output of over is the same as the output of over .
| Contract (acyclic CRPQ , edge-labeled graph ) |
| 1let ; let |
| 2let be an elimination order for and |
| 3foreach let // is a unary filter relation |
| 4foreach |
| 5if is a degree-1 variable in |
| 6let such that and some variable are contained in |
| 7let = the query after removing the atom |
| 8let ; update to |
| 9if is a degree-2 variable in |
| wlog, assume that for distinct variables and |
| 10let = the query after replacing atoms and with |
| 11let be the symmetric closure of |
| 12let where |
| and |
| 13let = the query after adding the atom with new variable , |
| 14return |
Given an acyclic CRPQ and an input graph , the procedure Contract in Figure 6 removes bound variables in following an elimination order for the query and adjusts the input graph such that the query output is preserved. The procedure starts with creating a collection of unary filter relations with (Line 3). The purpose of such a relation is to restrict the domain of the variable . At the beginning, there are no restrictions on the domains, so each filter relation consists of the whole vertex set of the input graph. We define and , where is the query graph of . Let be the sequence of graphs obtained after each elimination step following the elimination order . For each in , the procedure eliminates the variable from the query and obtains a query as follows.
Consider the case that is a degree-1 variable in (Lines 5–8). Assume that is the only atom in such that and some other variable are contained in . The procedure removes the atom from the query and updates the filter relation by restricting the domain of to all vertices in the input graph from which there is a path labeled by a string from to a vertex in the domain of .
Consider now the case that is a degree-2 variable in (Lines 9–10). The pseudocode in Figure 6 treats, without loss of generality, the case that the two atoms containing are of the form and . The procedure replaces the two atoms with the single atom , where: is a fresh alphabet symbol; results from by replacing every symbol with ; and is the regular expression that defines the reverse language of . As it will be clear when we extend the input graph in Line 12, the symbol ensures that the path satisfying ends up at a vertex that is contained in the domain of variable . The regular expression makes sure that the graph contains a path from to that satisfies . The two cases not treated in the pseudocode are handled analogously: in case the atom pair containing is of the form or , it is replaced by the atom or the atom , respectively.
When no more bound variables can be eliminated, the procedure adjusts the input graph and the query (obtained from after eliminating the variables in ) as follows (Lines 11–13). It constructs the symmetric closure of and, for each filter relation , performs the following steps: (1) It adds a self-loop edge labeled by the symbol to every vertex in that belongs to ; (2) it adds the atom to the query , where is a fresh variable. By construction, this ensures that every vertex in the input graph that is mapped to variable is contained in the filter relation .
The initialization of the filter relations in Line 3 and their update in Line 8 can be done in time (by Lemma 18). The symmetric closure can be constructed by a single pass over the edge relation of . For each filter relation , adding to the symmetric closure self-loops of the form for each takes also time. Hence:
Proposition 20.
Given an acyclic CRPQ and an edge-labeled graph , the procedure Contract creates in data complexity a CRPQ and an edge-labeled graph such that the output of over is the same as the output of over .
5.2 Evaluation via Decomposition
Our overall evaluation strategy for acyclic CRPQs decomposes a given CRPQ using a free-connex tree decomposition, computes the outputs of the queries induced by the bags and joins these outputs to obtain the output of the original CRPQ. We start with an example.
Example 21.
We continue with the CRPQ from Example 19. Figure 2 gives on the left a free-connex tree decomposition for with the bags , , , and . For each , we have an induced query whose body is the same as the body of and whose set of free variables is the intersection of with the free variables of . For instance, the free variables of are . The procedure Contract in Figure 6 converts into a CRPQ by eliminating all bound variables up to variable and creating some fresh non-join variables. We promote the bound variable to free and obtain the the query with free variables . Since its bound variables are non-join variables, the query is free-connex. We use the procedure EvalFreeConnex in Figure 4 to evaluate . After projecting the output of onto the variables , we obtain the output of . Similarly, we compute the outputs of the queries –. Finally, we obtain the output of by joining the outputs of –.
| EvalAcyclic (acyclic CRPQ , free-connex tree decomposition , edge-labeled graph ) |
| 1let be the CRPQs induced by the bags of |
| 2foreach |
| 3let |
| 4let = the query obtained by promoting the vars in to free in |
| 5let |
| 6let // We project away |
| 7return the output of |
Given an acyclic CRPQ , a free-connex tree decomposition for , and an input graph , the procedure EvalAcyclic in Figure 7 evaluates following the structure of the decomposition. The procedure first constructs the queries induced by the bags of . For each induced query , it performs the following steps. It executes the procedure Contract from Figure 6 on the pair and obtains a transformed pair . It constructs the query by turning the bound variables in that are also contained in into free variables. Then, it computes the output of the free-connex query on the edge-labeled graph using the procedure EvalFreeConnex from Figure 4. Then, it obtains from by projecting it onto the free variables of . Finally, it computes the output of by joining the relations .
Proposition 22.
Given an acyclic CRPQ , a free-connex tree decomposition for , and an edge-labeled graph , the procedure EvalAcyclic evaluates in data complexity, where OUT is the size of the output of and is the contraction width of .
6 Conclusion
In this paper, we introduced an output-sensitive algorithm for evaluating acyclic CRPQs. For free-connex acyclic CRPQs, it is a natural extension of the seminal Yannakakis algorithm [30] where the query atoms now correspond to non-materialized binary relations defined by RPQs.
Our result extends to acyclic conjunctive two-way regular path queries (C2RPQs) [13]. Given an alphabet , a C2RPQ may use the symbol for any to express that a -labeled edge in the input graph is traversed in the opposite direction. Recall from Section 2 that the symmetric closure of an edge-labeled graph contains, for each edge in labeled by some symbol , a corresponding edge in the opposite direction labeled by . To evaluate a C2RPQ on an edge-labeled graph , we use the symmetric closure and adapt the procedure Contract in Figure 6 as follows: for each RPQ , the expression is obtained from by replacing every symbol with and every symbol with .
CRPQ minimization [14] is complementary to our algorithm and can benefit it. When applied to a CRPQ, the minimization yields an equivalent CRPQ whose contraction width can be smaller than that of the original CRPQ.
There are two immediate questions that naturally follow from our paper.
-
(1)
Can the complexity of the output-sensitive evaluation of CRPQs match the complexity of the best-known approach for the output-sensitive evaluation of acyclic conjunctive queries [19], dubbed the output-sensitive Yannakakis in Section 1? Very recent work [1] shows that this is indeed possible. In particular, the improved complexity from [1] for acyclic CRPQs matches the complexity of acyclic conjunctive queries, despite the widely-held belief in the literature that the former ought to be harder than the latter.
-
(2)
Is there an output-sensitive algorithm for cyclic CRPQs? It is already open whether the recent output-sensitive algorithm for acyclic conjunctive queries [19] can be extended to cyclic conjunctive queries. Although there is an output-sensitive algorithm for listing triangles [6], it remains open whether it can be extended to arbitrary cyclic queries.
References
- [1] Mahmoud Abo Khamis, Alexandru-Mihai Hurjui, Ahmet Kara, Dan Olteanu, and Dan Suciu. Acyclic Conjunctive Regular Path Queries are no Harder than Corresponding Conjunctive Queries. arXiv, 2025. URL: https://arxiv.org/abs/2512.11129.
- [2] Mahmoud Abo Khamis, Alexandru-Mihai Hurjui, Ahmet Kara, Dan Olteanu, Dan Suciu, and Zilu Tian. Output-Sensitive Evaluation of Acyclic Conjunctive Regular Path Queries. arXiv, 2025. doi:10.48550/arXiv.2509.20204.
- [3] Mahmoud Abo Khamis, Hung Q. Ngo, and Dan Suciu. PANDA: Query Evaluation in Submodular Width. TheoretiCS, Volume 4, April 2025. doi:10.46298/theoretics.25.12.
- [4] Renzo Angles, Marcelo Arenas, Pablo Barceló, Aidan Hogan, Juan L. Reutter, and Domagoj Vrgoc. Foundations of Modern Query Languages for Graph Databases. ACM Comput. Surv., 50(5):68:1–68:40, 2017. doi:10.1145/3104031.
- [5] Pablo Barceló Baeza. Querying Graph Databases. In PODS, pages 175–188, 2013. doi:10.1145/2463664.2465216.
- [6] Andreas Björklund, Rasmus Pagh, Virginia Vassilevska Williams, and Uri Zwick. Listing Triangles. In ICALP, volume 8572, pages 223–234, 2014. doi:10.1007/978-3-662-43948-7_19.
- [7] Johann Brault-Baron. De la pertinence de l’énumération: complexité en logiques propositionnelle et du premier ordre. PhD thesis, Université de Caen, 2013.
- [8] Johann Brault-Baron. Hypergraph Acyclicity Revisited. ACM Comput. Surv., 49(3):54:1–54:26, 2016. doi:10.1145/2983573.
- [9] Diego Calvanese, Giuseppe De Giacomo, Maurizio Lenzerini, and Moshe Y. Vardi. Reasoning on Regular Path Queries. SIGMOD Rec., 32(4):83–92, 2003. doi:10.1145/959060.959076.
- [10] Katrin Casel and Markus L. Schmid. Fine-Grained Complexity of Regular Path Queries. LMCS, 19(4), 2023. doi:10.46298/LMCS-19(4:15)2023.
- [11] Tamara Cucumides, Juan L. Reutter, and Domagoj Vrgoc. Size Bounds and Algorithms for Conjunctive Regular Path Queries. In ICDT, pages 13:1–13:17, 2023. doi:10.4230/LIPIcs.ICDT.2023.13.
- [12] Alin Deutsch, Nadime Francis, Alastair Green, and et al. Graph Pattern Matching in GQL and SQL/PGQ. In SIGMOD, pages 2246–2258, 2022. doi:10.1145/3514221.3526057.
- [13] Diego Figueira and Rémi Morvan. Approximation and Semantic Tree-Width of Conjunctive Regular Path Queries. In ICDT, pages 15:1–15:19, 2023. doi:10.4230/LIPIcs.ICDT.2023.15.
- [14] Diego Figueira, Rémi Morvan, and Miguel Romero. Minimizing Conjunctive Regular Path Queries. Proc. ACM Manag. Data, 3(2), 2025. doi:10.1145/3725237.
- [15] Nadime Francis, Amélie Gheerbrant, Paolo Guagliardo, Leonid Libkin, Victor Marsault, Wim Martens, Filip Murlak, Liat Peterfreund, Alexandra Rogova, and Domagoj Vrgoc. A Researcher’s Digest of GQL (Invited Talk). In ICDT, pages 1:1–1:22, 2023. doi:10.4230/LIPIcs.ICDT.2023.1.
- [16] Nadime Francis, Alastair Green, Paolo Guagliardo, Leonid Libkin, Tobias Lindaaker, Victor Marsault, Stefan Plantikow, Mats Rydberg, Petra Selmer, and Andrés Taylor. Cypher: An Evolving Query Language for Property Graphs. In SIGMOD, pages 1433–1445, 2018. doi:10.1145/3183713.3190657.
- [17] Steve Harris and Andy Seaborne. SPARQL 1.1 Query Language. W3C Recommendation. http://www.w3.org/TR/sparql11-query/, 2013.
- [18] John E. Hopcroft, Rajeev Motwani, and Jeffrey D. Ullman. Introduction to Automata Theory, Languages, and Computation, 3rd Ed. Pearson intl ed. Addison-Wesley, 2007.
- [19] Xiao Hu. Output-Optimal Algorithms for Join-Aggregate Queries. Proc. ACM Manag. Data, 3(2):104:1–104:27, 2025. doi:10.1145/3725241.
- [20] Mahmoud Abo Khamis, Ahmet Kara, Dan Olteanu, and Dan Suciu. Output-Sensitive Evaluation of Regular Path Queries. Proc. ACM Manag. Data, 3(2):105:1–105:20, 2025. doi:10.1145/3725242.
- [21] Mahmoud Abo Khamis, Hung Q. Ngo, and Atri Rudra. FAQ: Questions Asked Frequently. In PODS, pages 13–28, 2016. doi:10.1145/2902251.2902280.
- [22] Wim Martens and Tina Trautner. Evaluation and Enumeration Problems for Regular Path Queries. In ICDT, pages 19:1–19:21, 2018. doi:10.4230/LIPIcs.ICDT.2018.19.
- [23] Alberto O. Mendelzon and Peter T. Wood. Finding Regular Simple Paths in Graph Databases. SIAM J. Comput., 24(6):1235–1258, 1995. doi:10.1137/S009753979122370X.
- [24] Hung Q. Ngo, Ely Porat, Christopher Ré, and Atri Rudra. Worst-Case Optimal Join Algorithms. J. ACM, 65(3):16:1–16:40, 2018. doi:10.1145/3180143.
- [25] Dan Olteanu and Jakub Závodný. Size Bounds for Factorised Representations of Query Results. ACM Trans. Database Syst., 40(1):2:1–2:44, 2015. doi:10.1145/2656335.
- [26] Property Graph Query Language. Pgql 1.4 specification. https://pgql-lang.org/spec/1.4/, 2021.
- [27] TigerGraph Team. TigerGraph Documentation – Version 3.1. https://docs.tigergraph.com/, 2021.
- [28] Todd L. Veldhuizen. Triejoin: A Simple, Worst-Case Optimal Join Algorithm. In ICDT, pages 96–106, 2014. doi:10.5441/002/ICDT.2014.13.
- [29] Peter T. Wood. Query Languages for Graph Databases. SIGMOD Rec., 41(1):50–60, 2012. doi:10.1145/2206869.2206879.
- [30] Mihalis Yannakakis. Algorithms for Acyclic Database Schemes. In VLDB, pages 82–94, 1981.
Appendix A Comparison with Related Work – Examples
In the following, we compare the performance of our algorithm with the performance of OSPG+OSYan via illustrative examples. The following table restates the evaluation times achieved by OSPG+OSYan and our algorithm for free-connex acyclic (first row) and general acyclic (second row) CRPQs. We recall the parameters appearing in the complexity expressions: and are the vertex and edge sets of the input graph, respectively; OUT is the output size of the query; and are the maximal sizes of the uncalibrated and calibrated outputs, respectively, of any RPQ appearing in the query.
| CRPQs | OSPG+OSYan | Our algorithm | ||||
|
||||||
|
|
We start the comparison with a free-connex acyclic query. On this type of queries, our algorithm performs at least as good as OSPG+OSYan and outperforms OSPG+OSYan when is asymptotically larger than .
Example 23.
Consider the free-connex acyclic CRPQ , which asks for all triples of vertices in the input graph such that there is a path from to labeled by and a path from to labeled by . The free-connex fractional hypertree width of is , while its contraction width is 0.
We evaluate over the edge-labeled graph , where , , , and , for some . The graph is depicted in Figure 8 (left).
We have and . The output of each of the RPQs and is , so of size . The output of and therefore the calibrated outputs of the RPQs are empty. Hence, and . Plugging these quantities into the complexity expressions of the algorithms, we observe that OSPG+OSYan requires time, whereas our approach requires only time.
The next example illustrates a case where our approach outperforms OSPG+OSYan on a CRPQ that is not free-connex acyclic but has contraction width 0.
Example 24.
Consider the 2-path CRPQ which asks for all pairs of vertices in the input graph such that there are a vertex , a path from to labeled with and an edge from to labeled with . This query is acyclic, but not free-connex. It has free-connex fractional hypertree width 2 and contraction width 0.
Consider the edge-labeled graph , where , , , , and for some . This graph is visualized in Figure 8 (right).
We have , , and . The output of consists of the tuples , hence . Our algorithm needs time to evaluate this query on the input graph, whereas OSPG+OSYan requires time.
The next example illustrates a case where our algorithm outperforms OSPG+OSYan on a query whose contraction width is greater than 0.
Example 25.
Consider the 3-star CRPQ , which asks for all triples of vertices such that there is a vertex , a path from to labeled by , an edge from to labeled by , and an edge from to labeled by . The query is acyclic but not free-connex. It has free-connex fractional hypertree width and contraction width .
Consider the edge-labeled graph , where , , , and for some . The graph is visualized in Figure 9 (left).
We observe that , , and . The output of consists of the single tuple , hence . These quantities imply that OSPG+OSYan needs , while our algorithm needs only time.
Next, we illustrate a CRPQ and an input graph where OSPG+OSYan outperforms our algorithm. This CRPQ degenerates to a conjunctive query. Given the size of the outputs of the RPQs, the final output of the CRPQ achieves asymptotically its worst-case size.
Example 26.
Consider the following simplified variant of the 3-star CRPQ from Example 25: . The query asks for all vertex triples such that there is a vertex , an edge from to labeled by , an edge from to labeled by , and an edge from to labeled by . Just as in Example 25, the query has free-connex fractional hypertree width and contraction width .
We evaluate the query over the edge-labeled graph , where , , , and for some . The graph is shown in Figure 9 (right).
We have and . The outputs of the RPQs , , and are , , and , respectively. The output of is . Hence, and . We observe that OSPG+OSYan runs in time, while our algorithms requires time.
Appendix B Proof of Proposition 22
Proposition 22. [Restated, see original statement.]
Given an acyclic CRPQ , a free-connex tree decomposition for , and an edge-labeled graph , the procedure EvalAcyclic evaluates in data complexity, where OUT is the size of the output of and is the contraction width of .
Consider an acyclic CRPQ , a free-connex tree decomposition for , and an edge-labeled graph . We first show that the procedure EvalAcyclic produces the correct output of over . Since the body of each induced query is the same as the body of and the union of the free variables of the induced queries is the set , the join of the outputs of the induced queries is exactly the output of . All bound variables in each query constructed in Line 4 are non-join variables, which implies that is free-connex. Hence, EvalFreeConnex produces the correct output of over . By construction, each is the output of over . Hence, the output of in Line 7 is the output of .
Now, we show that the procedure EvalAcyclic runs within the time bound stated in the proposition. For , we denote by , , and the output sizes of the queries , , and , respectively. The execution of Contract on a pair in Line 3 takes time (Proposition 20). The procedure EvalFreeConnex called in Line 5 runs in time, where is the maximal size of the calibrated output of any RPQ occurring in (Proposition 16). Let be the number of variables in that are promoted from bound to free in Line 4. It holds and . Since the output of is the projection of the output of onto the free variables of , it also holds . The contraction width of is the maximal number of promoted variables over all bags of . Hence, we can write the time complexity of EvalFreeConnex in Line 5 as . The size of the output of EvalFreeConnex is bounded by the same complexity expression. The relation in Line 6 can be computed by a single pass over the relation , which takes time linear in the size of . The query in Line 7 constitutes a full acyclic conjunctive query. Hence, we can use the Yannakakis algorithm [30] to compute the output of in time , which simplifies to due to . This implies that the overall time of the procedure EvalAcyclic is .
