Find all nodes and edges connecting a set of nodes

I am trying to retrieve all the nodes and edges (edges can be optional if it improves performance) that connect a set of nodes n, m, x, and y

I know the connectivity in advance:
(n)-[]->(m)-[]->(x)
(y)-[*]->(x)

What would be the most efficient way to gather all the nodes (without duplicates)?

What I have tried:

  • apoc.path.expandConfig
  • allShortestPaths
  • regular path matching

What I have found is that I can only get a decent performance with apoc.path.expandConfig, searching for other approaches (that I might have missed).

Hi,

If you just need to find distinct nodes then try
Match(n) return distinct n

@intouch.vivek The graph is much-much larger, I am only interested in a subgraph between a set of nodes

You can try nodes(path)

or you also look into https://neo4j.com/docs/labs/apoc/current/graph-querying/expand-subgraph-nodes/

Should subgraphNodes and expandConfig return the same result/nodes if used with the same uniqueness setting?

For that you need to do a POC as haven't used expandConfig