如何计算 GraphX,Scala 中两个节点之间的距离?
How to calculate distance between two nodes in GraphX, Scala?
我想计算每个节点到汇聚节点之间的最大距离。汇节点是没有外边的节点。我找到了最短距离的函数,但我想知道最大距离。
要计算 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"))
我想计算每个节点到汇聚节点之间的最大距离。汇节点是没有外边的节点。我找到了最短距离的函数,但我想知道最大距离。
要计算 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"))