Djikstra with Cost Cypher Implementation?

34 views
Skip to first unread message

Ben Campbell

unread,
Sep 16, 2016, 9:11:48 AM9/16/16
to Neo4j
Anyone know when the Djikstra/A* with cost implementation is being implemented in cypher? It appears to have been accessible from the Web API for a long time, but no easy way to use it when using bolt? 

Is there a reliable workaround? I basically want to call shortestPath but specify a cost parameter stored on each relationship. I've seen workarounds on stackoverflow, but these appear to work after obtaining all the results so become inefficient? 

Michael Hunger

unread,
Sep 16, 2016, 11:39:25 AM9/16/16
to ne...@googlegroups.com
Hi Ben,

it is exposed as user defined procedure in the apoc library.


Install Instructions (just download and drop the jar into NEO4J_HOME/plugins) are in the readme: 



apoc.algo.dijkstra(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 'distance') YIELD path, weight

run dijkstra with relationship property name as cost function

apoc.algo.dijkstraWithDefaultWeight(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 'distance', 10) YIELD path, weight

run dijkstra with relationship property name as cost function and a default weight if the property does not exist




In Neo4j 3.0+ shortest path functions pull in predicates into the evaluation, so something like

 WHERE reduce(sum = 0 , x in rels(path) |  sum + x.weight )  < 100 

should work




On Fri, Sep 16, 2016 at 12:47 PM, Ben Campbell <campb...@hotmail.com> wrote:
Anyone know when the Djikstra/A* with cost implementation is being implemented in cypher? It appears to have been accessible from the Web API for a long time, but no easy way to use it when using bolt? 

Is there a reliable workaround? I basically want to call shortestPath but specify a cost parameter stored on each relationship. I've seen workarounds on stackoverflow, but these appear to work after obtaining all the results so become inefficient? 

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Campbell

unread,
Sep 19, 2016, 5:59:21 PM9/19/16
to Neo4j
Hi Michael,

This looks just the ticket. Having a look through the documentation it seems to solve a lot of my Neo4j Cypher woes, wish I had heard of it earlier.

Cheers,

Ben
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.

Michael Hunger

unread,
Sep 19, 2016, 6:05:37 PM9/19/16
to ne...@googlegroups.com
Glad it helped :)

To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages