Aroon Aroon - 22 days ago 6
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 {
Iterator.empty
}
},
(a, b) => math.min(a, b)
)


OutPut:

(Dest Node,shortest Distance )
(18,991.6112077930221)
(38,379.8315724661152)
(14,1442.036238189988)
(22,1494.3250833673349)
(30,451.119421079875)
(10,1525.2322402611999)
(56,2313.01408644027)
(20,970.9823341266101)


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

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.

Comments