Print Connected Components Scala
You can always translate loops into a tail-recursive function.  Then all you need to do is pass state around as function parameters instead of mutating it in place.  IIRC, Scala supports tail-call optimization of direct recursion with the appropriate annotation.  That should be good enough in this case.

You actually have done this for BFS_r.  You basically just need to do the same thing with the rest of the code.

But first, you're using a lot of Lists that would probably be better off as Sets or Maps.  Also, you're keeping track of the nodes that have been visited (globalVisits).  I would keep track of the portion of the graph that has not been visited instead -- at least with a tail-recursive solution, since we'll have to get rid of the flatMap.

The first thing is to replace the flatMap with a tail-recursive function:

@tailrec
def loop(unvisited: Map[String, List[String]], accumulator: List[List[String]]): List[List[String]] = {
  if unvisited.empty {
    // We're done =&gt; return the accumulator
    return acc
  }
  else{
    // Get the next seed node
    // (Since maps are typically unordered, head is an arbitrary node
    //  that hasn't been visited yet.)
    val seed = unvisited.head

    // Find the component connected to seed
    component = BFS_r(List(seed), List(List(seed)).get.flatten)

    // Iterate, with new unvisited map and component list
    // The new unvisited will have all the nodes in component removed
    // The new accumulator has component added
    return loop(unvisited -- component, component :: accumulator)
}


Then kick it off with:

return loop(adjMap, List())


I haven't done a lot of work with Scala yet, so I can't say this is 100% valid Scala or will work at all.  But hopefully the intention is clear.  One important to note is that Map and List here are the immutable variants (i.e., from scala.collections.immutable rather than scala.collections.mutable).