How to get the number of a node connected nodes in constant time?

Hi everyone,

As you may know, you can get the number of relationships of a node in constant time thanks to the COUNT{(n)-[]->()} syntax, which appears as a getDegree operation in the query plan and is just a read (the value is precomputed at the node level by Neo4j).

This is just great for huge or densely connected graphs.

I am looking for something similar but that counts the actual nodes (not the relationships), does any of you know about something like that? I'm starting to think I'll have to make something custom for this...

Thanks a lot!

Not sure what you are asking for.

The count subquery can count anything, as it counts the number of items returned in the subquery’s query. The query can be more than a pattern as you used. It can be a cypher query that can find your nodes and count what is returned.

Thanks, I guess I should've insisted on the important part of the question which was "in constant time".
"Normal" COUNT subqueries will have the temporal complexity of the subquery they're performing, which in my case would be related to the number of relationships of the node.

That's why I mentioned the query plan getDegree operation which performs exactly what I need for the number of relationships. I figured maybe there was something similar, or a simple workaround, for the number of nodes.

Are you asking to get the number of connected nodes for a given node? That number would be the same as the number of relationships, since a relationship has to have a start and an end node. Is this what you are asking?

Both should be equal to:

 count{(n)--()}

Are you asking to count something different?

I just realized you could have a different node count versus relationship count, if a single node has multiple relationships to the given node that you are counting for. Is this a use case for you? If so, the query would have to get the distinct related nodes to tally your count. You can use the following for a given 'n'. It would not be constant time, since each related node would have to be interrogated to get the distinct list to count.

count{match(n)--(m) return distinct m}

Yep this is exactly my use case.
Thanks for the answer, I really need constant time for performance reasons so I guess I'll work around it for now !