Check if edge (u, v) exists in the graph

I wonder if there is an easy way of checking if an edge between two given vertices, u and v, exists in a graph.

It would be great if, as an extension, we could also specify the type or types for the edge we are looking for (commonly named e_types in GSQL queries).

Here is a solution.

create query check_edges_exist(vertex u,vertex v,set<string> edges) for graph test{
    MapAccum<string,bool> @@result;
    start = {u};
    l = select s from start:s post-accum
         foreach one_edge in edges do
             if s.neighbors(one_edge).contains(v)
             then @@result += (one_edge->True)
             else @@result += (one_edge->False)
    print @@result;

1 Like

Another way (untested, just thinking in my head):

CREATE QUERY check_edges_exist(VERTEX u, VERTEX v, SET<STRING> e_types) {
    OrAccum @@found;
    Start = {u};
    Result = SELECT s FROM Start:s -(e_types:e)- :t
        WHERE t == v
        ACCUM @@found += True;
    PRINT @@found;
1 Like

Thanks a lot for both solutions. Is there any difference between using BOOL and OrAccum? This is another approach:

CREATE QUERY edge_exists(VERTEX u, VERTEX v, SET<STRING> e_types) FOR GRAPH demo_fc_graph RETURNS (BOOL) {
  BOOL exists = FALSE;

  Start = { u };
  check = SELECT s FROM Start:s -(e_types)-> :t
          ACCUM IF t == v THEN exists = TRUE END;

  RETURN exists;