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/82/paths
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/77", "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/82", "nodes" : [ "http://localhost:7474/db/data/node/82", "http://localhost:7474/db/data/node/81", "http://localhost:7474/db/data/node/77" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/59", "http://localhost:7474/db/data/relationship/68" ], "end" : "http://localhost:7474/db/data/node/77" }, { "start" : "http://localhost:7474/db/data/node/82", "nodes" : [ "http://localhost:7474/db/data/node/82", "http://localhost:7474/db/data/node/78", "http://localhost:7474/db/data/node/77" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/60", "http://localhost:7474/db/data/relationship/66" ], "end" : "http://localhost:7474/db/data/node/77" } ]
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/66/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/61", "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/66", "nodes" : [ "http://localhost:7474/db/data/node/66", "http://localhost:7474/db/data/node/62", "http://localhost:7474/db/data/node/61" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/37", "http://localhost:7474/db/data/relationship/43" ], "end" : "http://localhost:7474/db/data/node/61" }
Example request
POST
http://localhost:7474/db/data/node/72/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/75", "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/72", "nodes" : [ "http://localhost:7474/db/data/node/72", "http://localhost:7474/db/data/node/73", "http://localhost:7474/db/data/node/75" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/46", "http://localhost:7474/db/data/relationship/47" ], "end" : "http://localhost:7474/db/data/node/75" }
Example request
POST
http://localhost:7474/db/data/node/56/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/59", "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/56", "nodes" : [ "http://localhost:7474/db/data/node/56", "http://localhost:7474/db/data/node/57", "http://localhost:7474/db/data/node/54", "http://localhost:7474/db/data/node/55", "http://localhost:7474/db/data/node/52", "http://localhost:7474/db/data/node/53", "http://localhost:7474/db/data/node/59" ], "length" : 6, "relationships" : [ "http://localhost:7474/db/data/relationship/23", "http://localhost:7474/db/data/relationship/25", "http://localhost:7474/db/data/relationship/27", "http://localhost:7474/db/data/relationship/30", "http://localhost:7474/db/data/relationship/32", "http://localhost:7474/db/data/relationship/33" ], "end" : "http://localhost:7474/db/data/node/59" }
Copyright © 2014 Neo Technology