How to get group of connected nodes?

I have a group of nodes connected like this:

Within a group, each of the nodes will be connected to similar nodes by 1 or more hops.

I need to get a group members separately ? Not really worried about how they are connected, as long as they are connected, they should be returned.
How can I do this?

I am expecting this result from the above graph:
[0,1,2,3,4]
[5,6,7,8,9]

Hello @karthiksk :slight_smile:

Have a look at this topic.

Regards,
Cobra

Thank you @cobra for helping.

Actually all these nodes are not directly connected... there may be some other nodes and relationships in between them, how do we specify them in the WWC query?

I have below nodes and relationships:

create(d:Label{name:"D"})
create(d1:Label{name:"D1"})
create(d2:Label{name:"D2"})
create(d3:Label{name:"D3"})
create(d4:Label{name:"D4"})
create(d5:Label{name:"D5"})


create(c1:CLabel{name:"c1"})
create(c2:CLabel{name:"c2"})
create(c3:CLabel{name:"c3"})
create(c4:CLabel{name:"c4"})
create(c5:CLabel{name:"cs"})

create(e:Label{name:"e"})
create(e1:Label{name:"e1"})
create(e2:Label{name:"e2"})
create(e3:Label{name:"e3"})
create(e4:Label{name:"e4"})
create(e5:Label{name:"es"})

create(f1:CLabel{name:"f1"})
create(f2:CLabel{name:"f2"})
create(f3:CLabel{name:"f3"})
create(f4:CLabel{name:"f4"})
create(f5:CLabel{name:"fs"})

MERGE (d)-[:CONTAINS]->(c1)-[:CONNECTED]->(d1)
MERGE (d)-[:CONTAINS]->(c2)-[:CONNECTED]->(d2)
MERGE (d)-[:CONTAINS]->(c3)-[:CONNECTED]->(d3)
MERGE (d)-[:CONTAINS]->(c4)-[:CONNECTED]->(d4)
MERGE (d)-[:CONTAINS]->(c5)-[:CONNECTED]->(d5)

MERGE (e)-[:CONTAINS]->(f1)-[:CONNECTED]->(e1)
MERGE (e)-[:CONTAINS]->(f2)-[:CONNECTED]->(e2)
MERGE (e)-[:CONTAINS]->(f3)-[:CONNECTED]->(e3)
MERGE (e)-[:CONTAINS]->(f4)-[:CONNECTED]->(e4)
MERGE (e)-[:CONTAINS]->(f5)-[:CONNECTED]->(e5)
RETURN *

I expect this result:
[D,D1,D2,D3,D4,D5]
[E,E1,E2,E3,E4,E5]

This query is not giving me expected results!

CALL gds.wcc.stream({
nodeProjection: "Label",
relationshipProjection: "CONNECTED"
})
YIELD nodeId, componentId
RETURN componentId, collect(gds.util.asNode(nodeId).name) AS myNodes
ORDER BY size(myNodes) DESC;

How to solve this problem now ?

You were pretty close :slight_smile:

CALL gds.wcc.stream({
    nodeProjection: "*",
    relationshipProjection: "*"
})
YIELD nodeId, componentId
WITH componentId, gds.util.asNode(nodeId) AS n
WHERE n:Label
RETURN componentId, collect(n.name) AS nodes

Thanks a lot @cobra :slight_smile:

1 Like