我们的团队是Gelly api的新手。我们正在寻求实现一个简单的用例,它将列出来自初始顶点的所有路径-例如
输入边csv文件是1,2\n2,3\n3,4\n1,5\n5,6
所需的输出将是(从1开始的完整路径)1,2,3,4\n 1,5,6
有人能帮帮忙吗。
发布于 2016-08-03 05:35:21
你可以使用Gelly's iteration abstractions中的一种,例如顶点中心迭代。从源顶点开始,您可以迭代地扩展路径,每个superstep一个跳跃。在接收到路径时,顶点将其ID附加到路径并将其传播到其传出的邻居。如果顶点没有传出邻居,那么它将打印/存储路径,并且不会进一步传播它。为了避免循环,顶点还可以在传播之前检查其ID是否存在于路径中。compute函数可能如下所示:
public static final class ComputePaths extends ComputeFunction<Integer, Boolean, NullValue, ArrayList<Integer>> {
@Override
public void compute(Vertex<Integer, Boolean> vertex, MessageIterator<ArrayList<Integer>> paths) {
if (getSuperstepNumber() == 1) {
// the source propagates its ID
if (vertex.getId().equals(1)) {
ArrayList<Integer> msg = new ArrayList<>();
msg.add(1);
sendMessageToAllNeighbors(msg);
}
}
else {
// go through received messages
for (ArrayList<Integer> p : paths) {
if (!p.contains(vertex.getId())) {
// if no cycle => append ID and forward to neighbors
p.add(vertex.getId());
if (!vertex.getValue()) {
sendMessageToAllNeighbors(p);
}
else {
// no out-neighbors: print p
System.out.println(p);
}
}
else {
// found a cycle => print the path and don't propagate further
System.out.println(p);
}
}
}
}
}在这段代码中,我假设您已经对顶点进行了预处理,以便用"true“值标记那些没有外部邻居的顶点。例如,您可以使用graph.outDegrees()来查找这些内容。
请记住,枚举大而密集的图中的所有路径的计算成本很高。中间路径状态可以非常迅速地爆炸。您可以考虑使用一种比使用into的ArrayList更紧凑的方式来表示路径,但如果您有一个直径较大的密集图,则要注意其成本。如果你不需要路径本身,但你只对可达性或最短路径感兴趣,那么有更有效的算法。
https://stackoverflow.com/questions/38682878
复制相似问题