grazie shodan,ma ho risolto con una lista FIFO e una funzione non ricorsiva.
In pratica faccio fare alla lista quello che fa lo stack con le variabili.
Ho letto qui molte cose interessanti sui grafi , soprattutto il depth-first search.
Ho adattato questa funzione:
codice:
Another version, without the recursion:

dfs(graph G)
{
  list L = empty
  tree T = empty
  choose a starting vertex x
  search(x)
  while(L is not empty)
  {
    remove edge (v, w) from beginning of L
    if w not yet visited
    {
      add (v, w) to T
      search(w)
    }
  }
}
   
search(vertex v)
{
  visit v
  for each edge (v, w)
    add edge (v, w) to the beginning of L
}