Prev |  | Next

## 20.16. 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`.

### 20.16.1. Find all shortest paths

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

Figure 20.62. Final Graph

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"
    } ]

### 20.16.2. 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 20.63. Final Graph

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"
    }

### 20.16.3. 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 20.64. Final Graph

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"
    }

### 20.16.4. 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 20.65. Final Graph

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"
    }

### 20.16.5. 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 20.66. Final Graph

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

Prev | Up | Next
| Home |