import scala.collection.mutable class Graph(nodes: Int) { private val adjList: Array[List[Int]] = Array.fill(nodes)(List()) def addEdge(u: Int, v: Int): Unit = { adjList(u) ::= v adjList(v) ::= u } def findPath(start: Int, end: Int): Option[List[Int]] = { val visited = Array.fill(nodes)(false) val path = mutable.ListBuffer.empty[Int] def dfs(current: Int): Boolean = { visited(current) = true path += current if (current == end) { return true } for (neighbor <- adjList(current)) { if (!visited(neighbor) && dfs(neighbor)) { return true } } path -= current false } if (dfs(start)) { Some(path.toList) } else { None } } } object Main { def main(args: Array[String]): Unit = { val graph = new Graph(6) graph.addEdge(0, 1) graph.addEdge(0, 2) graph.addEdge(1, 3) graph.addEdge(2, 4) graph.addEdge(3, 5) val startNode = 0 val endNode = 5 val path = graph.findPath(startNode, endNode) path match { case Some(p) => println(s"Path between $startNode and $endNode: ${p.mkString(" -> ")}") case None => println(s"No path found between $startNode and $endNode") } } }