How to get all connected nodes in neo4j
Asked Answered
W

3

24

enter image description here

I want to get list of all connected nodes starting from node 0 as shown in the diagram

Wen answered 11/7, 2017 at 10:43 Comment(2)
Can you be a little more precise ? what is result do you want to have ?Modeling
I want to get a list of all the connected nodes. For example in the above case when I search for connected nodes for 0, it should return nodes- 1,2,3.Wen
S
17

Based on your comment:

I want to get a list of all the connected nodes. For example in the above case when I search for connected nodes for 0, it should return nodes- 1,2,3

This query will do what you want:

MATCH ({id : 0})-[*]-(connected)
RETURN connected

The above query will return all nodes connected with a node with id=0 (I'm considering that the numbers inside the nodes are values of an id property) in any depth, both directions and considering any relationship type. Take a look in the section Relationships in depth of the docs.

While this will work fine for small graphs note that this is a very expensive operation. It will go through the entire graph starting from the start point ({id : 0}) considering any relationship type. This is really not a good idea for production environments.

Smirk answered 11/7, 2017 at 11:18 Comment(2)
in case of cyclic graph , it is giving duplicate nodesWen
@chetandev Try: MATCH (root {id : 0})-[*]-(connected) WHERE root <> connected RETURN distinct connectedSmirk
E
8

If you wish to match the nodes that have a relationship to another node, you can use this:

MATCH (n) MATCH (n)-[r]-() RETURN n,r

It will return you all the nodes that have a relationship to another node or nodes, irrespective of the direction of the relationship.

If you wish to add a constraint you can do it this way:

MATCH (n:Label {id:"id"}) MATCH (n)-[r]-() RETURN n,r
Electorate answered 11/7, 2017 at 15:3 Comment(2)
Can I do something to get a deep nodes i.e not just ones directly connected to nNazi
MATCH (n:Label {id:"id"}) MATCH(q:Deep_Node {id:"id"}) MATCH (n)-[r]-(q) RETURN n,r,qElectorate
S
7

For larger or more heavily interconnected graphs, APOC Procedures offers a more efficient means of traversal that returns all nodes in a subgraph.

As others have already mentioned, it's best to use labels on your nodes, and add either an index or a unique constraint on the label+property for fast lookup of your starting node.

Using a label of "Label", and a parameter of idParam, a query to get nodes of the subgraph with APOC would be:

MATCH (n:Label {id:$idParam})
CALL apoc.path.subgraphNodes(n, {minLevel:1}) YIELD node
RETURN node

Nodes will be distinct, and the starting node will not be returned with the rest.

EDIT

There's currently a restriction preventing usage of minLevel in subgraphNodes(), you can use either filter out the starting node yourself, or use apoc.path.expandConfig() using uniqueness:'NODE_GLOBAL' to get the same effect.

Seavey answered 11/7, 2017 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.