Prev |  | Next

## 18.11. 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.11.1. Find all shortest paths

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

Figure 18.33. Final Graph

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

### 18.11.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.34. Final Graph

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

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

Figure 18.35. 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" : 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"
    }

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

Figure 18.36. Final Graph

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

Copyright © 2011 Neo Technology

Prev | Up | Next
| Home |