Returning one or more paths during a graph traversal is a central feature of regular path queries in the GQL. To illustrate we will use the following example of a travel network:

We are interested in all the SHORTEST PATHS available from Bayreuth to Santiago.
Let’s start by creating a sample dataset
create graph travel_network {
node city ({name string, country string})
, edge train (city)-[{duration int}]->(city) -- duration in minutes
, edge flight (city)-[{code string, duration int}]->(city) -- duration in minutes
, edge car (city)-[{tolls float}]->(city)
};
use travel_network;
-- Create City nodes
INSERT
(n_bayreuth:city {_id: 'Bayreuth', name: 'Bayreuth', country: 'Germany'})
, (n_nürnberg:city {_id: 'Nürnberg', name: 'Nürnberg', country: 'Germany'})
, (n_stuttgart:city {_id: 'Stuttgart', name: 'Stuttgart', country: 'Germany'})
, (n_frankfurt:city {_id: 'Frankfurt', name: 'Frankfurt', country: 'Germany'})
, (n_paris:city {_id:'Paris', name: 'Paris', country: 'France'})
, (n_amsterdam:city {_id:'Amsterdam', name: 'Amsterdam', country: 'Netherlands'})
, (n_rome:city {_id:'Rome', name: 'Rome', country: 'Italy'})
, (n_santiago:city {_id:'Santiago', name: 'Santiago', country: 'Chile'})
, (n_bayreuth)-[:train]->(n_nürnberg)
, (n_nürnberg)-[:train]->(n_bayreuth)
, (n_nürnberg)-[:train {duration: 120}]->(n_stuttgart)
, (n_nürnberg)-[:train]->(n_frankfurt)
, (n_frankfurt)-[:train]->(n_paris)
, (n_stuttgart)-[:train]->(n_paris)
, (n_paris)-[:flight {code: 'AF406', duration: 780}]->(n_santiago)
, (n_amsterdam)-[:flight]->(n_santiago)
, (n_amsterdam)-[:flight]->(n_rome)
, (n_rome)-[:car {tolls: 74}]->(n_bayreuth)
, (n_frankfurt)-[:train]->(n_amsterdam)
;We can use the following GQL to get all the possible SHORTEST PATHS.
match paths = all shortest (a:city {_id:'Bayreuth'})-[edges:train|flight]->{1,}(b:city {_id:'Santiago'})
for edge in edges
return distinct table(edge._from, edge._to, labels(edge), pnodeIds(paths), length(paths));This will return each of the path decomposed into the each hop (step) as following

In the above GQL Query a is matched to the city of Bayreuth, and b to Santiago city. The PATH(s) are all possible combinations of the edges between between a and b which minimize the number of hops. In our case 4 is the minimum number of hops and there 3 possible PATHs that qualify.
Leave a Reply