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/83/paths
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/78", "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/83", "nodes" : [ "http://localhost:7474/db/data/node/83", "http://localhost:7474/db/data/node/82", "http://localhost:7474/db/data/node/78" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/58", "http://localhost:7474/db/data/relationship/67" ], "end" : "http://localhost:7474/db/data/node/78" }, { "start" : "http://localhost:7474/db/data/node/83", "nodes" : [ "http://localhost:7474/db/data/node/83", "http://localhost:7474/db/data/node/79", "http://localhost:7474/db/data/node/78" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/59", "http://localhost:7474/db/data/relationship/65" ], "end" : "http://localhost:7474/db/data/node/78" } ]
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/67/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/62", "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/67", "nodes" : [ "http://localhost:7474/db/data/node/67", "http://localhost:7474/db/data/node/63", "http://localhost:7474/db/data/node/62" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/36", "http://localhost:7474/db/data/relationship/42" ], "end" : "http://localhost:7474/db/data/node/62" }
Example request
POST
http://localhost:7474/db/data/node/73/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/76", "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/73", "nodes" : [ "http://localhost:7474/db/data/node/73", "http://localhost:7474/db/data/node/74", "http://localhost:7474/db/data/node/76" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/45", "http://localhost:7474/db/data/relationship/46" ], "end" : "http://localhost:7474/db/data/node/76" }
Example request
POST
http://localhost:7474/db/data/node/57/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/60", "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/57", "nodes" : [ "http://localhost:7474/db/data/node/57", "http://localhost:7474/db/data/node/58", "http://localhost:7474/db/data/node/55", "http://localhost:7474/db/data/node/56", "http://localhost:7474/db/data/node/53", "http://localhost:7474/db/data/node/54", "http://localhost:7474/db/data/node/60" ], "length" : 6, "relationships" : [ "http://localhost:7474/db/data/relationship/22", "http://localhost:7474/db/data/relationship/24", "http://localhost:7474/db/data/relationship/26", "http://localhost:7474/db/data/relationship/29", "http://localhost:7474/db/data/relationship/31", "http://localhost:7474/db/data/relationship/32" ], "end" : "http://localhost:7474/db/data/node/60" }
Copyright © 2013 Neo Technology