4

I am applying BFS using the Graph frames in Scala, How can I sum the edges weights of the selected shortest path. I have Following Code:

    import org.graphframes._
    import org.apache.spark.sql.DataFrame
    val v = sqlContext.createDataFrame(List(
      ("1", "Al"),
      ("2", "B"),
      ("3", "C"),
      ("4", "D"),
      ("5", "E")
    )).toDF("id", "name")

    val e = sqlContext.createDataFrame(List(
      ("1", "3", 5),
      ("1", "2", 8),
      ("2", "3", 6),
      ("2", "4", 7),
      ("2", "1", 8),
      ("3", "1", 5),
      ("3", "2", 6),
      ("4", "2", 7),
      ("4", "5", 8),
      ("5", "4", 8)
    )).toDF("src", "dst", "property")
val g = GraphFrame(v, e)
val paths: DataFrame = g.bfs.fromExpr("id = '1'").toExpr("id = '5'").run()
paths.show()

OutPut of Above code is:

+------+-------+-----+-------+-----+-------+-----+                              
|  from|     e0|   v1|     e1|   v2|     e2|   to|
+------+-------+-----+-------+-----+-------+-----+
|[1,Al]|[1,2,8]|[2,B]|[2,4,7]|[4,D]|[4,5,8]|[5,E]|
+------+-------+-----+-------+-----+-------+-----+

But I need Output Like this:

+----+-------+-----------+---------+
|    |source |Destination| Distance|
+----+-------+-----------+---------+
| e0 |   1    | 2        | 8       |
+----+-------+-----------+---------+
| e1 |   2    | 4        | 7       |
+----+-------+-----------+---------+
| e2 |   4    | 5        | 8       |
+----+-------+-----------+---------+

Unlike the above example my graph is huge, it might actually return a large number of edges.

Yasir Arfat
  • 645
  • 1
  • 8
  • 21
  • Aww man I need something like this too. I don't understand why the BFS algorithm is implemented like this... – oliver Oct 12 '17 at 08:29
  • 1
    Try the shortestPaths function: `results = g.shortestPaths(landmarks=["1", "2","3"])` and then `results.select("id", explode("distances"))` – drkostas May 30 '18 at 12:24

0 Answers0