Get paths between all set of vertices supplied

Hi there, I’m trying to write a query to get all the paths between a set of vertices being supplied as a parameter.

So for example, I could supply User vertices of A, B, C, D.

I wish to find the shortest path from A to B, A to C, A to D, B to C and so on… I’m not too concerned about what type of edges I can use, and I also hope to not get ‘repeated’ paths, i.e if I found a path from A to D, I don’t have to get a path from D to A as it would just be the same but in reverse.

So far, this is the code I came up with, I’ve tried using FOR loops as well but couldn’t seem to get the syntax right. Any advice would be greatly appreciated. If you’ve done a similar query as well, it would also greatly help if you could share a code snippet! Thanks in advance!

CREATE QUERY all_paths_from_set (SET<String> start) FOR GRAPH name {
  MinAccum<INT> @min_dis;
  OrAccum @or_visited;
  ListAccum<VERTEX> @path_list;
  SetAccum<EDGE> @@edge_set;

  ResultSet = {};
  ListAccum<VERTEX> @@start_list;
  allUsers = {User.*};
  cm = SELECT au FROM allUsers:au WHERE IN start ACCUM @@start_list += au;
  FOREACH i IN @@start_list DO
    // This does not run as there is an error with making a set in the next statement
    Source = {i};
    Res = SELECT t FROM Source:s -((<_|_>)*)- cm:t
      ACCUM t.@path_list = s.@path_list + [t];
    ResultSet = ResultSet UNION Res;
  PRINT ResultSet;


I’m thinking you could maybe use All-Pairs Shortest Path and modifying it slightly to run the unweighted shortest path.

Let me know if this helps!

1 Like