Skip to content

Instantly share code, notes, and snippets.

@treadstone90
Created October 23, 2014 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treadstone90/5ee73a21a23a99b7a523 to your computer and use it in GitHub Desktop.
Save treadstone90/5ee73a21a23a99b7a523 to your computer and use it in GitHub Desktop.
Blog BFS bottom up
trait DirectedSerialAncestorManager extends SerialAncestorManager{
var _graph: SerialDirectedGraph = _
def getAncestor(id: Int): IndexedSeq[Int] = {
_graph.getParents(id)
}
def getVertices: IndexedSeq[Int] = (0 to _graph.numberVertices - 1)
}
trait SBottomUpUpdater extends FrontierUpdater with SerialAncestorManager {
def update(frontier: BitSet, parents: Array[Int]): Seq[Int] = {
val next = mutable.BitSet()
val vertices = getVertices
val frontierSet = frontier.toSet
(vertices.filter(parents(_) == -1)).foreach { node =>
val neighbors = (getAncestor(node))
neighbors.find(frontierSet) match {
case Some(ancestor) => {
parents(node) = ancestor
next(node) = true
}
case None => None
}
}
next.toBuffer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment