1
votes

This might be a rookie Gremlin question :

Let's say I have this graph

A [knows---> ] B

A [knows---> ] C

D [knows---> ] C

I want to traverse this graph and find nodes whom only A knows , in this case B and not C because both A and D know C. Is there a way to do this with Gremlin ?

Edit : Sorry I should have been more explicit in the question initially The number of incoming edges can actually be variable.

g.addV('A').as('a')
  .addV('B').as('b')
  .addV('C').as('c')
  .addV('D').as('d')
  .addV('E').as('e')
  .addV('F')as('f')
  .addE('knows').from('a').to('c')
  .addE('knows').from('b').to('c')
  .addE('knows').from('a').to('f')
  .addE('knows').from('b').to('f')
  .addE('knows').from('d').to('f')

In this case I want only C and not F because A & B know C and F both but D also knows F so I don't want F.

1

1 Answers

2
votes

It is helpful to have a small sample graph. Here is one that matches your question.

g.addV('A').as('a').
  addV('B').as('b').
  addV('C').as('c').
  addV('D').as('d').
  addE('knows').from('a').to('b').
  addE('knows').from('a').to('c').
  addE('knows').from('d').to('c')    

Using that graph the query to find friends unique to A could be written as

gremlin> g.V().hasLabel('A').
               out('knows').
               filter(__.in('knows').count().is(1)).
               path().
               by(label)  

==>[A,B] 

Edited based on the updated question.

OK so given the additional criteria, I believe this gives you what you need

gremlin>  g.V().hasLabel('A','B').
......1>        out().
......2>        groupCount().
......3>        unfold().
......4>        filter(select(values).is(2)).
......5>        select(keys).
......6>        where(__.in('knows').count().is(2)).
......7>        label()  

==>C