Neo4j comes with a number of built-in graph algorithms. They are performed from a start node. The traversal is controlled by the URI and the body sent with the request.
The algorithm to choose. If not set, default is shortestPath
.
algorithm
can have one of these values:
shortestPath
allSimplePaths
allPaths
dijkstra
(optional with cost_property
and default_cost
parameters)
1
.
The shortestPath
algorithm can find multiple paths between the same
nodes, like in this example.
Example request
POST
http://localhost:7474/db/data/node/191/paths
Accept:
application/json
Content-Type:
application/json
{ "to":"http://localhost:7474/db/data/node/186", "max_depth":3, "relationships":{ "type":"to", "direction":"out" }, "algorithm":"shortestPath" }
Example response
200:
OK
Content-Type:
application/json
[ { "start" : "http://localhost:7474/db/data/node/191", "nodes" : [ "http://localhost:7474/db/data/node/191", "http://localhost:7474/db/data/node/187", "http://localhost:7474/db/data/node/186" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/85", "http://localhost:7474/db/data/relationship/91" ], "end" : "http://localhost:7474/db/data/node/186" }, { "start" : "http://localhost:7474/db/data/node/191", "nodes" : [ "http://localhost:7474/db/data/node/191", "http://localhost:7474/db/data/node/190", "http://localhost:7474/db/data/node/186" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/84", "http://localhost:7474/db/data/relationship/93" ], "end" : "http://localhost:7474/db/data/node/186" } ]
If no path algorithm is specified, a ShortestPath
algorithm with a max
depth of 1 will be chosen. In this example, the max_depth
is set to 3
in order to find the shortest path between 3 linked nodes.
Example request
POST
http://localhost:7474/db/data/node/198/path
Accept:
application/json
Content-Type:
application/json
{ "to":"http://localhost:7474/db/data/node/193", "max_depth":3, "relationships":{ "type":"to", "direction":"out" }, "algorithm":"shortestPath" }
Example response
200:
OK
Content-Type:
application/json
{ "start" : "http://localhost:7474/db/data/node/198", "nodes" : [ "http://localhost:7474/db/data/node/198", "http://localhost:7474/db/data/node/194", "http://localhost:7474/db/data/node/193" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/95", "http://localhost:7474/db/data/relationship/101" ], "end" : "http://localhost:7474/db/data/node/193" }
Example request
POST
http://localhost:7474/db/data/node/213/path
Accept:
application/json
Content-Type:
application/json
{ "to":"http://localhost:7474/db/data/node/216", "cost_property":"cost", "relationships":{ "type":"to", "direction":"out" }, "algorithm":"dijkstra" }
Example response
200:
OK
Content-Type:
application/json
{ "weight" : 2.0, "start" : "http://localhost:7474/db/data/node/213", "nodes" : [ "http://localhost:7474/db/data/node/213", "http://localhost:7474/db/data/node/214", "http://localhost:7474/db/data/node/216" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/117", "http://localhost:7474/db/data/relationship/118" ], "end" : "http://localhost:7474/db/data/node/216" }
Example request
POST
http://localhost:7474/db/data/node/204/path
Accept:
application/json
Content-Type:
application/json
{ "to":"http://localhost:7474/db/data/node/207", "cost_property":"cost", "relationships":{ "type":"to", "direction":"out" }, "algorithm":"dijkstra" }
Example response
200:
OK
Content-Type:
application/json
{ "weight" : 6.0, "start" : "http://localhost:7474/db/data/node/204", "nodes" : [ "http://localhost:7474/db/data/node/204", "http://localhost:7474/db/data/node/205", "http://localhost:7474/db/data/node/202", "http://localhost:7474/db/data/node/203", "http://localhost:7474/db/data/node/200", "http://localhost:7474/db/data/node/201", "http://localhost:7474/db/data/node/207" ], "length" : 6, "relationships" : [ "http://localhost:7474/db/data/relationship/104", "http://localhost:7474/db/data/relationship/106", "http://localhost:7474/db/data/relationship/108", "http://localhost:7474/db/data/relationship/111", "http://localhost:7474/db/data/relationship/113", "http://localhost:7474/db/data/relationship/114" ], "end" : "http://localhost:7474/db/data/node/207" }
Copyright © 2011 Neo Technology