【问题标题】:Given two specific vertices, how to get the subgraph between them in Titan?给定两个特定顶点,如何在 Titan 中获取它们之间的子图?
【发布时间】:2017-07-04 23:32:40
【问题描述】:

我们有数百万个顶点和数千万条边来形成一个有向图。有人知道如何在 Titan 中的两个给定顶点之间获取子图吗?

【问题讨论】:

    标签: gremlin titan nosql


    【解决方案1】:

    TinkerPop 提供了两种子图方法:

    1. subgraph step
    2. subgraph strategy

    使用subgraph 步骤将子图“弹出”到单独的图实例中。您可以在以下示例中看到图“g”如何被子图化为“知道”边的新 Graph 实例:

    gremlin> subGraph = g.E().hasLabel('knows').subgraph('subGraph').cap('subGraph').next() //(1)
    ==>tinkergraph[vertices:3 edges:2]
    gremlin> sg = subGraph.traversal()
    ==>graphtraversalsource[tinkergraph[vertices:3 edges:2], standard]
    gremlin> sg.E() //(2)
    ==>e[7][1-knows->2]
    ==>e[8][1-knows->4]
    

    如果您只想遍历图表的子集,您可以使用Subgraph 策略:

    gremlin> graph = TinkerFactory.createTheCrew()
    ==>tinkergraph[vertices:6 edges:14]
    gremlin> g = graph.traversal().withStrategies(SubgraphStrategy.build().
               vertices(or(hasNot('location'),properties('location').count().is(gt(3)))).
               edges(hasLabel('develops')).
               vertexProperties(or(hasLabel(neq('location')),hasNot('endTime'))).create())
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:14], standard]
    gremlin> g.V().valueMap(true)
    ==>[id:1,label:person,name:[marko],location:[santa fe]]
    ==>[id:8,label:person,name:[matthias],location:[seattle]]
    ==>[id:10,label:software,name:[gremlin]]
    ==>[id:11,label:software,name:[tinkergraph]]
    gremlin> g.E().valueMap(true)
    ==>[id:13,label:develops,since:2009]
    ==>[id:14,label:develops,since:2010]
    ==>[id:21,label:develops,since:2012]
    

    根据您的问题的标题,听起来您只需要两个顶点的图形,它们之间有边。我想听起来你会想使用subgraph 步骤。当然,得到这个结果似乎比这更容易:

    gremlin> g = TinkerFactory.createModern().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.V(1).bothE().where(otherV().hasId(2))
    ==>e[7][1-knows->2]
    

    拥有边列表在某种意义上构成子图,尤其是在两个已知顶点的情况下。

    【讨论】:

    • 感谢您的回复,斯蒂芬马莱特!是否可以更改为 Titan 0.5.4 的命令?目前,我们没有升级到 Titan 1.0 的计划。
    • TinkerPop 2.x 和 Titan 0.5.x 中没有内置子图函数。你必须编写自己的方法来做到这一点。不过,您可以很容易地找到两个顶点之间的边:g.v(1).bothE.as('x').bothV.retain([g.v(3)]).back('x') 复制自 gremlindocs.com
    • 好!我们将更详细地尝试它。顺便说一句,您知道如何在 Titan 中获取有向图中的所有循环吗?是否有内置函数或者我们应该编写自己的方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多