How to get degree of a node while filtering using MATCH in neo4j
Asked Answered
I

3

7

I have a graph with 4 levels. While filtering using MATCH, how can i get the "degree" of a node? I am always getting a "degree" of 1.

Here is my query:

MATCH (k)-[r*]->(n:ABC)
WITH k,r,n,count(k) as degree
WHERE k.Value='30 ' AND degree > 1
RETURN n,r,k,degree;
Infundibulum answered 25/9, 2015 at 5:29 Comment(5)
Please share your queryEne
The query is : MATCH (k)-[r*]->(n:ABC) WITH k,r,n,count(k) as degree WHERE k.Value='30 ' AND degree > 1 return n,r,k,degreeInfundibulum
I have added the query to your question. Can you explain what exactly you mean by "degree"?Nitrobenzene
It is easy to explain why you currently always get 1, but to figure out how to get the "degree" value you want, we need to know what that term means to you. Is it (a) the number of distinct k nodes, (b) the number of relationships that each distinct k node has, (c) the number of paths that each distinct k node shares with each distinct n node, or something else?Nitrobenzene
By the term degree I meant that k distinct nodes which connect to n-node and satisfies the where condition.Infundibulum
N
12

You are getting count of 1 because you aggregated over all 3, start-node, end-node, and all relationships in the path.

This is the most efficient way.

MATCH (k)
WITH k, size((k)-[:TYPE]->()) as degree
WHERE k.Value='30 ' AND degree > 1
MATCH (k)-[r:TYPE]->(n:ABC)
RETURN n,r,k,degree;
Nguyetni answered 26/9, 2015 at 20:7 Comment(8)
Micheal could you point to the Size() function in the Neo4j docs ?Yockey
Docs here: neo4j.com/docs/developer-manual/current/cypher/#functions-sizePhenyl
neo4j.com/docs/developer-manual/current/cypher/functions/scalar/…Bumgardner
1. Yes this is much more efficient. Since number of edges are stored within the node. If one node is connected to 1000 nodes, by doing this you don't need to actually access the other 1000 nodes. 2. But this is slightly different from what the OP want. This is the number of distinctive EDGES from a node, the OP want the number of distinctive NODES connected from a node.Luteous
@Luteous Hmm, I think in graph theory, the degree (or valency) of a vertex of a graph is the number of edges incident to the vertex, with loops counted twice - and OP is asking for degree, not the number of NODES.Malevolent
@Malevolent you are right. I was only looking at the OP's original code rather than reading his title.Luteous
I have apretty large graph 4m nodes and 40m edges - this WITH .., size.. as degree line maxes my CPUs and ultimately crashes neo4jKibitka
I want to get count number of edges by a property of edge. For example: get number of edges for each node in which "edge.x > 10" x is a property of edgeParian
M
2

Neo4j size function is now deprecated, and apoc.node.degree is recommended instead;

MATCH (k)-[r*]->(n:ABC)
WITH k,r,n, apoc.node.degree(k) as degree
WHERE k.Value='30 ' AND degree > 1
RETURN n,r,k,degree;

NB You will need APOC to be installed on your local Neo4j instance

Malevolent answered 5/10, 2022 at 14:39 Comment(0)
G
1

More information would be helpful, but in general you can get the degree of a node by doing something like:

MATCH (n)--(other)
WHERE n.id = {id}
RETURN count(other)

If you want to find degrees for many nodes you can leave out the WHERE or specify a more generic query:

MATCH (n)--(other)
WHERE n.property = {value}
RETURN n, count(other)
Gamopetalous answered 25/9, 2015 at 6:36 Comment(1)
Using id() is certainly the way to get the Neo4j internal ID, but since those IDs can be recycled I generally use a property (in this case id) as an exampleGamopetalous

© 2022 - 2024 — McMap. All rights reserved.