Abhijit Sarkar
Abhijit Sarkar

Reputation: 24518

Node ranking in Neo4J Cypher

This is a follow up of my question How to do relative node ordering using Neo4J Cypher. I accepted the answer there, but later realized that my use case is more involved. Following is an piece of the graph I'm working with, with the desired node ordering shown in the table. Clearly, I should be ordering by indegree, but for nodes with the same indegree, relationships between them need to be considered (nodes S and A in the graph). Cycles are not allowed.

How can I achieve this? enter image description here

Upvotes: 0

Views: 639

Answers (1)

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24518

The following seems to do the job (thanks to this). I'm new to Cypher and Neo4J, thus, other answers are welcome, as well as comments about the following if it may not work in some case.

MATCH p = allShortestPaths((a:App)-[:DEPENDS_ON*0..10]->(b:App))
WHERE a <> b
WITH b.name as bn, count(p) AS c
WITH COLLECT({name:bn, count:c}) AS paths
MATCH (a:App) WHERE NOT ()-[:DEPENDS_ON]->(a)
WITH paths + COLLECT({name:a.name, count:0}) AS allPaths
UNWIND allPaths AS path
WITH path.name AS name, path.count AS count
RETURN name, count
ORDER BY count DESC

For the aforementioned graph, the query above produces the following results:

╒═════════╤═══════╕
│"name"   │"count"│
╞═════════╪═══════╡
│"C"      │4      │
├─────────┼───────┤
│"S"      │3      │
├─────────┼───────┤
│"A"      │1      │
├─────────┼───────┤
│"GW"     │0      │
├─────────┼───────┤
│"GC"     │0      │
└─────────┴───────┘

Upvotes: 1

Related Questions