Add the number of common neighbors between two vertices as an attribute to the edge between the same vertices

Hello TigerGraph Team,

I need to find the number of common neighbors between any two vertex pairs of type “Car”. Then I want to add the number of common neighbors as an attribute to the edge SIMILAR_TO between vertices of type “Car”.

Here is my approach:

CREATE QUERY create_SIMILAR_TO() FOR GRAPH cbit_sna syntax v2 {
vertex common_v;
start = {Car.*};
res = select c from
start:s - () -:n - () - Car:c
where s != c
accum common_v = s.neighbors() INTERSECT c.neighbors();
insert into SIMILAR_TO (from, to, score) values (s Car, c Car, common_v.size());
print common_v.size();
}

The query returned null and inserted only one edge of type SIMILAR_TO was added. I don’t know how this was happening. Is there a way to fix this ? Or can someone propose an alternative to solve my problem? Thanks.

A couple quick comments:

s and c are alias values that are used on the SELECT statement. You appear to be using those values on the subsequent INSERT statement, so I am surprised that query would even install properly.

could you consider doing the INSERT in a POST ACCUM clause of the SELECT statement?

1 Like

Hello Mark,

Apologies for the late reply. I tried what you suggested - doing INSERT inside POST-ACCUM, but it did not work unfortunately. Now I have changed my strategy and am only doing INTERSECT inside ACCUM. So my new query becomes:

CREATE QUERY create_SIMILAR_TO() FOR GRAPH cbit_sna syntax v2 {
SumAccum @@cnt = -1;
ArrayAccum<BagAccum> @@common_v[];
start = {Car.*};
res = select c from
start:s - (<DRIVE) - Person - (DRIVE>) - Car:c
where s != c
accum @@cnt += 1,
@@common_v[@@cnt] += s.neighbors() INTERSECT c.neighbors();
print @@common_v;
}

And the GraphStudio gives me the following error - “Oops! Looks like something went wrong. Please retry later. If problem persists, please contact TigerGraph support team”, which I have no idea what it means and how to fix it. There is no red underscore indicating syntax error but when I tried to install this query GraphStudio told me there is an coding error which I cannot spot.

Could you help me on that. Many Thanks !

I can definitely help, I just want to be clear on the goals, since it changed a bit from your first example.

I believe your main requirement is that for each unique pair of cars, you want the count of people that drive both cars, right? So the connection between Lamborghini and Hyundai is going to be very low :slight_smile:

Here are a few additional ideas:

  1. the problem with using the neighbors function and the INTERSECT is that you are essentially doing extra work unnecessarily. Your multi hop query already visits the Person vertex each time that a person has a unique pair so a simpler accumulator achieves the goal without the intersect
  2. by just doing s != c you are actually going to hit the same pair twice, source to target then target to source.

My little example works, and addresses both of those issues - also instead of just printing them, you could also put an INSERT statement in a POST ACCUM clause, but I suggest trying this simple example first

CREATE DISTRIBUTED QUERY create_Similar( ) FOR GRAPH cbit_sna  syntax v2 {
SumAccum<INT> @@cnt ;

MapAccum<VERTEX, INT> @count;
start = {Car.*};
res = select s from
start:s - (<DRIVE) - Person:p - (DRIVE>) - Car:c
where vertex_to_int(s)  > vertex_to_int(c) 
accum @@cnt += 1, s.@count += (c -> 1);
	
	out = SELECT s FROM res:s WHERE s.@count.size() > 0;
	PRINT out;
print @@cnt;
}

Hi Mark,

Thanks for your amazing suggestions and example! They work perfectly and are exactly what I was looking for!

Now I followed what you said and added a POST-ACCUM + INSERT EDGE combo after the SELECT statement.

CREATE OR REPLACE QUERY create_SIMILAR_TO() FOR GRAPH cbit_sna syntax v2 {
MapAccum<VERTEX, INT> @count;
start = {Car.*};

res = select s from
start:s - (<DRIVE) - Person:p - (DRIVE>) - Car:c
where vertex_to_int( s ) > vertex_to_int( c )
accum s.@count += (c -> 1);

out = SELECT s FROM res:s WHERE s.@count.size() > 0
post-accum FOREACH (c, n_common) in s.@count do
insert into SIMILAR_TO (FROM, TO, score) values (s Car, c Car, n_common)
end;
}

I expected it to work but instead I got a red underscore under the INSERT statement and the error message goes - “Edge SIMILAR_TO’s valid TO vertex types don’t cover all vertex types, so a universal vertex type expression cannot be used here.” I have no clue why it occurred, since the edge schema SIMILAR_TO is - UNDIRECTED EDGE SIMILAR_TO(FROM Car, TO Car, score FLOAT), which match my arguments in the INSERT clause.

I have checked the INSERT part in the GSQL Language Reference https://docs.tigergraph.com/dev/gsql-ref/querying/data-modification-statements#dml-sub-insert but it didn’t help much.

So here are my two questions:

  1. What modifications do I need to make the query work? Think I’m getting closer to the solution.
  2. Somehow I could not find function vertex_to_int() in the Reference Guide. I assume it converts the vertex into a unique ID?

Appreciate it!

Try this, change the accumulator to explicitly state the vertex type

MapAccum<VERTEX<Car> , INT> @count;

It works! Thank you for solving my problem!

2 Likes