0

I'm trying to obtain the shortest path between two nodes in a graph using the dijkstra algorithm in Orientdb, using javascript. My output looks like below. I would like to know the length of the result (no of nodes) and print the properties of the edge between the consecutive nodes.

Unfortunately, I'm unable to find the correct length (always returns 1) and access the name/@rid (like #15:0,#15:4 etc) of each node. Can you please help me on how can I do it? Thanks.

result = db.query("SELECT dijkstra((select from destination where location ='A'),(select from destination where location ='B'), 'length','out')");   

[
    {
        "@type": "d",
        "@rid": "#-2:1",
        "@version": 0,
        "dijkstra": [
            "#15:0",
            "#15:4",
            "#15:2"
        ],
        "@fieldTypes": "dijkstra=z"
    }
]
Dileep
  • 71
  • 8
  • Please learn how to format your code so it's readable. In the visual editor button, use the "Code" button, which looks like this: `{}` – random_user_name Aug 12 '16 at 16:27
  • Hi, have you tried using dijkstra algorithm function in SQL? See an example here: http://orientdb.com/docs/last/SQL-Functions.html#dijkstra hope it may help in some way. – Oleksandr Gubchenko Aug 12 '16 at 16:30
  • @cale_b: thanks, I've updated the format now. – Dileep Aug 12 '16 at 17:07
  • @OleksandrGubchenko: Yes, I've looked at it and am using it the same way. I'd like to understand how do I parse its output. There is no mention of that in the documentation. – Dileep Aug 12 '16 at 17:08
  • @Dileep, you can use expand to expand information from RID, example: `SELECT expand(dijkstra(#7:0, #8:10, 'weight')) FROM V` and than just query for the property you need with a sub-query. Can you post the query that you are using or your js function? – Oleksandr Gubchenko Aug 12 '16 at 17:14
  • @OleksandrGubchenko, I have posted my query in the question. Expand gives me the description of each node in the output. But I want to fetch the rid of each node so that I can apply function like outE to print the edge details. Also I would need to calculate the number of nodes in the output because my shortest route would involve traversal via multiple nodes. – Dileep Aug 12 '16 at 17:33
  • Just add a sub-query like this: `select outE(YourEdgeName) from (SELECT expand(dijkstra(#7:0, #8:10, 'weight')) FROM V)` and you will obtain rids, if you want properties, just expand it again. To calculate the number of nodes just use `count` function. – Oleksandr Gubchenko Aug 12 '16 at 17:41

1 Answers1

2

Try this query:

select dijkstra.size(),dijkstra.location from (SELECT dijkstra((select from destination where location ='a'),(select from destination where location ='b'), 'length','out'))

I've tried it on 2.2.7 and here is my output: enter image description here

Hope it helps.

Oleksandr Gubchenko
  • 1,369
  • 6
  • 17
  • Thanks. It worked. I was wondering is there a way i can access the elements individually. i tried dijkstra2[0] but doesn't seem to work. – Dileep Aug 19 '16 at 15:08