educative.io

Is the visited array here necessary? Is it to guard against cycles in the graph or is it an optimization?

 private fun findPath(graph: Map<Int, IntArray>, start: Int, end: Int): Boolean {
        if (start == end) {
            return true
        }

        val children = graph.getOrDefault(start, intArrayOf())

        children.forEach {
            return findPath(graph, it, end)
        }

        return false
    }

Hi @Nathan_Sass

We keep track of the visited nodes so that we don’t visit the nodes repeatedly which can result in an infinite search.