title: Kevin Bacon path in Java gives a stack overflow?
body: <p>I'm pretty new to Java and I have a problem in my code, where I want to find the shortest path from <em>actor to movie to actor to movie to Kevin Bacon</em>. This is stored in a <code>list</code> which would go <code>"Actor A, Movie A, Actor B, Movie B, Kevin Bacon"</code>. I thought that the best way of doing this would be to do it <code>recursively</code>. However, I am getting a <code>StackOverflowError</code>. </p>
  
  <p>I store the <em>actors</em> and <em>movies</em> in a <code>HashMap&lt;String, HashSet&lt;String&gt;&gt;</code>. Both <em>actors</em> and <em>movies</em> are <code>keys</code> - if the <em>actor</em> is called, then it returns a <code>HashSet</code> of the <em>movies</em> the <em>actor</em> has been in, and if the <em>movie</em> is called, it returns a <code>HashSet</code> of the <em>actors</em> it has. The <code>findCostars</code> method finds all the <em>actors</em> a given <em>actor</em> has costarred with. </p>
  
  <p>Here is my code. Any help would be seriously appreciated!</p>
  
  <pre><code>public List&lt;String&gt; findBaconPath (String actor) throws IllegalArgumentException {
      ArrayList&lt;String&gt; actors = new ArrayList&lt;String&gt;();
      actors.add(actor);
      ArrayList&lt;String&gt; path = helper(actors, actor);
      return path;
  }
  
  public ArrayList&lt;String&gt; helper(ArrayList&lt;String&gt; curr, String actor) {
      ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
      HashSet&lt;String&gt; movies = myMovies.get(actor);
      ArrayList&lt;String&gt; coStars = (ArrayList&lt;String&gt;) findCostars(actor);
      Iterator&lt;String&gt; it = movies.iterator();
      while (it.hasNext()) {
          String next = it.next();
          HashSet&lt;String&gt; movAct = myMovies.get(next);
          if (movAct.contains("Bacon, Kevin")) {
              list.add("Bacon, Kevin");
              list.add(next);
              list.add(actor);
              return list;
          } else {
              Iterator&lt;String&gt; itAct = coStars.iterator();
              while(itAct.hasNext()) {
                  curr.add(next);
                  String nextActorValue = itAct.next();
                  curr.add(nextActorValue);
                  helper(curr, nextActorValue);
              }
          }
      }
      return null;
  }
  </code></pre>