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/206/paths
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/201", "max_depth" : 3, "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "shortestPath" }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
[ { "start" : "http://localhost:7474/db/data/node/206", "nodes" : [ "http://localhost:7474/db/data/node/206", "http://localhost:7474/db/data/node/205", "http://localhost:7474/db/data/node/201" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/162", "http://localhost:7474/db/data/relationship/171" ], "end" : "http://localhost:7474/db/data/node/201" }, { "start" : "http://localhost:7474/db/data/node/206", "nodes" : [ "http://localhost:7474/db/data/node/206", "http://localhost:7474/db/data/node/202", "http://localhost:7474/db/data/node/201" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/163", "http://localhost:7474/db/data/relationship/169" ], "end" : "http://localhost:7474/db/data/node/201" } ]
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/190/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/185", "max_depth" : 3, "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "shortestPath" }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
{ "start" : "http://localhost:7474/db/data/node/190", "nodes" : [ "http://localhost:7474/db/data/node/190", "http://localhost:7474/db/data/node/186", "http://localhost:7474/db/data/node/185" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/140", "http://localhost:7474/db/data/relationship/146" ], "end" : "http://localhost:7474/db/data/node/185" }
Example request
POST
http://localhost:7474/db/data/node/196/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/199", "cost_property" : "cost", "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "dijkstra" }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
{ "weight" : 2.0, "start" : "http://localhost:7474/db/data/node/196", "nodes" : [ "http://localhost:7474/db/data/node/196", "http://localhost:7474/db/data/node/197", "http://localhost:7474/db/data/node/199" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/149", "http://localhost:7474/db/data/relationship/150" ], "end" : "http://localhost:7474/db/data/node/199" }
Example request
POST
http://localhost:7474/db/data/node/180/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/183", "cost_property" : "cost", "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "dijkstra" }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
{ "weight" : 6.0, "start" : "http://localhost:7474/db/data/node/180", "nodes" : [ "http://localhost:7474/db/data/node/180", "http://localhost:7474/db/data/node/181", "http://localhost:7474/db/data/node/178", "http://localhost:7474/db/data/node/179", "http://localhost:7474/db/data/node/176", "http://localhost:7474/db/data/node/177", "http://localhost:7474/db/data/node/183" ], "length" : 6, "relationships" : [ "http://localhost:7474/db/data/relationship/126", "http://localhost:7474/db/data/relationship/128", "http://localhost:7474/db/data/relationship/130", "http://localhost:7474/db/data/relationship/133", "http://localhost:7474/db/data/relationship/135", "http://localhost:7474/db/data/relationship/136" ], "end" : "http://localhost:7474/db/data/node/183" }
Copyright © 2014 Neo Technology