Algorithm Search⌘K

Problemadvanced

Regular path query evaluation

Also called RPQ, regular path query.

Find the pairs of nodes of an edge-labeled graph that are joined by a path whose labels spell a word of a given regular language.

Regular path query evaluation is the problem of finding the pairs of nodes of an edge-labeled graph that are joined by a path whose labels, read in order, spell a word of a given regular language. It is the basic query of graph databases: "all pairs of people joined by a chain of knows edges" is the query knows+\mathit{knows}^{+}. When any path counts, the problem reduces to reachability in the product of the graph and an automaton for the language, so it is solved in time linear in the graph for each source node. When the path must be simple, the problem is NP-complete already for some fixed languages (Mendelzon and Wood 1995).

In plain words

Think of a graph as a map of places joined by roads, and give each road a label, such as "train" or "bus". A regular path query asks: from which place to which place can you travel so that the list of road labels follows a pattern? A pattern could be "any number of train rides, then one bus ride".

To answer it, you walk through the graph and through a small machine that reads the pattern at the same time. Each step uses one road and moves the machine by that road's label. You reach an answer when the machine says the pattern is complete. This is fast. The problem becomes hard when you also ask that the trip never visits the same place twice.

Definition

Input. An edge-labeled graph G=(V,E)G = (V, E) over a finite alphabet Σ\Sigma, with E⊆V×Σ×VE \subseteq V \times \Sigma \times V; and a regular language L⊆Σ∗L \subseteq \Sigma^{*}, given by a regular expression rr or by a nondeterministic finite automaton A=(S,Σ,δ,s0,F)A = (S, \Sigma, \delta, s_0, F) with δ⊆S×(Σ∪{ε})×S\delta \subseteq S \times (\Sigma \cup \{\varepsilon\}) \times S. The size of AA is ∣A∣=∣S∣+∣δ∣|A| = |S| + |\delta|.

A path from uu to vv is a sequence of edges (v0,a1,v1),(v1,a2,v2),…,(vn−1,an,vn)(v_0, a_1, v_1), (v_1, a_2, v_2), \ldots, (v_{n-1}, a_n, v_n) in EE with v0=uv_0 = u and vn=vv_n = v, for some n≥0n \geq 0. Its label is the word a1a2⋯ana_1 a_2 \cdots a_n; the empty path has the empty word as label. The path is simple if v0,…,vnv_0, \ldots, v_n are distinct.

Output. The answer of the query LL on GG is

L(G)={ (u,v)∈V×V∣some path from u to v has its label in L }.L(G) = \{\, (u, v) \in V \times V \mid \text{some path from } u \text{ to } v \text{ has its label in } L \,\}.

Under simple path semantics, only simple paths count. The decision versions ask whether a given pair (u,v)(u, v) is in the answer.

The answer is a binary relation over VV, so a regular path query is a query in the sense of the relational model: the graph is a database with one binary relation EaE_a per label aa, and L(G)L(G) is a relation computed from it. For the query a∗a^{*} the answer is the reflexive and transitive closure of EaE_a, which relational algebra cannot express; so regular path queries are outside the relational calculus but inside Datalog.

Variants

  • Single source, single pair, all pairs. Fix uu and return all vv; fix both and decide; or return the whole relation. The all-pairs answer can have ∣V∣2|V|^2 pairs.
  • Two-way RPQs (2RPQs). The alphabet also has an inverse label a−a^{-} for each aa, read along an aa-edge from its head to its tail. Add the reversed edges to EE and the problem becomes an ordinary RPQ.
  • Conjunctive RPQs (CRPQs). A conjunctive query whose atoms are RPQs, such as ∃z (x→a+z∧y→bz)\exists z\, (x \xrightarrow{a^{+}} z \land y \xrightarrow{b} z). This adds the joins of conjunctive queries, and combined complexity becomes NP-complete, as for conjunctive queries.
  • Path semantics. Arbitrary paths (walks), trails (no repeated edge) and simple paths (no repeated node). The query languages of graph systems choose among these.
  • Returning paths. Return one witness path per pair, a shortest one, or all paths up to a bound. Under arbitrary path semantics there can be infinitely many paths, so systems return a finite selection.

Examples

Take nodes 1,2,3,41, 2, 3, 4 and the five edges

1→a2,2→b3,3→a4,4→b1,2→a4,1 \xrightarrow{a} 2,\quad 2 \xrightarrow{b} 3,\quad 3 \xrightarrow{a} 4,\quad 4 \xrightarrow{b} 1,\quad 2 \xrightarrow{a} 4,

and the language L=(ab)+L = (ab)^{+}, with the automaton of three states: s0→as1s_0 \xrightarrow{a} s_1, s1→bs2s_1 \xrightarrow{b} s_2, s2→as1s_2 \xrightarrow{a} s_1, and F={s2}F = \{s_2\}.

  • Arbitrary paths. From node 1, the path 1,2,31, 2, 3 has label abab, so (1,3)(1, 3) is an answer. The path 1,2,3,4,11, 2, 3, 4, 1 has label abababab, so (1,1)(1, 1) is an answer. A breadth-first search of the product from (1,s0)(1, s_0) visits (1,s0),(2,s1),(3,s2),(4,s1),(1,s2)(1, s_0), (2, s_1), (3, s_2), (4, s_1), (1, s_2) and stops; the visited pairs with state s2s_2 give the answers 33 and 11. From nodes 2 and 3 the answers are also {1,3}\{1, 3\}, and from node 4 there are none, since node 4 has no aa-edge. So L(G)={1,2,3}×{1,3}L(G) = \{1, 2, 3\} \times \{1, 3\}, six pairs.
  • Simple paths. From node 1, the path 1,2,31, 2, 3 is simple and gives (1,3)(1, 3). The only path from 1 to 1 with a label in LL is 1,2,3,4,11, 2, 3, 4, 1 or longer, and each of them repeats node 1. So under simple path semantics the answers from node 1 are {3}\{3\}, not {1,3}\{1, 3\}.
  • Product size. The product has at most 4⋅3=124 \cdot 3 = 12 nodes, and its edges are the pairs of a graph edge and a transition with the same label: 8 edges here, against the bound ∣E∣⋅∣δ∣=5⋅3=15|E| \cdot |\delta| = 5 \cdot 3 = 15.

Known algorithms

Product construction. Build the product graph P=G×AP = G \times A. Its nodes are the pairs (v,s)(v, s) with v∈Vv \in V and s∈Ss \in S. It has an edge (v,s)→(w,s′)(v, s) \to (w, s') for each edge (v,a,w)∈E(v, a, w) \in E and transition (s,a,s′)∈δ(s, a, s') \in \delta with the same label aa, and an edge (v,s)→(v,s′)(v, s) \to (v, s') for each ε\varepsilon-transition (s,ε,s′)(s, \varepsilon, s').

Claim. (u,v)∈L(G)(u, v) \in L(G) if and only if some (v,f)(v, f) with f∈Ff \in F is reachable from (u,s0)(u, s_0) in PP.

Proof. A path in PP from (u,s0)(u, s_0) to (v,f)(v, f) projects on its first component to a path of GG from uu to vv (the ε\varepsilon-steps stay at one node), and on its second component to a run of AA that reads the label of that path and ends in ff; so the label is in LL. Conversely, a path of GG from uu to vv with label w∈Lw \in L and an accepting run of AA on ww combine, step by step, into a path of PP from (u,s0)(u, s_0) to an accepting pair (v,f)(v, f). □\square

Algorithm (single source). Store the out-edges of each node grouped by label, and the transitions of each state grouped by label. Run a breadth-first or depth-first search on PP from (u,s0)(u, s_0), building the edges of PP on the fly: at a pair (v,s)(v, s), for each out-edge (v,a,w)(v, a, w), look up the aa-transitions of ss, and also follow the ε\varepsilon-transitions of ss. Output each vv such that some visited pair (v,f)(v, f) has f∈Ff \in F.

Running time. The search visits each pair at most once, so at most ∣V∣⋅∣S∣|V| \cdot |S| pairs. At a pair (v,s)(v, s) it scans the out-edges of vv and, for each edge with label aa, the aa-transitions of ss; it also scans the ε\varepsilon-transitions of ss. Summed over all vv and ss, the scans of edges cost ∣E∣⋅∣S∣|E| \cdot |S|, the matched pairs cost ∑a∣Ea∣⋅∣δa∣≤∣E∣⋅∣δ∣\sum_{a} |E_a| \cdot |\delta_a| \leq |E| \cdot |\delta|, where EaE_a and δa\delta_a are the edges and transitions with label aa, and the ε\varepsilon-scans cost ∣V∣⋅∣δ∣|V| \cdot |\delta|. The total is

O(∣V∣⋅∣S∣+∣E∣⋅∣S∣+∣E∣⋅∣δ∣+∣V∣⋅∣δ∣)=O((∣V∣+∣E∣)⋅∣A∣)O\big(|V| \cdot |S| + |E| \cdot |S| + |E| \cdot |\delta| + |V| \cdot |\delta|\big) = O\big((|V| + |E|) \cdot |A|\big)

for one source. This is O(∣E∣⋅∣A∣)O(|E| \cdot |A|) when every node has an edge. All pairs follow by one search per source, in time O(∣V∣⋅(∣V∣+∣E∣)⋅∣A∣)O(|V| \cdot (|V| + |E|) \cdot |A|). From a regular expression rr, the Thompson construction gives an automaton with ∣A∣=O(∣r∣)|A| = O(|r|), so the bounds hold with ∣r∣|r| in place of ∣A∣|A|.

Datalog. The same product is a linear Datalog program with one binary predicate PsP_s per state: Ps0(x,x)P_{s_0}(x, x) for each node xx, the rule Ps′(x,z)←Ps(x,y),Ea(y,z)P_{s'}(x, z) \leftarrow P_s(x, y), E_a(y, z) for each transition (s,a,s′)(s, a, s'), and Ans(x,y)←Pf(x,y)\mathit{Ans}(x, y) \leftarrow P_f(x, y) for f∈Ff \in F. Semi-naive evaluation of this program computes the all-pairs answer.

Simple paths. For simple path semantics no polynomial algorithm is known in general. A search over the simple paths of GG, pruned by the automaton state, is exponential in the worst case. Mendelzon and Wood (1995) gave polynomial cases, among them graphs without cycles, where every path is simple and the product algorithm applies.

Bounds

  • Arbitrary paths. O((∣V∣+∣E∣)⋅∣A∣)O((|V| + |E|) \cdot |A|) time for one source, by the product search above. The decision problem is in NL in combined complexity: guess the path in PP one step at a time, storing only the current pair (v,s)(v, s) in O(log⁡∣V∣+log⁡∣S∣)O(\log |V| + \log |S|) bits. It is NL-hard already in data complexity, for the fixed query a∗a^{*}, which is directed graph reachability. So it is NL-complete in data and in combined complexity (see NL).
  • Simple paths. NP-complete for some fixed languages, so in data complexity (Mendelzon and Wood 1995). Membership in NP holds because a simple path has at most ∣V∣−1|V| - 1 edges and is a certificate. Bagan, Bonifati and Groz (2013) showed a trichotomy: for each fixed regular language, the simple path problem is in AC0, NL-complete or NP-complete.
  • Containment. Query L1L_1 is contained in L2L_2 on every graph if and only if L1⊆L2L_1 \subseteq L_2: if w∈L1∖L2w \in L_1 \setminus L_2, the graph that is a single path labeled ww separates them, since its only path from the first to the last node has label ww. So containment of RPQs given by regular expressions is PSPACE-complete, as language containment is (see regex equivalence).

Reductions

  • Reachability to RPQ. Label every edge aa; then (u,v)(u, v) is in the answer of a∗a^{*} if and only if vv is reachable from uu. This gives the NL lower bound.
  • RPQ to reachability. The product construction above, computable in logarithmic space.
  • Two disjoint paths to simple RPQ. The problem "given a directed graph HH and distinct nodes s1,t1,s2,t2s_1, t_1, s_2, t_2, are there node-disjoint paths from s1s_1 to t1t_1 and from s2s_2 to t2t_2?" is NP-complete (Fortune, Hopcroft and Wyllie 1980; see vertex-disjoint paths). Build GG from HH: label every edge of HH with aa, and add one new edge t1→bs2t_1 \xrightarrow{b} s_2. Ask whether GG has a simple path from s1s_1 to t2t_2 with a label in a∗ba∗a^{*} b a^{*}. Such a path uses the only bb-edge once, so it is a path of HH from s1s_1 to t1t_1, the edge to s2s_2, and a path of HH from s2s_2 to t2t_2; it is simple exactly when the two parts share no node. So the answer is yes if and only if the disjoint paths exist, and the simple path problem for the fixed language a∗ba∗a^{*} b a^{*} is NP-hard.
  • CQ to CRPQ. A conjunctive query over binary relations is a CRPQ whose regular expressions are single labels, so NP-hardness of CQ evaluation in combined complexity carries over to CRPQs.

Applications

  • Graph query languages. Cruz, Mendelzon and Wood (1987) introduced the language G, whose query graphs have edges labeled by regular expressions; RPQs are its core. SPARQL property paths, Cypher variable-length patterns and the path patterns of GQL are RPQs or extensions of them, and they differ mainly in the path semantics: arbitrary paths, trails or simple paths.
  • Recursive queries in relational systems. A recursive SQL query over an edge table that follows a label pattern is an RPQ, evaluated by the Datalog program above.
  • Semistructured data and knowledge graphs. Queries such as "all subclasses of a class, at any depth" are RPQs of the form subClassOf∗\mathit{subClassOf}^{*} (survey by Angles et al. 2017).
  • Program analysis. Reachability with a regular pattern over a graph of a program, such as a call graph with labeled edges, is an RPQ; the product with an automaton is the same construction.

References

  1. Alberto O. Mendelzon and Peter T. Wood, Finding regular simple paths in graph databases, SIAM Journal on Computing 24(6), 1235 to 1258 (1995), polynomial evaluation under arbitrary paths, NP-completeness under simple paths for fixed regular languages, and polynomial cases
  2. Isabel F. Cruz, Alberto O. Mendelzon and Peter T. Wood, A graphical query language supporting recursion, Proceedings of the ACM SIGMOD International Conference on Management of Data, 323 to 330 (1987), the graph query language G, whose query graphs have edges labeled by regular expressions
  3. Guillaume Bagan, Angela Bonifati and Benoit Groz, A trichotomy for regular simple path queries on graphs, Proceedings of the 32nd ACM Symposium on Principles of Database Systems (PODS), 261 to 272 (2013), for each fixed regular language, simple path evaluation is in AC0, NL-complete or NP-complete
  4. Steven Fortune, John Hopcroft and James Wyllie, The directed subgraph homeomorphism problem, Theoretical Computer Science 10(2), 111 to 121 (1980), two node-disjoint paths between given pairs in a directed graph is NP-complete
  5. Renzo Angles, Marcelo Arenas, Pablo Barcelo, Aidan Hogan, Juan Reutter and Domagoj Vrgoc, Foundations of modern query languages for graph databases, ACM Computing Surveys 50(5), article 68 (2017), path queries, their semantics (arbitrary paths, simple paths, trails) and conjunctive regular path queries

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a known algorithm
  • a contract.yaml