'OrientDB - get all paths from one vertex
I'm using OrientDB Community Edition 3.2.3.
This is the graph of my data: [1]: https://i.stack.imgur.com/5yK7C.png
I would like to get all the paths that are connected to the node #17:9.
I tried the following traverse command:
select $path from (traverse in() from #17:9)
results that I am getting:
[#17:9]
[#17:9 #17:2]
[#17:9 #17:2 #17:4]
[#17:9 #17:2 #17:4 #17:3]
[#17:9 #17:2 #17:4 #17:0]
[#17:9 #17:2 #17:5]
results that I want to get:
[#17:9]
[#17:9 #17:2]
[#17:9 #17:2 #17:4]
[#17:9 #17:2 #17:4 #17:3]
[#17:9 #17:2 #17:4 #17:0]
[#17:9 #17:2 #17:5]
[#17:9 #17:2 #17:5 #17:0]
the last path is not included in the traversal. Is there the possibility to get this result?
would really appreciate any help on this.
Solution 1:[1]
The reason you are not getting the last path is because, OrientDB traversal does not visit the nodes that have already been visited.
Currently there is no easy way to enumerate all the full paths from one vertex to another. You can get the shortest path, any of the paths but not all the paths with a query.
The solution to this that I have been able to work out is extremely ugly but it works if there are no self-loops.
The solution requires two steps viz.:
- Select all the out-going / in-coming or both edges as required from the destination vertex
- Use multiple Traverse queries or one Traverse query with multiple OR clauses to select from Traversal Paths that have one of the edges from step 1
Below is an example:
Query to select vertices of destination
SELECT @rid FROM (TRAVERSE bothE() FROM #60:40 MAXDEPTH 1) WHERE $depth>0
Select all the edges from #60:40 rid
The result in the graph that I am considering is:
#15:15
#14:15
#17:12
#67:30
Query to
Get the paths from the source vertex till one of the edges:
SELECT $path FROM (TRAVERSE * FROM #51:0 WHILE @rid!=#60:40) WHERE ('#15:15' IN $path.asList()) OR
('#14:15' IN $path.asList()) OR
('#17:12' IN $path.asList()) OR
('#67:30' IN $path.asList())
Where '#51:0' is the starting vertex in my case.
Above is the partial view of the graph (the entire graph is too big)
In your case, you could use the '#17:0' as the destination vertex and the '#17:9' as the source vertex.
NOTE: The all the paths end in a 'dangling' edge whose out vertex is implicitly the destination vertex.
If someone can provide a shorter and better solution or optimize the above, it would be great. Since I am grappling with the same use case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |