Ensure end nodes are root nodes using apoc.path.subgraphAll

Hi,

I could really use some help. I need the paths that the query returns to have end nodes that are root nodes (nodes with no incoming relationships, only outgoing relationships) before it starts counting things.

This query gives me larger counts than it should, so I think it's overcounting or counting things it shouldn't. Is there a way to use apoc.path.subgraphAll to ensure the end nodes are root nodes?

If there is something like this where tier_2_vendor (company A) in the first line is root end node and tier_3_vendor (company B) in second line is root end node:

AAA<--manufacturer<--tier_2_vendor (company A)
ZZZ<--manufacturer<--tier_2_vendor (company A)<--tier_3_vendor (company B)

and im counting tier_2_vendor's, then the count here should only be two.
I'm really new to neo4j and I keep getting confused when looking through the docs, any help would be appreciated.

MATCH (f:code)
WHERE f.code IN ["AAA", "ZZZ"]
CALL apoc.path.subgraphAll(f, {
    relationshipFilter: "<",
    labelFilter: "+manufacturer|+tier_2_vendor|+tier_3_vendor"
})
YIELD nodes
UNWIND nodes AS n
UNWIND labels(n) AS label
WITH label, count(n) AS count
RETURN label, count

If you're counting tier_2_vendor's with no incoming relationships, wouldn't the count be 1?

I agree with @finbar.good, so I am confused too.

Your query is counting all nodes returned from both subgraphs tallied by label.

If you only want to count end nodes, then you can filter the other nodes out before counting.

MATCH (f:code)
WHERE f.code IN ["AAA", "ZZZ"]
CALL apoc.path.subgraphAll(f, {
    relationshipFilter: "<",
    labelFilter: "+manufacturer|+tier_2_vendor|+tier_3_vendor"
})
YIELD nodes
UNWIND nodes AS n
WITH n
WHERE not exists( (n)<--() )
UNWIND labels(n) AS label
WITH label, count(n) AS count
RETURN label, count

Note: if your nodes have only a single label, then you can use head(labels(n)) to get the label. This removes the need to unwind. For example:

MATCH (f:code)
WHERE f.code IN ["AAA", "ZZZ"]
CALL apoc.path.subgraphAll(f, {
    relationshipFilter: "<",
    labelFilter: "+manufacturer|+tier_2_vendor|+tier_3_vendor"
})
YIELD nodes
UNWIND nodes AS n
WITH n
WHERE not exists( (n)<--() )
RETURN head(labels(n)) as label, count(n) AS count