Find intersection and union of all directly connected vertices of a given set of vertices

39 views
Skip to first unread message

Ümit Akkuş

unread,
Sep 19, 2016, 1:45:16 PM9/19/16
to Aurelius
What is the best way to write the queries for finding the intersection and union of all directly connected vertices of a given set of vertices? We can assume that the number of vertices in the given set is around 10, and the expected number of results would be in the 10Ks.

Does it make sense to run it in one query or create 10 seperate queries, run them in parallel and join the results afterwards? What would be the differences?

HadoopMarc

unread,
Sep 19, 2016, 3:41:44 PM9/19/16
to Aurelius


Hi Ümit

Yes, you can write a single query for the union and a single query for the intersection.

Union (not tested):
 
g.V().hasId(1,2,3,4,5,6,7,8,9,10).both().dedup()


Intersection (not tested):

g.V().as('a').match( __.as('a').out('created').hasId(1), __.as('a').out('created').hasId(2), __.as('a').out('created').hasId(3), __.as('a').out('created').hasId(4), __.as('a').out('created').hasId(5), __.as('a').out('created').hasId(6), __.as('a').out('created').hasId(7), __.as('a').out('created').hasId(8), __.as('a').out('created').hasId(9), __.as('a').out('created').hasId(10)).select('a')


Of course, you can also write something with a loop (repeat step), but the queries above are easy to understand.

HTH,    Marc

Op maandag 19 september 2016 19:45:16 UTC+2 schreef Ümit Akkuş:

Daniel Kuppitz

unread,
Sep 19, 2016, 4:13:48 PM9/19/16
to aureliu...@googlegroups.com
The intersection query doesn't have to be a global graph scan.

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

gremlin> // union

gremlin> g.V(ids).both().dedup()
==>v[3]
==>v[2]
==>v[4]
==>v[5]
==>v[1]

gremlin> // intersection

gremlin> matchPatterns = ids.tail().collect {__.as("a").both().hasId(it)}.toArray() as Traversal[]
==>[StartStep@[a], VertexStep(BOTH,vertex), HasStep([~id.eq(4)])]
==>[StartStep@[a], VertexStep(BOTH,vertex), HasStep([~id.eq(6)])]
gremlin> g.V(ids.head()).both().dedup().match(matchPatterns).select("a")
==>v[3]

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Aurelius" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aureliusgraphs+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/aureliusgraphs/24258e11-3062-423f-be40-7760b067114c%40googlegroups.com.

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

HadoopMarc

unread,
Sep 20, 2016, 5:32:03 AM9/20/16
to Aurelius
Ooh, beautiful!


Marc

Op maandag 19 september 2016 19:45:16 UTC+2 schreef Ümit Akkuş:
What is the best way to write the queries for finding the intersection and union of all directly connected vertices of a given set of vertices? We can assume that the number of vertices in the given set is around 10, and the expected number of results would be in the 10Ks.
Reply all
Reply to author
Forward
0 new messages