kcchiu
kcchiu

Reputation: 1

Given two specific vertices, how to get the subgraph between them in Titan?

We have millions of vertices and ten of millions of edges to form a directed graph. Does anybody know how to get the subgraph in Titan between two given vertices?

Upvotes: 0

Views: 790

Answers (1)

stephen mallette
stephen mallette

Reputation: 46216

TinkerPop provides two methods of subgraphing:

  1. There is subgraph step and
  2. There is subgraph strategy

Use subgraph step to "pop off" a subgraph into a separate graph instance. You can see in the following example how the graph "g" is subgraphed into a new Graph instance of "knows" edges:

gremlin> subGraph = g.E().hasLabel('knows').subgraph('subGraph').cap('subGraph').next() //(1)
==>tinkergraph[vertices:3 edges:2]
gremlin> sg = subGraph.traversal()
==>graphtraversalsource[tinkergraph[vertices:3 edges:2], standard]
gremlin> sg.E() //(2)
==>e[7][1-knows->2]
==>e[8][1-knows->4]

You would use Subgraph strategy in cases where you want to traverse a subset of the graph only:

gremlin> graph = TinkerFactory.createTheCrew()
==>tinkergraph[vertices:6 edges:14]
gremlin> g = graph.traversal().withStrategies(SubgraphStrategy.build().
           vertices(or(hasNot('location'),properties('location').count().is(gt(3)))).
           edges(hasLabel('develops')).
           vertexProperties(or(hasLabel(neq('location')),hasNot('endTime'))).create())
==>graphtraversalsource[tinkergraph[vertices:6 edges:14], standard]
gremlin> g.V().valueMap(true)
==>[id:1,label:person,name:[marko],location:[santa fe]]
==>[id:8,label:person,name:[matthias],location:[seattle]]
==>[id:10,label:software,name:[gremlin]]
==>[id:11,label:software,name:[tinkergraph]]
gremlin> g.E().valueMap(true)
==>[id:13,label:develops,since:2009]
==>[id:14,label:develops,since:2010]
==>[id:21,label:develops,since:2012]

Based on the title of your question it sounds like you just want a graph of the two vertices with the edges between them. I suppose it sounds like you would want to use subgraph step. Of course, it seems even easier than that to get that result:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).bothE().where(otherV().hasId(2))
==>e[7][1-knows->2]

Having the list of edges constitute a subgraph in a sense especially in the case of two known vertices.

Upvotes: 2

Related Questions