cancel
Showing results for 
Search instead for 
Did you mean: 

Counting path of nodes between nodes

VilladsClaes
Node Clone

Labels: "RED_NODE", "YELLOW_NODE", "GREEN_NODE"
Properties: id (1,2,3...), name ('This one', 'this other one',...), type (=label)

I wish to count the nodes from the red node onwards.

I wish to count the nodes from the red node and all connected nodes in any path

I wish to count only the nodes from the red node with name="this is the first" and not the nodes connected to the red node with the name="not this one"

So the desired output should be:
Nodes connected to "this is the first" = 12
Nodes connected to "not this one" = 4

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Something like this should solve your issue

MATCH (a:RED_NODE)-[*]->(b)
RETURN a.name AS name, count(b) AS nb_nodes

Regards,
Cobra

View solution in original post

3 REPLIES 3

Cobra
Ninja
Ninja

Hello @VilladsClaes

Without to know labels and propeties, I cannot write a query that fits your need but I can give you an algorithm:

MATCH path=(...)
RETURN start_node, size(nodes(path))

Regards,
Cobra

Cobra
Ninja
Ninja

Something like this should solve your issue

MATCH (a:RED_NODE)-[*]->(b)
RETURN a.name AS name, count(b) AS nb_nodes

Regards,
Cobra

ameyasoft
Graph Maven
Try this:

//Nodes connected to "this is the first"....

match (d:RED_NODE) where d.id = 1  
CALL apoc.neighbors.athop(d, ">|<", 10) YIELD node
with count(node) as lvl10
RETURN lvl10

//Nodes connected to "not this one"

match (d:RED_NODE) where d.id = 2  
CALL apoc.neighbors.athop(d, ">|<", 4) YIELD node
with count(node) as lvl4
RETURN lvl4
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.