Create subgraph of disconnected graph.

281 views
Skip to first unread message

Collin Scangarella

unread,
Feb 17, 2016, 2:21:51 PM2/17/16
to Gremlin-users
I'm trying to write a traversal that creates a subgraph of a disconnected graph. Each user of the graph is disconnected from the others - I'd like to create a subgraph which includes every edge and vertex which is connected to a particular user.

The behaviour is to encapsulate every vertex emitted by the following traversal in it's own subgraph:

g.V(userVertexId)
       
.emit()
       
.repeat(
                both
()
               
.simplePath())
       
.dedup()

I've tried several other sub graph traversals with no success - here's the most recent one which seems to enter an infinite loop:

subgraph = g.V(userVertexId)
.repeat(
or(
__.inE()
.subgraph('subGraph')
.outV(),
__.outE
.subgraph('subGraph')
.inV())
.simplePath())
.cap('subGraph')
.next()

What's wrong with this traversal?

Daniel Kuppitz

unread,
Feb 18, 2016, 4:43:31 AM2/18/16
to gremli...@googlegroups.com
Hi Collin,

let's start with the modern graph:

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

and disconnect peter from all the other vertices:

gremlin> g.V().has("name","peter").bothE().drop()

Now let's create a subgraph for marko:

gremlin> sg = g.V().has("name","marko").repeat(bothE().subgraph("sg").otherV().simplePath()).cap("sg").next()
==>tinkergraph[vertices:5 edges:5]

Verify that the subgraph doesn't contain peter and everything else was cloned properly:

gremlin> sg.traversal().V().valueMap(true)
==>[name:[marko], id:1, label:person, age:[29]]
==>[name:[vadas], id:2, label:person, age:[27]]
==>[name:[lop], id:3, lang:[java], label:software]
==>[name:[josh], id:4, label:person, age:[32]]
==>[name:[ripple], id:5, lang:[java], label:software]
gremlin> sg.traversal().E().valueMap(true)
==>[weight:0.5, id:7, label:knows]
==>[weight:1.0, id:8, label:knows]
==>[weight:0.4, id:9, label:created]
==>[weight:1.0, id:10, label:created]
==>[weight:0.4, id:11, label:created]

Of course peter is now a special case as the vertex has no more edges. If this can happen in you application, you would have to handle that accordingly (start with if (g.V(userVertexId).bothE().hasNext()) ...).

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/9a7283c7-bccc-4d6d-955c-da0f138994cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages