21.18. Graph Algorithms

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:

algorithm

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

Find all shortest paths

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

Figure 21.65. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/115/paths
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/110",
  "max_depth" : 3,
  "relationships" : {
    "type" : "to",
    "direction" : "out"
  },
  "algorithm" : "shortestPath"
}

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "directions" : [ "->", "->" ],
  "start" : "http://localhost:7474/db/data/node/115",
  "nodes" : [ "http://localhost:7474/db/data/node/115", "http://localhost:7474/db/data/node/114", "http://localhost:7474/db/data/node/110" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/78", "http://localhost:7474/db/data/relationship/87" ],
  "end" : "http://localhost:7474/db/data/node/110"
}, {
  "directions" : [ "->", "->" ],
  "start" : "http://localhost:7474/db/data/node/115",
  "nodes" : [ "http://localhost:7474/db/data/node/115", "http://localhost:7474/db/data/node/111", "http://localhost:7474/db/data/node/110" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/79", "http://localhost:7474/db/data/relationship/85" ],
  "end" : "http://localhost:7474/db/data/node/110"
} ]

Find one of the shortest paths

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.

Figure 21.66. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/108/path
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/103",
  "max_depth" : 3,
  "relationships" : {
    "type" : "to",
    "direction" : "out"
  },
  "algorithm" : "shortestPath"
}

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "directions" : [ "->", "->" ],
  "start" : "http://localhost:7474/db/data/node/108",
  "nodes" : [ "http://localhost:7474/db/data/node/108", "http://localhost:7474/db/data/node/104", "http://localhost:7474/db/data/node/103" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/69", "http://localhost:7474/db/data/relationship/75" ],
  "end" : "http://localhost:7474/db/data/node/103"
}

Execute a Dijkstra algorithm and get a single path

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.

Figure 21.67. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/121/path
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/118",
  "cost_property" : "cost",
  "relationships" : {
    "type" : "to",
    "direction" : "out"
  },
  "algorithm" : "dijkstra"
}

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "directions" : [ "->", "->", "->" ],
  "weight" : 1.5,
  "start" : "http://localhost:7474/db/data/node/121",
  "nodes" : [ "http://localhost:7474/db/data/node/121", "http://localhost:7474/db/data/node/120", "http://localhost:7474/db/data/node/117", "http://localhost:7474/db/data/node/118" ],
  "length" : 3,
  "relationships" : [ "http://localhost:7474/db/data/relationship/89", "http://localhost:7474/db/data/relationship/91", "http://localhost:7474/db/data/relationship/92" ],
  "end" : "http://localhost:7474/db/data/node/118"
}

Execute a Dijkstra algorithm with equal weights on relationships

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.

Figure 21.68. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/127/path
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/124",
  "cost_property" : "cost",
  "relationships" : {
    "type" : "to",
    "direction" : "out"
  },
  "algorithm" : "dijkstra"
}

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "directions" : [ "->", "->" ],
  "weight" : 2.0,
  "start" : "http://localhost:7474/db/data/node/127",
  "nodes" : [ "http://localhost:7474/db/data/node/127", "http://localhost:7474/db/data/node/125", "http://localhost:7474/db/data/node/124" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/95", "http://localhost:7474/db/data/relationship/100" ],
  "end" : "http://localhost:7474/db/data/node/124"
}

Execute a Dijkstra algorithm and get multiple paths

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.

Figure 21.69. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/101/paths
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "to" : "http://localhost:7474/db/data/node/98",
  "cost_property" : "cost",
  "relationships" : {
    "type" : "to",
    "direction" : "out"
  },
  "algorithm" : "dijkstra"
}

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "directions" : [ "->", "->", "->" ],
  "weight" : 1.5,
  "start" : "http://localhost:7474/db/data/node/101",
  "nodes" : [ "http://localhost:7474/db/data/node/101", "http://localhost:7474/db/data/node/100", "http://localhost:7474/db/data/node/97", "http://localhost:7474/db/data/node/98" ],
  "length" : 3,
  "relationships" : [ "http://localhost:7474/db/data/relationship/62", "http://localhost:7474/db/data/relationship/64", "http://localhost:7474/db/data/relationship/65" ],
  "end" : "http://localhost:7474/db/data/node/98"
}, {
  "directions" : [ "->", "->" ],
  "weight" : 1.5,
  "start" : "http://localhost:7474/db/data/node/101",
  "nodes" : [ "http://localhost:7474/db/data/node/101", "http://localhost:7474/db/data/node/96", "http://localhost:7474/db/data/node/98" ],
  "length" : 2,
  "relationships" : [ "http://localhost:7474/db/data/relationship/63", "http://localhost:7474/db/data/relationship/67" ],
  "end" : "http://localhost:7474/db/data/node/98"
} ]