How do I perform the functions of shortestPath()
and allShortestPaths()
in py2neo?
In Cypher, I'd execute something like:
START beginning=node(4), end=node(452)
MATCH p = shortestPath(beginning-[*..500]-end)
RETURN p
I've tried what I thought was the equivalent (below), but this doesn't work (these relationships work in cypher, and the node_* objects are indeed the correct nodes
>>> rels = list(graph_db.match(start_node=node_4, end_node=node_452))
>>> rels
[]
I don't want to steal jjaderberg's comment but here is how you could run your Cypher query with py2neo. As far as I know graph algorithms are not implemented.
query_string = "START beginning=node(4), end=node(452)
MATCH p = shortestPath(beginning-[*..500]-end)
RETURN p"
result = neo4j.CypherQuery(graph_db, query_string).execute()
for r in result:
print type(r) # r is a py2neo.util.Record object
print type(r.p) # p is a py2neo.neo4j.Path object
You can then get what you need from your path as described here: http://book.py2neo.org/en/latest/paths/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With