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. These are the parameters that can be used:
The algorithm to choose. If not set, default is shortestPath
.
algorithm
can have one of these values:
shortestPath
allSimplePaths
allPaths
dijkstra
(optionally with cost_property
and default_cost
parameters)
shortestPath
, where applicable.
Default is 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/201/paths
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/196", "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/201", "nodes" : [ "http://localhost:7474/db/data/node/201", "http://localhost:7474/db/data/node/200", "http://localhost:7474/db/data/node/196" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/138", "http://localhost:7474/db/data/relationship/147" ], "end" : "http://localhost:7474/db/data/node/196" }, { "start" : "http://localhost:7474/db/data/node/201", "nodes" : [ "http://localhost:7474/db/data/node/201", "http://localhost:7474/db/data/node/197", "http://localhost:7474/db/data/node/196" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/139", "http://localhost:7474/db/data/relationship/145" ], "end" : "http://localhost:7474/db/data/node/196" } ]
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 a maximum of 3 linked nodes.
Example request
POST
http://localhost:7474/db/data/node/194/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/189", "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/194", "nodes" : [ "http://localhost:7474/db/data/node/194", "http://localhost:7474/db/data/node/190", "http://localhost:7474/db/data/node/189" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/129", "http://localhost:7474/db/data/relationship/135" ], "end" : "http://localhost:7474/db/data/node/189" }
This example is running a Dijkstra algorithm over a graph with different
cost properties on different relationships. Note that the request URI
ends with /path
which means a single path is what we want here.
Example request
POST
http://localhost:7474/db/data/node/207/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/204", "cost_property" : "cost", "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "dijkstra" }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
{ "weight" : 1.5, "start" : "http://localhost:7474/db/data/node/207", "nodes" : [ "http://localhost:7474/db/data/node/207", "http://localhost:7474/db/data/node/206", "http://localhost:7474/db/data/node/203", "http://localhost:7474/db/data/node/204" ], "length" : 3, "relationships" : [ "http://localhost:7474/db/data/relationship/149", "http://localhost:7474/db/data/relationship/151", "http://localhost:7474/db/data/relationship/152" ], "end" : "http://localhost:7474/db/data/node/204" }
The following is executing a Dijkstra search on a graph with equal weights on all relationships. This example is included to show the difference when the same graph structure is used, but the path weight is equal to the number of hops.
Example request
POST
http://localhost:7474/db/data/node/213/path
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/210", "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/213", "nodes" : [ "http://localhost:7474/db/data/node/213", "http://localhost:7474/db/data/node/211", "http://localhost:7474/db/data/node/210" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/155", "http://localhost:7474/db/data/relationship/160" ], "end" : "http://localhost:7474/db/data/node/210" }
This example is running a Dijkstra algorithm over a graph with different
cost properties on different relationships. Note that the request URI
ends with /paths
which means we want multiple paths returned, in case
they exist.
Example request
POST
http://localhost:7474/db/data/node/187/paths
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "to" : "http://localhost:7474/db/data/node/184", "cost_property" : "cost", "relationships" : { "type" : "to", "direction" : "out" }, "algorithm" : "dijkstra" }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
[ { "weight" : 1.5, "start" : "http://localhost:7474/db/data/node/187", "nodes" : [ "http://localhost:7474/db/data/node/187", "http://localhost:7474/db/data/node/186", "http://localhost:7474/db/data/node/183", "http://localhost:7474/db/data/node/184" ], "length" : 3, "relationships" : [ "http://localhost:7474/db/data/relationship/122", "http://localhost:7474/db/data/relationship/124", "http://localhost:7474/db/data/relationship/125" ], "end" : "http://localhost:7474/db/data/node/184" }, { "weight" : 1.5, "start" : "http://localhost:7474/db/data/node/187", "nodes" : [ "http://localhost:7474/db/data/node/187", "http://localhost:7474/db/data/node/182", "http://localhost:7474/db/data/node/184" ], "length" : 2, "relationships" : [ "http://localhost:7474/db/data/relationship/123", "http://localhost:7474/db/data/relationship/127" ], "end" : "http://localhost:7474/db/data/node/184" } ]
Copyright © 2014 Neo Technology