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/245/paths
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/240", "max_depth" : 3, "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "shortestPath" }
Example response
200:
OK
Content-Type:
application/json
[ { "start" : "http://localhost:7474/db/data/node/245", "nodes" : [ "http://localhost:7474/db/data/node/245", "http://localhost:7474/db/data/node/244", "http://localhost:7474/db/data/node/240" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/124", "http://localhost:7474/db/data/relationship/133" ], "end" : "http://localhost:7474/db/data/node/240" }, { "start" : "http://localhost:7474/db/data/node/245", "nodes" : [ "http://localhost:7474/db/data/node/245", "http://localhost:7474/db/data/node/241", "http://localhost:7474/db/data/node/240" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/125", "http://localhost:7474/db/data/relationship/131" ], "end" : "http://localhost:7474/db/data/node/240" } ]
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/252/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/247", "max_depth" : 3, "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "shortestPath" }
Example response
200:
OK
Content-Type:
application/json
{ "start" : "http://localhost:7474/db/data/node/252", "nodes" : [ "http://localhost:7474/db/data/node/252", "http://localhost:7474/db/data/node/248", "http://localhost:7474/db/data/node/247" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/135", "http://localhost:7474/db/data/relationship/141" ], "end" : "http://localhost:7474/db/data/node/247" }
Example request
POST
http://localhost:7474/db/data/node/267/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/270", "cost_property" : "cost", "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "dijkstra" }
Example response
200:
OK
Content-Type:
application/json
{ "weight" : 2.0, "start" : "http://localhost:7474/db/data/node/267", "nodes" : [ "http://localhost:7474/db/data/node/267", "http://localhost:7474/db/data/node/268", "http://localhost:7474/db/data/node/270" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/157", "http://localhost:7474/db/data/relationship/158" ], "end" : "http://localhost:7474/db/data/node/270" }
Example request
POST
http://localhost:7474/db/data/node/258/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/261", "cost_property" : "cost", "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "dijkstra" }
Example response
200:
OK
Content-Type:
application/json
{ "weight" : 6.0, "start" : "http://localhost:7474/db/data/node/258", "nodes" : [ "http://localhost:7474/db/data/node/258", "http://localhost:7474/db/data/node/259", "http://localhost:7474/db/data/node/256", "http://localhost:7474/db/data/node/257", "http://localhost:7474/db/data/node/254", "http://localhost:7474/db/data/node/255", "http://localhost:7474/db/data/node/261" ], "length" : 6, "relationships" : [ "http://localhost:7474/db/data/relationship/144", "http://localhost:7474/db/data/relationship/146", "http://localhost:7474/db/data/relationship/148", "http://localhost:7474/db/data/relationship/151", "http://localhost:7474/db/data/relationship/153", "http://localhost:7474/db/data/relationship/154" ], "end" : "http://localhost:7474/db/data/node/261" }
Copyright © 2012 Neo Technology