Is there an efficient way with given two nodes to find a set of their common nodes (with defined relationships).
For example, having nodes A1
, B1
, C1
-C4
connected with relationships x
and y
:
A1 --x--> C1
A1 --x--> C2
A1 --x--> C3
B1 --y--> C2
B1 --y--> C3
B1 --y--> C4
a common node set for A1(x)
and B1(y)
would be [C2, C3]
.
In Gremlin (http://gremlin.tinkerpop.com), this is expressed as such:
Here is what each step does:
Tada!
Good luck, Marko.
http://markorodriguez.com
In many cases the structure of the domain can be leveraged to improve performance. Let's say that you know that in general your
A
entities have lessx
relationships compared to the number ofy
relationships on theB
entities. Then you could traverse two steps from the A node and see where theB
node shows up, and filter out theC
nodes this way. Here's some code for this approach:Another way would be to start two threads that scan the relationships from either side.
A third approach would be to create a specialized index that would help answering this kind of queries, which would obviously hurt insert performance.