hardfork
hardfork

Reputation: 3261

What's the best way to find out if there is a path between 2 nodes in Neo4j?

I have a Neo4j project with 100k nodes and 5m relations. My problem: An algorithm like "shortest path" takes 2-4ms to find the shortest path.

MATCH p = shortestPath((p1:Person{nickname:"sievers_amara"})-
[:follows*..5]->(p2:Person{nickname:"burghardt_giulia"}))
WHERE p1 <> p2
RETURN p

But my algorithm to find out if there is a path between 2 nodes takes around 200ms... It should be harder to find the shortest path, than finding out if there is a path or not... This is my code to find out if there is a path:

MATCH p=(p1:Person{nickname:"sievers_amara"})-[r:follows*1..5]->(p2:Person{nickname:"burghardt_giulia"})
WHERE p1 <> p2
RETURN p LIMIT 1

What can I improve?

Edit: Putting PROFILE in front of my "is there a path" query results in: enter image description here

Upvotes: 0

Views: 85

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

shortestPath() uses breadth-first for expansion, so it's using the quickest means to detect that a path exists, and it doesn't continue expanding once the first path is found.

Variable-length expansions use depth-first expansion, so even if there is a very short path to the node in question, there's no guarantee that short path will be explored first, so in this case many paths are being tried (and found to not match) before the first match is found (and the path for that match may not be the shortest path at all).

Upvotes: 3

Related Questions