在遍历期间将顶点的传入边添加到set属性

问题描述 投票:0回答:1

我想要做的是深度优先遍历,在每一步我将入边(实际上是顶点)添加到集合中。最后,我希望步骤中的每个节点都有一个遍历的传入顶点列表。最初我为每个传入边添加一个简单属性:

g.V().has('Name', 'startnode').repeat(__.in()).emit().property('degree', union(values('degree'), constant(1)).sum())

我最终获得了一个属性程度,它包含了传入边数。我现在想要一组传入边而不仅仅是一个计数。类似的东西:

g.V().has('Name', 'R1\\B').repeat(__.in()).emit().property(set, 'incoming', XXX)

这就是XXX。我需要将它设置为什么?即遍历中的当前输入顶点。

gremlin directed-acyclic-graphs
1个回答
1
投票

为了访问边缘,您需要显式遍历它们。

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().hasLabel('software').
           repeat(__.inE().as('e').outV()).
             emit().
           property(set, 'incoming', select(last, 'e')).iterate()
gremlin> g.V().valueMap()
==>[incoming:[e[9][1-created->3],e[8][1-knows->4]],name:[marko],age:[29]]
==>[name:[vadas],age:[27]]
==>[name:[lop],lang:[java]]
==>[incoming:[e[11][4-created->3],e[10][4-created->5]],name:[josh],age:[32]]
==>[name:[ripple],lang:[java]]
==>[incoming:[e[12][6-created->3]],name:[peter],age:[35]]

我不建议存放整个边缘;边缘ID可能没问题。

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().hasLabel('software').
           repeat(__.inE().as('e').outV()).
             emit().
           property(set, 'incoming', select(last, 'e').by(id)).iterate()
gremlin> g.V().valueMap()
==>[incoming:[9,8],name:[marko],age:[29]]
==>[name:[vadas],age:[27]]
==>[name:[lop],lang:[java]]
==>[incoming:[11,10],name:[josh],age:[32]]
==>[name:[ripple],lang:[java]]
==>[incoming:[12],name:[peter],age:[35]]

UPDATE

要收集相邻的顶点id,它将是:

gremlin> g.V().hasLabel('software').
           repeat(__.as('v').in()).
             emit().
           property(set, 'incoming', select(last, 'v').by(id)).iterate()
gremlin> g.V().valueMap()
==>[incoming:[3,4],name:[marko],age:[29]]
==>[name:[vadas],age:[27]]
==>[name:[lop],lang:[java]]
==>[incoming:[3,5],name:[josh],age:[32]]
==>[name:[ripple],lang:[java]]
==>[incoming:[3],name:[peter],age:[35]]
© www.soinside.com 2019 - 2024. All rights reserved.