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 . 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 over a finite alphabet , with ; and a regular language , given by a regular expression or by a nondeterministic finite automaton with . The size of is .
A path from to is a sequence of edges in with and , for some . Its label is the word ; the empty path has the empty word as label. The path is simple if are distinct.
Output. The answer of the query on is
Under simple path semantics, only simple paths count. The decision versions ask whether a given pair is in the answer.
The answer is a binary relation over , so a regular path query is a query in the sense of the relational model: the graph is a database with one binary relation per label , and is a relation computed from it. For the query the answer is the reflexive and transitive closure of , 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 and return all ; fix both and decide; or return the whole relation. The all-pairs answer can have pairs.
- Two-way RPQs (2RPQs). The alphabet also has an inverse label for each , read along an -edge from its head to its tail. Add the reversed edges to and the problem becomes an ordinary RPQ.
- Conjunctive RPQs (CRPQs). A conjunctive query whose atoms are RPQs, such as . 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 and the five edges
and the language , with the automaton of three states: , , , and .
- Arbitrary paths. From node 1, the path has label , so is an answer. The path has label , so is an answer. A breadth-first search of the product from visits and stops; the visited pairs with state give the answers and . From nodes 2 and 3 the answers are also , and from node 4 there are none, since node 4 has no -edge. So , six pairs.
- Simple paths. From node 1, the path is simple and gives . The only path from 1 to 1 with a label in is or longer, and each of them repeats node 1. So under simple path semantics the answers from node 1 are , not .
- Product size. The product has at most nodes, and its edges are the pairs of a graph edge and a transition with the same label: 8 edges here, against the bound .
Known algorithms
Product construction. Build the product graph . Its nodes are the pairs with and . It has an edge for each edge and transition with the same label , and an edge for each -transition .
Claim. if and only if some with is reachable from in .
Proof. A path in from to projects on its first component to a path of from to (the -steps stay at one node), and on its second component to a run of that reads the label of that path and ends in ; so the label is in . Conversely, a path of from to with label and an accepting run of on combine, step by step, into a path of from to an accepting pair .
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 from , building the edges of on the fly: at a pair , for each out-edge , look up the -transitions of , and also follow the -transitions of . Output each such that some visited pair has .
Running time. The search visits each pair at most once, so at most pairs. At a pair it scans the out-edges of and, for each edge with label , the -transitions of ; it also scans the -transitions of . Summed over all and , the scans of edges cost , the matched pairs cost , where and are the edges and transitions with label , and the -scans cost . The total is
for one source. This is when every node has an edge. All pairs follow by one search per source, in time . From a regular expression , the Thompson construction gives an automaton with , so the bounds hold with in place of .
Datalog. The same product is a linear Datalog program with one binary predicate per state: for each node , the rule for each transition , and for . 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 , 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. time for one source, by the product search above. The decision problem is in NL in combined complexity: guess the path in one step at a time, storing only the current pair in bits. It is NL-hard already in data complexity, for the fixed query , 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 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 is contained in on every graph if and only if : if , the graph that is a single path labeled separates them, since its only path from the first to the last node has label . 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 ; then is in the answer of if and only if is reachable from . 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 and distinct nodes , are there node-disjoint paths from to and from to ?" is NP-complete (Fortune, Hopcroft and Wyllie 1980; see vertex-disjoint paths). Build from : label every edge of with , and add one new edge . Ask whether has a simple path from to with a label in . Such a path uses the only -edge once, so it is a path of from to , the edge to , and a path of from to ; 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 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 (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
- 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
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 3RG9NX Edit this page History Report an error Markdown