k Paths in AQL

General query idea

This type of query finds all paths between two given documents, startVertex and targetVertex in your graph. The paths are restricted by minimum and maximum length of the paths.

Every such path will be returned as a JSON object with two components:

  • an array containing the vertices on the path
  • an array containing the edges on the path

Example

Let us take a look at a simple example to explain how it works. This is the graph that we are going to find some paths on:

Train Connection Map

Each ellipse stands for a train station with the name of the city written inside of it. They are the vertices of the graph. Arrows represent train connections between cities and are the edges of the graph. The numbers near the arrows describe how long it takes to get from one station to another. They are used as edge weights.

Let us assume that we want to go from Aberdeen to London by train.

Here we have a couple of alternatives:

a) Straight way

  1. Aberdeen
  2. Leuchars
  3. Edinburgh
  4. York
  5. London

b) Detour at York

  1. Aberdeen
  2. Leuchars
  3. Edinburgh
  4. York
  5. Carlisle
  6. Birmingham
  7. London

c) Detour at Edinburgh

  1. Aberdeen
  2. Leuchars
  3. Edinburgh
  4. Glasgow
  5. Carlisle
  6. Birmingham
  7. London

d) Detour at Edinburgh to York

  1. Aberdeen
  2. Leuchars
  3. Edinburgh
  4. Glasgow
  5. Carlisle
  6. York
  7. London

Note that we only consider paths as valid that do not contain the same vertex twice. The following alternative would visit Aberdeen twice and will not be returned by k Paths:

  1. Aberdeen
  2. Inverness
  3. Aberdeen
  4. Leuchars
  5. Edinburgh
  6. York
  7. London

Example Use Cases

The use-cases for k Paths are about the same as for unweighted k Shortest Paths. The main difference is that k Shortest Paths will enumerate all paths with increasing length. It will stop as soon as a given limit is reached. k Paths will instead only enumerate all paths within a given range of path length, and are thereby upper-bounded.

The k Paths traversal can be used as foundation for several other algorithms:

  • Transportation of any kind (e.g. road traffic, network package routing)
  • Flow problems: We need to transfer items from A to B, which alternatives do we have? What is their capacity?

Syntax

The syntax for k Paths queries is similar to the one for K Shortest Path with the addition to define the minimum and maximum length of the path.

It is highly recommended that you use a reasonable maximum path length or a LIMIT statement, as k Paths is a potentially expensive operation. On large connected graphs it can return a large number of paths.

Working with named graphs

FOR path
  IN MIN..MAX OUTBOUND|INBOUND|ANY K_PATHS
  startVertex TO targetVertex
  GRAPH graphName
  [OPTIONS options]
  • FOR: emits the variable path which contains one path as an object containing vertices and edges of the path.
  • IN MIN..MAX: the minimal and maximal depth for the traversal:
    • min (number, optional): paths returned by this query will have at least a length of min many edges. If not specified, it defaults to 1. The minimal possible value is 0.
    • max (number, optional): paths returned by this query will have at most a length of max many edges. If omitted, max defaults to min. Thus only the vertices and edges in the range of min are returned. max cannot be specified without min.
  • OUTBOUND|INBOUND|ANY: defines in which direction edges are followed (outgoing, incoming, or both)
  • K_PATHS: the keyword to compute all Paths
  • startVertex TO targetVertex (both string|object): the two vertices between which the paths will be computed. This can be specified in the form of a document identifier string or in the form of an object with the attribute _id. All other values will lead to a warning and an empty result. This is also the case if one of the specified documents does not exist.
  • GRAPH graphName (string): the name identifying the named graph. Its vertex and edge collections will be looked up.
  • OPTIONS options (object, optional): used to modify the execution of the search. Right now there are no options that trigger an effect. However, this may change in the future.

Working with collection sets

FOR path
  IN MIN..MAX OUTBOUND|INBOUND|ANY K_PATHS
  startVertex TO targetVertex
  edgeCollection1, ..., edgeCollectionN
  [OPTIONS options]

Instead of GRAPH graphName you can specify a list of edge collections. The involved vertex collections are determined by the edges of the given edge collections.

Traversing in mixed directions

For k paths with a list of edge collections you can optionally specify the direction for some of the edge collections. Say for example you have three edge collections edges1, edges2 and edges3, where in edges2 the direction has no relevance, but in edges1 and edges3 the direction should be taken into account. In this case you can use OUTBOUND as general search direction and ANY specifically for edges2 as follows:

FOR vertex IN OUTBOUND K_PATHS
  startVertex TO targetVertex
  edges1, ANY edges2, edges3

All collections in the list that do not specify their own direction will use the direction defined after IN (here: OUTBOUND). This allows to use a different direction for each collection in your path search.

Examples

We load an example graph to get a named graph that reflects some possible train connections in Europe and North America.

Train Connection Map

arangosh> var examples = require("@arangodb/graph-examples/example-graph.js");
arangosh> var graph = examples.loadGraph("kShortestPathsGraph");
arangosh> db.places.toArray();
arangosh> db.connections.toArray();
Show execution results
Hide execution results
[ 
  { 
    "_key" : "Inverness", 
    "_id" : "places/Inverness", 
    "_rev" : "_fyvMSQG---", 
    "label" : "Inverness" 
  }, 
  { 
    "_key" : "Aberdeen", 
    "_id" : "places/Aberdeen", 
    "_rev" : "_fyvMSQG--_", 
    "label" : "Aberdeen" 
  }, 
  { 
    "_key" : "Leuchars", 
    "_id" : "places/Leuchars", 
    "_rev" : "_fyvMSQK---", 
    "label" : "Leuchars" 
  }, 
  { 
    "_key" : "StAndrews", 
    "_id" : "places/StAndrews", 
    "_rev" : "_fyvMSQK--_", 
    "label" : "StAndrews" 
  }, 
  { 
    "_key" : "Edinburgh", 
    "_id" : "places/Edinburgh", 
    "_rev" : "_fyvMSQK--A", 
    "label" : "Edinburgh" 
  }, 
  { 
    "_key" : "Glasgow", 
    "_id" : "places/Glasgow", 
    "_rev" : "_fyvMSQK--B", 
    "label" : "Glasgow" 
  }, 
  { 
    "_key" : "York", 
    "_id" : "places/York", 
    "_rev" : "_fyvMSQO---", 
    "label" : "York" 
  }, 
  { 
    "_key" : "Carlisle", 
    "_id" : "places/Carlisle", 
    "_rev" : "_fyvMSQO--_", 
    "label" : "Carlisle" 
  }, 
  { 
    "_key" : "Birmingham", 
    "_id" : "places/Birmingham", 
    "_rev" : "_fyvMSQO--A", 
    "label" : "Birmingham" 
  }, 
  { 
    "_key" : "London", 
    "_id" : "places/London", 
    "_rev" : "_fyvMSQO--B", 
    "label" : "London" 
  }, 
  { 
    "_key" : "Brussels", 
    "_id" : "places/Brussels", 
    "_rev" : "_fyvMSQO--C", 
    "label" : "Brussels" 
  }, 
  { 
    "_key" : "Cologne", 
    "_id" : "places/Cologne", 
    "_rev" : "_fyvMSQS---", 
    "label" : "Cologne" 
  }, 
  { 
    "_key" : "Toronto", 
    "_id" : "places/Toronto", 
    "_rev" : "_fyvMSQS--_", 
    "label" : "Toronto" 
  }, 
  { 
    "_key" : "Winnipeg", 
    "_id" : "places/Winnipeg", 
    "_rev" : "_fyvMSQS--A", 
    "label" : "Winnipeg" 
  }, 
  { 
    "_key" : "Saskatoon", 
    "_id" : "places/Saskatoon", 
    "_rev" : "_fyvMSQS--B", 
    "label" : "Saskatoon" 
  }, 
  { 
    "_key" : "Edmonton", 
    "_id" : "places/Edmonton", 
    "_rev" : "_fyvMSQS--C", 
    "label" : "Edmonton" 
  }, 
  { 
    "_key" : "Jasper", 
    "_id" : "places/Jasper", 
    "_rev" : "_fyvMSQW---", 
    "label" : "Jasper" 
  }, 
  { 
    "_key" : "Vancouver", 
    "_id" : "places/Vancouver", 
    "_rev" : "_fyvMSQW--_", 
    "label" : "Vancouver" 
  } 
]
[ 
  { 
    "_key" : "62748", 
    "_id" : "connections/62748", 
    "_from" : "places/Inverness", 
    "_to" : "places/Aberdeen", 
    "_rev" : "_fyvMSQW--A", 
    "travelTime" : 3 
  }, 
  { 
    "_key" : "62750", 
    "_id" : "connections/62750", 
    "_from" : "places/Aberdeen", 
    "_to" : "places/Inverness", 
    "_rev" : "_fyvMSQW--B", 
    "travelTime" : 2.5 
  }, 
  { 
    "_key" : "62752", 
    "_id" : "connections/62752", 
    "_from" : "places/Aberdeen", 
    "_to" : "places/Leuchars", 
    "_rev" : "_fyvMSQa---", 
    "travelTime" : 1.5 
  }, 
  { 
    "_key" : "62754", 
    "_id" : "connections/62754", 
    "_from" : "places/Leuchars", 
    "_to" : "places/Aberdeen", 
    "_rev" : "_fyvMSQa--_", 
    "travelTime" : 1 
  }, 
  { 
    "_key" : "62756", 
    "_id" : "connections/62756", 
    "_from" : "places/Leuchars", 
    "_to" : "places/Edinburgh", 
    "_rev" : "_fyvMSQa--A", 
    "travelTime" : 1.5 
  }, 
  { 
    "_key" : "62758", 
    "_id" : "connections/62758", 
    "_from" : "places/Edinburgh", 
    "_to" : "places/Leuchars", 
    "_rev" : "_fyvMSQa--B", 
    "travelTime" : 3 
  }, 
  { 
    "_key" : "62760", 
    "_id" : "connections/62760", 
    "_from" : "places/Edinburgh", 
    "_to" : "places/Glasgow", 
    "_rev" : "_fyvMSQe---", 
    "travelTime" : 1 
  }, 
  { 
    "_key" : "62762", 
    "_id" : "connections/62762", 
    "_from" : "places/Glasgow", 
    "_to" : "places/Edinburgh", 
    "_rev" : "_fyvMSQe--_", 
    "travelTime" : 1 
  }, 
  { 
    "_key" : "62764", 
    "_id" : "connections/62764", 
    "_from" : "places/Edinburgh", 
    "_to" : "places/York", 
    "_rev" : "_fyvMSQe--A", 
    "travelTime" : 3.5 
  }, 
  { 
    "_key" : "62766", 
    "_id" : "connections/62766", 
    "_from" : "places/York", 
    "_to" : "places/Edinburgh", 
    "_rev" : "_fyvMSQe--B", 
    "travelTime" : 4 
  }, 
  { 
    "_key" : "62768", 
    "_id" : "connections/62768", 
    "_from" : "places/Glasgow", 
    "_to" : "places/Carlisle", 
    "_rev" : "_fyvMSQe--C", 
    "travelTime" : 1 
  }, 
  { 
    "_key" : "62770", 
    "_id" : "connections/62770", 
    "_from" : "places/Carlisle", 
    "_to" : "places/Glasgow", 
    "_rev" : "_fyvMSQi---", 
    "travelTime" : 1 
  }, 
  { 
    "_key" : "62772", 
    "_id" : "connections/62772", 
    "_from" : "places/Carlisle", 
    "_to" : "places/York", 
    "_rev" : "_fyvMSQi--_", 
    "travelTime" : 2.5 
  }, 
  { 
    "_key" : "62774", 
    "_id" : "connections/62774", 
    "_from" : "places/York", 
    "_to" : "places/Carlisle", 
    "_rev" : "_fyvMSQi--A", 
    "travelTime" : 3.5 
  }, 
  { 
    "_key" : "62776", 
    "_id" : "connections/62776", 
    "_from" : "places/Carlisle", 
    "_to" : "places/Birmingham", 
    "_rev" : "_fyvMSQi--B", 
    "travelTime" : 2 
  }, 
  { 
    "_key" : "62778", 
    "_id" : "connections/62778", 
    "_from" : "places/Birmingham", 
    "_to" : "places/Carlisle", 
    "_rev" : "_fyvMSQm---", 
    "travelTime" : 1 
  }, 
  { 
    "_key" : "62780", 
    "_id" : "connections/62780", 
    "_from" : "places/Birmingham", 
    "_to" : "places/London", 
    "_rev" : "_fyvMSQm--_", 
    "travelTime" : 1.5 
  }, 
  { 
    "_key" : "62782", 
    "_id" : "connections/62782", 
    "_from" : "places/London", 
    "_to" : "places/Birmingham", 
    "_rev" : "_fyvMSQm--A", 
    "travelTime" : 2.5 
  }, 
  { 
    "_key" : "62784", 
    "_id" : "connections/62784", 
    "_from" : "places/Leuchars", 
    "_to" : "places/StAndrews", 
    "_rev" : "_fyvMSQm--B", 
    "travelTime" : 0.2 
  }, 
  { 
    "_key" : "62786", 
    "_id" : "connections/62786", 
    "_from" : "places/StAndrews", 
    "_to" : "places/Leuchars", 
    "_rev" : "_fyvMSQq---", 
    "travelTime" : 0.2 
  }, 
  { 
    "_key" : "62788", 
    "_id" : "connections/62788", 
    "_from" : "places/York", 
    "_to" : "places/London", 
    "_rev" : "_fyvMSQq--_", 
    "travelTime" : 1.8 
  }, 
  { 
    "_key" : "62790", 
    "_id" : "connections/62790", 
    "_from" : "places/London", 
    "_to" : "places/York", 
    "_rev" : "_fyvMSQq--A", 
    "travelTime" : 2 
  }, 
  { 
    "_key" : "62792", 
    "_id" : "connections/62792", 
    "_from" : "places/London", 
    "_to" : "places/Brussels", 
    "_rev" : "_fyvMSQq--B", 
    "travelTime" : 2.5 
  }, 
  { 
    "_key" : "62794", 
    "_id" : "connections/62794", 
    "_from" : "places/Brussels", 
    "_to" : "places/London", 
    "_rev" : "_fyvMSQu---", 
    "travelTime" : 3.5 
  }, 
  { 
    "_key" : "62796", 
    "_id" : "connections/62796", 
    "_from" : "places/Brussels", 
    "_to" : "places/Cologne", 
    "_rev" : "_fyvMSQu--_", 
    "travelTime" : 2 
  }, 
  { 
    "_key" : "62798", 
    "_id" : "connections/62798", 
    "_from" : "places/Cologne", 
    "_to" : "places/Brussels", 
    "_rev" : "_fyvMSQu--A", 
    "travelTime" : 1.5 
  }, 
  { 
    "_key" : "62800", 
    "_id" : "connections/62800", 
    "_from" : "places/Toronto", 
    "_to" : "places/Winnipeg", 
    "_rev" : "_fyvMSQu--B", 
    "travelTime" : 36 
  }, 
  { 
    "_key" : "62802", 
    "_id" : "connections/62802", 
    "_from" : "places/Winnipeg", 
    "_to" : "places/Toronto", 
    "_rev" : "_fyvMSQy---", 
    "travelTime" : 35 
  }, 
  { 
    "_key" : "62804", 
    "_id" : "connections/62804", 
    "_from" : "places/Winnipeg", 
    "_to" : "places/Saskatoon", 
    "_rev" : "_fyvMSQy--_", 
    "travelTime" : 12 
  }, 
  { 
    "_key" : "62806", 
    "_id" : "connections/62806", 
    "_from" : "places/Saskatoon", 
    "_to" : "places/Winnipeg", 
    "_rev" : "_fyvMSQy--A", 
    "travelTime" : 5 
  }, 
  { 
    "_key" : "62808", 
    "_id" : "connections/62808", 
    "_from" : "places/Saskatoon", 
    "_to" : "places/Edmonton", 
    "_rev" : "_fyvMSQy--B", 
    "travelTime" : 12 
  }, 
  { 
    "_key" : "62810", 
    "_id" : "connections/62810", 
    "_from" : "places/Edmonton", 
    "_to" : "places/Saskatoon", 
    "_rev" : "_fyvMSQy--C", 
    "travelTime" : 17 
  }, 
  { 
    "_key" : "62812", 
    "_id" : "connections/62812", 
    "_from" : "places/Edmonton", 
    "_to" : "places/Jasper", 
    "_rev" : "_fyvMSQ2---", 
    "travelTime" : 6 
  }, 
  { 
    "_key" : "62814", 
    "_id" : "connections/62814", 
    "_from" : "places/Jasper", 
    "_to" : "places/Edmonton", 
    "_rev" : "_fyvMSQ2--_", 
    "travelTime" : 5 
  }, 
  { 
    "_key" : "62816", 
    "_id" : "connections/62816", 
    "_from" : "places/Jasper", 
    "_to" : "places/Vancouver", 
    "_rev" : "_fyvMSQ2--A", 
    "travelTime" : 12 
  }, 
  { 
    "_key" : "62818", 
    "_id" : "connections/62818", 
    "_from" : "places/Vancouver", 
    "_to" : "places/Jasper", 
    "_rev" : "_fyvMSQ2--B", 
    "travelTime" : 13 
  } 
]

Suppose we want to query all routes from Aberdeen to London.

FOR p IN 1..10 OUTBOUND K_PATHS 'places/Aberdeen' TO 'places/London'
  GRAPH 'kShortestPathsGraph'
      RETURN { places: p.vertices[*].label, travelTimes: p.edges[*].travelTime }
Show query results
Hide query results
[
  {
    "places": [
      "Aberdeen",
      "Leuchars",
      "Edinburgh",
      "York",
      "London"
    ],
    "travelTimes": [
      1.5,
      1.5,
      3.5,
      1.8
    ]
  },
  {
    "places": [
      "Aberdeen",
      "Leuchars",
      "Edinburgh",
      "Glasgow",
      "Carlisle",
      "Birmingham",
      "London"
    ],
    "travelTimes": [
      1.5,
      1.5,
      1,
      1,
      2,
      1.5
    ]
  },
  {
    "places": [
      "Aberdeen",
      "Leuchars",
      "Edinburgh",
      "Glasgow",
      "Carlisle",
      "York",
      "London"
    ],
    "travelTimes": [
      1.5,
      1.5,
      1,
      1,
      2.5,
      1.8
    ]
  },
  {
    "places": [
      "Aberdeen",
      "Leuchars",
      "Edinburgh",
      "York",
      "Carlisle",
      "Birmingham",
      "London"
    ],
    "travelTimes": [
      1.5,
      1.5,
      3.5,
      3.5,
      2,
      1.5
    ]
  }
]

If we ask for routes that don’t exist we get an empty result (from Aberdeen to Toronto):

FOR p IN 1..10 OUTBOUND K_PATHS 'places/Aberdeen' TO 'places/Toronto'
  GRAPH 'kShortestPathsGraph'
      RETURN { places: p.vertices[*].label, travelTimes: p.edges[*].travelTime }
Show query results
Hide query results
[]

And finally clean up by removing the named graph:

arangosh> var examples = require("@arangodb/graph-examples/example-graph.js");
arangosh> examples.dropGraph("kShortestPathsGraph");
Show execution results
Hide execution results