Is there a faster way to insert an edge between neighbors

Hello everyone!

I have a graph of about two thousand vertices (of course I want to test more nodes, but the query speed is too slow).
I want to insert an edge between any neighbors of each vertex, but my current query always times out (even though I set the timeout threshold to 60 seconds), so I wanted to find a faster way to insert.

To speed things up, I tried to reduce the number of queries and use of the for loop, but it was still slow.
Here are two of the query methods I came up with, but it still says timeout, where insert_edge is the subquery I wrote.

  ListAccum<INT> @Adj;
   S1 = SELECT t FROM Bus:s -(BRANCH>|BRANCH_REVERSE>)- Bus:t WHERE s.Bus_Type<0 AND t.Bus_Type<0 AND t.id>s.id 
        ACCUM s.@Adj += t.id ;
   S2 = SELECT s FROM Bus:s  WHERE s.Bus_Type<0 ACCUM
        FOREACH one IN s.@Adj DO
          FOREACH two IN s.@Adj DO
            IF one > two THEN   # This ensures that there is only one insertion between the two nodes,       
                IF s.Bus_Type == -1 THEN insert_edge(one, two, "BRANCH", -1) END,
                IF s.Bus_Type == -2 THEN insert_edge(one, two, "BRANCH", -2) END
            END
          END
        END;
   S1 = SELECT two FROM Bus:one -(BRANCH>|BRANCH_REVERSE>)- Bus:mid- (BRANCH>|BRANCH_REVERSE>)- Bus:two 
       WHERE one.Bus_Type<0 AND mid.Bus_Type<0 AND two.Bus_Type<0 AND one.id>mid.id AND two.id>mid.id AND two.id>one.id
            AND not two.neighbors().contains(one)
       ACCUM IF mid.Bus_Type == -1 THEN insert_edge(one.id, two.id, "BRANCH", -1) END,
             IF mid.Bus_Type == -2 THEN insert_edge(one.id, two.id, "BRANCH", -2) END;

I would be very grateful if anyone could help me !

Wow, I suddenly noticed the TigerGraph Community Members label on the right side of my ID. How did that happen :smiley:

1 Like

First thing I would try is to eliminate the subquery and have it all self contained, just to see if that speeds things up. Also, you could simplify the first example a bit, like this

  ListAccum<INT> @Adj;
   S1 = SELECT t FROM Bus:s -(BRANCH>|BRANCH_REVERSE>)- Bus:t WHERE s.Bus_Type<0 AND t.Bus_Type<0 AND t.id>s.id 
        ACCUM s.@Adj += t.id 
       POST-ACCUM
  
        FOREACH one IN s.@Adj DO
          FOREACH two IN s.@Adj DO

               
            IF one > two THEN   # This ensures that there is only one insertion between the two nodes,       
                INSERT INTO BRANCH (one, two, s.Bus_Type) 
            END
          END
        END;
2 Likes

Thank you so much, I did it as you suggested, it worked, and it didn’t take too long to execute

This taught me the combination of ACCUM and post-Accum. Thank you very much indeed.

Finally, I would like to ask whether the use of subqueries will significantly increase query execution time.

1 Like

@kwonder That depends on the complexity of the subquery. The best thing to do is give it a try and then test the results.