Neo4j Graph Database Tutorial: How to build a Route Planner and other Examples

Often in the life of developer’s life there is a scenario where using a relational database tends to get complicated or sometimes even slow – especially when there are fragments with multiple relationships or multiple connections present. This often leads to complex database queries or desperate software engineers trying to handle those problems with their ORM framework.

A possible solution might be to switch from a relational database to a graph database – and – neo4j is our tool of choice here. In the following tutorial we’re going to implement several examples to demonstrate the strengths of a graph database .. from a route planner to a social graph.

Example: Using the Indexer

Often we want to search for a node with a specific property e.g. an id or another attribute without having to traverse the node graph. Luckily for us neo4j comes with my favourite indexer, Lucene and allows to find a node in no time by searching for its indexed properties.

In the following example, we’re creating two nodes – each node has its properties name and id – and we’re adding those properties to the Lucene index so that we’re able to search for these properties..

Example: Building a Route Planner

In the following example we’re simulating a railway network – railway stations are connected with other railway stations and each connection between two stations has a property: the distance (in miles).

We’re using Dijkstras algorithm to calculate the shortest route from London to Bristol and Northampton to Brighton here.

searching for the shortest route from London to Bristol..
London - Bristol with a distance of: 198.0 and via:
London Brighton Portsmouth Southampton Bristol
searching for the shortest route from Northampton to Brighton..
Northampton - Brighton with a distance of: 180.0 and via:
Northampton Oxford Southampton Portsmouth Brighton

Example: Mapping Users to Roles to Permissions

As another example we’re going to implement a system that assigns users to roles and permissions to roles.

There are three different roles: guest, user and admin and three permissions: read, write and administer.

We’re now building a graph to map the different permissions but you might ask where there is the advantage of using a graph for this?

We could make the scenario a bit more difficult and say that there are combined roles .. e.g. role root is a part of the role admin and the permissions read, write and administer are all parts of a permission named godmode – using a graph there we only need to descend the graph until we’ve got all permissions for the user mapped by the different possible paths in the graph.

Looking up permissions for user with name=Winston
Winston has the role: user
Winston has permission: read at depth: 1
Winston has permission: write at depth: 1
Looking up permissions for user with name=Slimer
Slimer has the role: admin
Slimer has permission: read at depth: 1
Slimer has permission: write at depth: 1
Slimer has permission: administer at depth: 1

Tutorial Sources

yED Graph Editor

All the graph drawing in this tutorial were made using the free yED graph editor from yWorks – it’s really easy and comfortable to use and has some nice features!

You’re able to download it here or simply start the editor from here using Java Web Start.

The yED Graph Editor

Troubleshooting

“Exception in thread “main” java.util.NoSuchElementException: More than one element in org.neo4j.index.impl.lucene.LuceneIndex$1@12789d2. First element is ‘Node[2]‘ and the second element is ‘Node[4]‘
at org.neo4j.helpers.collection.IteratorUtil.singleOrNull(IteratorUtil.java:116)
at org.neo4j.index.impl.lucene.IdToEntityIterator.getSingle(IdToEntityIterator.java:88)
at org.neo4j.index.impl.lucene.IdToEntityIterator.getSingle(IdToEntityIterator.java:32)
at com.hascode.tutorial.IndexSearchExample.main(IndexSearchExample.java:45)” – if you’re running the tutorial sources or you’re just experimenting – you eventually might need to reset your graph or your node index – the following method helps you there

11 Responses to “Neo4j Graph Database Tutorial: How to build a Route Planner and other Examples”

Thanks for your work on this example. Did you test the route planner on real world data? Because I guess it (dijkstra and your ram/cpu) won’t scale beyond the size of a city with this approach … would be nice if you can try it though (importing some osm etc)

Hi,
I saw a very good example on this website for Building a Route Planner in Neo4j. I want to know where i wil get the complete project for this with librabry files? I want to use this example and in additionally i want improve it by building benchmark in it.
Thanks