Aroon Aroon - 1 year ago 93
Scala Question

Want Distance between Source and Destination node only, Not among all nodes in Graph Network

I want to calculate the shortest Distance Among Source and destination node using Scala and GraphX . But Following code gives me the Shortest distance among all nodes.

val sourceId: VertexId = 37
val g = graph.mapVertices( (id, _) =>
if (id == sourceId) 0.0
else Double.PositiveInfinity
val sssp = g.pregel(Double.PositiveInfinity)(
(id, dist, newDist) => math.min(dist, newDist),
triplet => {
if (triplet.srcAttr + triplet.attr < triplet.dstAttr) {
Iterator((triplet.dstId, triplet.srcAttr + triplet.attr))
else {
(a, b) => math.min(a, b)


(Dest Node,shortest Distance )

For example: I want to calculate the distance between Source node:37 to Destination node:18, not among all nodes.
Can you please Guide me how I can specify the destination node in this code, so that I can find the just distance between source and destination, Not among All nodes.

Answer Source

What about filtering the output, and leaving only the node that you're interested in? In your case this would be:

sssp.vertices.filter { case (destId, _) =>
  destId == 18

which would leave (18,991.6112077930221) in the output.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download