Java 广度优先搜索并打印Kevin Bacon路径,重新访问访问的节点?

Java 广度优先搜索并打印Kevin Bacon路径,重新访问访问的节点?,java,shortest-path,breadth-first-search,Java,Shortest Path,Breadth First Search,我在试图查找凯文·培根号码的程序中实现广度优先搜索时遇到了一些问题。我创建了一个临时哈希集来存储访问过的节点,但是程序会继续访问节点,尽管我从列表中删除了节点,并将其反馈到队列中 演员和电影的信息存储在一个HashMap(String,HashSet(String))中——因此,如果您输入一个演员作为密钥,您将获得他们所扮演的电影,如果您输入一个电影作为密钥,您将获得在其中扮演过的演员。下面是我的代码和System.out.prints打印的内容。谢谢你的帮助 public List<St

我在试图查找凯文·培根号码的程序中实现广度优先搜索时遇到了一些问题。我创建了一个临时哈希集来存储访问过的节点,但是程序会继续访问节点,尽管我从列表中删除了节点,并将其反馈到队列中

演员和电影的信息存储在一个HashMap(String,HashSet(String))中——因此,如果您输入一个演员作为密钥,您将获得他们所扮演的电影,如果您输入一个电影作为密钥,您将获得在其中扮演过的演员。下面是我的代码和System.out.prints打印的内容。谢谢你的帮助

public List<String> findBaconPath (String actor) throws IllegalArgumentException {
    ArrayList<String> list = new ArrayList<String>();
    HashSet<String> visited = new HashSet<String>();
    visited.add(actor);
    LinkedList<String> bfs = new LinkedList<String>();
    bfs.add(actor);
    while (!bfs.isEmpty()){
        String curr = bfs.remove(); //The current actor we are looking at.
        visited.add(curr);
        HashSet<String> mov = myMovies.get(curr);
        Iterator<String> it = mov.iterator();
        while (it.hasNext()){
            String next = it.next(); //The next movie in the iterator.
            visited.add(next);
            HashSet<String> coStars = myMovies.get(next);
            coStars.removeAll(visited);
            if (coStars.contains("Bacon, Kevin")){
                list.add(curr);
                list.add(next);
                list.add("Bacon, Kevin");
                System.out.println(list.toString());
                return list;
            }
            else {
                list.add(curr);
                list.add(next);
                System.out.println("This is what is in visited: "+visited.toString());
                System.out.println("This is what is in coStars pre removal. "+ coStars.toString());
                coStars.removeAll(visited);
                System.out.println("This is what is in coStars post removal. "+ coStars.toString());
                Iterator<String> cos = coStars.iterator();
                while (cos.hasNext()){
                    bfs.add(cos.next());
                }
            }
        }

    }
    return list;
}
public List findBaconPath(字符串参与者)抛出IllegalArgumentException{
ArrayList=新建ArrayList();
访问的HashSet=新HashSet();
添加(演员);
LinkedList bfs=新建LinkedList();
添加(演员);
而(!bfs.isEmpty()){
字符串curr=bfs.remove();//我们正在查看的当前参与者。
已访问。添加(curr);
HashSet mov=myMovies.get(curr);
迭代器it=mov.Iterator();
while(it.hasNext()){
String next=it.next();//迭代器中的下一个电影。
已访问。添加(下一步);
HashSet coStars=myMovies.get(下一步);
主演:removeAll(访问);
如果(主演包含(“培根,凯文”)){
列表。添加(当前);
列表。添加(下一步);
添加(“培根,凯文”);
System.out.println(list.toString());
退货清单;
}
否则{
列表。添加(当前);
列表。添加(下一步);
System.out.println(“这是访问的内容:+visted.toString());
System.out.println(“这是coStars预移除中的内容。”+coStars.toString());
主演:removeAll(访问);
System.out.println(“这是移除后的coStars中的内容。”+coStars.toString());
迭代器cos=coStars.Iterator();
while(cos.hasNext()){
添加(cos.next());
}
}
}
}
退货清单;
}
  • 这就是我们正在访问的内容:[D,电影7]
  • 这就是《脱口秀》中的配角。[D,A,B,C]
  • 这就是《脱口秀》中的配角。[甲、乙、丙]
  • 这是正在访问的内容:[D,电影7,电影2]
  • 这就是《脱口秀》中的配角。[D、E、A、B]
  • 这就是《脱口秀》中的配角。[E,A,B]
  • 这就是我们访问的内容:[D,A,电影7,电影2]
  • 这就是《脱口秀》中的配角。[甲、乙、丙]
  • 这就是《脱口秀》中的配角。[乙,丙]
  • 这就是我们访问的内容:[D,A,电影7,电影2]
  • 这就是《脱口秀》中的配角。[E,A,B]
  • 这就是《脱口秀》中的配角。[E,B]
  • 这是正在访问的内容:[D,A,电影7,电影2,电影1]
  • 这就是《脱口秀》中的配角。[甲、乙、丙]
  • 这就是《脱口秀》中的配角。[乙,丙]
  • [D,电影7,D,电影2,A,电影7,A,电影2,A,电影1,A,电影0,培根,凯文]

如果你能更好地区分你访问列表中的电影和演员,而不是将他们混为一谈,这可能会有所帮助。比如:

[(Movie1, Actor1, Actor2), (Movie2, Actor1, Actor2, Actor3)]
然后,当您删除所有内容时,您始终可以通过电影标题识别“已访问”中的条目

此外,每个循环调用removeAll两次。一次是在“if(coStars.contains(“Bacon,Kevin”){”之前,一次是在打印“post remove”之前

顺便说一句,这就是答案吗?电影7,电影2,电影1,电影0是的,这就是答案。你这是什么意思,比如…把电影和演员联系起来?