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/50/paths
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/45", "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/50", "nodes" : [ "http://localhost:7474/db/data/node/50", "http://localhost:7474/db/data/node/49", "http://localhost:7474/db/data/node/45" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/50", "http://localhost:7474/db/data/relationship/59" ], "end" : "http://localhost:7474/db/data/node/45" }, { "start" : "http://localhost:7474/db/data/node/50", "nodes" : [ "http://localhost:7474/db/data/node/50", "http://localhost:7474/db/data/node/46", "http://localhost:7474/db/data/node/45" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/51", "http://localhost:7474/db/data/relationship/57" ], "end" : "http://localhost:7474/db/data/node/45" } ]
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/34/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/29", "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/34", "nodes" : [ "http://localhost:7474/db/data/node/34", "http://localhost:7474/db/data/node/30", "http://localhost:7474/db/data/node/29" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/28", "http://localhost:7474/db/data/relationship/34" ], "end" : "http://localhost:7474/db/data/node/29" }
Example request
POST
http://localhost:7474/db/data/node/40/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/43", "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/40", "nodes" : [ "http://localhost:7474/db/data/node/40", "http://localhost:7474/db/data/node/41", "http://localhost:7474/db/data/node/43" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/37", "http://localhost:7474/db/data/relationship/38" ], "end" : "http://localhost:7474/db/data/node/43" }
Example request
POST
http://localhost:7474/db/data/node/24/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/27", "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/24", "nodes" : [ "http://localhost:7474/db/data/node/24", "http://localhost:7474/db/data/node/25", "http://localhost:7474/db/data/node/22", "http://localhost:7474/db/data/node/23", "http://localhost:7474/db/data/node/20", "http://localhost:7474/db/data/node/21", "http://localhost:7474/db/data/node/27" ], "length" : 6, "relationships" : [ "http://localhost:7474/db/data/relationship/14", "http://localhost:7474/db/data/relationship/16", "http://localhost:7474/db/data/relationship/18", "http://localhost:7474/db/data/relationship/21", "http://localhost:7474/db/data/relationship/23", "http://localhost:7474/db/data/relationship/24" ], "end" : "http://localhost:7474/db/data/node/27" }
Copyright © 2014 Neo Technology