cancel
Showing results for 
Search instead for 
Did you mean: 

Order the result by the number of relationship of each node in a subgraph

chandrautama_lu
Node Link

I have a directed multigraph. Using the following query, I managed to get the set of nodes that is connected with the node n1_34

MATCH (n1:Node{uuid: "n1_34"}) -[r]- (n2:Node) RETURN n2, r

This yields the nodes n1_1187, n2_2280, n2_1834 and n2_722 and their relationship as seen in the figure.
2X_0_0266e32c1fdc1f4557f69e938630d66bdbad3b23.png

From here I need to sort the result based on the number their relationships within this subgraph. That means, n1_1187 should be on top with 4 relationships while the others comes after that with 1 relationship.

I have tried and reapplied the solution provided by related question: (Stackoverflow) Extract subgraph from Neo4j graph with Cypher and Count relationship on each nodes. None of these works. The former gives the same result as my original query and the latter gives all the relationships that each node has within the entire graph.

How can I get the relationships within the subgraph?

1 ACCEPTED SOLUTION

Sometimes you just need someone to question your logic to help you. I have been thinking about the solution for days. Thank you Cobra.

Here is the query I need (in case anyone needs it):

MATCH (n1:Node{uuid: "n1_34"}) -- (n2:Node)
MATCH (n1) -- () -[r]- (n2) RETURN n2.uuid AS uuid, count(r) AS n
ORDER BY n DESC

I will surely take a look at the article if I need to project a subgraph. Thanks again.

View solution in original post

9 REPLIES 9

Cobra
Ninja
Ninja

Hello @chandrautama.lucky and welcome to the Neo4j community

MATCH (n1:Node{uuid: "n1_34"})-[r]-(n2:Node)
RETURN n2.uuid AS uuid, count(r) AS n
ORDER BY n DESC

Regards,
Cobra

Thank you, Cobra but this is not what I need. count(r) gives the number of relationship r between n1 (node with uuid "n1_34") and the n2 nodes.

Maybe I am not very clear in the question. Here is the subgraph with n1, n2 and r as the results:

2X_6_6f3e714b46dcf781fbcc5eccfbcfbe02f77531fc.png

Seeing the figure, you can see why count(r) isn't the one I am looking for and will give the number relationship between n1 and each n2 nodes. Hence, 1 for each n2 nodes.

Maybe you have an alternative?

Cobra
Ninja
Ninja

Can you write the result you would like to get based on your last screenshot?

OK. This my question: Given the node "n1_34" find

  1. The neighbouring nodes and

  2. The relationship it has among themselves excluding the relationship with "n1_34"

The result I expected

uuid    | n 
---------------
n1_1187 | 4
n2_2280 | 1
n2_1834 | 1
n2_722  | 1
n2_932  | 1

With the query MATCH (n1:Node{uuid: "n1_34"}) -[r]- (n2:Node) RETURN n2, r , I can get the neighbours. But then somehow I need to traverse each node in context of the subgraph. I dont know how.

Cobra
Ninja
Ninja

I'm sorry, but the query I gave you should return this result.

But then somehow I need to traverse each node in context of the subgraph.

What do you mean?

I'm sorry, but the query I gave you should return this result.

I tried it. But your query gives me

uuid    | n 
---------------
n2_722  | 1
n2_1834 | 1
n2_932  | 1
n2_2280 | 1
n1_1187 | 1

I just think I need to traverse the n2 since I need their relationships not the n1's relationship. I once thought this following query will work: MATCH (n1:Node{uuid: "n1_34"}) -- (n2:Node) -[r]- () RETURN n2.uuid AS uuid, count(r) AS n ORDER BY n DESC. But this gives me the number of the relationship of the n2 nodes in an entire graph like this:

uuid    | n 
---------------
n2_1834 |1672|
n2_2280 | 170|
n2_722  |  68|
n2_932  |  54|
n1_1187 |   4|

Hence I said I need the traversal in context of the subgraph.

Cobra
Ninja
Ninja

Oh I get it , you should have a look at the documentation here, it will should you how to make projections

Sometimes you just need someone to question your logic to help you. I have been thinking about the solution for days. Thank you Cobra.

Here is the query I need (in case anyone needs it):

MATCH (n1:Node{uuid: "n1_34"}) -- (n2:Node)
MATCH (n1) -- () -[r]- (n2) RETURN n2.uuid AS uuid, count(r) AS n
ORDER BY n DESC

I will surely take a look at the article if I need to project a subgraph. Thanks again.

No problem

You could optimise a bit I think:

MATCH (n1:Node{uuid: "n1_34"})-()-[r]-(n2:Node)
RETURN n2.uuid AS uuid, count(r) AS n
ORDER BY n DESC
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.