mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Add transitiveClosure() to DynamicGraph.
This commit is contained in:
parent
c2441f939a
commit
67cb7a5360
2 changed files with 38 additions and 0 deletions
|
@ -30,6 +30,28 @@ class DynamicGraph<T> {
|
|||
private val dependedUpon = HashMultimap.create<Node<T>, Node<T>>()
|
||||
private val dependingOn = HashMultimap.create<Node<T>, Node<T>>()
|
||||
|
||||
fun transitiveClosure(root: T): Set<T> {
|
||||
val result = hashSetOf<T>()
|
||||
val seen = hashSetOf<T>()
|
||||
val toProcess = arrayListOf<T>().apply {
|
||||
add(root)
|
||||
}
|
||||
while (toProcess.any()) {
|
||||
val newToProcess = arrayListOf<T>()
|
||||
toProcess.forEach {
|
||||
if (! seen.contains(it)) {
|
||||
result.add(it)
|
||||
newToProcess.addAll(dependedUpon[Node(it)].map { it.value })
|
||||
seen.add(it)
|
||||
}
|
||||
}
|
||||
toProcess.clear()
|
||||
toProcess.addAll(newToProcess)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun addNode(t: T) = synchronized(nodes) {
|
||||
nodes.add(Node(t))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue