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/251/paths
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/246", "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/251", "nodes" : [ "http://localhost:7474/db/data/node/251", "http://localhost:7474/db/data/node/250", "http://localhost:7474/db/data/node/246" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/126", "http://localhost:7474/db/data/relationship/135" ], "end" : "http://localhost:7474/db/data/node/246" }, { "start" : "http://localhost:7474/db/data/node/251", "nodes" : [ "http://localhost:7474/db/data/node/251", "http://localhost:7474/db/data/node/247", "http://localhost:7474/db/data/node/246" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/127", "http://localhost:7474/db/data/relationship/133" ], "end" : "http://localhost:7474/db/data/node/246" } ]
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/258/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/253", "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/258", "nodes" : [ "http://localhost:7474/db/data/node/258", "http://localhost:7474/db/data/node/254", "http://localhost:7474/db/data/node/253" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/137", "http://localhost:7474/db/data/relationship/143" ], "end" : "http://localhost:7474/db/data/node/253" }
Example request
POST
http://localhost:7474/db/data/node/273/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/276", "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/273", "nodes" : [ "http://localhost:7474/db/data/node/273", "http://localhost:7474/db/data/node/274", "http://localhost:7474/db/data/node/276" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/159", "http://localhost:7474/db/data/relationship/160" ], "end" : "http://localhost:7474/db/data/node/276" }
Example request
POST
http://localhost:7474/db/data/node/264/path
Accept:
application/json
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/267", "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/264", "nodes" : [ "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/260", "http://localhost:7474/db/data/node/261", "http://localhost:7474/db/data/node/267" ], "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/267" }
Copyright © 2012 Neo Technology