Search code examples
graphneo4jcypher

Extract subgraph from Neo4j graph with Cypher


Suppose I have a collection of 5 nodes in Neo4j, such that each node in the collection is connected to at least one other node in the collection. I want to extract the subgraph formed by the collection of nodes and their interactions from Neo4j. Currently, I'm using a really primitive method that involves attempting to find a match from each node in the system to every other node:

MATCH p=(n)-[]->(m)
WHERE id(n) IN [3,4,5,6,7] AND id(m) IN [3,4,5,6,7]
RETURN relationships(p);

However, this query is both redundant and inefficient; it must go through every permutation of nodes in the collection (e.g. it will match node #3 and #4, and ALSO #4 and #3). Is there any more efficient way of getting the subgraph formed by these nodes using Cypher ONLY (no Java)?

Here's an example of what I mean by "subgraph": I want Cypher to return all the relationships marked in green. Please note that the nodes in the collection don't have to be isolated from the rest of the graph; they can still have relationships with other nodes in the graph, but I want Cypher to return only the interactions in green.

I want Cypher to return all the relationships marked in green. Please note that the nodes in the collection don't have to be isolated from the rest of the graph; they can still have relationships with other nodes in the graph, but I want Cypher to return only the relationships in green.


Solution

  • I want to extend on Richards answer, you can also restrict it to nodes whose id's are in separate groups.

    MATCH (n) WHERE id(n) IN [3,4,5,6,7]
    MATCH p=(n)-->(m) 
    WHERE id(n) < id(m) AND id(m) IN [3,4,5,6,7]
    RETURN relationships(p);
    

    results in

    enter image description here