Abstract 1 Introduction 2 Preliminaries 3 A Width Measure for Acyclic CRPQs 4 Evaluation of Free-Connex Acyclic CRPQs 5 Evaluation of General Acyclic CRPQs 6 Conclusion References Appendix A Comparison with Related Work – Examples Appendix B Proof of Proposition 22

Output-Sensitive Evaluation of Acyclic Conjunctive Regular Path Queries

Mahmoud Abo Khamis ORCID RelationalAI, Berkeley, CA, USA Alexandru-Mihai Hurjui ORCID University of Zurich, Switzerland Ahmet Kara ORCID OTH Regensburg, Germany Dan Olteanu ORCID University of Zurich, Switzerland Dan Suciu ORCID University of Washington, Seattle, WA, USA Zilu Tian ORCID University of Zurich, Switzerland
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 algorithms
Copyright and License:
[Uncaptioned image] © Mahmoud Abo Khamis, Alexandru-Mihai Hurjui, Ahmet Kara, Dan Olteanu, Dan Suciu, and
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 analysis
Related Version:
Extended technical report: https://arxiv.org/abs/2509.20204 [2]
Funding:
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 Funk

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 G=(V,E,Σ) be a directed graph, where V is the set of vertices and E is the set of edges labeled with symbols from an alphabet Σ. A regular path query, or RPQ for short, r is a regular expression over Σ. Its semantics is given by the set of vertex pairs (v,u) such that u can be reached in G from v via a path labeled with a string from the language defined by r. A conjunctive regular path query, or CRPQ for short, Q is a conjunction of binary atoms, with each atom defined by a regular path query: Q(𝑭)=i[n]ri(𝑿i), where each ri is an RPQ, each 𝑿i is a pair of variables, and 𝑭i[n]𝑿i is the set of free variables.

Our main result is stated by the following theorem:

Theorem 1.

Any acyclic CRPQ Q over an edge-labeled graph G=(V,E,Σ) can be evaluated in 𝒪(|E|+|E|OUT1/2|V|𝖼𝗐/2+OUT|V|𝖼𝗐) data complexity, where OUT is the size of the output of Q and 𝖼𝗐 is the contraction width of Q.

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 Q into a free-connex CRPQ Q such that the output of Q can be obtained from the output of Q. 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 Q. 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 |V| for each promoted bound variable. The minimum necessary number of promoted bound variables gives the contraction width 𝖼𝗐 of Q, which is a novel parameter specific to acyclic CRPQs. The size of the output of Q can thus be at most OUT|V|𝖼𝗐, where OUT is the size of the output of Q.

In the second stage, our algorithm evaluates the free-connex acyclic CRPQ Q. For this, it first computes the calibrated outputs of the RPQs in Q in time proportional to their sizes. For an RPQ atom r(X,Y) in Q, where S{X,Y} is the set of free variables of Q that occur in this atom, its calibrated output is the projection of the output of Q onto S. This calibration ensures that the time to compute the individual RPQs does not exceed the size of the output of Q. 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 Q. 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 Q. The output of Q is then computed by projecting away the columns of the promoted bound variables from the output of Q.

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 𝖿𝖼-𝖿𝗁𝗍𝗐=1 and contraction width 𝖼𝗐=0. The k-path CRPQs, where the join variables are bound and the other variables are free, have 𝖿𝖼-𝖿𝗁𝗍𝗐=2 and 𝖼𝗐=0. The star CRPQs, where the join variable is bound and all other variables are free, have unbounded free-connex fractional hypertree width and 𝖼𝗐=1.

Of particular interest are CRPQs with contraction width 0, since our algorithm evaluates them with the lowest complexity among all CRPQs: 𝒪(|E|+|E|OUT1/2+OUT). 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 k-path CRPQs with k2 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 OUTc of the calibrated output of any RPQ in the query. Note that by construction, OUTcOUT. Also, since all RPQ atoms are binary, OUTc|V|2.

Proposition 3.
  • For any free-connex acyclic CRPQ Q, it holds 𝖼𝗐(Q)=0.

  • Any free-connex acyclic CRPQ Q over an edge-labeled graph G=(V,E,Σ) can be evaluated in 𝒪(|E|+|E|OUTc1/2+OUT) 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 𝒪(|V||E|) 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 𝒪(|V|ω), where ω is the matrix multiplication exponent [10] and currently ω=2.37. algorithm to compute RPQs with 𝒪((|V||E|)1ϵ) data complexity333The lower bound in [10] has |V|+|E| instead of |E|, but the proof also holds for the formulation that just uses |E|. for any ϵ>0, unless the combinatorial Boolean Matrix Multiplication conjecture fails [10].444The combinatorial Boolean Matrix Multiplication conjecture is as follows: Given two n×n Boolean matrices A,B, there is no combinatorial algorithm that multiplies A and B in 𝒪(n3ϵ) for any ϵ>0. 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 𝒪(|E|+|E|OUT1/2) data complexity, where OUT is the output size of the RPQ [20].555The data complexity of OSPG is 𝒪(|E|Δ+min(OUT/Δ,|V|)|E|) [20]. The user-defined threshold Δ was set to |E|1/2 in [20], yielding 𝒪(|E|3/2+min(OUT|E|1/2,|V||E|)). For Δ=OUT1/2, we obtain instead 𝒪(|E|OUT1/2+|E|), where the additive term |E| is necessary to scan the input data even when for OUT=0. The complexity of OSPG is at most the complexity of PG, since OUT|V|2.

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 Q. This stage turns the CRPQ into an equivalent conjunctive query Q over materialized binary relations. The algorithm then evaluates Q 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 𝒪(|E|+|E|OUTa1/2) data complexity, where OUTa|V|2 is the maximum size of the non-calibrated output of the RPQs.666OUTa is not to be confused with OUTc, which is the maximum size of the calibrated output of any RPQ in Q. It holds that OUTcOUTa and there can be instances for which OUTc=0 yet OUTa=|V|2. Then, Q can be evaluated using the classical Yannakakis algorithm, called here Yan, [30] with 𝒪(OUTaOUT) data complexity, or using its output-sensitive refinement [19], called here OSYan, in 𝒪(OUTaOUT11/𝖿𝖼-𝖿𝗁𝗍𝗐(Q)+OUT) data complexity, where 𝖿𝖼-𝖿𝗁𝗍𝗐(Q) is the free-connex fractional hypertree width of Q and OUT is the output of Q (and also of Q). Overall, the best complexity for evaluating acyclic CRPQs using prior work can therefore be achieved by combining OSPG and OSYan: 𝒪(|E|+|E|OUTa1/2+OUTaOUT11/𝖿𝖼-𝖿𝗁𝗍𝗐(Q)+OUT). 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 Q consists of all quadratically many pairs of vertices in the data graph, i.e., OUTa=𝒪(|V|2), while the query output is empty, i.e., OUT=0. In that case, our algorithm takes time 𝒪(|E|), whereas OSPG+OSYan takes 𝒪(|E||V|).

For free-connex acyclic CRPQs, which have free-connex fractional hypertree width 1, OSPG+OSYan needs 𝒪(|E|+|E|OUTa1/2+OUTa+OUT), which simplifies to 𝒪(|E|+|E|OUTa1/2+OUT), since the time 𝒪(|E|OUTa1/2) to compute the RPQs cannot be smaller than the maximum output size of the RPQs. In contrast, our algorithm needs 𝒪(|E|+|E|OUTc1/2+OUT) (Proposition 16). Since OUTcOUTa, this implies that for free-connex acyclic queries, our algorithm performs at least as good as OSPG+OSYan and outperforms OSPG+OSYan in case OUTa is larger than OUTc. 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 k-path CRPQ Q1(X1,Xk+1)=r1(X1,X2),,rk(Xk,Xk+1), where r1,,rk are RPQs. The contraction width of Q1 is 𝖼𝗐=0, since we can compose the k RPQs into a single RPQ r and rewrite Q1 into the free-connex query Q1(X1,Xk+1)=r(X1,Xk+1). The free-connex fractional hypertree width of Q1 is 𝖿𝖼-𝖿𝗁𝗍𝗐=2. Thus, Q1 can be computed in time 𝒪(|E|+|E|OUT1/2+OUT) using our algorithm (Theorem 1) and in time 𝒪(|E|+|E|OUTa1/2+OUTaOUT1/2+OUT) using OSPG+OSYan. Depending on the scale of OUTa, one algorithm is asymptotically faster than the other. Our algorithm outperforms OSPG+OSYan when OUTa=ω(|E|) or OUTa=ω(OUT), i.e., when the RPQ outputs are asymptotically larger than |E| or the query output size OUT.  

Example 5.

Consider the k-star CRPQ Q2(X1,,Xk)=r1(Y,X1),,rk(Y,Xk), where r1,,rk are RPQs for k>2. The contraction width of Q2 is 𝖼𝗐=1 and the free-connex fractional hypertree width is 𝖿𝖼-𝖿𝗁𝗍𝗐=k. Thus, Q2 can be computed in time 𝒪(|E|+|E|OUT1/2|V|1/2+OUT|V|) using our algorithm (Theorem 1) and in time 𝒪(|E|+|E|OUTa1/2+OUTaOUT11/k+OUT) 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 OUTa 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 OUTa=𝒪(|V|2) and OUT=o(1), our algorithm is faster by an 𝒪(|V|1/2) factor. Our algorithm achieves the stated complexity as follows. It first promotes the bound variable Y to free, so that the obtained query Q2 becomes free-connex. Then it evaluates Q2 with the same complexity as computing RPQs using OSPG, i.e., in time 𝒪(|E|+|E|OUT1/2). The output of Q2 can be a multiplicative factor |V| larger than the output of Q2. The complexity thus has OUT|V| 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 n, we define [n]={1,2,,n}. In case n=0, we have [n]=.

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 L(r) the language defined by a regular expression r. A language is regular if it can be defined by a regular expression.

The reverse rev(w) of a string wΣ is defined recursively as follows: rev(ϵ)=ϵ and rev(w)=σrev(v) if w=vσ for some vΣ and σΣ. The reverse of a language L is LR={rev(w)wL}. For every regular expression r, we can construct in 𝒪(|r|) time a regular expression rR that defines the reverse of L(r) [18].

Edge-Labeled Graphs

An edge-labeled graph is a directed graph G=(V,E,Σ), where V is a set of vertices, Σ is an alphabet, and EV×Σ×V is a set of labeled edges. A triple (u,σ,v)E denotes an edge from vertex u to vertex v 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 p in G from vertex u to vertex v consists of a sequence u=w0,w1,wk=v of vertices from V and a sequence σ1,,σk of symbols from Σ for some k0 such that for every i[k], there is an edge (wi1,σi,wi)E. We say that the string σ1σ2σk is the label of the path p.

The symmetric closure G^ of G contains for each edge in G labeled by some symbol σ, an additional edge in opposite direction labeled by a fresh symbol σ^. That is, G^=(V,E^,Σ^), where Σ^=Σ{σ^σΣ} and E^=E{(v,σ^,u)(u,σ,v)E}.

Conjunctive Regular Path Queries

A regular path query (RPQ) over an alphabet Σ is a regular expression using Σ. The output of an RPQ r evaluated over an edge-labeled graph G=(V,E,Σ) is the set of all pairs (u,v) of vertices such that G contains a path from u to v labeled by a string from L(r).

A conjunctive regular path query (CRPQ), or query for short, is of the form

Q(𝑭)=r1(X1,Y1)rn(Xn,Yn), (1)

where: each ri is an RPQ; each ri(Xi,Yi) is an atom; vars(Q)={Xi,Yii[n]} is the set of variables; free(Q)=𝑭vars(Q) is the set of free variables; bound(Q)=vars(Q)free(Q) is the set of bound variables; atoms(Q) 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 Q is an undirected multigraph GQ=(V,E), where V is the variable set of Q and E is a multiset that contains an undirected edge {Xi,Yi} for each atom ri(Xi,Yi), i.e., E={{{Xi,Yi}ri(Xi,Yi)atoms(Q)}}. Hence, an atom ri(Xi,Yi) with Xi=Yi is represented by the singleton set {Xi} 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 u1,e1,u2,e2,,ek1,uk of vertices u1,,uk and edges e1,,ek1 for k2 such that (1) uiuj for i,j[k1] and ij, (2) eem for ,m[k1] and m, and (3) u1=uk.

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 r(X,X)) nor two distinct atoms that involve the same pair of variables (e.g., two atoms of the form r1(X,Y) and r2(Y,X)):

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 {u}) 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 r(X,X) is already as hard to evaluate as the cyclic conjunctive query Q(X1)=Ra(X1,X2)Ra(X2,X3)Ra(Xk,X1), for any k3, where the materialized relation Ra consists of all vertex pairs in the input graph that are connected by an edge labeled by the symbol a. Indeed, we can choose r to be the regular expression ak, which consists of the concatenation of k occurrences of a. A similar observation holds for CRPQs containing a subquery of the form r1(X,Y)r2(X,Y) or r1(X,Y)r2(Y,X).

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 T of the query graph contains a connected subtree whose variables are exactly T’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 Q from Eq. (1) over an edge-labeled graph G=(V,E,Σ) using homomorphisms, which are mappings from the query variables to the vertices of G [4]. A mapping μ:free(Q)V is in the output of Q, if it can be extended to a mapping μ:free(Q)bound(Q)V such that G has a path from μ(Xi) to μ(Yi) labeled by a string from L(ri), for every i[n]. For convenience, we represent a mapping μ by the tuple (μ(X))Xfree(Q), assuming a fixed order on the free variables.

The calibrated output of an RPQ ri in Q consists of all tuples in the projection of the output of Q onto the variables Xi and Yi. 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 R(X1,,Xk), where R references a k-ary materialized relation for some k0. They also use queries of the form Q(𝑭)=r(X,Y)i[k]Ri(𝑿i) where r is an RPQ and each Ri 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 Q has the following input and output:

Input An edge-labeled graph G=(V,E,Σ)
Output The set of all tuples in the output of Q over G

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 |V| of vertices and the number |E| of edges of the input graph, the size OUT of the output of the query, and the maximum size OUTc of the calibrated output of any RPQ in the query.

We consider the RAM model of computation and assume that each materialized relation R(𝑿) 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 σ𝒀=𝒚R 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 Q, we take a free-connex tree decomposition of Q 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 Q is the minimal contraction width of any free-connex tree decomposition of Q.

3.1 Elimination Procedure for CRPQ Graphs

Consider a cycle-free undirected graph G=(V,E). The degree of a vertex u in G is the number of its neighbors, i.e., the number of vertices vV with uv and {u,v}E. We define two contraction operations on G that eliminate vertices of degree 1 or 2:

  • Elimination of a degree-2 vertex: A graph G is the result of the elimination of a degree-2 vertex u in G if {u,v1},{u,v2}E for distinct v1,v2V and G=(V{u},E{{u,v1},{u,v2}}{{v1,v2}}). That is, G is obtained from G by removing u and the two edges to its only neighbors v1 and v2 and adding a new edge connecting v1 and v2.

  • Elimination of a degree-1 vertex: A graph G is the result of the elimination of a degree-1 vertex u in G if {u,v}E for some vV and G=(V,E), such that: (1) E=E{{u,v}} and; (2) V=V{u,v} if there is no v with {v,v}E, or V=V{u} otherwise. In other words, G results from G by removing the vertex u, the edge to its only neighbor v, and also v if v has no neighbor other than u.

We refer to the graph G obtained from G after eliminating a vertex u by 𝖾𝗅𝗂𝗆𝗂𝗇𝖺𝗍𝖾(G,u).

Definition 9 (Elimination Order).

Consider an acyclic CRPQ Q and its query graph GQ. An elimination order for Q is a sequence ω=(X1,,Xk) of bound variables such that there is a sequence of undirected graphs (G0,G1,,Gk) with G0=GQ, Gi=𝖾𝗅𝗂𝗆𝗂𝗇𝖺𝗍𝖾(Gi1,Xi) for i[k], and all bound variables in Gk have degree at least 3. We define ω(GQ)=(G1,,Gk) and call Gk a contracted query graph of Q.

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 Q by 𝖼𝗀(Q).

Example 11.

Consider the following CRPQ whose query graph is depicted on the left in Figure 1 with free variables being underlined: Q(A,C,E,G,H)=r1(A,B)r2(B,C)r3(B,D)r4(E,D)r5(A,F)r6(F,G)r7(F,H)r8(F,I)r9(I,J). The degree of the bound variable D is 2, so we eliminate it and obtain the second graph in Figure 1. Next, we eliminate the bound variable J with degree 1, which results in the third graph in Figure 1. Finally, we eliminate the bound variable I with degree 1, resulting in the fourth graph in Figure 1. This graph is the contracted query graph of Q, since the only remaining bound variables B and F have both degree 3.  

Figure 1: First graph: Query graph of the CRPQ from Example 11. The free variables are underlined. Second, third, and fourth graph: The graphs obtained after eliminating the bound variables D, J, and I, respectively.

3.2 Contraction Width

Definition 12 (Tree Decomposition).

A tree decomposition of a CRPQ Q is a pair 𝒯=(T,χ), where T is a tree with vertices V(T) and χ:V(T)2vars(Q) maps each vertex t of T to a subset χ(t) of variables of Q such that the following properties hold:

  • for every r(X,Y)atoms(Q), it holds X,Yχ(t) for some tV(T), and

  • for every Xvars(Q), the set {tXχ(t)} is a non-empty connected subtree of T.

The sets χ(t) are called the bags of the tree decomposition. We denote the set of bags of 𝒯 by bags(𝒯).

The CRPQ Q induced by a bag has the same body as Q and has the free variables free(Q).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 Q(𝑭)=i[n]Ri(𝑿i), the query induced by a bag is defined as Q()=i[n]Ri(𝑿i). 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 free(Q) of free variables. We use FTD(Q) to denote the set of all free-connex tree decompositions of the query Q.

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 𝖼𝗐(Q) of any acyclic CRPQ Q and the contraction width 𝖼𝗐(𝒯) of any tree decomposition 𝒯 of Q are:

𝖼𝗐(Q)=min𝒯FTD(Q)𝖼𝗐(𝒯)and𝖼𝗐(𝒯)=maxbags(𝒯)|bound(𝖼𝗀(Q))|,

where bound(𝖼𝗀(Q)) 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 Q from Example 11 and the trivial tree decomposition for Q with a single bag. The query induced by the single bag is the query Q itself. Hence, the contraction width of this decomposition is 2, as shown in Example 11.

Now, consider the free-connex tree decomposition 𝒯 for Q from Figure 2 (left). The figure next shows the query graph of the induced query Q for the bag ={A,C,E}. The next graph is the query graph obtained after eliminating D. The second graph from the right is obtained after eliminating the variables J, I, H, and G one after the other. We obtain the rightmost graph after eliminating F. This graph is the contracted query graph of Q, since its only bound variable B is of degree 3. Hence, |bound(𝖼𝗀(Q))|=1. Analogously, we can show that the number of bound variables in the contracted graphs of each of the other induced queries is 1. Hence, 𝖼𝗐(𝒯)=1. It can be shown that 𝖼𝗐(Q)=1.  

Figure 2: Left to right: Free-connex tree decomposition for the CRPQ Q from Example 11; query graph of the query Q induced by ={A,C,E}; graph obtained after eliminating D; graph obtained after eliminating J, I, H, and G; contracted query graph obtained after eliminating F.

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 Q(A,B,D)=r1(A,B)r2(B,C)r3(B,D)r4(A,E)r5(E,F) and its free-connex tree decomposition 𝒯 consisting of the two bags 1={A,B,D} and 2={A,B,C,E,F}. The induced query Q1 is the same as Q. Figure 3 depicts on the left the query graph of Q1 and right to it the graphs obtained after the elimination of the bound variables F, C, and E. The final contracted query graph has no bound variables. Similarly, we can show that contracted query graph of Q2 has no bound variables. This implies 𝖼𝗐(𝒯)=𝖼𝗐(Q)=0.  

Figure 3: Left to right: Query graph of the query Q1 in Example 15; graphs obtained after eliminating the variables F, C, and E.

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 Q(𝑭), edge-labeled graph G)
1 let Q(𝑭)=r1(X1,Y1)rn(Xn,Yn)
2 let J be a free-top join tree for Q;   let ri(Xi,Yi) = root of J;   let =
3 BottomUp(ri(Xi,Yi),𝑭,J,,G);   TopDown(ri(Xi,Yi),𝑭,J,,G);
4 assume is {Ri1(𝑿i1),,Rik(𝑿ik)}
5 return the output of Q(𝑭)=Ri1(𝑿i1)Rik(𝑿ik)
BottomUp(atom ri(Xi,Yi), variable set 𝑭,
tree J, relation set , edge-labeled graph G)
1 foreach rc(Xc,Yc)children(ri(Xi,Yi),J)
2 BottomUp(rc(Xc,Yc),𝑭,J,,G)
3 if ri has no parent
4 let Ri({Xi,Yi}𝑭)=ri(Xi,Yi)
rc(Xc,Yc)children(ri(Xi,Yi),J)Bci({Xc,Yc}{Xi,Yi})
5 update to {Ri}
6 else
7 let rp(Xp,Yp)=parent(ri(Xi,Yi),J)
8 let Bip({Xi,Yi}{Xp,Yp})=ri(Xi,Yi)
rc(Xc,Yc) children(ri(Xi,Yi),J)Bci({Xc,Yc}{Xi,Yi})
TopDown(atom ri(Xi,Yi), variable set 𝑭,
tree J, relation set , edge-labeled graph G)
1 if {Xi,Yi}𝑭=
2 return
3 if ri has parent
4 let rp(Xp,Yp)=parent(ri(Xi,Yi),J)
5 let Tpi({Xi,Yi}𝑿)=Rp(𝑿)
6 let Ri({Xi,Yi}𝑭)=ri(Xi,Yi)
Tpi({Xi,Yi}𝑿)
7 update to {Ri}
8 foreach rc(Xc,Yc)children(ri(Xi,Yi),J)
9 TopDown(rc(Xc,Yc),𝑭,J,,G)
Figure 4: Evaluating a free-connex acyclic CRPQ Q(𝑭). We denote by parent(ri(Xi,Yi),J) the parent and by children(ri(Xi,Yi),J) the set of children of atom ri(Xi,Yi) in the tree J. The relations of the form Ri, Bij, and Tij are computed over the edge-labeled graph G.

The procedure EvalFreeConnex in Figure 4 describes our evaluation strategy for a free-connex acyclic CRPQ Q and an edge-labeled graph G. It starts with constructing a free-top join tree J for Q, which means that the atoms containing a free variable form a connected subtree in J 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 Ri whose join is the output of Q. 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 ri(Xi,Yi) a relation Bip that joins ri(Xi,Yi) with the relations Bci computed at the children of ri(Xi,Yi) and projects the result onto the common variables of ri(Xi,Yi) and its parent atom (Lines 7-8 of BottomUp). If ri(Xi,Yi) is the root, the procedure computes the relation Ri, defined analogously to Bip except that its set of variables is the intersection of {Xi,Yi} with the free variables of Q (Lines 3–4 of BottomUp). The relation Ri is the calibrated output of ri(Xi,Yi).

The main purpose of the subprocedure TopDown is to materialize at each atom ri(Xi,Yi) that is not the root of the join tree and contains at least one free variable of Q, the calibrated output Ri of the RPQ ri. For this, the procedure computes at each such atom ri(Xi,Yi) with parent rp(Xp,Yp), a relation Tpi that is the projection of the relation Rp, computed at the parent atom, onto the common variables of Rp and ri(Xi,Yi). Then, it materializes the relation Ri by computing the projection of the join of ri(Xi,Yi) and Tpi onto the free variables of Q (Lines 4–6 of TopDown).

After the execution of BottomUp and TopDown, the set ={Ri1,,Rik} 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 Q(𝑭)=j[k]Rij, which is the output of Q (Line 5 of EvalFreeConnex). We can show:

Proposition 16.

Given a free-connex acyclic CRPQ Q and an edge-labeled graph G=(V,E,Σ), the procedure EvalFreeConnex evaluates Q in 𝒪(|E|+|E|OUTc1/2+OUT) data complexity, where OUTc is the maximum size of the calibrated output of any RPQ in Q.

Example 17.
Figure 5: Left: Join tree for the free-connex query from Example 17. Right: The materialized relations computed by our evaluation strategy.

Consider the CRPQ Q(X,Y,U)=r1(X,Y)r2(Y,Z)r3(Z,W)r4(X,U). Figure 5 depicts a free-top join tree for the query, along with the materialized relations computed by our evaluation strategy. The relations B32(Z), B21(Y), B41(X), and R1(X,Y) are computed during the bottom-up pass over the join tree, while the relations T12(Y), T14(X), R2(Y), and R4(X,U) are materialized during the top-down pass. The relation B32 serves as a filter to discard tuples from the output of r2(Y,Z) that do not join with any tuple from the output of r3(Z,W). Similarly, B21 and B41 serve as filters to discard tuples from the output of r1(X,Y) that do not appear in the final output.

The relations T12 and T14 filter out tuples from the outputs of r2(Y,Z) and r4(X,U), respectively, that do not join with any tuple from the output of R1(X,Y). The relation R1(X,Y) is the calibrated output of r1(X,Y), i.e., it is the projection of the output of Q onto the variables X and Y. The relations R2 and R4 are the calibrated outputs of r2(Y,Z) and r4(X,U), respectively. We obtain the final output of Q by joining R1, R2, and R4.  

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 Q can be constructed in constant time. The relations Ri1,,Rik occurring in the query Q in Line 4 of the procedure EvalFreeConnex are the calibrated outputs of RPQs that appear in a connected subtree of the join tree for Q. This means that the query Q forms a full acyclic conjunctive query. Hence, its output can be computed in 𝒪(maxj[k]|Rij|+OUT) time using the Yannakakis algorithm [30]. Since the relations Rij are calibrated, the time complexity simplifies to 𝒪(OUT). It remains to show that all relations constructed by the subprocedures BottomUp and TopDown can be computed in 𝒪(|E|+|E|OUTc1/2) time. To show this, we use the following lemma:

Lemma 18.

Consider an edge-labeled graph G=(V,E,Σ) and the query Q(𝐅)=r(X,Y)i[k]Ui(X) i[]Wi(Y) i[m]Zi() for some k,,m, where r(X,Y) is an RPQ and each Ui, Wi, and Zi is a materialized relation. The query Q can be evaluated in 𝒪(|E|) data complexity if 𝐅{X,Y}, and in 𝒪(|E|+|E|OUT1/2) data complexity if 𝐅={X,Y}, 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 Bip computed in the procedure BottomUp is of the form V(𝑿)=ri(Xi,Yi)j[k]Uj(Xi) j[]Wj(Yi) j[m]Zj() for some relations Uj, Wj, and Zj and k,,m, where 𝑿{Xi,Yi}. By Lemma 18, the relation can be computed in time 𝒪(|E|).

The definition of the relations Ri 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 {Xi,Yi}. By Lemma 18, each relation Ri can be computed in 𝒪(|E|+|E|OUT1/2) time. Since Ri is the calibrated output of ri(Xi,Yi), we can write the complexity as 𝒪(|E|+|E|OUTc1/2).

Each relation Tpi computed in TopDown is a projection of the relation Rp and can thus be computed by a single pass over Rp. Since Rp is computed in 𝒪(|E|+|E|OUTc1/2) time, its size is upper-bounded by the same asymptotic complexity. Hence, each relation Tpi can be computed in 𝒪(|E|+|E|OUTc1/2) 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 Q and a free-connex tree decomposition for Q, we first create CRPQs induced by the bags of the decomposition. Then, we evaluate each induced query Q as follows. We begin by eliminating bound variables following an elimination order for Q (see Section 3.1) while preserving the query output. Then, we promote the remaining bound variables of Q to free variables, which turns Q into a free-connex acyclic query Q′′. Afterwards, we evaluate Q′′ using our strategy for free-connex acyclic queries (see Section 4) and project the output onto the free variables of Q. Finally, we obtain the output of Q 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 Q(A,C,E,G,H)=r1(A,B)r2(B,C)r3(B,D)r4(E,D)r5(A,F)r6(F,G)r7(F,H)r8(F,I),r9(I,J) from Example 11. The query graph of Q and the graphs obtained after the elimination of each of the variables in the elimination order (D,J,I) are depicted in Figure 1. Let G be the input graph. We start with constructing the symmetric closure G^ of G. The graph G^ 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 Q and the graph G^ are adjusted.

At the elimination step for the variable D, we replace the atoms r3(B,D) and r4(E,D) by the atom r3r^4R(B,E); here, r^4 is obtained from r4 by replacing every symbol σ with σ^ and r^4R is the regular expression that defines the reverse language of r^4. Observe that the input graph G contains three vertices u,v,w such that there is a path labeled by r3 from u to v and a path labeled by r4 from w to v if and only if G^ has two vertices u and w connected by a path labeled by r3r^4R.

At the elimination step for the variable J, we remove the atom r9(I,J) from the query. We also compute a filter relation RI defined by RI(I)=r9(I,J) that consists of all vertices in the input graph from which there is a path labeled by a string from L(r9).

At the elimination step for the variable I, we remove the atom r8(F,I) from the query. Then, we compute a filter relation RF defined by RF(F)=r8(F,I)RI(I) and add to each vertex uRF in the input graph, a self-loop edge (u,σF,u) using a fresh symbol σF. Finally, we add to the query the atom σF(F,F) for some fresh variable F.

Let the query and input graph obtained after the above transformations be denoted as Q and G^, respectively. The query Q is of the form Q(A,C,E,G,H)=r1(A,B)r2(B,C)r3r^4R(B,E)r5(A,F)r6(F,G)r7(F,H)σF(F,F). The contracted query graph of Q (rightmost graph in Figure 1) is the query graph of Q after skipping the atom σF(F,F). The output of Q over G is the same as the output of Q over G^.  

Contract (acyclic CRPQ Q, edge-labeled graph G=(V,E,Σ))
1let Q0=Q; let G0=GQ
2let ω=(X1,,Xk) be an elimination order for Q0 and ω(G0)=(G1,,Gk)
3foreach Xvars(Q0) let RX=V    // RX is a unary filter relation
4foreach i[k]
5if Xi is a degree-1 variable in Gi1
6let r(𝑿)atoms(Qi1) such that Xi and some variable Y are contained in 𝑿
7let Qi = the query Qi1 after removing the atom r(𝑿)
8let RY(Y)=RY(Y); update RY(Y) to RY(Y)=r(𝑿)RY(Y)RXi(Xi)
9if Xi is a degree-2 variable in Gi1
wlog, assume that r1(Y,Xi),r2(Z,Xi)atoms(Qi1) for distinct variables Y and Z
10let Qi = the query Qi1 after replacing atoms r1(Y,Xi) and r2(Z,Xi) with r1σXir^2R(Y,Z)
11let G^=(V,E^,Σ^) be the symmetric closure of G
12let G^=(V,E^,Σ^) where Σ^=Σ^{σXσXΣ^ and Xvars(Q)}
and E^=E^{(u,σX,u)Xvars(Q) and uRX}
13let Qk = the query Qk after adding the atom σX(X,X¯) with new variable X¯, Xvars(Qk)
14return (Qk,G^)
Figure 6: Eliminating bound variables in an acyclic CRPQ Q following an elimination order ω as defined in Section 3.1 and adjusting the input graph G. Notation: r^2 is obtained from r2 by replacing every symbol σ with σ^; r^2R is the regular expression that defines the reverse language of r^2.

Given an acyclic CRPQ Q and an input graph G, the procedure Contract in Figure 6 removes bound variables in Q following an elimination order ω=(X1,,Xk) 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 RX with Xvars(Q) (Line 3). The purpose of such a relation RX is to restrict the domain of the variable X. 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 Q0:=Q and G0:=GQ, where GQ is the query graph of Q. Let ω(G0)=(G1,,Gk) be the sequence of graphs obtained after each elimination step following the elimination order ω. For each Xi in ω, the procedure eliminates the variable Xi from the query Qi1 and obtains a query Qi as follows.

Consider the case that Xi is a degree-1 variable in Gi1 (Lines 5–8). Assume that r(𝑿) is the only atom in Qi1 such that Xi and some other variable Y are contained in 𝑿. The procedure removes the atom from the query and updates the filter relation RY by restricting the domain of Y to all vertices in the input graph from which there is a path labeled by a string from L(r) to a vertex in the domain of Xi.

Consider now the case that Xi is a degree-2 variable in Gi1 (Lines 9–10). The pseudocode in Figure 6 treats, without loss of generality, the case that the two atoms containing Xi are of the form r1(Y,Xi) and r2(Z,Xi). The procedure replaces the two atoms with the single atom r1σXir^2R(Y,Z), where: σXi is a fresh alphabet symbol; r^2 results from r2 by replacing every symbol σ with σ^; and r^2R is the regular expression that defines the reverse language of r^2. As it will be clear when we extend the input graph in Line 12, the symbol σXi ensures that the path satisfying r1 ends up at a vertex that is contained in the domain of variable Xi. The regular expression r^2R makes sure that the graph contains a path from Z to Xi that satisfies r2. The two cases not treated in the pseudocode are handled analogously: in case the atom pair containing Xi is of the form (r1(Y,Xi),r2(Xi,Z)) or (r1(Xi,Y),r2(Xi,Z)), it is replaced by the atom r1σXir2(Y,Z) or the atom r^1RσXir2(Y,Z), respectively.

When no more bound variables can be eliminated, the procedure adjusts the input graph G and the query Qk (obtained from Q after eliminating the variables in ω) as follows (Lines 11–13). It constructs the symmetric closure G^ of G and, for each filter relation RX, performs the following steps: (1) It adds a self-loop edge labeled by the symbol σX to every vertex in G^ that belongs to RX; (2) it adds the atom σX(X,X¯) to the query Qk, where X¯ is a fresh variable. By construction, this ensures that every vertex in the input graph that is mapped to variable X is contained in the filter relation RX.

The initialization of the filter relations in Line 3 and their update in Line 8 can be done in time 𝒪(|E|) (by Lemma 18). The symmetric closure G^ can be constructed by a single pass over the edge relation of G. For each filter relation RX, adding to the symmetric closure self-loops of the form (u,σX,u) for each uRX takes also 𝒪(|V|)=𝒪(|E|) time. Hence:

Proposition 20.

Given an acyclic CRPQ Q and an edge-labeled graph G=(V,E,Σ), the procedure Contract creates in 𝒪(|E|) data complexity a CRPQ Q and an edge-labeled graph G such that the output of Q over G is the same as the output of Q over G.

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 Q from Example 19. Figure 2 gives on the left a free-connex tree decomposition for Q with the bags 1={A,C,E}, 2={A,B,C,D,E}, 3={A,G,H}, and 4={A,F,G,H,I}. For each i, we have an induced query Qi whose body is the same as the body of Q and whose set of free variables is the intersection of i with the free variables of Q. For instance, the free variables of Q1 are {A,C,E}. The procedure Contract in Figure 6 converts Q1 into a CRPQ Q1 by eliminating all bound variables up to variable B and creating some fresh non-join variables. We promote the bound variable B to free and obtain the the query Q1′′ with free variables {A,B,C,E}. Since its bound variables are non-join variables, the query Q1′′ is free-connex. We use the procedure EvalFreeConnex in Figure 4 to evaluate Q1′′. After projecting the output of Q1′′ onto the variables {A,C,E}, we obtain the output of Q1. Similarly, we compute the outputs of the queries Q2Q4. Finally, we obtain the output of Q by joining the outputs of Q1Q4.  

EvalAcyclic (acyclic CRPQ Q(𝑭), free-connex tree decomposition 𝒯, edge-labeled graph G)
1let Q1(𝑭1),,Qk(𝑭k) be the CRPQs induced by the bags of 𝒯
2foreach i[k]
3let (Qi,Gi)=Contract(Qi,G)
4let Qi′′ = the query obtained by promoting the vars in bound(Qi)bound(Qi) to free in Qi
5let Ri(𝑿i)=EvalFreeConnex(Qi′′,Gi)
6let Ri(𝑭i)=Ri(𝑿i)      // We project away 𝑿i𝑭i
7return the output of Q(𝑭)=R1(𝑭1)Rk(𝑭k)
Figure 7: Evaluating an acyclic CRPQ Q over an edge-labeled graph G following a free-connex tree decomposition 𝒯 for Q. The procedures Contract and EvalFreeConnex are given in Figures 6 and 4, respectively.

Given an acyclic CRPQ Q, a free-connex tree decomposition 𝒯 for Q, and an input graph G, the procedure EvalAcyclic in Figure 7 evaluates Q following the structure of the decomposition. The procedure first constructs the queries Q1,,Qk induced by the bags of 𝒯. For each induced query Qi, it performs the following steps. It executes the procedure Contract from Figure 6 on the pair (Qi,G) and obtains a transformed pair (Qi,Gi). It constructs the query Qi′′ by turning the bound variables in Qi that are also contained in Qi into free variables. Then, it computes the output Ri of the free-connex query Qi′′ on the edge-labeled graph Gi using the procedure EvalFreeConnex from Figure 4. Then, it obtains Ri from Ri by projecting it onto the free variables of Qi. Finally, it computes the output of Q by joining the relations R1,,Rk.

Proposition 22.

Given an acyclic CRPQ Q, a free-connex tree decomposition 𝒯 for Q, and an edge-labeled graph G=(V,E,Σ), the procedure EvalAcyclic evaluates Q in 𝒪(|E|+|E|OUT1/2|V|𝖼𝗐/2+OUT|V|𝖼𝗐) data complexity, where OUT is the size of the output of Q and 𝖼𝗐 is the contraction width of 𝒯.

The proof of Proposition 22 is given in Appendix B. Using a free-connex tree decomposition 𝒯 for Q with 𝖼𝗐(𝒯)=𝖼𝗐(Q), Proposition 22 immediately implies Theorem 1.

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 G^ of an edge-labeled graph G contains, for each edge in G labeled by some symbol σ, a corresponding edge in the opposite direction labeled by σ^. To evaluate a C2RPQ Q on an edge-labeled graph G, we use the symmetric closure G^ and adapt the procedure Contract in Figure 6 as follows: for each RPQ r, the expression r^ is obtained from r 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. (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. (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: V and E are the vertex and edge sets of the input graph, respectively; OUT is the output size of the query; OUTa and OUTc are the maximal sizes of the uncalibrated and calibrated outputs, respectively, of any RPQ appearing in the query.

CRPQs OSPG+OSYan Our algorithm
free-con.
acyclic
|E|+|E|OUTa1/2+OUT |E|+|E|OUTc1/2+OUT
general
acyclic
|E|+|E|OUTa1/2+OUTaOUT11/𝖿𝖼-𝖿𝗁𝗍𝗐+
OUT
|E|+|E|OUT1/2|V|𝖼𝗐/2+OUT|V|𝖼𝗐

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 OUTa is asymptotically larger than OUTc.

Example 23.

Consider the free-connex acyclic CRPQ Q(X,Y,Z)=aaa(X,Y)bbb(Y,Z), which asks for all triples (u,v,w) of vertices in the input graph such that there is a path from u to v labeled by aaa and a path from v to w labeled by bbb. The free-connex fractional hypertree width 𝖿𝖼-𝖿𝗁𝗍𝗐(Q) of Q is 1, while its contraction width 𝖼𝗐(Q) is 0.

We evaluate Q over the edge-labeled graph G=(V1V2{v},EaEb,{a,b}), where V1={u1,,un}, V2={w1,,wn}, Ea={(ui,a,v),(v,a,wi)i[n]}, and Eb={(ui,b,v),(v,b,wi)i[n]}, for some n. The graph G is depicted in Figure 8 (left).

We have |V|=2n+1 and |E|=4n. The output of each of the RPQs aaa and bbb is {(ui,wj)i,j[n]}, so of size n2. The output of Q and therefore the calibrated outputs of the RPQs are empty. Hence, OUT=OUTc=0 and OUTa=n2. Plugging these quantities into the complexity expressions of the algorithms, we observe that OSPG+OSYan requires 𝒪(n2) time, whereas our approach requires only 𝒪(n) time.  

Figure 8: The edge-labeled graphs from Examples 23 (left) and 24 (right).
Figure 9: The edge-labeled graphs from Examples 25 (left) and 26 (right).

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 Q(X,Z)=aaa(X,Y)b(Y,Z) which asks for all pairs of vertices (u,w) in the input graph such that there are a vertex v, a path from u to v labeled with aaa and an edge from v to w labeled with b. 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 G=(V1V2V3{v,v0},EaEb,{a,b}), where V1={u0,,un}, V2={w1,,wn}, V3={z1,,zn}, Ea={(wi,a,v),(v,a,ui)i[n]}{(u0,a,v0),(v0,a,w1)}, and Eb={(w1,b,zi)i[n]} for some n. This graph is visualized in Figure 8 (right).

We have |V|=3n+3, |E|=3n+2, and OUTa=Θ(n2). The output of Q consists of the tuples {(u0,zi)i[n]}, hence OUT=n. Our algorithm needs 𝒪(n3/2) time to evaluate this query on the input graph, whereas OSPG+OSYan requires 𝒪(n5/2) 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 Q(X1,X2,X3)=aaa(X1,X)b(X2,X)c(X3,X), which asks for all triples (u,v,w) of vertices such that there is a vertex z, a path from u to z labeled by aaa, an edge from v to z labeled by b, and an edge from w to z labeled by c. The query is acyclic but not free-connex. It has free-connex fractional hypertree width 3 and contraction width 1.

Consider the edge-labeled graph G=(V1V2{u0,v0,v,z1,z2},EaEbc,{a,b,c}), where V1={u1,,un}, V2={w1,,wn}, Ea={(wi,a,v),(v,a,ui)i[n]} {(u0,a,v0),(v0,a,w1)}, and Ebc={(z1,b,w1),(z2,c,w1)} for some n. The graph G is visualized in Figure 9 (left).

We observe that |V|=2n+5, |E|=2n+4, and OUTa=Θ(n2). The output of Q consists of the single tuple (u0,z1,z2), hence OUT=1. These quantities imply that OSPG+OSYan needs 𝒪(n2), while our algorithm needs only 𝒪(n3/2) 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: Q(X1,X2,X3)=a(X1,X)b(X2,X)c(X3,X). The query asks for all vertex triples (u,v,w) such that there is a vertex z, an edge from u to z labeled by a, an edge from v to z labeled by b, and an edge from w to z labeled by c. Just as in Example 25, the query has free-connex fractional hypertree width 3 and contraction width 1.

We evaluate the query over the edge-labeled graph G=(V1V2V3{v},E,{a,b,c}), where V1={u1,,un}, V2={w1,,wn}, V3={z1,,zn}, and E = {(ui,a,v), (wi,b,v), (zi,c,v) i[n]} for some n. The graph G is shown in Figure 9 (right).

We have |V|=3n+1 and |E|=3n. The outputs of the RPQs a, b, and c are {(ui,v)i[n]}, {(wi,v)i[n]}, and {(zi,v)i[n]}, respectively. The output of Q is {(ui,wj,zk)i,j,k[n]}. Hence, OUTa=n and OUT=n3. We observe that OSPG+OSYan runs in 𝒪(n3) time, while our algorithms requires 𝒪(n4) time.  

Appendix B Proof of Proposition 22

Proposition 22. [Restated, see original statement.]

Given an acyclic CRPQ Q, a free-connex tree decomposition 𝒯 for Q, and an edge-labeled graph G=(V,E,Σ), the procedure EvalAcyclic evaluates Q in 𝒪(|E|+|E|OUT1/2|V|𝖼𝗐/2+OUT|V|𝖼𝗐) data complexity, where OUT is the size of the output of Q and 𝖼𝗐 is the contraction width of 𝒯.

Consider an acyclic CRPQ Q(𝑭), a free-connex tree decomposition 𝒯 for Q, and an edge-labeled graph G=(V,E,Σ). We first show that the procedure EvalAcyclic produces the correct output of Q over G. Since the body of each induced query Qi is the same as the body of Q 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 Q. All bound variables in each query Qi′′ constructed in Line 4 are non-join variables, which implies that Qi′′ is free-connex. Hence, EvalFreeConnex produces the correct output of Qi′′ over G. By construction, each Ri is the output of Qi over G. Hence, the output of Q in Line 7 is the output of Q.

Now, we show that the procedure EvalAcyclic runs within the time bound stated in the proposition. For i[k], we denote by OUTi, OUTi, and OUTi′′ the output sizes of the queries Qi, Qi, and Qi′′, respectively. The execution of Contract on a pair (Qi,G) in Line 3 takes 𝒪(|E|) time (Proposition 20). The procedure EvalFreeConnex called in Line 5 runs in 𝒪(|E|+|E|OUTc′′1/2+OUT′′) time, where OUTc′′ is the maximal size of the calibrated output of any RPQ occurring in Qi′′ (Proposition 16). Let p be the number of variables in Qi that are promoted from bound to free in Line 4. It holds OUTc′′OUTi′′ and OUTi′′OUTi|V|p=OUTi|V|p. Since the output of Qi is the projection of the output of Q onto the free variables of Qi, it also holds OUTi|V|pOUT|V|p. 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 𝒪(|E|+|E|OUT1/2|V|𝖼𝗐/2+OUT|V|𝖼𝗐). The size of the output Ri of EvalFreeConnex is bounded by the same complexity expression. The relation Ri in Line 6 can be computed by a single pass over the relation Ri, which takes time linear in the size of Ri. The query Q in Line 7 constitutes a full acyclic conjunctive query. Hence, we can use the Yannakakis algorithm [30] to compute the output of Q in time 𝒪(maxi[k]|Ri|+OUT), which simplifies to 𝒪(OUT) due to maxi[k]|Ri|OUT. This implies that the overall time of the procedure EvalAcyclic is 𝒪(|E|+|E|OUT1/2|V|𝖼𝗐/2+OUT|V|𝖼𝗐).