Paths between nodes excluding certain edges

228 views
Skip to first unread message

Luiz Celso Gomes Jr.

unread,
Nov 9, 2012, 11:01:12 AM11/9/12
to gremli...@googlegroups.com
Hi, all!

I need to find the paths between two nodes following both in/out edges but excluding edges whose labels are in a list. Below is the solution I came up with, but it isn't working. Is there an easier way of doing that?

Thanks!

def g = TinkerGraphFactory.createTinkerGraph()
def origs = [g.v(4)]
def dest = '2'

def NOTFOLLOW = ["bla"]

def paths = origs._().as('start')
.copySplit(
_().inE.filter{!(it.label in NOTFOLLOW)}.outV, 
_().outE.filter{!(it.label in NOTFOLLOW)}.inV
).fairMerge()
.simplePath().loop('start'){it.loops<=3}{(it.object.id==dest)}
.path().toList()
paths.each{println it}

Luiz Celso Gomes Jr.

unread,
Nov 9, 2012, 2:34:54 PM11/9/12
to gremli...@googlegroups.com
It's funny that if I don't use the "loop" step, just copying the code multiple times, everything works perfectly. Would that be a bug? Or am I doing something wrong in the loop parameters?

E.g., this works:

paths = origs._().as('start')
.copySplit(
_().inE.filter{!(it.label in NOTFOLLOW)}.outV, 
_().outE.filter{!(it.label in NOTFOLLOW)}.inV
).fairMerge()
.simplePath()
.copySplit(
_().inE.filter{!(it.label in NOTFOLLOW)}.outV, 
_().outE.filter{!(it.label in NOTFOLLOW)}.inV
).fairMerge()
.simplePath()
        .path().toList()

I do need the loop step though..
:(
Thanks

Stephen Mallette

unread,
Nov 11, 2012, 1:11:55 PM11/11/12
to gremli...@googlegroups.com
I took the recipe that finds paths between vertices in gremlindocs

http://gremlindocs.com/#recipes/paths-between-two-vertices

and modified it slightly so that it has multiple starts looking for
destinations at v[5] and has edge label filtering:

gremlin> origs = [g.v(1),g.v(3)]
==>v[1]
==>v[3]
gremlin> origs._().both('knows','created').loop(1){it.loops<=3 &&
!(it.object.id in ['1','5'])}.has('id','5').path
==>[v[1], v[4], v[5]]
==>[v[1], v[3], v[4], v[5]]
==>[v[3], v[4], v[5]]

perhaps that will help you solve your problem...

Stephen

On Fri, Nov 9, 2012 at 2:34 PM, Luiz Celso Gomes Jr.
> --
>
>

Luiz Celso Gomes Jr.

unread,
Nov 11, 2012, 1:16:32 PM11/11/12
to gremli...@googlegroups.com
That's nice Stephen, thanks. The problem is that I don't know which edges to traverse. I just know which edges not to traverse. It's been surprisingly hard to find a way around it.
:(


--



Marko Rodriguez

unread,
Nov 11, 2012, 3:43:49 PM11/11/12
to gremli...@googlegroups.com
Hi,

What do you mean, "edges not to traverse." By label? If so, you can do:

x = ['a','b','c']
origs._().both.filter{!x.contains(it.label)}.loop(2){ ... }

In a future release, we will be support has and hasNot with ORing values.


HTH,
Marko.
--
 
 

Luiz Celso Gomes Jr.

unread,
Nov 11, 2012, 5:32:12 PM11/11/12
to gremli...@googlegroups.com
Thanks Marko, but you are filtering out nodes, not edges, right? I need to avoid certain edges (that's why I had that complicated code in the first email, so that I could use filter in the edges).

Makes sense?

Thanks!


--
 
 

Marko Rodriguez

unread,
Nov 11, 2012, 5:52:14 PM11/11/12
to gremli...@googlegroups.com
Hi,

Sorry, my code snippet is wrong. Do this:

x = ['a','b','c']
origs._().bothE.filter{!x.contains(it.label)}.bothV.loop(2){ ... }
Therefore, only edges that do not have labels specified in x.

HTH,
Marko.
--
 
 

Luiz Celso Gomes Jr.

unread,
Nov 11, 2012, 6:45:46 PM11/11/12
to gremli...@googlegroups.com
I think this doubles the number of nodes, because bothV returns the head and tail of any edge not filtered. I'm pretty sure about that, but I'll test again when I get to my computer.

Maybe the best solution would be to add a variant of the both (and in/out) step in gremlin? I think I could try that in the near future...

Marko, what about the code in the first email? Do you see any obivious error? That code would do it for me, but I was getting a weird behavior. If you think the code makes some sense, I'll look into whats going on.

Thanks a bunch!
--
 
 

--
 
 

Stephen Mallette

unread,
Nov 17, 2012, 6:29:26 AM11/17/12
to gremli...@googlegroups.com
I've been tinkering with this issue since it posted and can't quite
get it right. Luiz, did you ever get it figured out?

Stephen

On Sun, Nov 11, 2012 at 6:45 PM, Luiz Celso Gomes Jr.
> --
>
>
Reply all
Reply to author
Forward
0 new messages