【发布时间】: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 上有效地执行此类算法?
【问题讨论】: