-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from scalan/v0.5.1
v0.5.1
- Loading branch information
Showing
316 changed files
with
15,775 additions
and
25,066 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package scalan.util | ||
|
||
import scalan.{DFunc, DFuncAdapter} | ||
import debox.{Buffer => DBuffer} | ||
|
||
import scala.reflect.ClassTag | ||
import spire.syntax.all.cfor | ||
|
||
object GraphUtilEx { | ||
/** | ||
* Returns the strongly connected components | ||
* of the graph rooted at the startNodes argument, | ||
* whose edges are given by the function argument `succ`. | ||
* | ||
* @see Tarjan, Robert E.. “Depth-First Search and Linear Graph Algorithms.” SIAM J. Comput. 1 (1972): 146-160. | ||
*/ | ||
def stronglyConnectedComponents[@specialized(Int) T: ClassTag](startNodes: DBuffer[T], succ: DFunc[T, DBuffer[T]]): DBuffer[DBuffer[T]] = { | ||
val tarjan = new Tarjan[T](succ) | ||
|
||
// top level loop of the algorithm | ||
cfor(0)(_ < startNodes.length, _ + 1) { i => | ||
val node = startNodes(i) | ||
tarjan.visit(node) | ||
} | ||
|
||
tarjan.res | ||
} | ||
|
||
def stronglyConnectedComponents[@specialized(Int) T: ClassTag](start: Array[T])(succ: T => DBuffer[T]): DBuffer[DBuffer[T]] = { | ||
stronglyConnectedComponents(DBuffer.fromArray(start), new DFuncAdapter(succ)) | ||
} | ||
|
||
def buildScheduleForResult(startNodes: DBuffer[Int], neighbours: DFunc[Int, DBuffer[Int]]): DBuffer[Int] = { | ||
val components = GraphUtilEx.stronglyConnectedComponents(startNodes, neighbours) | ||
val nComponents = components.length | ||
if (nComponents == 1) { | ||
components(0) | ||
} else { | ||
val res = DBuffer.ofSize[Int](components(0).length) | ||
cfor(0)(_ < nComponents, _ + 1) { i => | ||
res ++= components(i) | ||
} | ||
res | ||
} | ||
} | ||
|
||
val initTarjan = new Tarjan[Int](new DFuncAdapter((i: Int) => DBuffer(i))) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package scalan.util | ||
|
||
import scala.reflect.ClassTag | ||
import spire.syntax.all.cfor | ||
import scalan.DFunc | ||
import debox.{Buffer => DBuffer, Map => DMap} | ||
|
||
/** Implementation of the SCC part of Tarjan algorithm. */ | ||
final class Tarjan[@specialized(Int) T: ClassTag](private var getNeighbours: DFunc[T, DBuffer[T]]) { | ||
private var id = 0 | ||
private var stack: DBuffer[T] = DBuffer.empty | ||
private val mark = DMap.ofSize[T,Int](127) | ||
|
||
val res: DBuffer[DBuffer[T]] = DBuffer.empty | ||
|
||
/** Explore the next graph node. Returns quickly if the node is already marked. | ||
* @return mark assigned to `node` */ | ||
def visit(node: T): Int = { | ||
val n = mark.getOrElse(node, -1) | ||
if (n >= 0) return n | ||
|
||
id += 1 | ||
|
||
mark.update(node, id) | ||
stack += node | ||
|
||
var min: Int = id | ||
val neighbours = getNeighbours(node) | ||
cfor(0)(_ < neighbours.length, _ + 1) { i => | ||
val child = neighbours(i) | ||
val m = visit(child) | ||
|
||
if (m < min) | ||
min = m | ||
} | ||
|
||
if (min == mark(node)) { | ||
val scc = DBuffer.empty[T] | ||
|
||
var loop: Boolean = true | ||
do { | ||
val element = stack.pop | ||
scc += element | ||
mark.update(element, Integer.MAX_VALUE) | ||
loop = element != node | ||
} while (loop) | ||
|
||
res += scc | ||
} | ||
min | ||
} | ||
} |
Oops, something went wrong.