簡體   English   中英

Tinkerpop 3:使用Gremlin遍歷計算連接的組件

[英]Tinkerpop 3: compute connected components with Gremlin traversal

我認為標簽很好地解釋了我的問題:)

我一直在嘗試編寫Gremlin遍歷,以計算帖子結尾處描述的簡單圖的連接組件。

我嘗試過

g.V().repeat(both('e')).until(cyclicPath()).dedup().tree().by('name').next()

獲得

==>a={b={a={}, c={b={}}, d={c={d={}}}}, c={d={c={}}}}
==>e={f={e={}, g={f={}}}, h={f={h={}}}}
==>g={f={g={}}}

這是不好的,因為cyclicPath過濾器在到達g之前從e終止了遍歷。 顯然,如果我刪除until子句,則會出現無限循環。 此外,如果我使用simplePath則遍歷將在一步之后結束。 有什么方法可以告訴它以深度優先的順序探索節點嗎?

干杯!

a = graph.addVertex(T.id, 1, "name", "a")
b = graph.addVertex(T.id, 2, "name", "b")
c = graph.addVertex(T.id, 3, "name", "c")
d = graph.addVertex(T.id, 4, "name", "d")
e = graph.addVertex(T.id, 5, "name", "e")
f = graph.addVertex(T.id, 6, "name", "f")
g = graph.addVertex(T.id, 7, "name", "g")
h = graph.addVertex(T.id, 8, "name", "h")

a.addEdge("e", b)
a.addEdge("e", c)
b.addEdge("e", c)
b.addEdge("e", d)
c.addEdge("e", d)

e.addEdge("e", f)
e.addEdge("e", h)
f.addEdge("e", h)
f.addEdge("e", g)

Gremlin-users組中也討論了此查詢。 這是我想出的解決方案。 @Daniel Kuppitz還有一個有趣的解決方案,您可以在提到的線程中找到它。

我認為,如果在無向圖中始終是正確的,則遍歷已連接組件的“最后一個”節點要么導致先前訪問的節點( cyclicPath() ),要么具有度數<= 1,則此查詢應該有效

gV().repeat(both('e')).until( cyclicPath().or().both('e').count().is(lte(1)) ).dedup().tree().by('name').next()

在我的示例中,它提供了以下輸出

gremlin>  g.V().repeat(both('e')).until(cyclicPath().or().both('e').count().is(lte(1))).dedup().tree().by('name').next()
==>a={b={a={}, c={b={}}, d={c={d={}}}}, c={d={c={}}}}
==>e={f={e={}, g={}, h={f={}}}, h={f={h={}}}}

只是為了增強運行良好的@Alberto版本,您可以使用simplePath()遍歷步驟( http://tinkerpop.apache.org/docs/current/reference/#simplepath-step )確保遍歷器確實不重復其通過圖形的路徑

g.V().repeat(both().simplePath())
  .until(bothE().count().is(lte(1)))
  .dedup().tree().by('name').next()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM