Skip to content

Instantly share code, notes, and snippets.

@tolgap
Created June 18, 2014 15:26
Show Gist options
  • Save tolgap/7ee9fe981dd82e439121 to your computer and use it in GitHub Desktop.
Save tolgap/7ee9fe981dd82e439121 to your computer and use it in GitHub Desktop.
Dijkstra algorithm in Neo4j with example

Adding nodes and relationships to DB

CREATE (Rotterdam:City {title:'Rotterdam'})
CREATE (DenHaag:City {title:'Den Haag'})
CREATE (Leiden:City {title:'Leiden'})
CREATE (Delft:City {title:'Delft'})
CREATE (Amsterdam:City {title:'Amsterdam'})
CREATE (Utrecht:City {title:'Utrecht'})
CREATE (Arnhem:City {title:'Arnhem'})
CREATE (Groningen:City {title:'Groningen'})
CREATE (Maastricht:City {title:'Maastricht'})
CREATE (DenBosch:City {title:'Den Bosch'})
CREATE (Tilburg:City {title:'Tilburg'})

CREATE
  (Utrecht)-[:NEIGHBOUR {weight:6}]->(Rotterdam),
  (Utrecht)-[:NEIGHBOUR {weight:7}]->(DenHaag),
  (Utrecht)-[:NEIGHBOUR {weight:8}]->(Leiden),
  (Utrecht)-[:NEIGHBOUR {weight:6}]->(Tilburg),
  (Utrecht)-[:NEIGHBOUR {weight:9}]->(Arnhem),
  (Rotterdam)-[:NEIGHBOUR {weight:4}]->(DenHaag),
  (Rotterdam)-[:NEIGHBOUR {weight:2}]->(Delft),
  (Rotterdam)-[:NEIGHBOUR {weight:7}]->(DenBosch),
  (DenHaag)-[:NEIGHBOUR {weight:2}]->(Leiden),
  (DenHaag)-[:NEIGHBOUR {weight:2}]->(Delft),
  (DenHaag)-[:NEIGHBOUR {weight:4}]->(Amsterdam),
  (Leiden)-[:NEIGHBOUR {weight:3}]->(Amsterdam),
  (Arnhem)-[:NEIGHBOUR {weight:11}]->(Groningen),
  (Arnhem)-[:NEIGHBOUR {weight:8}]->(Tilburg),
  (Arnhem)-[:NEIGHBOUR {weight:10}]->(Maastricht),
  (Tilburg)-[:NEIGHBOUR {weight:4}]->(DenBosch),
  (Tilburg)-[:NEIGHBOUR {weight:5}]->(Maastricht)

##Shortest path using Dijkstra algorithm

Send request to http://localhost:7474/db/data/node/{node_id}/paths where node_id is the node id of the city.

Use these headers:

Accept: application/json; charset=UTF-8
Content-Type: application/json

And insert this into the body:

{
  "to": "http://localhost:7474/db/data/node/{node_id}",
  "cost_property": "weight",
  "relationships" : {
    "type": "NEIGHBOUR"
  },
  "algorithm": "dijkstra"
}

Where node_id is the node id of which city you want to go to.

The DB will respond with something like this:

[
  {
      "weight": 15,
      "start": "http://localhost:7474/db/data/node/175",
      "nodes": [
          "http://localhost:7474/db/data/node/175",
          "http://localhost:7474/db/data/node/172",
          "http://localhost:7474/db/data/node/171",
          "http://localhost:7474/db/data/node/180"
      ],
      "length": 3,
      "relationships": [
          "http://localhost:7474/db/data/relationship/263",
          "http://localhost:7474/db/data/relationship/258",
          "http://localhost:7474/db/data/relationship/260"
      ],
      "end": "http://localhost:7474/db/data/node/180"
  },
  {
      "weight": 15,
      "start": "http://localhost:7474/db/data/node/175",
      "nodes": [
          "http://localhost:7474/db/data/node/175",
          "http://localhost:7474/db/data/node/172",
          "http://localhost:7474/db/data/node/174",
          "http://localhost:7474/db/data/node/171",
          "http://localhost:7474/db/data/node/180"
      ],
      "length": 4,
      "relationships": [
          "http://localhost:7474/db/data/relationship/263",
          "http://localhost:7474/db/data/relationship/262",
          "http://localhost:7474/db/data/relationship/259",
          "http://localhost:7474/db/data/relationship/260"
      ],
      "end": "http://localhost:7474/db/data/node/180"
  }
]

The top element in the array is the shortest path with least steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment