One of the central property or the very reason for the existence of any graph is to be able to efficiently find connection between any given pair of nodes and in many cases also finding the shortest path. BangDB provides very efficient way to achieve this in different scenarios.

Find the shortest path between given two nodes, without worrying about relationships in between. This means we are just interested in finding the shortest path.

`CREATE GRAPH g4`
`USE GRAPH g4`
``````CREATE (city:A {"lat":12.97,"lon":77.59})-[BYROAD {"distance":100}]->(city:B {"lat":12.29,"lon":76.64})

CREATE (city:A)-[BYAIR {"distance":20}]->(city:B)

CREATE (city:B)-[BYAIR {"distance":10}]->(city:C)

CREATE (city:C)-[BYAIR {"distance":10}]->(city:D)

CREATE (city:B)-[BYAIR {"distance":25}]->(city:D)

CREATE (city:A)-[BYAIR {"distance":25}]->(city:C)``````

Let's assume that we have four cities, A,B,C and D. They are connected via road and air. Distances by road (in green) and by air (in blue) are shown in the graph below. Also, the latitude and longitude pairs are provided for all the cities.

We would like to know the shortest distance between city A and D, by road, air and finally by latitude and longitude based. Let's write the query now.

First let's find out by road without the weight (without considering the distance).

``S=>(city:A)-[#SHORT_PATH *]->(city:D)``
``````{
"nodes":[
{
"name":"A",
"label":"city"
},
{
"label":"city",
"name":"D"
}
],
"edges":[
{
}
],
"path_count":1
}``````

As we see, without considering the distance, it picks the shortest path means of number of connections in between.

Now, let's find out the shortest path by road, so we will use distance here

``S=>(city:A)-[#SHORT_PATH BYROAD {weight="distance"}]->(city:D)``
``````{
"nodes":[
{
"name":"A",
"label":"city"
},
{
"name":"B",
"label":"city"
},
{
"name":"C",
"label":"city"
},
{
"label":"city",
"name":"D"
}
],
"path_count":1,
"edges":[
{
},
{
},
{
}
]
}``````

Now, let's run the query for shortest distance by AIR

``S=>(city:A)-[#SHORT_PATH BYAIR {weight="distance"}]->(city:D)``
``````{
"edges":[
{
"rel":"BYAIR"
},
{
"rel":"BYAIR"
}
],
"nodes":[
{
"label":"city",
"name":"A"
},
{
"name":"C",
"label":"city"
},
{
"label":"city",
"name":"D"
}
],
"path_count":1
}``````

If we run the query to find shortest distance by any means (Road or Air), we will get the right results in this case too. Notice the '*' after #SHORT_PATH to indicate any relation.

``S=>(city:A)-[#SHORT_PATH * {weight="distance"}]->(city:D)``
``````{
"path_count":1,
"nodes":[
{
"label":"city",
"name":"A"
},
{
"label":"city",
"name":"C"
},
{
"name":"D",
"label":"city"
}
],
"edges":[
{
"rel":"BYAIR"
},
{
"rel":"BYAIR"
}
]
}``````

Finally, let's use latitude and longitude to compute the distance.

``S=>(city:A)-[#SHORT_PATH * {geo_distance="lat.lon"}]->(city:D)``
``````{
"path_count":1,
"nodes":[
{
"label":"city",
"name":"A"
},
{
"label":"city",
"name":"D"
}
],
"edges":[
{