Prev |  | Next

## 18.14. Built-in 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.

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

### 18.14.1. Find all shortest paths

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

Figure 18.73. Final Graph

Example request

- `POST` `http://localhost:7474/db/data/node/16/paths`
- `Accept:` `application/json`
- `Content-Type:` `application/json`

    {
      "to" : "http://localhost:7474/db/data/node/11",
      "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/16",
      "nodes" : [ "http://localhost:7474/db/data/node/16", "http://localhost:7474/db/data/node/12", "http://localhost:7474/db/data/node/11" ],
      "length" : 2,
      "relationships" : [ "http://localhost:7474/db/data/relationship/1", "http://localhost:7474/db/data/relationship/7" ],
      "end" : "http://localhost:7474/db/data/node/11"
    }, {
      "start" : "http://localhost:7474/db/data/node/16",
      "nodes" : [ "http://localhost:7474/db/data/node/16", "http://localhost:7474/db/data/node/15", "http://localhost:7474/db/data/node/11" ],
      "length" : 2,
      "relationships" : [ "http://localhost:7474/db/data/relationship/0", "http://localhost:7474/db/data/relationship/9" ],
      "end" : "http://localhost:7474/db/data/node/11"
    } ]

### 18.14.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.

Figure 18.74. Final Graph

Example request

- `POST` `http://localhost:7474/db/data/node/23/path`
- `Accept:` `application/json`
- `Content-Type:` `application/json`

    {
      "to" : "http://localhost:7474/db/data/node/18",
      "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/23",
      "nodes" : [ "http://localhost:7474/db/data/node/23", "http://localhost:7474/db/data/node/19", "http://localhost:7474/db/data/node/18" ],
      "length" : 2,
      "relationships" : [ "http://localhost:7474/db/data/relationship/11", "http://localhost:7474/db/data/relationship/17" ],
      "end" : "http://localhost:7474/db/data/node/18"
    }

### 18.14.3. Execute a Dijkstra algorithm with similar weights on relationships

Figure 18.75. Final Graph

Example request

- `POST` `http://localhost:7474/db/data/node/38/path`
- `Accept:` `application/json`
- `Content-Type:` `application/json`

    {
      "to" : "http://localhost:7474/db/data/node/41",
      "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/38",
      "nodes" : [ "http://localhost:7474/db/data/node/38", "http://localhost:7474/db/data/node/39", "http://localhost:7474/db/data/node/41" ],
      "length" : 2,
      "relationships" : [ "http://localhost:7474/db/data/relationship/33", "http://localhost:7474/db/data/relationship/34" ],
      "end" : "http://localhost:7474/db/data/node/41"
    }

### 18.14.4. Execute a Dijkstra algorithm with weights on relationships

Figure 18.76. Final Graph

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" : 6.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/27", "http://localhost:7474/db/data/node/28", "http://localhost:7474/db/data/node/25", "http://localhost:7474/db/data/node/26", "http://localhost:7474/db/data/node/32" ],
      "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/32"
    }

Copyright © 2012 Neo Technology

Prev | Up | Next
| Home |