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/253/paths
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/248", "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/253", "nodes" : [ "http://localhost:7474/db/data/node/253", "http://localhost:7474/db/data/node/252", "http://localhost:7474/db/data/node/248" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/126", "http://localhost:7474/db/data/relationship/135" ], "end" : "http://localhost:7474/db/data/node/248" }, { "start" : "http://localhost:7474/db/data/node/253", "nodes" : [ "http://localhost:7474/db/data/node/253", "http://localhost:7474/db/data/node/249", "http://localhost:7474/db/data/node/248" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/127", "http://localhost:7474/db/data/relationship/133" ], "end" : "http://localhost:7474/db/data/node/248" } ]
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/260/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/255", "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/260", "nodes" : [ "http://localhost:7474/db/data/node/260", "http://localhost:7474/db/data/node/256", "http://localhost:7474/db/data/node/255" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/137", "http://localhost:7474/db/data/relationship/143" ], "end" : "http://localhost:7474/db/data/node/255" }
Example request
POST
http://localhost:7474/db/data/node/275/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/278", "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/275", "nodes" : [ "http://localhost:7474/db/data/node/275", "http://localhost:7474/db/data/node/276", "http://localhost:7474/db/data/node/278" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/159", "http://localhost:7474/db/data/relationship/160" ], "end" : "http://localhost:7474/db/data/node/278" }
Example request
POST
http://localhost:7474/db/data/node/266/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/269", "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/266", "nodes" : [ "http://localhost:7474/db/data/node/266", "http://localhost:7474/db/data/node/267", "http://localhost:7474/db/data/node/264", "http://localhost:7474/db/data/node/265", "http://localhost:7474/db/data/node/262", "http://localhost:7474/db/data/node/263", "http://localhost:7474/db/data/node/269" ], "length" : 6, "relationships" : [ "http://localhost:7474/db/data/relationship/146", "http://localhost:7474/db/data/relationship/148", "http://localhost:7474/db/data/relationship/150", "http://localhost:7474/db/data/relationship/153", "http://localhost:7474/db/data/relationship/155", "http://localhost:7474/db/data/relationship/156" ], "end" : "http://localhost:7474/db/data/node/269" }
Copyright © 2012 Neo Technology