How to extract all subgraph from neo4j

854 views
Skip to first unread message

Kai Wang

unread,
Apr 11, 2017, 10:02:29 AM4/11/17
to Neo4j
Hi,all,

I'm using neo4j and I build a large graph that has a lot of subgraph. For example, in the pic below there are 4 subgraphs. How can I 
get 4 groups of nodes by cypher query? I mean  it can return [node1, node2, node3], [node4, node5], [node6, node7, node8] ,each list represent a subgraph.





Michael Hunger

unread,
Apr 11, 2017, 3:05:41 PM4/11/17
to ne...@googlegroups.com
1. you probably don't need bi-directional relationships, one is good enough.

2. one way to get the subgraphs is to get one node of each sub-graph and return all paths for that node up to a certain length.

MATCH (n:Node) where n.name IN ["foo", "bar"]
MATCH path = (n)-[*..5]-()
UNWIND nodes(path) as node
RETURN n, collect(distinct node) as nodes

3. you can also use the path-expand and clustering algorithms in the APOC procedure library

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kai Wang

unread,
Apr 11, 2017, 9:10:16 PM4/11/17
to Neo4j
Thanks. My frustration is that I want to automatically detect all subgraph, which means I dont't know how to determine a list of "one node of each sub-graph". But using collect in cypher is pretty good hint. I'm also concerned about the performance.


在 2017年4月12日星期三 UTC+8上午3:05:41,Michael Hunger写道:
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages