# Select intermediate nodes between two nodes

128 views

### Davide

Jan 9, 2015, 11:43:32 AM1/9/15
Hi,
I'm struggling with the following problem:

1.
I have a node A that is connected with some nodes B1,B2,B3 and then I have some other nodes C, D, E that are connected to some nodes Bx (x = 1,2,3,...).
So A can be connected to some of the other nodes C,D,E through a subset of B1,B2,B3. I'd like to identify, for each node connected to A in this way, the subset of B nodes. Something like: C={B1,B3},D={B2},E={B1,B2}.

I spent a couple of days on that but I've not been able to obtain anything close to the expected result. Any help or suggestion will be very appreciated.

In addition to this I'm trying to solve the following point:

2.
I'd like to filter the above result keeping only the node connected to A through a specific subset of B nodes (e.g. only C={B1,B3},E={B1,B2} and not D as its connected only through B2).

I'm trying to solve this in Java, but also any help in Groovy with be much appreciated!

Thanks

Davide

### Daniel Kuppitz

Jan 9, 2015, 1:42:53 PM1/9/15
Hi,

g = TinkerGraph.open()

And here's the query you're looking for:

==>[v[C]:[v[B1], v[B3]], v[D]:[v[B2]], v[E]:[v[B1], v[B2]]]

Regarding the 2nd question: Simply add an except (or retain) step after the first out step.

Cheers,
Daniel

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

### Davide

Jan 12, 2015, 9:11:46 AM1/12/15
Hi,

Could you please clarify how to use the except/retain step in this scenario? It's not clear to me how to apply them to filter the result of the groubBy keeping only the keys that are mapped with a specific sub-set of values.

Many thanks,
Davide

### Daniel Kuppitz

Jan 12, 2015, 9:19:11 AM1/12/15
It depends on the subset - you either want to keep B1 and B3 or filter out B2:

==>[v[C]:[v[B1], v[B3]], v[E]:[v[B1]]]
==>[v[C]:[v[B1], v[B3]], v[E]:[v[B1]]]

Cheers,
Daniel

### Davide

Jan 12, 2015, 10:55:26 AM1/12/15
Thanks for your answer. Unfortunately my scenario is a bit more complex:

I don't have a specific set of nodes that I want to keep or exclude, I just want to keep the node at the 3rd level (C, D, ...) only if at the 2nd level I have at least two nodes in the set (B1,B2,B3). So (B1,B2) is ok as well as (B1,B3,B4), and (B1,B4) is not.

The main problem is that retain/except look like working as a filter on the 2nd level nodes connected to the 1st one. Instead I'm trying to filter on the nodes on the 2nd level that are in common with the 1st and 3rd ones.

Is it possible to use and/or conditions withe retain/exclude functions? Something like: retain([B1,B2] or [B1,B3] or [B2,B3]) ?

Unfortunately is not even easy to explain in plain words :)

Thanks,
Davide

### Daniel Kuppitz

Jan 12, 2015, 12:21:08 PM1/12/15
only if at the 2nd level I have at least two nodes in the set

Then this might help:

gremlin>   def x = it.sideEffects("x")
gremlin>   it.sideEffects("x", x.grep { it.value.size() >= 2 }.collectEntries())
gremlin> }.cap("x")
==>[v[C]:[v[B1], v[B3]], v[E]:[v[B1], v[B2]]]

Or, if you no longer need a traversal, simply do this:

==>v[C]={v[B1]=1, v[B3]=1}
==>v[E]={v[B1]=1, v[B2]=1}

Cheers,
Daniel

### Davide

Jan 13, 2015, 12:16:25 PM1/13/15