orientdb icon indicating copy to clipboard operation
orientdb copied to clipboard

shortest path

Open deluxxx opened this issue 12 years ago • 3 comments

Hello, I need return all my friends of friends and (short road) I created schema where I have relationships : I have 10 users and relationships : Mirek known - > Sylwia Mirek known -> Marcin known - > Seba Mirek known -> Jarek Mirek known -> Edek known -> Kaszczuk known -> Ana known -> unknown Mirek known -> Damian known Edek known -> Kaszczuk known -> Ana known -> unknown

And I need all friends of friends Mirek #28:2184 so that is : Seba #28:2191 Kaszczuk #28:2189

When I use that functions select shortestPath(#28:2184,#28:2189).asString()

is OK [v(User)[#28:2184], v(User)[#28:2185], v(User)[#28:2189]]

But when I use that : select $path, $depth from (traverse in,out,in_Friend,out_Friend from #28:2184 while $depth <= 4) where @class='User'

To seba is ok : #28:2184.out_Friend[idx:1].#26:1229.in.#28:2183.out_Friend[idx:1].#26:1246.in.#28:2191 but to kaszczuk go around : #28:2184.out_Friend[idx:0].#26:1227.in.#28:2182.out_Friend[idx:1].#26:1235.in.#28:2185.out_Friend[idx:2].#26:1240.in.#28:2189 screenshot from 2013-12-06 11 31 47 screenshot from 2013-12-06 11 32 46 screenshot from 2013-12-06 11 34 27

deluxxx avatar Dec 06 '13 14:12 deluxxx

What's the problem? depth level > 4?

By the way this query is too expensive if you only look only for friendships. Try rather this:

select $path, $depth from (traverse both('Friend') from #28:2184 while $depth <= 4)

By using both() it jumps the edges, just traverse them.

lvca avatar Dec 11 '13 12:12 lvca

Hello, I'm sorry you had to wait so long, I have graph relationship all edges here are friend bidirectional. I need something like traverse + shortestPath

untitled diagram

I need friends of friends. Level 2 friendship.

We start in Mirek SHOULD BE : First level friends are : Damian , Edyta , Jarek, Marcin , Sylwia

Second level friends are : Kasczuk , Seba

Third level friends are : Ana

Fourth level friends are : Abdul

When I execute query : select $path, $depth from (traverse both('Friend') from #28:1342 while $depth <= 4)

but we have different : Edyta should be to first level but she is in second level. Kaszczuk should be to second level but she is on third level.

screenshot from 2013-12-19 14 38 59

deluxxx avatar Dec 19 '13 13:12 deluxxx

By default OrientDB uses depth-first strategy, while for your case the breadth-first is more correct. Try:

select $path, $depth from (traverse both('Friend') from #28:1342 while $depth <= 4 strategy BREADTH_FIRST)

Fore more info: https://github.com/orientechnologies/orientdb/wiki/Java-Traverse#traversing-strategies

lvca avatar Dec 19 '13 14:12 lvca