Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:15:49+00:00 2026-06-15T05:15:49+00:00

I’m pretty new to Java and I have a problem in my code, where

  • 0

I’m pretty new to Java and I have a problem in my code, where I want to find the shortest path from actor to movie to actor to movie to Kevin Bacon. This is stored in a list which would go "Actor A, Movie A, Actor B, Movie B, Kevin Bacon". I thought that the best way of doing this would be to do it recursively. However, I am getting a StackOverflowError.

I store the actors and movies in a HashMap<String, HashSet<String>>. Both actors and movies are keys – if the actor is called, then it returns a HashSet of the movies the actor has been in, and if the movie is called, it returns a HashSet of the actors it has. The findCostars method finds all the actors a given actor has costarred with.

Here is my code. Any help would be seriously appreciated!

public List<String> findBaconPath (String actor) throws IllegalArgumentException {
    ArrayList<String> actors = new ArrayList<String>();
    actors.add(actor);
    ArrayList<String> path = helper(actors, actor);
    return path;
}

public ArrayList<String> helper(ArrayList<String> curr, String actor) {
    ArrayList<String> list = new ArrayList<String>();
    HashSet<String> movies = myMovies.get(actor);
    ArrayList<String> coStars = (ArrayList<String>) findCostars(actor);
    Iterator<String> it = movies.iterator();
    while (it.hasNext()) {
        String next = it.next();
        HashSet<String> movAct = myMovies.get(next);
        if (movAct.contains("Bacon, Kevin")) {
            list.add("Bacon, Kevin");
            list.add(next);
            list.add(actor);
            return list;
        } else {
            Iterator<String> itAct = coStars.iterator();
            while(itAct.hasNext()) {
                curr.add(next);
                String nextActorValue = itAct.next();
                curr.add(nextActorValue);
                helper(curr, nextActorValue);
            }
        }
    }
    return null;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:15:50+00:00Added an answer on June 15, 2026 at 5:15 am

    You’re getting a stack overflow because your search is depth-first, and you don’t exclude the graph nodes you’ve already visited.

    Since you probably want the shortest path, try implementing Breadth-first search.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been unable to fix a problem with Java Unicode and encoding. The
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.