Neo4j find n largest connected graphs with specific node types

131 Views Asked by At

Relatively new to Neo4j, but I am looking for some starting pointers or code examples for the following problem: I have a neo4j graph that contains 3 node types (Humans, Pets and Homes). I have 2 relationship types: FRIENDS and LIVING (see image below).

I would like to visualize the n-largest Human-Human "clusters" without the Human-Pets or Home-Human connections. So just clusters of Human friend groups. Any idea how I would go about that? See below, I would like to find and visualize the red subgraph as it is the largest but generally, the top n would be great:

enter image description here

1

There are 1 best solutions below

1
On

This will work. apoc.path.spanningTree() is relatively fast.

MATCH(h:Human)
CALL apoc.path.spanningTree(h, {
    relationshipFilter: "FRIENDS|LIVING",
    labelFilter:"Human",
    minLevel: 1,
    maxLevel: 999
}) YIELD path
WITH MAX(SIZE(NODES(path))) AS max
MATCH(h:Human)
CALL apoc.path.spanningTree(h, {
    relationshipFilter: "FRIENDS|LIVING",
    labelFilter:"Human",
    minLevel: 1,
    maxLevel: max
}) YIELD path
WHERE SIZE(NODES(path)) = max
RETURN path;