如何计算GraphX,Scala中两个节点之间的距离?

时间:2017-04-19 00:36:37

标签: scala apache-spark graph spark-graphx

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

1 个答案:

答案 0 :(得分:2)

要计算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"))