Tinkerpop Gremlin深度优先搜索顺序

编程入门 行业动态 更新时间:2024-10-05 13:18:55
本文介绍了Tinkerpop Gremlin深度优先搜索顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个非常简单的示例图,正在尝试进行深度优先查询.假设图形边缘看起来像这样

I have a very simple example graph which I am trying to get a depth first query on. Let's say the graph edges look like this

A->B A->C B->D B->E C->F C->G

从A开始的深度优先搜索应返回

A depth first search from A should return

A-B-D-E-C-F-G

但是如果我能得到以下命令,那就更好了

But if I could get the below order it would be even better

D-E-B-A-F-G-C-A

如何创建将输出此订单的Gremlin查询?如果我做这样的事情

How can I create a Gremlin query that will output this order? If I do something like this

g.V('A').repeat(outE().inV()).emit()

我得到的A,B,C,D,E,F,G的订单优先.我不知道如何从上面得到想要的订单.

I get an order of A,B,C,D,E,F,G which is breadth first. I can't work out how to get the order I want above.

推荐答案

为其他人复制,下面是示例图:

For others to reproduce, here's the sample graph:

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开始的深度优先搜索应返回

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

这有点像是从后面卷起的小路.这很棘手,但可行:

This one is kinda rolling up the paths from behind. It's tricky, but doable:

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]

但是,仅获取路径并在应用程序端进行排序可能要容易得多.

However, it's probably a lot easier to just get the paths and do the ordering on the application side.

更多推荐

Tinkerpop Gremlin深度优先搜索顺序

本文发布于:2023-11-29 02:05:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644861.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   深度   Tinkerpop   Gremlin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!