How to calculate weight between nodes?

Hello, how are you guys?

I'm trying to calculate the weight of the relation between nodes, I know how to do using a script like in node or python language, but I think there is a better way using some algo or cypher language.

Use case:

CREATE (p:SystemType {name: 'A'});
CREATE (p:SystemType {name: 'B'});
CREATE (P:SystemType {name: 'C'});
CREATE (p:SystemType {name: 'D'});
MATCH (a:SystemType), (b:SystemType) WHERE a.name = 'A' AND b.name = 'B' CREATE (a)-[r:RELTYPE {description: 'x'}]->(b) RETURN type(r);
MATCH (a:SystemType), (b:SystemType) WHERE a.name = 'B' AND b.name = 'C' CREATE (a)-[r:RELTYPE {description: 'rt'}]->(b) RETURN type(r);
MATCH (a:SystemType), (b:SystemType) WHERE a.name = 'D' AND b.name = 'A' CREATE (a)-[r:RELTYPE {description: 'y'}]->(b) RETURN type(r);
MATCH (a:SystemType), (b:SystemType) WHERE a.name = 'A' AND b.name = 'B' CREATE (a)-[r:RELTYPE {description: 'tt'}]->(b) RETURN type(r);
MATCH (a:SystemType), (b:SystemType) WHERE a.name = 'D' AND b.name = 'B' CREATE (a)-[r:RELTYPE {description: 'bbb'}]->(b) RETURN type(r);
MATCH (a:SystemType), (b:SystemType) WHERE a.name = 'B' AND b.name = 'D' CREATE (a)-[r:RELTYPE {description: 'wer'}]->(b) RETURN type(r);
MATCH (a:SystemType), (b:SystemType) WHERE a.name = 'B' AND b.name = 'C' CREATE (a)-[r:RELTYPE {description: 'mm'}]->(b) RETURN type(r);
MATCH (a:SystemType), (b:SystemType) WHERE a.name = 'B' AND b.name = 'C' CREATE (a)-[r:RELTYPE {description: 'rtrrtnc'}]->(b) RETURN type(r);

Expect result:
B<>D = relation with weight 2
D<>A = relation with weight 1
A<>B= relation with weight 2
B<>C= relation with weight 3

Basic I don't care if the relation is in one direction or other, I just want see the number of connections between 2 nodes, create a new relation with a weight property.

Any idea?

I tried this cypher:

MATCH (n:SystemType)-[r:RELTYPE]->(n1:SystemType)
return n.name, n1.name , count(r)

The result:

n.name n1.name count(r)
D A 1
D B 1
A B 2
B C 3
B D 1

In the query above D|B and B|D is the same for me. Is it clear?

This will remove the problem of A|B and B|A both being returned.

Hi john, thanks for the answer.

What is the operation < in the where clause doing? It is a math operation?

Today I came to this solution, it seems a little bit over, but it works:

MATCH (n:SystemType)-[r:RELTYPE]->(n1:SystemType)
WITH id(n) as na, id(n1) as nb, count(r) as total
WITH COLLECT({ na:na, nb:nb, total:total}) as list
UNWIND list as item
MATCH (a:SystemType), (b:SystemType) WHERE id(a) = item.na  AND id(b) = item.nb MERGE (a)-[rTest:TEST]-(b)
ON CREATE SET rTest.weigth= item.total
ON MATCH SET rTest.weigth=(rTest.weigth+ item.total)

I did some testing and didn't work :(

n.name	n1.name	count(r)
"A"	"B"	2
"B"	"C"	3
"B"	"D"	1

Since some connections are missing i changed the query to and it worked.

MATCH (n:SystemType)-[r:RELTYPE]-(n1:SystemType)
WHERE id(n) < id(n1)
return n.name, n1.name , count(r)
1 Like

Ah yes. The ‘< ‘operator compares if the left node id is less than the right node id. If the direction of the relationship doesn’t matter using the id(a) < id(b) will remove having the situation where (a) is matched to (b) and (b) is matched to (a). Only one of the matches will be returned.