Algorithm of the Week: Orienteering and Finding the Shortest Path
Algorithm of the Week: Orienteering and Finding the Shortest Path
Join the DZone community and get the full member experience.
Join For FreeDiscover Tarantool's unique features which include powerful stored procedures, SQL support, smart cache, and the speed of 1 million ACID transactions on a single CPU core!
Originally authored by Rik Van Bruggen
As you may know by now, I like beer. A lot. Why else would I keep writing and talking about it? But there’s more to life than sweet beverages, and one of the things that I have been doing for as long as I can remember is orienteering. I have been practicing the sport in Belgium since 1984 when I was 11 years old. My dad used to take me to races all across the continent; we truly had a blast. I still orienteer almost every week, and so does my dad. Now I take my eight and tenyear old kids with me to the races, and their granddad cheers them on every step of the way. It’s a fantastic family sport.
So what does that have to do with neo4j? Well, orienteering is all about “ finding the shortest path”: the fastest route from start to finish. Fast can be short. Fast can also mean that it is better to take a detour; if it is easier to run the longer route, than to walk the shorter route, you are better off choosing the longer route. In essence, every orienteering race is … a graph problem waiting to be solved in the middle of nature.
Orienteering = a green graph problem
In case you don’t know: orienteering races are a bit like an obstacle race. Every participant gets assigned a course, out there in the green forests and fields, and along that course are sequences of beacons that one needs to get to in order. Such a sequence is … a path on a graph  you have to choose how to navigate from obstacle to obstacle, from node to node.Essentially, the orienteer has to navigate and choose the fastest route. Finding the fastest route effectively this boils down to a “weighted shortest path” calculation. You calculate the shortest path using
 distance: shorter = better
 runnability: higher = better. Runnability can be affected by the type of terrain (running on a road? through a field? through a forest? through a forest with soil covered with plants? over a hill? through a valley? …)
Example: a 2 control race in Antwerp, Belgium
As you can see  the race assignment is a graph.
If we then look at every leg separately, you can see that for every leg, there are a number of route options.
The red route is the safe choice  running along the roads  but takes quite a detour. The blue route cuts straight across the field  but then requires me to go straight through the forest for a short distance.  The red route is the shortest  but requires me to run straight through the forest. The blue route just races along the forest road.  The red route just goes straight to the finish line. The blue route cuts through the forest and then follows the road. The green route safely hurries along the roads. 
So 3 controls, and different routes with different characteristics. As you can see in the schematic representations, every route has different “waypoints”  specific points of interest that I can identify on the map, and recognize in the “field”. These waypoints are extremely important for the navigation exercise that we are doing  they allow us to break the problem up in smaller pieces and evaluate our options.
Graph Database Model to Navigate

Control nodes: the race beacons that I have to pass by

The alternative route choices, decomposed in waypoints.

From the start to control 1: I have 0>0.11>0.12>0.13>0.14>0.15 as one route and 0>0.21>0.22>1 as another route

From control 1 to control 2 I have 1>1.11>1.12>1.13>2 as one route option, and 1>0.15>1.21>2 as another option.

From control 2 to the finish I have 3 options: 2>2.11>3, 2>2.21>2.22>3 and 2>1.21>2.31>2.32>3.
Graphs Algorithms to Win the Race
In order to calculate the best route to win the race, I need to calculate the shortest path across the graph  which is standard functionality of neo4j. But because there’s more to it than running the course in straight lines between controls, I need to incorporate weights (distance, runnability) to get a realistic estimate of what would be the best route choice. To do so I am using a technique so well demonstrated by Ian Robinson on his blog last June.
find the shortest path by distance only:

find the best estimate of the fastest path, as a function of distance/runnability. In a real race this would probably be the route that I would choose  as it would give me the best chance of winning the race.
Many Applications for Weighted Shortest Paths
Obviously orienteering is not a business application, but in logistics, planning, impact analysis and many other applications, weighted shortest path algorithms will have a great potential. Whether it is to find out how things are related to eachother, determining the most efficient way to get something from point A to point B, or finding out who would be affected by a particular type of capacity outage  the approach that I used for my orienteering problem would work just as nicely!
Discover Tarantool's unique features such as powerful stored procedures, SQL support, smart cache, and the speed of 1 million ACID transactions on a single CPU.
Opinions expressed by DZone contributors are their own.
{{ parent.title  parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}