Skip to content

Instantly share code, notes, and snippets.

@redwrasse
Last active October 19, 2017 04:52
Show Gist options
  • Save redwrasse/2ea9a65ece6bdcbd8d82b3318e8f4c04 to your computer and use it in GitHub Desktop.
Save redwrasse/2ea9a65ece6bdcbd8d82b3318e8f4c04 to your computer and use it in GitHub Desktop.
Maximum value in the Pregel formalism in Spark
/**
* Maximum value algorithm: at each iteration each
* node sends to its neighbors the largest value it
* has ever seen.
*/
package graphalgorithms
import org.apache.spark.graphx._
object MaxValue {
val MAX_ITERATIONS = 100
def run(g: Graph[Int, Int]): Graph[Int, Int] = {
val mergeMsg = (a:Int, b: Int) => math.max(a, b)
val activeDirection = EdgeDirection.Either
val numIterations = MAX_ITERATIONS
val initialMsg = Integer.MIN_VALUE
val vprog = (vid: VertexId, vd: Int, a: Int) => math.max(vd, a)
val sendMsg = (etr: EdgeTriplet[Int, Int]) => {
val a = math.max(etr.srcAttr, etr.dstAttr)
Iterator((etr.srcId, a), (etr.dstId, a))
}
Pregel(g, initialMsg, numIterations)(
vprog, sendMsg, mergeMsg)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment