4

My Graph Looks like this:

A --1--> B --2--> C --3--> D

|
4
|
V

E

I want to get the shortest paths from A to D. But I do not want the vertices but the edges that make up the path.

From here I ended up with:

 select expand(shortestPath) from (select shortestPath(A, D).outE())

But the result does not only contain the correct answers 1, 2, 3 but also 4, so all outgoing edges from the vertices that make up the path.

  • How could I get only the edges that make up the shortest path?
  • What if there are several shortest paths, how can I get all of them?

It would be cool if I could select shortestpath or dijkstra as a traversal strategy. IMO this is where they belong.

TylerH
  • 20,799
  • 66
  • 75
  • 101
pat
  • 2,600
  • 4
  • 20
  • 21

2 Answers2

0

Refer, this post, I have posted a possible solution using SQL along with explanation.

In brief, the SQL is :

SELECT FROM (TRAVERSE * FROM #51:0 WHILE 
                (@this INSTANCEOF V AND @rid IN (SELECT shortestPath(#51:0, #60:40).asList())) OR 
                (@this INSTANCEOF E AND (@this.in IN (SELECT shortestPath(#51:0, #60:40).asList()))) );
Ironluca
  • 3,402
  • 4
  • 25
  • 32
-1

OrientDB already has dijkstra function. Howerver to filter only the edges do:

select from (select expand( shortestPath(A, D) ) ) where @this instanceof 'E'
Lvca
  • 8,938
  • 2
  • 24
  • 25
  • Doing the expansion like you suggest returns 0 records. the expansion like this works: `select from (select expand(shortestPath) from (select shortestPath(#13:0, #13:2))) where @this instanceof 'E'` however with the where clause it returns 0 records. I am using `orientdb-community-2.0-rc1` but that should not matter?! – pat Feb 05 '15 at 17:22
  • 1
    Using 2.1.7-SNAPSHOT, `select expand(shortestpath(...))` only returns vertices. – peak Dec 09 '15 at 03:16
  • @pat is right, A and D should replaced with nodes IDs like pat replies. tnx – adramazany Jun 10 '20 at 04:11