Suppose I have graph with vertices A
, B
, C
, D
, E
, F
, G
, X
and they are connected using outgoing edge requires
like below
A -> X
B -> A
C -> B
D -> C
D -> E
E -> X
F -> G
F -> X
G -> X
Is it possible to reduce a given set of vertices, until each vertex in the set does not require
an another. For e.g:
input = [ C ]
desired output = [ C ]
input = [ A, B, C, D ]
desired output = [ A ]
input = [ A, E ]
desired output = [ A, E ]
input = [ A, D, E ]
desired output = [ A, E ]
input = [ A, D, E, G, F ]
desired output = [ A, E, G ]
I tried my best so far and got the items to be ignored. May be someone can help me how to filter the results in the query itself.
g.V().has('name', within('A', 'B', 'C', 'D')).
aggregate('N').
repeat(in().dedup()).
until(has('name', within('A', 'B', 'C', 'D'))).
aggregate('K').
V().has('name', within('A', 'B', 'C', 'D')).
where(without('K')).
dedup().
values('name')
You can also try it with the same data given in the question https://gremlify.com/or7cziqjps
Update: added Where
to filter the results, however, trying to simplify the query further to make it more efficient.
To make it easier for other's to try better solution, posting the query to add the data.
g.addV('node').as('1').
property(single, 'name', 'A').addV('node').as('2').
property(single, 'name', 'B').addV('node').as('3').
property(single, 'name', 'C').addV('node').as('4').
property(single, 'name', 'D').addV('node').as('5').
property(single, 'name', 'E').addV('node').as('6').
property(single, 'name', 'F').addV('node').as('7').
property(single, 'name', 'G').addV('node').as('8').
property(single, 'name', 'X').
addE('requires').from('1').to('8').
addE('requires').from('2').to('1').
addE('requires').from('3').to('2').
addE('requires').from('4').to('3').
addE('requires').from('4').to('5').
addE('requires').from('7').to('8').
addE('requires').from('6').to('7').
addE('requires').from('5').to('8')