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:B {"lat":12.29,"lon":76.64})-[BYROAD {"distance":50}]->(city:C {"lat":11.41,"lon":76.69})
CREATE (city:B {"lat":12.29,"lon":76.64})-[BYROAD {"distance":120}]->(city:D {"lat":13.34,"lon":76.11})
CREATE (city:A {"lat":12.97,"lon":77.59})-[BYROAD {"distance":190}]->(city:D {"lat":13.34,"lon":76.11})
CREATE (city:C {"lat":11.41,"lon":76.69})-[BYROAD {"distance":30}]->(city:D {"lat":13.34,"lon":76.11})
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":[
{
{"ft_lang_label":"__label__en","ft_lang_prob":0.7774205,"math_prob":0.970441,"size":3345,"snap":"2022-40-2023-06","text_gpt3_token_len":1028,"char_repetition_ratio":0.1756959,"word_repetition_ratio":0.125,"special_character_ratio":0.38893872,"punctuation_ratio":0.27493262,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97847164,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-02T09:03:55Z\",\"WARC-Record-ID\":\"<urn:uuid:2a713f38-77ea-4e7e-a759-355a9fda5370>\",\"Content-Length\":\"84345\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c8cecdd9-0d94-4bab-bfb4-3ad2e00acf6a>\",\"WARC-Concurrent-To\":\"<urn:uuid:6d5f91ec-45a3-41dd-ac21-50daadc4caa5>\",\"WARC-IP-Address\":\"23.23.232.217\",\"WARC-Target-URI\":\"https://bangdb.com/docs/bangdb-2-0-graph-and-cypher/user-guide/short-path-advance/\",\"WARC-Payload-Digest\":\"sha1:E6Q4YTO5V4K6F4IBI6CPRBCIGTHFQ7UF\",\"WARC-Block-Digest\":\"sha1:XXNTI2RRNSFNY3UHJZJ6JVEUTK4HKM2D\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337307.81_warc_CC-MAIN-20221002083954-20221002113954-00669.warc.gz\"}"}