【问题标题】:Speeding up Titan graph traversals加速 Titan 图遍历
【发布时间】:2015-04-14 11:14:27
【问题描述】:

我一直在玩一个非常小的264,346 个顶点733,846 个边 的图。我使用BatchGraph 和推荐的设置将它批量导入Titan。这很好 - 它非常有效。

我现在正在玩图遍历,并使用 Java 库在 Java 中实现了一个快速的 Dijkstra 算法。我应该提到我有一个本地 Cassandra 节点,我的 Titan 数据库正在运行 - 它不是嵌入式节点。

对于此图中的平均路径,平均 Dijkstra 运行需要 1 分钟以上。这是非常长的。我希望在这样的图上运行 非常 慢的 Dijkstra 需要不到 10 秒(内存中的图平均查询这个图大小需要不到 1 秒)。

在 Titan 上高效运行此类算法的最佳做法是什么?

我将给出简单 Dijkstra 实现的部分内容,以防我访问顶点和边的方式不是最有效的方式。

获取图形实例:

TitanGraph graph = TitanFactory.open("cassandra:localhost");

Dijkstra 实现的部分内容(特别涉及图形访问):

public int run(long src, long trg){
    this.pqueue.add(new PQNode(0, src));
    this.nodeMap.put(src, new NodeInfo(src, 0, -1));
    int dist = Integer.MAX_VALUE;
    while (!this.pqueue.isEmpty()){
        PQNode current = this.pqueue.poll();
        NodeInfo nodeInfo = this.nodeMap.get(current.getNodeId());
        long u = nodeInfo.getNodeId();

        if (u == trg) {
            dist = nodeInfo.getDistance();
            break;
        }

        if (nodeInfo.isSeen())
            continue;

        this.expansion++;

        TitanVertex vertex = graph.getVertex(u);
        for (TitanEdge out: vertex.getEdges()){
            Direction dir = out.getDirection(vertex);
            if (dir.compareTo(Direction.OUT) != 0 && dir.compareTo(Direction.BOTH) != 0){
                continue;
            }

            TitanVertex v = out.getOtherVertex(vertex);
            long vId = (long)v.getId();
            NodeInfo vInfo = this.nodeMap.get(vId);
            if (vInfo == null){
                vInfo = new NodeInfo(vId, Integer.MAX_VALUE, -1);
                this.nodeMap.put(vId, vInfo);
            }
            int weight = out.getProperty("weight");
            int currentDistance = nodeInfo.getDistance() + weight;
            if (currentDistance < vInfo.getDistance()){
                vInfo.setParent(u);
                vInfo.setDistance(currentDistance);
                this.pqueue.add(new PQNode(currentDistance, vId));
            }
        }
        nodeInfo.setSeen(true);
    }


    return dist;
}

我应该如何继续尝试在 Titan 上有效地执行此类算法?

【问题讨论】:

    标签: titan gremlin


    【解决方案1】:

    忘记您的代码/算法,我不得不同意,正如您所说,您的图表“非常小”。这就引出了为什么选择使用 cassandra 作为后端的问题。对于这种大小的图表,我会尝试 berkeleydb 看看是否有帮助。或者,如果您想真正做到极致,只需使用最快的蓝图实现:TinkerGraph

    至于代码示例本身,如果您不需要,一个建议可能是停止迭代顶点上的所有边:

    for (TitanEdge out: vertex.getEdges()){
        Direction dir = out.getDirection(vertex);
        if (dir.compareTo(Direction.OUT) != 0 && dir.compareTo(Direction.BOTH) != 0){
                continue;
        }
    

    相反,如果你只是想在“边缘告诉 Titan:vertex.getEdges(Direction.IN)。那应该会给你一些额外的效率 - 如果你决定只加载到 TinkerGraph,我什至会这样做。这也将消除对那条getOtherVertex 行,因为那样你就知道out.getVertex(Direction.OUT),我猜它会更快。

    【讨论】:

      猜你喜欢
      • 2018-01-08
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2019-08-23
      相关资源
      最近更新 更多