Yesterday we got a question on Discord
Hi folks,
I am searching for some way to expand a graph from one source node like with Apoc.path.expand. But I want to filter on each depth depending on properties.
For example, I want to expand a graph on each depth further with only the top 5 page rank nodes. Does someone have suggestions?
in cypher you an express conditions on the relationships or nodes of a path or oder paths by sum of their weights.
MATCH path = (a:Node {id:$id})-[:REL*..5]->(end)
RETURN path, apoc.coll.sum([r in relationships(path) | r.weight]) as weigth
you could invert the weight and use dijkstra?
you can also add a gh issue for apoc.path.expand
to support filtering / costs
or write your own proc. feel free to add a gh issue
Thank you for your response
I think this approach want work for me.
I want to expand my subgraph iteratively.
For example:
-
Depth 1: Top 5 Nodes with the highest PageRank
-
Depth 2: Expand from each Node of Depth 1 respectively the top 5 Nodes with the highest PageRank …
With your approach I would get in a global scope.
Do you have any further suggestions, actually I am using a query with the collect() and unwind() trick, but it doesn’t really satisfy my problem?
it doesn’t do it recursively.
So either you have to spell out each level in the cypher query, e.g. with subqueries, or use a procedure to use the TraversalAPI for full control.
Here is an example for 3 levels:
MATCH (a:Node) WITH a ORDER BY a.pagerank DESC LIMIT 5
CALL { WITH a
MATCH (a)-[:NEXT]->(b) WITH b ORDER BY b.pagerank DESC LIMIT 5
CALL { WITH b
MATCH (b)-[:NEXT]->(c) WITH c ORDER BY c.pagerank DESC LIMIT 5
RETURN c
}
RETURN b,c
}
RETURN a,b,c
The other option you could try is to get all paths and then ORDER the result by the PR of the node on posX and limit to 5^levels
MATCH (a:Node)-[:NEXT*3]->(c) // or (a:Node)-[:NEXT]->(b)-[:NEXT]->(c)
RETURN a, nodes(path)[1] as b, c
ORDER BY a.pagerank DESC, b.pagerank DESC, c.pagerank DESC
LIMIT 125 // 5^3