6.9. Built-in Graph Algorithms

6.9.1. Find all shortest paths
6.9.2. Find one of the shortest paths between nodes
6.9.3. Execute a Dijkstra algorithm with similar weights on relationships
6.9.4. Execute a Dijkstra algorithm with weights on relationships

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.

algorithm

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)
max_depth
The maximum depth as an integer for the algorithms like ShortestPath, where applicable. Default is 1.

6.9.1. Find all shortest paths

The shortestPath algorithm can find multiple paths between the same nodes, like in this example.

Example request

  • POST http://localhost:7474/db/data/node/7/paths
  • Accept: application/json
  • Content-Type: application/json
{"to":"http://localhost:7474/db/data/node/2", "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/7",
  "nodes" : [ "http://localhost:7474/db/data/node/7", "http://localhost:7474/db/data/node/3", "http://localhost:7474/db/data/node/2" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/1", "http://localhost:7474/db/data/relationship/7" ],
  "end" : "http://localhost:7474/db/data/node/2"
}, {
  "start" : "http://localhost:7474/db/data/node/7",
  "nodes" : [ "http://localhost:7474/db/data/node/7", "http://localhost:7474/db/data/node/6", "http://localhost:7474/db/data/node/2" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/0", "http://localhost:7474/db/data/relationship/9" ],
  "end" : "http://localhost:7474/db/data/node/2"
} ]

6.9.2. Find one of the shortest paths between nodes

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/14/path
  • Accept: application/json
  • Content-Type: application/json
{"to":"http://localhost:7474/db/data/node/9", "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/14",
  "nodes" : [ "http://localhost:7474/db/data/node/14", "http://localhost:7474/db/data/node/10", "http://localhost:7474/db/data/node/9" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/11", "http://localhost:7474/db/data/relationship/17" ],
  "end" : "http://localhost:7474/db/data/node/9"
}

6.9.3. Execute a Dijkstra algorithm with similar weights on relationships

Example request

  • POST http://localhost:7474/db/data/node/29/path
  • Accept: application/json
  • Content-Type: application/json
{"to":"http://localhost:7474/db/data/node/32", "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/29",
  "nodes" : [ "http://localhost:7474/db/data/node/29", "http://localhost:7474/db/data/node/30", "http://localhost:7474/db/data/node/32" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/33", "http://localhost:7474/db/data/relationship/34" ],
  "end" : "http://localhost:7474/db/data/node/32"
}

6.9.4. Execute a Dijkstra algorithm with weights on relationships

Example request

  • POST http://localhost:7474/db/data/node/20/path
  • Accept: application/json
  • Content-Type: application/json
{"to":"http://localhost:7474/db/data/node/23", "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/20",
  "nodes" : [ "http://localhost:7474/db/data/node/20", "http://localhost:7474/db/data/node/21", "http://localhost:7474/db/data/node/18", "http://localhost:7474/db/data/node/19", "http://localhost:7474/db/data/node/16", "http://localhost:7474/db/data/node/17", "http://localhost:7474/db/data/node/23" ],
  "length" : 6,
  "relationships" : [ "http://localhost:7474/db/data/relationship/20", "http://localhost:7474/db/data/relationship/22", "http://localhost:7474/db/data/relationship/24", "http://localhost:7474/db/data/relationship/27", "http://localhost:7474/db/data/relationship/29", "http://localhost:7474/db/data/relationship/30" ],
  "end" : "http://localhost:7474/db/data/node/23"
}