Find node nodes with all the same relationships except 1?

I would like to retrieve node couples, for which both nodes only differ from each other in a single relationship (with a certain label).

I have no clue how to do this :-S

Any help? pointers?

Hello,

Can you show us your graph model where we can see Label?

Hi,
Sorry, I was way to fast with my question.

Basically there are 2 scenario's:

  • couples of Person with the same dog(s) (exact the same connections to the same nodes)
  • couples of Persons with the same dogs(s)/animals but one (all the same connections to the same node, except for one. (with or without a label)
CREATE (john:Person {name: 'John'})
CREATE (joe:Person {name: 'Joe'})
CREATE (steve:Person {name: 'Steve'})
CREATE (sara:Person {name: 'Sara'})
CREATE (maria:Person {name: 'Maria'})
CREATE (dog1:Dog{name: 'dog1'})
CREATE (dog2:Dog{name: 'dog2'})
CREATE (dog3:Dog{name: 'dog3'})
CREATE (cat1:Cat{name: 'cat1'})
CREATE (cat2:Cat{name: 'cat2'})
CREATE (john)--(dog1)
CREATE (john)--(dog2)
CREATE (joe)--(dog1)
CREATE (joe)--(dog2)
CREATE (joe)--(cat1)
CREATE (sara)--(dog1)
CREATE (sara)--(dog2)
CREATE (sara)--(dog3)
CREATE (maria)--(dog1)
CREATE (maria)--(dog3)

solution should yield:
in case 1:
john -- joe
joe -- john

in case2:
john -- sara
sara -- john
sara -- maria
maria -- sara

I'm sorry but what you want to get is a bit unclear for me, you just want couples who have an animal in common?

Can you execute this command in Neo4j Desktop and show us what it shows please: CALL db.schema.visualization()

In scenario one, I would like to retrieve couple that are linked to exactly the same nodes.
So the nodes are identical when looking at relationships (not features)
Joe and John are connected to the same dog, and to nothing else.
I would like to retrieve
Joe, John

in scenario2 they are identical except that one of the two Person has an extra connection.
sara has one extra dog (dog3) in comparison with Joe and John.
I would like to retrieve
Joe, Sara
John, Sara

This might be simple, I don't know. I don't know how to enforce the same relationships on both nodes (when not knowing the relationships before hand).

So the query is: give me all node-couples with exactly the same relationships (scenario1). Scenario2: give me all node couple that only differ in a single relationship?

Hope this helps... Thanks for your time!

Can you give me the script to recreate your database please?

CREATE (john:Person {name: 'John'})
CREATE (joe:Person {name: 'Joe'})
CREATE (steve:Person {name: 'Steve'})
CREATE (sara:Person {name: 'Sara'})
CREATE (maria:Person {name: 'Maria'})
CREATE (dog1:Dog{name: 'dog1'})
CREATE (dog2:Dog{name: 'dog2'})
CREATE (dog3:Dog{name: 'dog3'})
CREATE (cat1:Cat{name: 'cat1'})
CREATE (cat2:Cat{name: 'cat2'})
CREATE (john)-[:has_dog]->(dog1)
CREATE (john)-[:has_dog]->(dog2)
CREATE (joe)-[:has_dog]->(dog1)
CREATE (joe)-[:has_dog]->(dog2)
CREATE (joe)-[:has_cat]->(cat1)
CREATE (sara)-[:has_dog]->(dog1)
CREATE (sara)-[:has_dog]->(dog2)
CREATE (sara)-[:has_dog]->(dog3)
CREATE (maria)-[:has_dog]->(dog1)
CREATE (maria)-[:has_dog]->(dog3)

To be honest, it's breaking my mind since 30min, what is the objective of this?

You can change this cypher request to get what you want :slight_smile:

MATCH (p1:Person)-[]->(a)
WITH p1.name AS p1m, collect(a.name) AS col
CALL apoc.cypher.run('
     WITH $p1m AS p1m, $col AS col
     MATCH (p2:Person)-[]->(a2)
     WHERE p1m <> p2.name
     WITH p2.name AS p2m, collect(a2.name) AS col2, p1m, col
     WHERE ALL(x IN col2 WHERE x IN col) RETURN p1m, p2m', {p1m:p1m, col:col})
YIELD value
RETURN value.p1m, value.p2m

auch, sorry...

You mean the things i describe are not clear, or what I want is difficult/complex?

I'll give it a shot as soon as possible.
Thanks for the help.

We are here to help so we will continue until the problem is solved:)

Did you tried the request I sent you?

It returns Persons who has pets in common:)
You can update it to get something more close to what you want:)