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/60/paths
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/55", "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/60", "nodes" : [ "http://localhost:7474/db/data/node/60", "http://localhost:7474/db/data/node/59", "http://localhost:7474/db/data/node/55" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/74", "http://localhost:7474/db/data/relationship/83" ], "end" : "http://localhost:7474/db/data/node/55" }, { "start" : "http://localhost:7474/db/data/node/60", "nodes" : [ "http://localhost:7474/db/data/node/60", "http://localhost:7474/db/data/node/56", "http://localhost:7474/db/data/node/55" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/75", "http://localhost:7474/db/data/relationship/81" ], "end" : "http://localhost:7474/db/data/node/55" } ]
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/44/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/39", "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/44", "nodes" : [ "http://localhost:7474/db/data/node/44", "http://localhost:7474/db/data/node/40", "http://localhost:7474/db/data/node/39" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/52", "http://localhost:7474/db/data/relationship/58" ], "end" : "http://localhost:7474/db/data/node/39" }
Example request
POST
http://localhost:7474/db/data/node/50/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/53", "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/50", "nodes" : [ "http://localhost:7474/db/data/node/50", "http://localhost:7474/db/data/node/51", "http://localhost:7474/db/data/node/53" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/61", "http://localhost:7474/db/data/relationship/62" ], "end" : "http://localhost:7474/db/data/node/53" }
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/37", "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/34", "nodes" : [ "http://localhost:7474/db/data/node/34", "http://localhost:7474/db/data/node/35", "http://localhost:7474/db/data/node/32", "http://localhost:7474/db/data/node/33", "http://localhost:7474/db/data/node/30", "http://localhost:7474/db/data/node/31", "http://localhost:7474/db/data/node/37" ], "length" : 6, "relationships" : [ "http://localhost:7474/db/data/relationship/38", "http://localhost:7474/db/data/relationship/40", "http://localhost:7474/db/data/relationship/42", "http://localhost:7474/db/data/relationship/45", "http://localhost:7474/db/data/relationship/47", "http://localhost:7474/db/data/relationship/48" ], "end" : "http://localhost:7474/db/data/node/37" }
Copyright © 2013 Neo Technology