On this page

Shortest path

One of the most important tasks with graph is to find if there is a connection between two nodes and if there are more than one then finds the shortest path
BangDB supports this with minor modifications in the Cypher construct (but to simplify only) and use this for finding the shortest path, here is how it goes

Let’s add a triple to have some multi-level connection. Let’s add a triple for movie “forrestGump” winning “oscar”

CREATE (Movie:forrestGump)-[WON_AWARD]->(Award:oscar)

{
"msg" : [
"success"
],
"errcode" : 0
}

Now, let’s find the shortest path between Tom Hanks and Oscar

S=>(Person:tom)-[#SHORT_PATH *]->(Award:oscar)

{
"nodes" : [
{
"label" : "Person",
"name" : "tom",
"_pk" : "2471870506:978343320853564008"
},
{
"_pk" : "889565392:3325115057284420805",
"name" : "forrestGump",
"label" : "Movie"
},
{
"label" : "Award",
"name" : "oscar",
"_pk" : "3098302716:362283378203006142"
}
],
"path_count" : 1,
"edges" : [
{
"rel" : "ACTED_IN",
"_to_node" : "889565392:3325115057284420805",
"_from_node" : "2471870506:978343320853564008"
},
{
"_to_node" : "3098302716:362283378203006142",
"_from_node" : "889565392:3325115057284420805",
"rel" : "WON_AWARD"
}
]
}

As you see, the returned result describes the path. If there was more than one path (more than one movie of Tom Hanks winning an Oscar, then it would have returned the shortest one. TO list all such paths between two nodes, we can use #ALL_PATH as shown in next section

Was this article helpful to you? Yes No