how to find path between any two vertices by max depth?

1,005 views
Skip to first unread message

pengzhi...@gmail.com

unread,
Nov 21, 2016, 8:55:38 AM11/21/16
to Gremlin-users
Hi,
   I have a set of vertices,i need to find any tow vertices path by max depth, and also ignore the direction, if path: A->B->C->D vs D->C->B->A, I want to dedup, it is mean only return one path of them.

   when i use gremlin type like this: g.V(1,2,3,4).repeat(__.bothE().otherV().simplePath()).times(3).hasId(1,2,3,4).path(). it return many paths only the direction reversed. how can i dedup the path ignore their direction?


Daniel Kuppitz

unread,
Nov 21, 2016, 9:01:36 AM11/21/16
to gremli...@googlegroups.com
Hi,

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]

gremlin> g.V(1,2).bothE().otherV().hasId(1,2).path()
==>[v[1],e[7][1-knows->2],v[2]]
==>[v[2],e[7][1-knows->2],v[1]]
gremlin> g.V(1,2).bothE().otherV().hasId(1,2).path().dedup().by(union(limit(local, 1), tail(local, 1)).order().by(id).limit(1))
==>[v[1],e[7][1-knows->2],v[2]]

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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/b5e5af9a-eb8d-41a6-9b3d-47c5af0745a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pengzhi...@gmail.com

unread,
Nov 21, 2016, 10:04:04 AM11/21/16
to Gremlin-users
Thanks.

I use this in gremlin console work fine. But i use in java, throw an exception: java.lang.UnsupportedOperationException: Use OrderGlobalStep.addComparator(TraversalComparator) to add a local child traversal:OrderGlobalStep.
and this is my java code:

GraphTraversal<Vertex, Path> paths = g.V(1,2)
.repeat(bothE().otherV().simplePath())
.times(3).hasId(1, 2)
.path()
.dedup()
.by(__.union(__.limit(Scope.local, 1), __.tail(Scope.local, 1)).order().by(T.id).limit(1))
.by("name").by(T.label); 

what wrong with my java code?




在 2016年11月21日星期一 UTC+8下午10:01:36,Daniel Kuppitz写道:
Hi,

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]

gremlin> g.V(1,2).bothE().otherV().hasId(1,2).path()
==>[v[1],e[7][1-knows->2],v[2]]
==>[v[2],e[7][1-knows->2],v[1]]
gremlin> g.V(1,2).bothE().otherV().hasId(1,2).path().dedup().by(union(limit(local, 1), tail(local, 1)).order().by(id).limit(1))
==>[v[1],e[7][1-knows->2],v[2]]

Cheers,
Daniel

On Mon, Nov 21, 2016 at 2:01 PM, <pengzhi...@gmail.com> wrote:
Hi,
   I have a set of vertices,i need to find any tow vertices path by max depth, and also ignore the direction, if path: A->B->C->D vs D->C->B->A, I want to dedup, it is mean only return one path of them.

   when i use gremlin type like this: g.V(1,2,3,4).repeat(__.bothE().otherV().simplePath()).times(3).hasId(1,2,3,4).path(). it return many paths only the direction reversed. how can i dedup the path ignore their direction?


--
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.

Daniel Kuppitz

unread,
Nov 21, 2016, 10:09:20 AM11/21/16
to gremli...@googlegroups.com
Not sure what you're trying to do there. Your dedup() has 3 by() modulators. I don't think that the same traversal works in the console.
Shouldn't the last 2 by()'s modulate path()?

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/aae574ce-9a72-4a83-a5a2-ada15ef982a3%40googlegroups.com.

pengzhi...@gmail.com

unread,
Nov 21, 2016, 10:11:43 AM11/21/16
to Gremlin-users
ok, like not work for me:

g.V(1,5,6).repeat(bothE().otherV().simplePath()).times(3).hasId(1,5,6).path().dedup().by(__.union(__.limit(local, 1), __.tail(local, 1)).order().by(id).limit(1));

 i get two paths:
v1 v3 v4 v5
v5 v4 v3 v6

but i want return three paths:
v1 v3 v4 v5
v5 v4 v3 v6
v1 v4 v3 v6




在 2016年11月21日星期一 UTC+8下午10:01:36,Daniel Kuppitz写道:
Hi,

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]

gremlin> g.V(1,2).bothE().otherV().hasId(1,2).path()
==>[v[1],e[7][1-knows->2],v[2]]
==>[v[2],e[7][1-knows->2],v[1]]
gremlin> g.V(1,2).bothE().otherV().hasId(1,2).path().dedup().by(union(limit(local, 1), tail(local, 1)).order().by(id).limit(1))
==>[v[1],e[7][1-knows->2],v[2]]

Cheers,
Daniel


On Mon, Nov 21, 2016 at 2:01 PM, <pengzhi...@gmail.com> wrote:
Hi,
   I have a set of vertices,i need to find any tow vertices path by max depth, and also ignore the direction, if path: A->B->C->D vs D->C->B->A, I want to dedup, it is mean only return one path of them.

   when i use gremlin type like this: g.V(1,2,3,4).repeat(__.bothE().otherV().simplePath()).times(3).hasId(1,2,3,4).path(). it return many paths only the direction reversed. how can i dedup the path ignore their direction?


--
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.

Daniel Kuppitz

unread,
Nov 21, 2016, 10:17:34 AM11/21/16
to gremli...@googlegroups.com
Yea, I just realized that the limit(1) was stupid. Here's the correct solution:

gremlin> g.V(1,3).as("first").repeat(bothE().otherV().simplePath()).times(1).as("last").path().by("name").by(label)
==>[marko,created,lop]
==>[marko,knows,vadas]
==>[marko,knows,josh]
==>[lop,created,marko]
==>[lop,created,josh]
==>[lop,created,peter]

gremlin> g.V(1,3).as("first").repeat(bothE().otherV().simplePath()).times(1).as("last").dedup().by(union(select("first"), select("last")).order().by(id).fold()).path().by("name").by(label)
==>[marko,created,lop]
==>[marko,knows,vadas]
==>[marko,knows,josh]
==>[lop,created,josh]
==>[lop,created,peter]

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/30a85591-9385-4f50-938e-cb2ec13243dd%40googlegroups.com.

pengzhi...@gmail.com

unread,
Nov 21, 2016, 10:34:01 AM11/21/16
to Gremlin-users
ok,thanks.

it is work in gremlin console.

g.V(1,5,6).as("first").repeat(bothE().otherV().simplePath()).times(3).hasId(1,5,6).as("last").dedup().by(union(select("first"), select("last")).order().by(id).fold()).path().by("name").by(label)

==>[marko,created,lop,created,josh,created,ripple]
==>[marko,knows,josh,created,lop,created,peter]
==>[ripple,created,josh,created,lop,created,peter]

but in java still has an exception:
 java.lang.UnsupportedOperationException: Use OrderGlobalStep.addComparator(TraversalComparator) to add a local child traversal:OrderGlobalStep
at org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep.addLocalChild(OrderGlobalStep.java:98)
at org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal.by(GraphTraversal.java:1016)
at org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal.by(GraphTraversal.java:1029)
at com.hiekn.titan.service.TitanEditServiceTest.getByEdgeTest23(TitanEditServiceTest.java:662)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 GraphTraversal<Vertex, Path> paths = g.V(vertexIds.toArray()).as("first")
.repeat(__.bothE().otherV().simplePath())
.times(3).hasId(vertexIds.toArray()).as("last")
.dedup()
.by(__.union(__.select("first"), __.select("last")).order().by(T.id).fold())
.path()
.by("name").by(T.label);



在 2016年11月21日星期一 UTC+8下午11:17:34,Daniel Kuppitz写道:

Robert Dale

unread,
Nov 21, 2016, 10:36:20 AM11/21/16
to gremli...@googlegroups.com
Looks like java is on Titan. Maybe a version compatibility issue?

Robert Dale

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/14a3b553-cf5b-4ef3-87ee-66dff694705e%40googlegroups.com.

pengzhi...@gmail.com

unread,
Nov 21, 2016, 10:39:50 AM11/21/16
to Gremlin-users
Yes, I use titan 1.0. So it looks like a bug in titan java api?



在 2016年11月21日星期一 UTC+8下午11:36:20,Robert Dale写道:

Robert Dale

pengzhi...@gmail.com

unread,
Nov 21, 2016, 10:51:20 AM11/21/16
to Gremlin-users
Hi,
   when dedup by "first" and "last", i found not correct. because if middle path different is also dedup.


在 2016年11月21日星期一 UTC+8下午9:55:38,pengzhi...@gmail.com写道:

Daniel Kuppitz

unread,
Nov 21, 2016, 11:55:11 AM11/21/16
to gremli...@googlegroups.com
Then you actually want to dedup by all edges:

gremlin> g.V(1,3).repeat(bothE().as("e").otherV().simplePath()).times(2).path().by("name").by(label)
==>[marko,created,lop,created,josh]
==>[marko,created,lop,created,peter]
==>[marko,knows,josh,created,ripple]
==>[marko,knows,josh,created,lop]
==>[lop,created,marko,knows,vadas]
==>[lop,created,marko,knows,josh]
==>[lop,created,josh,created,ripple]
==>[lop,created,josh,knows,marko]

gremlin> g.V(1,3).repeat(bothE().as("e").otherV().simplePath()).times(2).dedup().by(select(all, "e").order(local).by(id)).path().by("name").by(label)
==>[marko,created,lop,created,josh]
==>[marko,created,lop,created,peter]
==>[marko,knows,josh,created,ripple]
==>[marko,knows,josh,created,lop]
==>[lop,created,marko,knows,vadas]
==>[lop,created,marko,knows,josh]
==>[lop,created,josh,created,ripple]

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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/084cd93b-1a84-4ce5-90a5-d62c0a437919%40googlegroups.com.

pengzhi...@gmail.com

unread,
Nov 21, 2016, 10:06:18 PM11/21/16
to Gremlin-users
Ok, thank you very much.

yes, it's my fault, I am not express my ideas clear.

I will use one example express my idea:

In modern graph, suppose add a new edge between v6 -> v5.  g.V(6).next().addEdge("created", g.V(5).next()).

so, I want to find 3 times, any of [v1,v5,v6] can reach any of [v1,v5,v6]. 
In other word, i do not care the middle path, just care the path's head and tail, if a vertex form [v1,v5,v6] shown in head, then it can not be shown in tail.

I use this: 
g.V(1,6,5).repeat(bothE().as("e").otherV().simplePath()).times(3).hasId(1,6,5).path().by("n
ame").by(label)
i get this:
==>[marko,created,lop,created,josh,created,ripple]
==>[marko,created,lop,created,peter,created,ripple]
==>[marko,knows,josh,created,ripple,created,peter]
==>[marko,knows,josh,created,lop,created,peter]
==>[peter,created,lop,created,josh,created,ripple]
==>[peter,created,lop,created,josh,knows,marko]
==>[peter,created,ripple,created,josh,knows,marko]
==>[ripple,created,josh,created,lop,created,marko]
==>[ripple,created,josh,created,lop,created,peter]
==>[ripple,created,peter,created,lop,created,marko]

so the first 5 black font paths is my want result. and the last 5 red font paths is reversed form first 5 paths, I want to dedup the 5 red font paths.

You may find, v1 to v6 has 2 paths, but path is diffrent.



在 2016年11月22日星期二 UTC+8上午12:55:11,Daniel Kuppitz写道:
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Daniel Kuppitz

unread,
Nov 21, 2016, 11:21:57 PM11/21/16
to gremli...@googlegroups.com
What's wrong with my previous answer? Looks exactly like what you want:

gremlin> g.V(1,6,5).repeat(bothE().as("e").otherV().simplePath()).times(3).hasId(1,6,5).
           path().by("name").by(label)

==>[marko,created,lop,created,josh,created,ripple]
==>[marko,created,lop,created,peter,created,ripple]
==>[marko,knows,josh,created,ripple,created,peter]
==>[marko,knows,josh,created,lop,created,peter]
==>[peter,created,lop,created,josh,created,ripple]
==>[peter,created,lop,created,josh,knows,marko]
==>[peter,created,ripple,created,josh,knows,marko]
==>[ripple,created,josh,created,lop,created,marko]
==>[ripple,created,josh,created,lop,created,peter]
==>[ripple,created,peter,created,lop,created,marko]

gremlin> g.V(1,6,5).repeat(bothE().as("e").otherV().simplePath()).times(3).hasId(1,6,5).dedup().by(select(all, "e").order(local).by(id)).
           path().by("name").by(label)

==>[marko,created,lop,created,josh,created,ripple]
==>[marko,created,lop,created,peter,created,ripple]
==>[marko,knows,josh,created,ripple,created,peter]
==>[marko,knows,josh,created,lop,created,peter]
==>[peter,created,lop,created,josh,created,ripple]


Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/a51500fa-6ea6-4269-b03a-f0cd3c361608%40googlegroups.com.

pengzhi...@gmail.com

unread,
Nov 22, 2016, 12:15:58 AM11/22/16
to Gremlin-users
Oh, thanks, your answser is correct, and it work fine for me when add edge assign an id.  But if I am not assign an id, then throw an exception:

gremlin> g.V(1,6,5).repeat(bothE().as("e").otherV().simplePath()).times(3).hasId(1,6,5).dedup().by(select(all, "e").order(local).by(id)). path().by("name").by(label)
==>[marko,created,lop,created,josh,created,ripple]
java.lang.Integer cannot be cast to java.lang.Long
Type ':help' or ':h' for help.
Display stack trace? [yN]

I want to ask you if my edge id is string what should i do this Traversal? 
And i thought you have unserstand my idea, so wether there was existed another way to do this Traversal?


在 2016年11月22日星期二 UTC+8下午12:21:57,Daniel Kuppitz写道:

Daniel Kuppitz

unread,
Nov 22, 2016, 9:15:18 AM11/22/16
to gremli...@googlegroups.com
That's because of the IdManager being used. Nothing to worry about as you likely won't need this query for the toy graph.

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/7af45893-c1d2-4903-b7bc-8462cfca33bf%40googlegroups.com.

pengzhi...@gmail.com

unread,
Nov 22, 2016, 9:47:33 AM11/22/16
to Gremlin-users
yes, thanks.  In fact i use titan, the edge can not order by id, so do you have other suggest for me do that Traversal?


在 2016年11月22日星期二 UTC+8下午10:15:18,Daniel Kuppitz写道:

Daniel Kuppitz

unread,
Nov 22, 2016, 10:10:11 AM11/22/16
to gremli...@googlegroups.com

pengzhi...@gmail.com

unread,
Nov 22, 2016, 10:22:59 AM11/22/16
to Gremlin-users
um, unfortunately titan java api can not assign id. I read the source code, when user assign id will throw an exception,.

any way, thank for answer my question.


在 2016年11月22日星期二 UTC+8下午11:10:11,Daniel Kuppitz写道:

Daniel Kuppitz

unread,
Nov 22, 2016, 10:46:46 AM11/22/16
to gremli...@googlegroups.com
You don't need to assign an id. Each element will already have a unique id (see the Javadocs link in my previous answer). Just use the longId to order the edges.

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/5062a318-7dce-49a2-85cd-2a2afa4a652c%40googlegroups.com.

pengzhi...@gmail.com

unread,
Nov 22, 2016, 11:07:11 AM11/22/16
to Gremlin-users
yes, the vertex id auto assign a long id,but the edge id is not long, so i order by edge's id throw an exception. 



在 2016年11月22日星期二 UTC+8下午11:46:46,Daniel Kuppitz写道:

Daniel Kuppitz

unread,
Nov 22, 2016, 11:13:41 AM11/22/16
to gremli...@googlegroups.com
Well, apparently you don't want to read the Javadocs....

This is what it should look like in Titan: ....order(local).by {it.longId()}....

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/e6a0d4a4-f919-45ca-8149-07067c5fa801%40googlegroups.com.

pengzhi...@gmail.com

unread,
Nov 22, 2016, 11:31:23 AM11/22/16
to Gremlin-users
Um, i will try later, and now my edge id print like this : "30kcge-1ppe28-iz9-2hdi8" ..

在 2016年11月23日星期三 UTC+8上午12:13:41,Daniel Kuppitz写道:

pengzhi...@gmail.com

unread,
Nov 23, 2016, 2:57:14 AM11/23/16
to Gremlin-users
thank you very much.  now it is work fine for me.



在 2016年11月23日星期三 UTC+8上午12:13:41,Daniel Kuppitz写道:
Well, apparently you don't want to read the Javadocs....
Reply all
Reply to author
Forward
0 new messages