【问题标题】:How to calculate distance between two nodes in GraphX, Scala?如何计算GraphX,Scala中两个节点之间的距离?
【发布时间】:2017-04-19 00:36:37
【问题描述】:

我想计算每个节点到汇节点之间的最大距离。汇节点是没有外边缘的节点。我找到了最短距离的函数,但我想知道最大距离。

【问题讨论】:

    标签: scala apache-spark graph spark-graphx


    【解决方案1】:

    要计算GraphX中任意两个节点之间的最大距离,可以使用Pregel API

    代码可以是这样的:

    import org.apache.spark.graphx.{Graph, VertexId}
    import org.apache.spark.graphx.util.GraphGenerators
    
    // A graph with edge attributes containing distances
    val graph: Graph[Long, Double] =
      GraphGenerators.logNormalGraph(sc, numVertices = 100).mapEdges(e => e.attr.toDouble)
    val sourceId: VertexId = 42 // The ultimate source
    // Initialize the graph such that all vertices except the root have distance infinity.
    val initialGraph = graph.mapVertices((id, _) =>
        if (id == sourceId) 0.0 else Double.PositiveInfinity)
    val sssp = initialGraph.pregel(Double.PositiveInfinity)(
      (id, dist, newDist) => math.max(dist, newDist), // Vertex Program
      triplet => {  // Send Message
        if (triplet.srcAttr + triplet.attr < triplet.dstAttr) {
          Iterator((triplet.dstId, triplet.srcAttr + triplet.attr))
        } else {
          Iterator.empty
        }
      },
      (a, b) => math.max(a, b) // Merge Message
    )
    println(sssp.vertices.collect.mkString("\n"))
    

    【讨论】:

    • 以上答案将计算从一个源节点到作为目的地的多个节点的距离。然后我们必须在 sssp 中寻找目标节点。但不是这样做,是否可以仅计算从一个源目的地到一个目的地的距离?直接提供源和目的地,即 2 作为源,12 作为目的地。
    • 这可以使用图框吗?
    • 我还没有查看此应用程序的 GraphFrames。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2019-08-27
    • 1970-01-01
    • 2011-12-21
    • 2019-01-10
    相关资源
    最近更新 更多