Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Environment.nodesReachableFrom(Node) #898

Open
DanySK opened this issue Nov 12, 2021 · 0 comments
Open

Add Environment.nodesReachableFrom(Node) #898

DanySK opened this issue Nov 12, 2021 · 0 comments

Comments

@DanySK
Copy link
Member

DanySK commented Nov 12, 2021

There is often the need to build a graph of a network segment connected to a Node. This method does so. Stub idea:

private fun <T> Environment<T, *>.connectedNodes(center: Node<T>): Set<Node<T>> {
    fun locallyConnected(node: Node<T>) = getNeighborhood(node).neighbors.toSet()
    val toVisit = ArrayDeque(locallyConnected(center))
    val visited = mutableSetOf(center)
    val result = mutableSetOf(center)
    while (toVisit.isNotEmpty()) {
        val subject = toVisit.removeFirst()
        visited.add(subject)
        toVisit.addAll(locallyConnected(subject) - visited)
        result.add(subject)
    }
    return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant