get all the paths from a graph where path vertices are subset of given vertex list

743 views
Skip to first unread message

Deepak Garg

unread,
Feb 18, 2019, 10:57:52 AM2/18/19
to Gremlin-users
Hi Folks

I am new to gremlin. In my use-case I have a list of vertices and want to get all the paths ending to leaf node where all the path's vertices must be included in the given list of vertices (or subset of given list of vertices).

I tried to write query to get all the paths from a root node to the leaves:

g.V(1).repeat(out()).until(outE().count().is(0)).path()

Can you please help me with a following.

if vertexList = [1,2,3]

Get all the path from root to leaves where path vertices are subset of given list of vertices
e.g path 1->2->3->4 should not be selected
path 1->2 should be selected
        path 1->3 should be selected
        path 1->3->2 should be slected

Daniel Kuppitz

unread,
Feb 18, 2019, 11:02:50 AM2/18/19
to gremli...@googlegroups.com
A simple filter should do the job:

g.V(1).
  repeat(out().hasId(within(vertexList))).
    until(__.not(outE())).
  path()

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/89f9071f-5691-42fa-ac59-1422b8824625%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Deepak Garg

unread,
Feb 18, 2019, 12:47:08 PM2/18/19
to gremli...@googlegroups.com
Thanks Daniel for the reply.

I tried this for below scenario:

graph = TinkerGraph.open()
g = graph.traversal()
v1 = graph.addVertex(id, "a", label, "a")
v2 = graph.addVertex(id, "b", label, "b")
v3 = graph.addVertex(id, "c", label, "c")
 v1.addEdge("contains", v2)
v2.addEdge("contains", v3)
vertexList= ["b", "c"]
 g.V().repeat(out().hasId(within(vertexList))).until(__.not(outE())).path()

==>[v[a],v[b],v[c]]
==>[v[b],v[c]]

however output should be empty as the relationship is a->b->c and here 'a' is not included in the vertexList. Am I missing something?



For more options, visit https://groups.google.com/d/optout.


-- 
U.D.I.T

Sent by Nokia OVI (c)

Daniel Kuppitz

unread,
Feb 18, 2019, 4:43:00 PM2/18/19
to gremli...@googlegroups.com
Well, your initial example started from a specific vertex that was part of the list. I assumed that's always the case. If you start from all vertices, it's:

g.V().hasId(within(vertexList)).
  repeat(out().hasId(within(vertexList))).
    until(__.not(outE())).
  path()

Cheers,
Daniel


Reply all
Reply to author
Forward
0 new messages