Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-13-2020 09:33 AM
Hi!
I have a large graph of say, Person, and the relations between them are FRIEND.
Given a couple of Persons, say 3 or 4 of them, I want to find all paths between all of them of length 4, and I just want simple paths.
That is, say the persons are A, B and C. Then I want a path of length at most 4 between A and B, having at least one node in common with a path from A to C and a path from B to C.. or one could say i want the graph of persons connected to all of A, B and C with at most a certain distance..
I have been looking at the graph algorithms and stuff, and spanningTree would give me one path between all of them, and between any given pair of Persons there are path finding algorithms, but cant figure out a good way to do it for 4 Persons..
Any great suggestions? 🙂
05-13-2020 10:55 AM
Could you redefine your problem a bit, and provide some greater specificity?
05-13-2020 11:33 AM
Hi,
Does this clarify it?
To be honest Im not 100% certain what I want, but I want the "smallest" subgraph containing the nodes I specify, and the relations between them.. to sort of get the gist of how a subset of nodes are related and not miss any interconnectedness/
05-13-2020 02:44 PM
First, a couple quick notes:
(A)->(d)->(B)
is a path of length 2.(A)->(d)
(C)->(d)
(d)->(B)
(A)->(d)->(B)
(A)->(d)<-(C)
(B)<-(d)<-(A)
(B)<-(d)<-(C)
(C)->(d)<-(A)
(C)->(d)->(B)
The term for this is "match intersection," and there are many ways to go about it. Desired results, and the existing graph data, will dictate which methods are possible and perform better.
Any of these will solve the problem, each has different applications.
// can do intersections for lists of As, Bs, and Cs.
MATCH (a {name:"A"})-[]->(x)-[]->(b {name: "B"})
WITH a, b, x
MATCH (x)-[]->(c {name: "C"})
RETURN a, b, c, x
// Better for handling paths, and multiple hops
MATCH (a {name:"A"}), (b {name: "B"}), (c {name: "C"})
MATCH p1=(a)-[*2..3]-(b)
MATCH p2=(b)-[*2..3]-(c)
MATCH p3=(c)-[*2..3]-(a)
WITH a, b, c, apoc.coll.intersection(nodes(p1), nodes(p2)) as n1, nodes(p3) as n2
WITH a, b, c, apoc.coll.intersection(n1, n2) as n0
UNWIND n0 as n
MATCH p1=(a)-[]->(n)-[]->(b)
MATCH p2=(b)-[]->(n)-[]->(c)
MATCH p3=(c)-[]->(n)-[]->(a)
RETURN a, b, c, n, p1, p2, p3
All the sessions of the conference are now available online