Advertisement
Guest User

Untitled

a guest
May 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.06 KB | None | 0 0
  1. def PowerGraph(graph: Graph[V,E],
  2.     gatherf: Edge[V,E] => A,
  3.     sumf: (A, A) => A,
  4.     applyf: ((Id,V), A) => V,
  5.     signalf: Edge[V,E]=> Bool,
  6.     maxIter: Int): Graph[V,E] = {
  7.     // Extend the vertex data to include isActive
  8.     var glGraph = graph.mapVertices((Id,v) => (id, (true,v)))
  9.     // Loop while there are active vertices
  10.     var i = 0
  11.     var nActive = g.numVertices
  12.     while (i < maxIter && nActive > 0) {
  13.         // Execute the gather phase
  14.         val acc = glGraph.filterE(e => e.dst.isActive).aggregateNeighbors(gatherf, sumf)
  15.         // Execute the apply phase.
  16.         glGraph = glGraph.updateVertices(acc, applyf)
  17.         // Execute the Scatter Phase
  18.         val active = glGraph.filter(e=>e.src.isActive).aggregateNeighbors(scatterf, (a,b)=> a||b)
  19.         // Update activity status of vertices
  20.         glGraph = glGraph.updateVertices(active,
  21.                     ((id, (old,v)), active)=>(id,(active,v)))
  22.         // Count the number of active vertices
  23.         nActive = glGraph.vertices.map(v => v.isActive).reduce((a,b) => a + b)
  24.         i = i + 1
  25.     }
  26.     // Return the graph (without active flag)
  27.     return glGraph.mapVertices(v => (v.id, v.data))
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement