Reputation: 6374
How to do Breadth-first Search in a Paginated Manner using Neo4J Cypher?
For example, I have the following vertices
Vertex A
, Vertex B1, B2, B3, ...., Bn
(m B vertices), Vertex C1, C2, C3, ..., Cn
(n C vertices) and Vertex D1, D2, D3, ..., Dn
(k D vertices)
Now A
is the root and A's children are all B vertices and for each B vertex there are n C vertices as children and finally for each C vertex there are K D vertices as children. so the direction is simply top to bottom.
Now I want to find out all the vertices starting from say Vertex C1 and do BFS in a paginated manner because the Graph is huge. any idea how to accomplish this using Cypher?
Upvotes: 1
Views: 262
Reputation: 30417
Keep in mind that ordering is not guaranteed unless you have an ORDER BY clause.
Also, Cypher expansions use DFS by default, only shortestPath() and shortestPath() usage does BFS, and these aren't applicable for this case.
You can have more control over how the expansion behaves using the traversal API, which can be used from a stored procedure. Thankfully, APOC Procedures already has that done for you in its path expander procs, and default to using BFS.
You'll want to ensure you have the relationship type and directions correct when using the procedure.
Let's say that your graph only has outgoing relationships from root to leaf. In that case here's an example of usage:
MATCH (start:Vertex {name:$startVertex})
CALL apoc.path.subgraphNodes(start, {relationshipFilter:'>'}) YIELD node
RETURN node
SKIP $skip
LIMIT 100
You can pass startVertex
and skip
as parameters when calling the query.
Upvotes: 3