Tinkerpop Gremlin 深度优先搜索顺序
Tinkerpop Gremlin Depth First Search Order
我有一个非常简单的示例图,我试图对其进行深度优先查询。假设图的边看起来像这样
A->B
A->C
B->D
B->E
C->F
C->G
从 A 开始的深度优先搜索应该 return
A-B-D-E-C-F-G
不过如果能得到下面的顺序就更好了
D-E-B-A-F-G-C-A
如何创建将输出此订单的 Gremlin 查询?如果我这样做
g.V('A').repeat(outE().inV()).emit()
我得到了广度优先的顺序 A,B,C,D,E,F,G。我不知道如何得到上面我想要的顺序。
目前:你不能。
我们是同病相怜。这是我最初在 mailing list and then attempted a fix 上提出的一个已知问题,不幸的是,它仅在遍历中没有 emit
步骤时才有效。邀请您帮助修复它,我还没有时间深入挖掘。
供其他人复制,这里是示例图:
g = TinkerGraph.open().traversal()
g.addV().property(id, 'A').
addV().property(id, 'B').
addV().property(id, 'C').
addV().property(id, 'D').
addV().property(id, 'E').
addV().property(id, 'F').
addV().property(id, 'G').
addE('link').from(V('A')).to(V('B')).
addE('link').from(V('A')).to(V('C')).
addE('link').from(V('B')).to(V('D')).
addE('link').from(V('B')).to(V('E')).
addE('link').from(V('C')).to(V('F')).
addE('link').from(V('C')).to(V('G')).iterate()
A depth first search from A should return
A-B-D-E-C-F-G
gremlin> g.V('A').repeat(out('link')).until(__.not(outE('link'))).path().
unfold().dedup().id().fold()
==>[A,B,D,E,C,F,G]
But if I could get the below order it would be even better
D-E-B-F-G-C-A
这个有点从后面卷起路径。这很棘手,但可行:
gremlin> g.V('A').
repeat(outE('link').aggregate('edges').inV()).
until(__.not(outE('link'))).
flatMap(
union(identity(),
repeat(inE('link').where(within('edges')).as('current').
map(select('edges').unfold().
where(neq('current').and(without('done'))).
outV().where(without('ad')).fold()).as('bl').
select(last, 'current').store('done').
filter(outV().where(without('bl').and(without('ad')))).
outV().store('ad')).
emit())).
id().fold()
==>[D,E,B,F,G,C,A]
但是,仅获取路径并在应用程序端进行排序可能要容易得多。
我有一个非常简单的示例图,我试图对其进行深度优先查询。假设图的边看起来像这样
A->B
A->C
B->D
B->E
C->F
C->G
从 A 开始的深度优先搜索应该 return
A-B-D-E-C-F-G
不过如果能得到下面的顺序就更好了
D-E-B-A-F-G-C-A
如何创建将输出此订单的 Gremlin 查询?如果我这样做
g.V('A').repeat(outE().inV()).emit()
我得到了广度优先的顺序 A,B,C,D,E,F,G。我不知道如何得到上面我想要的顺序。
目前:你不能。
我们是同病相怜。这是我最初在 mailing list and then attempted a fix 上提出的一个已知问题,不幸的是,它仅在遍历中没有 emit
步骤时才有效。邀请您帮助修复它,我还没有时间深入挖掘。
供其他人复制,这里是示例图:
g = TinkerGraph.open().traversal()
g.addV().property(id, 'A').
addV().property(id, 'B').
addV().property(id, 'C').
addV().property(id, 'D').
addV().property(id, 'E').
addV().property(id, 'F').
addV().property(id, 'G').
addE('link').from(V('A')).to(V('B')).
addE('link').from(V('A')).to(V('C')).
addE('link').from(V('B')).to(V('D')).
addE('link').from(V('B')).to(V('E')).
addE('link').from(V('C')).to(V('F')).
addE('link').from(V('C')).to(V('G')).iterate()
A depth first search from A should return
A-B-D-E-C-F-G
gremlin> g.V('A').repeat(out('link')).until(__.not(outE('link'))).path().
unfold().dedup().id().fold()
==>[A,B,D,E,C,F,G]
But if I could get the below order it would be even better
D-E-B-F-G-C-A
这个有点从后面卷起路径。这很棘手,但可行:
gremlin> g.V('A').
repeat(outE('link').aggregate('edges').inV()).
until(__.not(outE('link'))).
flatMap(
union(identity(),
repeat(inE('link').where(within('edges')).as('current').
map(select('edges').unfold().
where(neq('current').and(without('done'))).
outV().where(without('ad')).fold()).as('bl').
select(last, 'current').store('done').
filter(outV().where(without('bl').and(without('ad')))).
outV().store('ad')).
emit())).
id().fold()
==>[D,E,B,F,G,C,A]
但是,仅获取路径并在应用程序端进行排序可能要容易得多。