在GraphX中计算图形直径的正确方法是什么

时间:2018-12-11 15:52:00

标签: scala graph-theory spark-graphx

我正在GraphX上实现一种算法,为此我还需要计算一些相对较小的图的直径。 问题在于GraphX没有任何无向图的概念,因此当使用ShortestPaths中的内置方法时,它显然会获取简化的有向路径。这对计算图形直径(任何一对节点之间的最长短路无向路径)没有太大帮助。

我想复制图形的边缘(而不是| E |而是2 | E |边缘),但是我不认为这是正确的方法。那么,有没有一种更好的方法可以在GraphX上做到这一点呢?

这是我的有向图代码:

// computing the query diameter
def getDiameter(graph: Graph[String, Int]):Long = {
    // Get ids of vertices of the graph 
    val vIds = graph.vertices.collect.toList.map(_._1) 
    // Compute list of shortest paths for every vertex in the graph
    val shortestPaths  = lib.ShortestPaths.run(graph, vIds).vertices.collect
    // extract only the distance values from a list of tuples <VertexId, Map> where map contains <key, value>: <dst vertex, shortest directed distance>
    val values = shortestPaths.map(element => element._2).map(element => element.values)

    // diamter is the longest shortest undirected distance between any pair of nodes in te graph
    val diameter  = values.map(m => m.max).max
    diameter 
}

1 个答案:

答案 0 :(得分:1)

GraphX实际上没有方向性的概念,您不会告诉它使用它。 如果查看ShortestPaths库的内部工作原理,您会发现它使用Pregel并且方向为默认(EdgeDirection.Either)。这意味着对于所有三元组,它将源和目标都添加到活动集中。 但是,如果您在sendMsg的{​​{1}}函数中指定仅将srcId保留在活动集中(如Pregel lib中那样),则某些顶点(仅具有向外的边)将不会重新评估。

无论如何,一种解决方案是编写自己的Diameter对象/库,也许看起来像这样(很大程度上基于ShortestPaths,所以也许还有更好的解决方案?)

ShortestPath
相关问题