3

i tried to find shortest path from single source to n vertices using code from link

val graph: Graph[Long, Double] =
  GraphGenerators.logNormalGraph(sc, numVertices = 100).mapEdges(e => e.attr.toDouble)
val sourceId: VertexId = 42

val initialGraph = graph.mapVertices((id, _) =>
    if (id == sourceId) 0.0 else Double.PositiveInfinity)
val sssp = initialGraph.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)
)
println(sssp.vertices.collect.mkString("\n"))

it gives me output shortest path from 42 to N verticies. however, how to find the shortest path between single source to single destination? i.e. source=42, dest = 135 then i want to find the shortest path between them.

thanks

Sugimiyanto
  • 320
  • 3
  • 18

1 Answers1

0

You can't find the shortest distance between two vertices otherwise i.e., without exploring the whole graph. Because, even if you stop your exploration once your message is received by the destination, nothing guarantees that it's the shortest distance. So, this algorithm is fine in terms of complexity. You should just pick the sssp value from the different destinations obtained.

PhiloJunkie
  • 1,111
  • 4
  • 13
  • 27