Question

I have created a graph db in Neo4j and want to use it for generalization purposes.

There are about 500,000 nodes (20 distinct labels) and 2.5 million relations (50 distinct types) between them.

In an example path : a -> b -> c-> d -> e

I want to find out the node without any incoming relations (which is 'a'). And I should do this for all the nodes (finding the nodes at the beginning of all possible paths that have no incoming relations).

I have tried several Cypher codes without any success:

match (a:type_A)-[r:is_a]->(b:type_A) 
with a,count (r) as count
where count = 0 
set a.isFirst = 'true'

or

match (a:type_A), (b:type_A) 
where not (a)<-[:is_a*..]-(b) 
set a.isFirst = 'true'

Where is the problem?!

Also, I have to create this code in neo4jClient, too.

Was it helpful?

Solution 2

If you want to find all nodes that have no incoming relationships, you can find them using OPTIONAL MATCH:

START n=node(*)
OPTIONAL MATCH n<-[r]-()
WITH n,r
WHERE r IS NULL 
RETURN n

OTHER TIPS

Your first query will only match paths where there is a relationship [r:is_a], so counting r can never be 0. Your second query will return any arbitrary pair of nodes labeled :typeA that aren't transitively related by [:is_a]. What you want is to filter on a path predicate. For the general case try

MATCH (a)
WHERE NOT ()-->a

This translates roughly "any node that does not have incoming relationships". You can specify the pattern with types, properties or labels as needed, for instance

MATCH (a:type_A)
WHERE NOT ()-[:is_a]->a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top